2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
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 $
33 * Implementation of the `witness' lock verifier. Originally implemented for
34 * mutexes in BSD/OS. Extended to handle generic lock objects and lock
40 * Pronunciation: 'wit-n&s
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
55 * 6 capitalized : a member of the Jehovah's Witnesses
59 * Special rules concerning Giant and lock orders:
61 * 1) Giant must be acquired before any other mutexes. Stated another way,
62 * no other mutex may be held when Giant is acquired.
64 * 2) Giant must be released when blocking on a sleepable lock.
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
71 * 3) Giant may be acquired before or after sleepable locks.
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.
84 #include <sys/cdefs.h>
85 __FBSDID("$FreeBSD$");
88 #include "opt_witness.h"
90 #include <sys/param.h>
93 #include <sys/kernel.h>
96 #include <sys/malloc.h>
97 #include <sys/mutex.h>
100 #include <sys/sysctl.h>
101 #include <sys/systm.h>
105 #include <machine/stdarg.h>
107 /* Note that these traces do not work with KTR_ALQ. */
109 #define KTR_WITNESS KTR_SUBSYS
111 #define KTR_WITNESS 0
114 /* Easier to stay with the old names. */
115 #define lo_list lo_witness_data.lod_list
116 #define lo_witness lo_witness_data.lod_witness
118 /* Define this to check for blessed mutexes */
121 #define WITNESS_COUNT 1024
122 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
124 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
125 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should
126 * probably be safe for the most part, but it's still a SWAG.
128 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
130 #define WITNESS_NCHILDREN 6
132 struct witness_child_list_entry;
136 struct lock_class *w_class;
137 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */
138 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */
139 struct witness_child_list_entry *w_children; /* Great evilness... */
144 u_char w_Giant_squawked:1;
145 u_char w_other_squawked:1;
146 u_char w_same_squawked:1;
147 u_char w_displayed:1;
150 struct witness_child_list_entry {
151 struct witness_child_list_entry *wcl_next;
152 struct witness *wcl_children[WITNESS_NCHILDREN];
156 STAILQ_HEAD(witness_list, witness);
159 struct witness_blessed {
165 struct witness_order_list_entry {
167 struct lock_class *w_class;
171 static int blessed(struct witness *, struct witness *);
173 static int depart(struct witness *w);
174 static struct witness *enroll(const char *description,
175 struct lock_class *lock_class);
176 static int insertchild(struct witness *parent, struct witness *child);
177 static int isitmychild(struct witness *parent, struct witness *child);
178 static int isitmydescendant(struct witness *parent, struct witness *child);
179 static int itismychild(struct witness *parent, struct witness *child);
180 static void removechild(struct witness *parent, struct witness *child);
181 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
182 static const char *fixup_filename(const char *file);
183 static struct witness *witness_get(void);
184 static void witness_free(struct witness *m);
185 static struct witness_child_list_entry *witness_child_get(void);
186 static void witness_child_free(struct witness_child_list_entry *wcl);
187 static struct lock_list_entry *witness_lock_list_get(void);
188 static void witness_lock_list_free(struct lock_list_entry *lle);
189 static struct lock_instance *find_instance(struct lock_list_entry *lock_list,
190 struct lock_object *lock);
191 static void witness_list_lock(struct lock_instance *instance);
193 static void witness_leveldescendents(struct witness *parent, int level);
194 static void witness_levelall(void);
195 static void witness_displaydescendants(void(*)(const char *fmt, ...),
196 struct witness *, int indent);
197 static void witness_display_list(void(*prnt)(const char *fmt, ...),
198 struct witness_list *list);
199 static void witness_display(void(*)(const char *fmt, ...));
200 static void witness_list(struct thread *td);
203 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking");
206 * If set to 0, witness is disabled. If set to a non-zero value, witness
207 * performs full lock order checking for all locks. At runtime, this
208 * value may be set to 0 to turn off witness. witness is not allowed be
209 * turned on once it is turned off, however.
211 static int witness_watch = 1;
212 TUNABLE_INT("debug.witness.watch", &witness_watch);
213 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
214 sysctl_debug_witness_watch, "I", "witness is watching lock operations");
218 * When KDB is enabled and witness_kdb is set to 1, it will cause the system
219 * to drop into kdebug() when:
220 * - a lock hierarchy violation occurs
221 * - locks are held when going to sleep.
228 TUNABLE_INT("debug.witness.kdb", &witness_kdb);
229 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
232 * When KDB is enabled and witness_trace is set to 1, it will cause the system
233 * to print a stack trace:
234 * - a lock hierarchy violation occurs
235 * - locks are held when going to sleep.
237 int witness_trace = 1;
238 TUNABLE_INT("debug.witness.trace", &witness_trace);
239 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
242 #ifdef WITNESS_SKIPSPIN
243 int witness_skipspin = 1;
245 int witness_skipspin = 0;
247 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
248 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN,
249 &witness_skipspin, 0, "");
251 static struct mtx w_mtx;
252 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
253 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
254 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
255 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
256 static struct witness_child_list_entry *w_child_free = NULL;
257 static struct lock_list_entry *w_lock_list_free = NULL;
259 static int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt;
260 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
261 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
262 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
264 SYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD,
265 &w_child_free_cnt, 0, "");
266 SYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0,
269 static struct witness w_data[WITNESS_COUNT];
270 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
271 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
273 static struct witness_order_list_entry order_lists[] = {
277 { "proctree", &lock_class_sx },
278 { "allproc", &lock_class_sx },
283 { "Giant", &lock_class_mtx_sleep },
284 { "filedesc structure", &lock_class_mtx_sleep },
285 { "pipe mutex", &lock_class_mtx_sleep },
286 { "sigio lock", &lock_class_mtx_sleep },
287 { "process group", &lock_class_mtx_sleep },
288 { "process lock", &lock_class_mtx_sleep },
289 { "session", &lock_class_mtx_sleep },
290 { "uidinfo hash", &lock_class_mtx_sleep },
291 { "uidinfo struct", &lock_class_mtx_sleep },
292 { "allprison", &lock_class_mtx_sleep },
297 { "filedesc structure", &lock_class_mtx_sleep },
298 { "accept", &lock_class_mtx_sleep },
299 { "so_snd", &lock_class_mtx_sleep },
300 { "so_rcv", &lock_class_mtx_sleep },
301 { "sellck", &lock_class_mtx_sleep },
306 { "so_rcv", &lock_class_mtx_sleep },
307 { "radix node head", &lock_class_mtx_sleep },
308 { "rtentry", &lock_class_mtx_sleep },
309 { "ifaddr", &lock_class_mtx_sleep },
312 * Multicast - protocol locks before interface locks, after UDP locks.
314 { "udpinp", &lock_class_mtx_sleep },
315 { "in_multi_mtx", &lock_class_mtx_sleep },
316 { "igmp_mtx", &lock_class_mtx_sleep },
317 { "if_addr_mtx", &lock_class_mtx_sleep },
320 * UNIX Domain Sockets
322 { "unp", &lock_class_mtx_sleep },
323 { "so_snd", &lock_class_mtx_sleep },
328 { "udp", &lock_class_mtx_sleep },
329 { "udpinp", &lock_class_mtx_sleep },
330 { "so_snd", &lock_class_mtx_sleep },
335 { "tcp", &lock_class_mtx_sleep },
336 { "tcpinp", &lock_class_mtx_sleep },
337 { "so_snd", &lock_class_mtx_sleep },
342 { "slip_mtx", &lock_class_mtx_sleep },
343 { "slip sc_mtx", &lock_class_mtx_sleep },
348 { "ddp_list_mtx", &lock_class_mtx_sleep },
349 { "ddp_mtx", &lock_class_mtx_sleep },
354 { "bpf global lock", &lock_class_mtx_sleep },
355 { "bpf interface lock", &lock_class_mtx_sleep },
356 { "bpf cdev lock", &lock_class_mtx_sleep },
361 { "nfsd_mtx", &lock_class_mtx_sleep },
362 { "so_snd", &lock_class_mtx_sleep },
367 { "system map", &lock_class_mtx_sleep },
368 { "vm page queue mutex", &lock_class_mtx_sleep },
369 { "vnode interlock", &lock_class_mtx_sleep },
370 { "cdev", &lock_class_mtx_sleep },
376 { "ap boot", &lock_class_mtx_spin },
378 { "rm.mutex_mtx", &lock_class_mtx_spin },
379 { "hptlock", &lock_class_mtx_spin },
380 { "sio", &lock_class_mtx_spin },
382 { "cy", &lock_class_mtx_spin },
384 { "scc_hwmtx", &lock_class_mtx_spin },
385 { "uart_hwmtx", &lock_class_mtx_spin },
386 { "zstty", &lock_class_mtx_spin },
387 { "ng_node", &lock_class_mtx_spin },
388 { "ng_worklist", &lock_class_mtx_spin },
389 { "fast_taskqueue", &lock_class_mtx_spin },
390 { "intr table", &lock_class_mtx_spin },
391 { "sleepq chain", &lock_class_mtx_spin },
392 { "sched lock", &lock_class_mtx_spin },
393 { "turnstile chain", &lock_class_mtx_spin },
394 { "td_contested", &lock_class_mtx_spin },
395 { "callout", &lock_class_mtx_spin },
396 { "entropy harvest mutex", &lock_class_mtx_spin },
397 { "syscons video lock", &lock_class_mtx_spin },
401 { "allpmaps", &lock_class_mtx_spin },
402 { "vm page queue free mutex", &lock_class_mtx_spin },
403 { "icu", &lock_class_mtx_spin },
405 { "smp rendezvous", &lock_class_mtx_spin },
406 #if defined(__i386__) || defined(__amd64__)
407 { "tlb", &lock_class_mtx_spin },
410 { "ipi", &lock_class_mtx_spin },
411 { "rtc_mtx", &lock_class_mtx_spin },
414 { "clk", &lock_class_mtx_spin },
415 { "mutex profiling lock", &lock_class_mtx_spin },
416 { "kse zombie lock", &lock_class_mtx_spin },
417 { "ALD Queue", &lock_class_mtx_spin },
419 { "MCA spin lock", &lock_class_mtx_spin },
421 #if defined(__i386__) || defined(__amd64__)
422 { "pcicfg", &lock_class_mtx_spin },
423 { "NDIS thread lock", &lock_class_mtx_spin },
425 { "tw_osl_io_lock", &lock_class_mtx_spin },
426 { "tw_osl_q_lock", &lock_class_mtx_spin },
427 { "tw_cl_io_lock", &lock_class_mtx_spin },
428 { "tw_cl_intr_lock", &lock_class_mtx_spin },
429 { "tw_cl_gen_lock", &lock_class_mtx_spin },
436 * Pairs of locks which have been blessed
437 * Don't complain about order problems with blessed locks
439 static struct witness_blessed blessed_list[] = {
441 static int blessed_count =
442 sizeof(blessed_list) / sizeof(struct witness_blessed);
446 * List of locks initialized prior to witness being initialized whose
447 * enrollment is currently deferred.
449 STAILQ_HEAD(, lock_object) pending_locks =
450 STAILQ_HEAD_INITIALIZER(pending_locks);
453 * This global is set to 0 once it becomes safe to use the witness code.
455 static int witness_cold = 1;
458 * This global is set to 1 once the static lock orders have been enrolled
459 * so that a warning can be issued for any spin locks enrolled later.
461 static int witness_spin_warn = 0;
464 * The WITNESS-enabled diagnostic code. Note that the witness code does
465 * assume that the early boot is single-threaded at least until after this
466 * routine is completed.
469 witness_initialize(void *dummy __unused)
471 struct lock_object *lock;
472 struct witness_order_list_entry *order;
473 struct witness *w, *w1;
477 * We have to release Giant before initializing its witness
478 * structure so that WITNESS doesn't get confused.
481 mtx_assert(&Giant, MA_NOTOWNED);
483 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
484 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
485 MTX_NOWITNESS | MTX_NOPROFILE);
486 for (i = 0; i < WITNESS_COUNT; i++)
487 witness_free(&w_data[i]);
488 for (i = 0; i < WITNESS_CHILDCOUNT; i++)
489 witness_child_free(&w_childdata[i]);
490 for (i = 0; i < LOCK_CHILDCOUNT; i++)
491 witness_lock_list_free(&w_locklistdata[i]);
493 /* First add in all the specified order lists. */
494 for (order = order_lists; order->w_name != NULL; order++) {
495 w = enroll(order->w_name, order->w_class);
498 w->w_file = "order list";
499 for (order++; order->w_name != NULL; order++) {
500 w1 = enroll(order->w_name, order->w_class);
503 w1->w_file = "order list";
504 if (!itismychild(w, w1))
505 panic("Not enough memory for static orders!");
509 witness_spin_warn = 1;
511 /* Iterate through all locks and add them to witness. */
512 while (!STAILQ_EMPTY(&pending_locks)) {
513 lock = STAILQ_FIRST(&pending_locks);
514 STAILQ_REMOVE_HEAD(&pending_locks, lo_list);
515 KASSERT(lock->lo_flags & LO_WITNESS,
516 ("%s: lock %s is on pending list but not LO_WITNESS",
517 __func__, lock->lo_name));
518 lock->lo_witness = enroll(lock->lo_type, LOCK_CLASS(lock));
521 /* Mark the witness code as being ready for use. */
526 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
529 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
533 value = witness_watch;
534 error = sysctl_handle_int(oidp, &value, 0, req);
535 if (error != 0 || req->newptr == NULL)
538 * XXXRW: Why a priv check here?
540 error = priv_check(req->td, PRIV_WITNESS);
543 if (value == witness_watch)
552 witness_init(struct lock_object *lock)
554 struct lock_class *class;
556 /* Various sanity checks. */
557 class = LOCK_CLASS(lock);
558 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
559 (class->lc_flags & LC_RECURSABLE) == 0)
560 panic("%s: lock (%s) %s can not be recursable", __func__,
561 class->lc_name, lock->lo_name);
562 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
563 (class->lc_flags & LC_SLEEPABLE) == 0)
564 panic("%s: lock (%s) %s can not be sleepable", __func__,
565 class->lc_name, lock->lo_name);
566 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
567 (class->lc_flags & LC_UPGRADABLE) == 0)
568 panic("%s: lock (%s) %s can not be upgradable", __func__,
569 class->lc_name, lock->lo_name);
572 * If we shouldn't watch this lock, then just clear lo_witness.
573 * Otherwise, if witness_cold is set, then it is too early to
574 * enroll this lock, so defer it to witness_initialize() by adding
575 * it to the pending_locks list. If it is not too early, then enroll
578 if (witness_watch == 0 || panicstr != NULL ||
579 (lock->lo_flags & LO_WITNESS) == 0)
580 lock->lo_witness = NULL;
581 else if (witness_cold) {
582 STAILQ_INSERT_TAIL(&pending_locks, lock, lo_list);
583 lock->lo_flags |= LO_ENROLLPEND;
585 lock->lo_witness = enroll(lock->lo_type, class);
589 witness_destroy(struct lock_object *lock)
591 struct lock_class *class;
594 class = LOCK_CLASS(lock);
596 panic("lock (%s) %s destroyed while witness_cold",
597 class->lc_name, lock->lo_name);
599 /* XXX: need to verify that no one holds the lock */
600 if ((lock->lo_flags & (LO_WITNESS | LO_ENROLLPEND)) == LO_WITNESS &&
601 lock->lo_witness != NULL) {
602 w = lock->lo_witness;
603 mtx_lock_spin(&w_mtx);
604 MPASS(w->w_refcount > 0);
608 * Lock is already released if we have an allocation failure
609 * and depart() fails.
611 if (w->w_refcount != 0 || depart(w))
612 mtx_unlock_spin(&w_mtx);
616 * If this lock is destroyed before witness is up and running,
617 * remove it from the pending list.
619 if (lock->lo_flags & LO_ENROLLPEND) {
620 STAILQ_REMOVE(&pending_locks, lock, lock_object, lo_list);
621 lock->lo_flags &= ~LO_ENROLLPEND;
627 witness_levelall (void)
629 struct witness_list *list;
630 struct witness *w, *w1;
633 * First clear all levels.
635 STAILQ_FOREACH(w, &w_all, w_list) {
640 * Look for locks with no parent and level all their descendants.
642 STAILQ_FOREACH(w, &w_all, w_list) {
644 * This is just an optimization, technically we could get
645 * away just walking the all list each time.
647 if (w->w_class->lc_flags & LC_SLEEPLOCK)
651 STAILQ_FOREACH(w1, list, w_typelist) {
652 if (isitmychild(w1, w))
655 witness_leveldescendents(w, 0);
657 ; /* silence GCC 3.x */
662 witness_leveldescendents(struct witness *parent, int level)
664 struct witness_child_list_entry *wcl;
667 if (parent->w_level < level)
668 parent->w_level = level;
670 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
671 for (i = 0; i < wcl->wcl_count; i++)
672 witness_leveldescendents(wcl->wcl_children[i], level);
676 witness_displaydescendants(void(*prnt)(const char *fmt, ...),
677 struct witness *parent, int indent)
679 struct witness_child_list_entry *wcl;
682 level = parent->w_level;
684 for (i = 0; i < indent; i++)
686 if (parent->w_refcount > 0)
687 prnt("%s", parent->w_name);
690 if (parent->w_displayed) {
691 prnt(" -- (already displayed)\n");
694 parent->w_displayed = 1;
695 if (parent->w_refcount > 0) {
696 if (parent->w_file != NULL)
697 prnt(" -- last acquired @ %s:%d", parent->w_file,
701 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
702 for (i = 0; i < wcl->wcl_count; i++)
703 witness_displaydescendants(prnt,
704 wcl->wcl_children[i], indent + 1);
708 witness_display_list(void(*prnt)(const char *fmt, ...),
709 struct witness_list *list)
713 STAILQ_FOREACH(w, list, w_typelist) {
714 if (w->w_file == NULL || w->w_level > 0)
717 * This lock has no anscestors, display its descendants.
719 witness_displaydescendants(prnt, w, 0);
724 witness_display(void(*prnt)(const char *fmt, ...))
728 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
731 /* Clear all the displayed flags. */
732 STAILQ_FOREACH(w, &w_all, w_list) {
737 * First, handle sleep locks which have been acquired at least
740 prnt("Sleep locks:\n");
741 witness_display_list(prnt, &w_sleep);
744 * Now do spin locks which have been acquired at least once.
746 prnt("\nSpin locks:\n");
747 witness_display_list(prnt, &w_spin);
750 * Finally, any locks which have not been acquired yet.
752 prnt("\nLocks which were never acquired:\n");
753 STAILQ_FOREACH(w, &w_all, w_list) {
754 if (w->w_file != NULL || w->w_refcount == 0)
756 prnt("%s\n", w->w_name);
761 /* Trim useless garbage from filenames. */
763 fixup_filename(const char *file)
768 while (strncmp(file, "../", 3) == 0)
774 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
777 if (witness_watch == 0 || panicstr != NULL)
780 /* Require locks that witness knows about. */
781 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
782 lock2->lo_witness == NULL)
785 MPASS(!mtx_owned(&w_mtx));
786 mtx_lock_spin(&w_mtx);
789 * If we already have either an explicit or implied lock order that
790 * is the other way around, then return an error.
792 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
793 mtx_unlock_spin(&w_mtx);
797 /* Try to add the new order. */
798 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
799 lock2->lo_type, lock1->lo_type);
800 if (!itismychild(lock1->lo_witness, lock2->lo_witness))
802 mtx_unlock_spin(&w_mtx);
807 witness_checkorder(struct lock_object *lock, int flags, const char *file,
810 struct lock_list_entry **lock_list, *lle;
811 struct lock_instance *lock1, *lock2;
812 struct lock_class *class;
813 struct witness *w, *w1;
817 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
822 * Try locks do not block if they fail to acquire the lock, thus
823 * there is no danger of deadlocks or of switching while holding a
824 * spin lock if we acquire a lock via a try operation. This
825 * function shouldn't even be called for try locks, so panic if
828 if (flags & LOP_TRYLOCK)
829 panic("%s should not be called for try lock operations",
832 w = lock->lo_witness;
833 class = LOCK_CLASS(lock);
835 file = fixup_filename(file);
837 if (class->lc_flags & LC_SLEEPLOCK) {
839 * Since spin locks include a critical section, this check
840 * implicitly enforces a lock order of all sleep locks before
843 if (td->td_critnest != 0 && !kdb_active)
844 panic("blockable sleep lock (%s) %s @ %s:%d",
845 class->lc_name, lock->lo_name, file, line);
848 * If this is the first lock acquired then just return as
849 * no order checking is needed.
851 if (td->td_sleeplocks == NULL)
853 lock_list = &td->td_sleeplocks;
856 * If this is the first lock, just return as no order
857 * checking is needed. We check this in both if clauses
858 * here as unifying the check would require us to use a
859 * critical section to ensure we don't migrate while doing
860 * the check. Note that if this is not the first lock, we
861 * are already in a critical section and are safe for the
864 if (PCPU_GET(spinlocks) == NULL)
866 lock_list = PCPU_PTR(spinlocks);
870 * Check to see if we are recursing on a lock we already own. If
871 * so, make sure that we don't mismatch exclusive and shared lock
874 lock1 = find_instance(*lock_list, lock);
876 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
877 (flags & LOP_EXCLUSIVE) == 0) {
878 printf("shared lock of (%s) %s @ %s:%d\n",
879 class->lc_name, lock->lo_name, file, line);
880 printf("while exclusively locked from %s:%d\n",
881 lock1->li_file, lock1->li_line);
882 panic("share->excl");
884 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
885 (flags & LOP_EXCLUSIVE) != 0) {
886 printf("exclusive lock of (%s) %s @ %s:%d\n",
887 class->lc_name, lock->lo_name, file, line);
888 printf("while share locked from %s:%d\n",
889 lock1->li_file, lock1->li_line);
890 panic("excl->share");
896 * Try locks do not block if they fail to acquire the lock, thus
897 * there is no danger of deadlocks or of switching while holding a
898 * spin lock if we acquire a lock via a try operation.
900 if (flags & LOP_TRYLOCK)
904 * Check for duplicate locks of the same type. Note that we only
905 * have to check for this on the last lock we just acquired. Any
906 * other cases will be caught as lock order violations.
908 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
909 w1 = lock1->li_lock->lo_witness;
911 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) ||
914 w->w_same_squawked = 1;
915 printf("acquiring duplicate lock of same type: \"%s\"\n",
917 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
918 lock1->li_file, lock1->li_line);
919 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
926 MPASS(!mtx_owned(&w_mtx));
927 mtx_lock_spin(&w_mtx);
929 * If we know that the the lock we are acquiring comes after
930 * the lock we most recently acquired in the lock order tree,
931 * then there is no need for any further checks.
933 if (isitmychild(w1, w)) {
934 mtx_unlock_spin(&w_mtx);
937 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
938 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
940 MPASS(j < WITNESS_COUNT);
941 lock1 = &lle->ll_children[i];
942 w1 = lock1->li_lock->lo_witness;
945 * If this lock doesn't undergo witness checking,
949 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
950 ("lock missing witness structure"));
954 * If we are locking Giant and this is a sleepable
955 * lock, then skip it.
957 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
958 lock == &Giant.mtx_object)
961 * If we are locking a sleepable lock and this lock
962 * is Giant, then skip it.
964 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
965 lock1->li_lock == &Giant.mtx_object)
968 * If we are locking a sleepable lock and this lock
969 * isn't sleepable, we want to treat it as a lock
970 * order violation to enfore a general lock order of
971 * sleepable locks before non-sleepable locks.
973 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
974 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
977 * If we are locking Giant and this is a non-sleepable
978 * lock, then treat it as a reversal.
980 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
981 lock == &Giant.mtx_object)
984 * Check the lock order hierarchy for a reveresal.
986 if (!isitmydescendant(w, w1))
990 * We have a lock order violation, check to see if it
991 * is allowed or has already been yelled about.
993 mtx_unlock_spin(&w_mtx);
996 * If the lock order is blessed, just bail. We don't
997 * look for other lock order violations though, which
1003 if (lock1->li_lock == &Giant.mtx_object) {
1004 if (w1->w_Giant_squawked)
1007 w1->w_Giant_squawked = 1;
1009 if (w1->w_other_squawked)
1012 w1->w_other_squawked = 1;
1015 * Ok, yell about it.
1017 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1018 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1020 "lock order reversal: (sleepable after non-sleepable)\n");
1021 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1022 && lock == &Giant.mtx_object)
1024 "lock order reversal: (Giant after non-sleepable)\n");
1026 printf("lock order reversal:\n");
1028 * Try to locate an earlier lock with
1029 * witness w in our list.
1032 lock2 = &lle->ll_children[i];
1033 MPASS(lock2->li_lock != NULL);
1034 if (lock2->li_lock->lo_witness == w)
1036 if (i == 0 && lle->ll_next != NULL) {
1038 i = lle->ll_count - 1;
1039 MPASS(i >= 0 && i < LOCK_NCHILDREN);
1044 printf(" 1st %p %s (%s) @ %s:%d\n",
1045 lock1->li_lock, lock1->li_lock->lo_name,
1046 lock1->li_lock->lo_type, lock1->li_file,
1048 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1049 lock->lo_name, lock->lo_type, file, line);
1051 printf(" 1st %p %s (%s) @ %s:%d\n",
1052 lock2->li_lock, lock2->li_lock->lo_name,
1053 lock2->li_lock->lo_type, lock2->li_file,
1055 printf(" 2nd %p %s (%s) @ %s:%d\n",
1056 lock1->li_lock, lock1->li_lock->lo_name,
1057 lock1->li_lock->lo_type, lock1->li_file,
1059 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1060 lock->lo_name, lock->lo_type, file, line);
1069 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
1071 * If requested, build a new lock order. However, don't build a new
1072 * relationship between a sleepable lock and Giant if it is in the
1073 * wrong direction. The correct lock order is that sleepable locks
1074 * always come before Giant.
1076 if (flags & LOP_NEWORDER &&
1077 !(lock1->li_lock == &Giant.mtx_object &&
1078 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1079 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1080 lock->lo_type, lock1->li_lock->lo_type);
1081 if (!itismychild(lock1->li_lock->lo_witness, w))
1082 /* Witness is dead. */
1085 mtx_unlock_spin(&w_mtx);
1093 kdb_enter(__func__);
1098 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1100 struct lock_list_entry **lock_list, *lle;
1101 struct lock_instance *instance;
1105 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1108 w = lock->lo_witness;
1110 file = fixup_filename(file);
1112 /* Determine lock list for this lock. */
1113 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1114 lock_list = &td->td_sleeplocks;
1116 lock_list = PCPU_PTR(spinlocks);
1118 /* Check to see if we are recursing on a lock we already own. */
1119 instance = find_instance(*lock_list, lock);
1120 if (instance != NULL) {
1121 instance->li_flags++;
1122 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1123 td->td_proc->p_pid, lock->lo_name,
1124 instance->li_flags & LI_RECURSEMASK);
1125 instance->li_file = file;
1126 instance->li_line = line;
1130 /* Update per-witness last file and line acquire. */
1134 /* Find the next open lock instance in the list and fill it. */
1136 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1137 lle = witness_lock_list_get();
1140 lle->ll_next = *lock_list;
1141 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1142 td->td_proc->p_pid, lle);
1145 instance = &lle->ll_children[lle->ll_count++];
1146 instance->li_lock = lock;
1147 instance->li_line = line;
1148 instance->li_file = file;
1149 if ((flags & LOP_EXCLUSIVE) != 0)
1150 instance->li_flags = LI_EXCLUSIVE;
1152 instance->li_flags = 0;
1153 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1154 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1158 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1160 struct lock_instance *instance;
1161 struct lock_class *class;
1163 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1164 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1166 class = LOCK_CLASS(lock);
1167 file = fixup_filename(file);
1168 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1169 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1170 class->lc_name, lock->lo_name, file, line);
1171 if ((flags & LOP_TRYLOCK) == 0)
1172 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
1173 lock->lo_name, file, line);
1174 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1175 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1176 class->lc_name, lock->lo_name, file, line);
1177 instance = find_instance(curthread->td_sleeplocks, lock);
1178 if (instance == NULL)
1179 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1180 class->lc_name, lock->lo_name, file, line);
1181 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1182 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1183 class->lc_name, lock->lo_name, file, line);
1184 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1185 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1186 class->lc_name, lock->lo_name,
1187 instance->li_flags & LI_RECURSEMASK, file, line);
1188 instance->li_flags |= LI_EXCLUSIVE;
1192 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1195 struct lock_instance *instance;
1196 struct lock_class *class;
1198 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1199 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1201 class = LOCK_CLASS(lock);
1202 file = fixup_filename(file);
1203 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1204 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1205 class->lc_name, lock->lo_name, file, line);
1206 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1207 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1208 class->lc_name, lock->lo_name, file, line);
1209 instance = find_instance(curthread->td_sleeplocks, lock);
1210 if (instance == NULL)
1211 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1212 class->lc_name, lock->lo_name, file, line);
1213 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1214 panic("downgrade of shared lock (%s) %s @ %s:%d",
1215 class->lc_name, lock->lo_name, file, line);
1216 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1217 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1218 class->lc_name, lock->lo_name,
1219 instance->li_flags & LI_RECURSEMASK, file, line);
1220 instance->li_flags &= ~LI_EXCLUSIVE;
1224 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1226 struct lock_list_entry **lock_list, *lle;
1227 struct lock_instance *instance;
1228 struct lock_class *class;
1233 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1237 class = LOCK_CLASS(lock);
1238 file = fixup_filename(file);
1240 /* Find lock instance associated with this lock. */
1241 if (class->lc_flags & LC_SLEEPLOCK)
1242 lock_list = &td->td_sleeplocks;
1244 lock_list = PCPU_PTR(spinlocks);
1245 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1246 for (i = 0; i < (*lock_list)->ll_count; i++) {
1247 instance = &(*lock_list)->ll_children[i];
1248 if (instance->li_lock == lock)
1251 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
1255 /* First, check for shared/exclusive mismatches. */
1256 if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1257 (flags & LOP_EXCLUSIVE) == 0) {
1258 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1259 lock->lo_name, file, line);
1260 printf("while exclusively locked from %s:%d\n",
1261 instance->li_file, instance->li_line);
1262 panic("excl->ushare");
1264 if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1265 (flags & LOP_EXCLUSIVE) != 0) {
1266 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1267 lock->lo_name, file, line);
1268 printf("while share locked from %s:%d\n", instance->li_file,
1270 panic("share->uexcl");
1273 /* If we are recursed, unrecurse. */
1274 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1275 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1276 td->td_proc->p_pid, instance->li_lock->lo_name,
1277 instance->li_flags);
1278 instance->li_flags--;
1282 /* Otherwise, remove this item from the list. */
1284 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1285 td->td_proc->p_pid, instance->li_lock->lo_name,
1286 (*lock_list)->ll_count - 1);
1287 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1288 (*lock_list)->ll_children[j] =
1289 (*lock_list)->ll_children[j + 1];
1290 (*lock_list)->ll_count--;
1293 /* If this lock list entry is now empty, free it. */
1294 if ((*lock_list)->ll_count == 0) {
1296 *lock_list = lle->ll_next;
1297 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1298 td->td_proc->p_pid, lle);
1299 witness_lock_list_free(lle);
1304 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1305 * exempt Giant and sleepable locks from the checks as well. If any
1306 * non-exempt locks are held, then a supplied message is printed to the
1307 * console along with a list of the offending locks. If indicated in the
1308 * flags then a failure results in a panic as well.
1311 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1313 struct lock_list_entry *lle;
1314 struct lock_instance *lock1;
1319 if (witness_cold || witness_watch == 0 || panicstr != NULL)
1323 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1324 for (i = lle->ll_count - 1; i >= 0; i--) {
1325 lock1 = &lle->ll_children[i];
1326 if (lock1->li_lock == lock)
1328 if (flags & WARN_GIANTOK &&
1329 lock1->li_lock == &Giant.mtx_object)
1331 if (flags & WARN_SLEEPOK &&
1332 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1338 printf(" with the following");
1339 if (flags & WARN_SLEEPOK)
1340 printf(" non-sleepable");
1341 printf(" locks held:\n");
1344 witness_list_lock(lock1);
1346 if (PCPU_GET(spinlocks) != NULL) {
1348 * Since we already hold a spinlock preemption is
1355 printf(" with the following");
1356 if (flags & WARN_SLEEPOK)
1357 printf(" non-sleepable");
1358 printf(" locks held:\n");
1360 n += witness_list_locks(PCPU_PTR(spinlocks));
1362 if (flags & WARN_PANIC && n)
1363 panic("witness_warn");
1365 else if (witness_kdb && n)
1366 kdb_enter(__func__);
1367 else if (witness_trace && n)
1374 witness_file(struct lock_object *lock)
1378 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1380 w = lock->lo_witness;
1385 witness_line(struct lock_object *lock)
1389 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1391 w = lock->lo_witness;
1395 static struct witness *
1396 enroll(const char *description, struct lock_class *lock_class)
1400 if (witness_watch == 0 || panicstr != NULL)
1402 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
1404 mtx_lock_spin(&w_mtx);
1405 STAILQ_FOREACH(w, &w_all, w_list) {
1406 if (w->w_name == description || (w->w_refcount > 0 &&
1407 strcmp(description, w->w_name) == 0)) {
1409 mtx_unlock_spin(&w_mtx);
1410 if (lock_class != w->w_class)
1412 "lock (%s) %s does not match earlier (%s) lock",
1413 description, lock_class->lc_name,
1414 w->w_class->lc_name);
1418 if ((w = witness_get()) == NULL)
1420 w->w_name = description;
1421 w->w_class = lock_class;
1423 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1424 if (lock_class->lc_flags & LC_SPINLOCK) {
1425 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1427 } else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1428 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1431 mtx_unlock_spin(&w_mtx);
1432 panic("lock class %s is not sleep or spin",
1433 lock_class->lc_name);
1435 mtx_unlock_spin(&w_mtx);
1438 * We issue a warning for any spin locks not defined in the static
1439 * order list as a way to discourage their use (folks should really
1440 * be using non-spin mutexes most of the time). However, several
1441 * 3rd part device drivers use spin locks because that is all they
1442 * have available on Windows and Linux and they think that normal
1443 * mutexes are insufficient.
1445 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn)
1446 printf("WITNESS: spin lock %s not in order list\n",
1451 /* Don't let the door bang you on the way out... */
1453 depart(struct witness *w)
1455 struct witness_child_list_entry *wcl, *nwcl;
1456 struct witness_list *list;
1457 struct witness *parent;
1459 MPASS(w->w_refcount == 0);
1460 if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1468 * First, we run through the entire tree looking for any
1469 * witnesses that the outgoing witness is a child of. For
1470 * each parent that we find, we reparent all the direct
1471 * children of the outgoing witness to its parent.
1473 STAILQ_FOREACH(parent, list, w_typelist) {
1474 if (!isitmychild(parent, w))
1476 removechild(parent, w);
1480 * Now we go through and free up the child list of the
1483 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1484 nwcl = wcl->wcl_next;
1486 witness_child_free(wcl);
1490 * Detach from various lists and free.
1492 STAILQ_REMOVE(list, w, witness, w_typelist);
1493 STAILQ_REMOVE(&w_all, w, witness, w_list);
1500 * Add "child" as a direct child of "parent". Returns false if
1501 * we fail due to out of memory.
1504 insertchild(struct witness *parent, struct witness *child)
1506 struct witness_child_list_entry **wcl;
1508 MPASS(child != NULL && parent != NULL);
1511 * Insert "child" after "parent"
1513 wcl = &parent->w_children;
1514 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1515 wcl = &(*wcl)->wcl_next;
1517 *wcl = witness_child_get();
1522 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1529 itismychild(struct witness *parent, struct witness *child)
1531 struct witness_list *list;
1533 MPASS(child != NULL && parent != NULL);
1534 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1535 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1537 "%s: parent (%s) and child (%s) are not the same lock type",
1538 __func__, parent->w_class->lc_name,
1539 child->w_class->lc_name);
1541 if (!insertchild(parent, child))
1544 if (parent->w_class->lc_flags & LC_SLEEPLOCK)
1552 removechild(struct witness *parent, struct witness *child)
1554 struct witness_child_list_entry **wcl, *wcl1;
1557 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1558 for (i = 0; i < (*wcl)->wcl_count; i++)
1559 if ((*wcl)->wcl_children[i] == child)
1563 (*wcl)->wcl_count--;
1564 if ((*wcl)->wcl_count > i)
1565 (*wcl)->wcl_children[i] =
1566 (*wcl)->wcl_children[(*wcl)->wcl_count];
1567 MPASS((*wcl)->wcl_children[i] != NULL);
1568 if ((*wcl)->wcl_count != 0)
1571 *wcl = wcl1->wcl_next;
1573 witness_child_free(wcl1);
1577 isitmychild(struct witness *parent, struct witness *child)
1579 struct witness_child_list_entry *wcl;
1582 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1583 for (i = 0; i < wcl->wcl_count; i++) {
1584 if (wcl->wcl_children[i] == child)
1592 isitmydescendant(struct witness *parent, struct witness *child)
1594 struct witness_child_list_entry *wcl;
1597 if (isitmychild(parent, child))
1600 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1602 for (i = 0; i < wcl->wcl_count; i++) {
1603 if (isitmydescendant(wcl->wcl_children[i], child))
1613 blessed(struct witness *w1, struct witness *w2)
1616 struct witness_blessed *b;
1618 for (i = 0; i < blessed_count; i++) {
1619 b = &blessed_list[i];
1620 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1621 if (strcmp(w2->w_name, b->b_lock2) == 0)
1625 if (strcmp(w1->w_name, b->b_lock2) == 0)
1626 if (strcmp(w2->w_name, b->b_lock1) == 0)
1633 static struct witness *
1638 if (witness_watch == 0) {
1639 mtx_unlock_spin(&w_mtx);
1642 if (STAILQ_EMPTY(&w_free)) {
1644 mtx_unlock_spin(&w_mtx);
1645 printf("%s: witness exhausted\n", __func__);
1648 w = STAILQ_FIRST(&w_free);
1649 STAILQ_REMOVE_HEAD(&w_free, w_list);
1651 bzero(w, sizeof(*w));
1656 witness_free(struct witness *w)
1659 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1663 static struct witness_child_list_entry *
1664 witness_child_get(void)
1666 struct witness_child_list_entry *wcl;
1668 if (witness_watch == 0) {
1669 mtx_unlock_spin(&w_mtx);
1675 mtx_unlock_spin(&w_mtx);
1676 printf("%s: witness exhausted\n", __func__);
1679 w_child_free = wcl->wcl_next;
1681 bzero(wcl, sizeof(*wcl));
1686 witness_child_free(struct witness_child_list_entry *wcl)
1689 wcl->wcl_next = w_child_free;
1694 static struct lock_list_entry *
1695 witness_lock_list_get(void)
1697 struct lock_list_entry *lle;
1699 if (witness_watch == 0)
1701 mtx_lock_spin(&w_mtx);
1702 lle = w_lock_list_free;
1705 mtx_unlock_spin(&w_mtx);
1706 printf("%s: witness exhausted\n", __func__);
1709 w_lock_list_free = lle->ll_next;
1710 mtx_unlock_spin(&w_mtx);
1711 bzero(lle, sizeof(*lle));
1716 witness_lock_list_free(struct lock_list_entry *lle)
1719 mtx_lock_spin(&w_mtx);
1720 lle->ll_next = w_lock_list_free;
1721 w_lock_list_free = lle;
1722 mtx_unlock_spin(&w_mtx);
1725 static struct lock_instance *
1726 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1728 struct lock_list_entry *lle;
1729 struct lock_instance *instance;
1732 for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1733 for (i = lle->ll_count - 1; i >= 0; i--) {
1734 instance = &lle->ll_children[i];
1735 if (instance->li_lock == lock)
1742 witness_list_lock(struct lock_instance *instance)
1744 struct lock_object *lock;
1746 lock = instance->li_lock;
1747 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1748 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
1749 if (lock->lo_type != lock->lo_name)
1750 printf(" (%s)", lock->lo_type);
1751 printf(" r = %d (%p) locked @ %s:%d\n",
1752 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1758 witness_thread_has_locks(struct thread *td)
1761 return (td->td_sleeplocks != NULL);
1765 witness_proc_has_locks(struct proc *p)
1769 FOREACH_THREAD_IN_PROC(p, td) {
1770 if (witness_thread_has_locks(td))
1778 witness_list_locks(struct lock_list_entry **lock_list)
1780 struct lock_list_entry *lle;
1784 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1785 for (i = lle->ll_count - 1; i >= 0; i--) {
1786 witness_list_lock(&lle->ll_children[i]);
1793 * This is a bit risky at best. We call this function when we have timed
1794 * out acquiring a spin lock, and we assume that the other CPU is stuck
1795 * with this lock held. So, we go groveling around in the other CPU's
1796 * per-cpu data to try to find the lock instance for this spin lock to
1797 * see when it was last acquired.
1800 witness_display_spinlock(struct lock_object *lock, struct thread *owner)
1802 struct lock_instance *instance;
1805 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
1807 pc = pcpu_find(owner->td_oncpu);
1808 instance = find_instance(pc->pc_spinlocks, lock);
1809 if (instance != NULL)
1810 witness_list_lock(instance);
1814 witness_save(struct lock_object *lock, const char **filep, int *linep)
1816 struct lock_list_entry *lock_list;
1817 struct lock_instance *instance;
1818 struct lock_class *class;
1820 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1821 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1823 class = LOCK_CLASS(lock);
1824 if (class->lc_flags & LC_SLEEPLOCK)
1825 lock_list = curthread->td_sleeplocks;
1827 if (witness_skipspin)
1829 lock_list = PCPU_GET(spinlocks);
1831 instance = find_instance(lock_list, lock);
1832 if (instance == NULL)
1833 panic("%s: lock (%s) %s not locked", __func__,
1834 class->lc_name, lock->lo_name);
1835 *filep = instance->li_file;
1836 *linep = instance->li_line;
1840 witness_restore(struct lock_object *lock, const char *file, int line)
1842 struct lock_list_entry *lock_list;
1843 struct lock_instance *instance;
1844 struct lock_class *class;
1846 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1847 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1849 class = LOCK_CLASS(lock);
1850 if (class->lc_flags & LC_SLEEPLOCK)
1851 lock_list = curthread->td_sleeplocks;
1853 if (witness_skipspin)
1855 lock_list = PCPU_GET(spinlocks);
1857 instance = find_instance(lock_list, lock);
1858 if (instance == NULL)
1859 panic("%s: lock (%s) %s not locked", __func__,
1860 class->lc_name, lock->lo_name);
1861 lock->lo_witness->w_file = file;
1862 lock->lo_witness->w_line = line;
1863 instance->li_file = file;
1864 instance->li_line = line;
1868 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1870 #ifdef INVARIANT_SUPPORT
1871 struct lock_instance *instance;
1872 struct lock_class *class;
1874 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1876 class = LOCK_CLASS(lock);
1877 if ((class->lc_flags & LC_SLEEPLOCK) != 0)
1878 instance = find_instance(curthread->td_sleeplocks, lock);
1879 else if ((class->lc_flags & LC_SPINLOCK) != 0)
1880 instance = find_instance(PCPU_GET(spinlocks), lock);
1882 panic("Lock (%s) %s is not sleep or spin!",
1883 class->lc_name, lock->lo_name);
1885 file = fixup_filename(file);
1888 if (instance != NULL)
1889 panic("Lock (%s) %s locked @ %s:%d.",
1890 class->lc_name, lock->lo_name, file, line);
1893 case LA_LOCKED | LA_RECURSED:
1894 case LA_LOCKED | LA_NOTRECURSED:
1896 case LA_SLOCKED | LA_RECURSED:
1897 case LA_SLOCKED | LA_NOTRECURSED:
1899 case LA_XLOCKED | LA_RECURSED:
1900 case LA_XLOCKED | LA_NOTRECURSED:
1901 if (instance == NULL) {
1902 panic("Lock (%s) %s not locked @ %s:%d.",
1903 class->lc_name, lock->lo_name, file, line);
1906 if ((flags & LA_XLOCKED) != 0 &&
1907 (instance->li_flags & LI_EXCLUSIVE) == 0)
1908 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1909 class->lc_name, lock->lo_name, file, line);
1910 if ((flags & LA_SLOCKED) != 0 &&
1911 (instance->li_flags & LI_EXCLUSIVE) != 0)
1912 panic("Lock (%s) %s exclusively locked @ %s:%d.",
1913 class->lc_name, lock->lo_name, file, line);
1914 if ((flags & LA_RECURSED) != 0 &&
1915 (instance->li_flags & LI_RECURSEMASK) == 0)
1916 panic("Lock (%s) %s not recursed @ %s:%d.",
1917 class->lc_name, lock->lo_name, file, line);
1918 if ((flags & LA_NOTRECURSED) != 0 &&
1919 (instance->li_flags & LI_RECURSEMASK) != 0)
1920 panic("Lock (%s) %s recursed @ %s:%d.",
1921 class->lc_name, lock->lo_name, file, line);
1924 panic("Invalid lock assertion at %s:%d.", file, line);
1927 #endif /* INVARIANT_SUPPORT */
1932 witness_list(struct thread *td)
1935 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1936 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
1938 if (witness_watch == 0)
1941 witness_list_locks(&td->td_sleeplocks);
1944 * We only handle spinlocks if td == curthread. This is somewhat broken
1945 * if td is currently executing on some other CPU and holds spin locks
1946 * as we won't display those locks. If we had a MI way of getting
1947 * the per-cpu data for a given cpu then we could use
1948 * td->td_oncpu to get the list of spinlocks for this thread
1951 * That still wouldn't really fix this unless we locked sched_lock
1952 * or stopped the other CPU to make sure it wasn't changing the list
1953 * out from under us. It is probably best to just not try to handle
1954 * threads on other CPU's for now.
1956 if (td == curthread && PCPU_GET(spinlocks) != NULL)
1957 witness_list_locks(PCPU_PTR(spinlocks));
1960 DB_SHOW_COMMAND(locks, db_witness_list)
1965 td = db_lookup_thread(addr, TRUE);
1971 DB_SHOW_COMMAND(alllocks, db_witness_list_all)
1977 * It would be nice to list only threads and processes that actually
1978 * held sleep locks, but that information is currently not exported
1981 FOREACH_PROC_IN_SYSTEM(p) {
1982 if (!witness_proc_has_locks(p))
1984 FOREACH_THREAD_IN_PROC(p, td) {
1985 if (!witness_thread_has_locks(td))
1987 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
1988 p->p_comm, td, td->td_tid);
1994 DB_SHOW_COMMAND(witness, db_witness_display)
1997 witness_display(db_printf);