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>
99 #include <sys/sysctl.h>
100 #include <sys/systm.h>
104 #include <machine/stdarg.h>
106 /* Define this to check for blessed mutexes */
109 #define WITNESS_COUNT 1024
110 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
112 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
113 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should
114 * probably be safe for the most part, but it's still a SWAG.
116 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
118 #define WITNESS_NCHILDREN 6
120 struct witness_child_list_entry;
124 struct lock_class *w_class;
125 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */
126 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */
127 struct witness_child_list_entry *w_children; /* Great evilness... */
132 u_char w_Giant_squawked:1;
133 u_char w_other_squawked:1;
134 u_char w_same_squawked:1;
135 u_char w_displayed:1;
138 struct witness_child_list_entry {
139 struct witness_child_list_entry *wcl_next;
140 struct witness *wcl_children[WITNESS_NCHILDREN];
144 STAILQ_HEAD(witness_list, witness);
147 struct witness_blessed {
153 struct witness_order_list_entry {
155 struct lock_class *w_class;
159 static int blessed(struct witness *, struct witness *);
161 static int depart(struct witness *w);
162 static struct witness *enroll(const char *description,
163 struct lock_class *lock_class);
164 static int insertchild(struct witness *parent, struct witness *child);
165 static int isitmychild(struct witness *parent, struct witness *child);
166 static int isitmydescendant(struct witness *parent, struct witness *child);
167 static int itismychild(struct witness *parent, struct witness *child);
168 static int rebalancetree(struct witness_list *list);
169 static void removechild(struct witness *parent, struct witness *child);
170 static int reparentchildren(struct witness *newparent,
171 struct witness *oldparent);
172 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
173 static void witness_displaydescendants(void(*)(const char *fmt, ...),
174 struct witness *, int indent);
175 static const char *fixup_filename(const char *file);
176 static void witness_leveldescendents(struct witness *parent, int level);
177 static void witness_levelall(void);
178 static struct witness *witness_get(void);
179 static void witness_free(struct witness *m);
180 static struct witness_child_list_entry *witness_child_get(void);
181 static void witness_child_free(struct witness_child_list_entry *wcl);
182 static struct lock_list_entry *witness_lock_list_get(void);
183 static void witness_lock_list_free(struct lock_list_entry *lle);
184 static struct lock_instance *find_instance(struct lock_list_entry *lock_list,
185 struct lock_object *lock);
186 static void witness_list_lock(struct lock_instance *instance);
188 static void witness_list(struct thread *td);
189 static void witness_display_list(void(*prnt)(const char *fmt, ...),
190 struct witness_list *list);
191 static void witness_display(void(*)(const char *fmt, ...));
194 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking");
197 * If set to 0, witness is disabled. If set to 1, witness performs full lock
198 * order checking for all locks. If set to 2 or higher, then witness skips
199 * the full lock order check if the lock being acquired is at a higher level
200 * (i.e. farther down in the tree) than the current lock. This last mode is
201 * somewhat experimental and not considered fully safe. At runtime, this
202 * value may be set to 0 to turn off witness. witness is not allowed be
203 * turned on once it is turned off, however.
205 static int witness_watch = 1;
206 TUNABLE_INT("debug.witness.watch", &witness_watch);
207 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
208 sysctl_debug_witness_watch, "I", "witness is watching lock operations");
212 * When KDB is enabled and witness_kdb is set to 1, it will cause the system
213 * to drop into kdebug() when:
214 * - a lock heirarchy violation occurs
215 * - locks are held when going to sleep.
222 TUNABLE_INT("debug.witness.kdb", &witness_kdb);
223 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
226 * When KDB is enabled and witness_trace is set to 1, it will cause the system
227 * to print a stack trace:
228 * - a lock heirarchy violation occurs
229 * - locks are held when going to sleep.
231 int witness_trace = 1;
232 TUNABLE_INT("debug.witness.trace", &witness_trace);
233 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
236 #ifdef WITNESS_SKIPSPIN
237 int witness_skipspin = 1;
239 int witness_skipspin = 0;
241 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
242 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN,
243 &witness_skipspin, 0, "");
245 static struct mtx w_mtx;
246 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
247 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
248 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
249 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
250 static struct witness_child_list_entry *w_child_free = NULL;
251 static struct lock_list_entry *w_lock_list_free = NULL;
253 static struct witness w_data[WITNESS_COUNT];
254 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
255 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
257 static struct witness_order_list_entry order_lists[] = {
258 { "proctree", &lock_class_sx },
259 { "allproc", &lock_class_sx },
260 { "Giant", &lock_class_mtx_sleep },
261 { "filedesc structure", &lock_class_mtx_sleep },
262 { "pipe mutex", &lock_class_mtx_sleep },
263 { "sigio lock", &lock_class_mtx_sleep },
264 { "process group", &lock_class_mtx_sleep },
265 { "process lock", &lock_class_mtx_sleep },
266 { "session", &lock_class_mtx_sleep },
267 { "uidinfo hash", &lock_class_mtx_sleep },
268 { "uidinfo struct", &lock_class_mtx_sleep },
269 { "allprison", &lock_class_mtx_sleep },
274 { "filedesc structure", &lock_class_mtx_sleep },
275 { "accept", &lock_class_mtx_sleep },
276 { "so_snd", &lock_class_mtx_sleep },
277 { "so_rcv", &lock_class_mtx_sleep },
278 { "sellck", &lock_class_mtx_sleep },
283 { "so_rcv", &lock_class_mtx_sleep },
284 { "radix node head", &lock_class_mtx_sleep },
285 { "rtentry", &lock_class_mtx_sleep },
286 { "ifaddr", &lock_class_mtx_sleep },
289 * UNIX Domain Sockets
291 { "unp", &lock_class_mtx_sleep },
292 { "so_snd", &lock_class_mtx_sleep },
297 { "udp", &lock_class_mtx_sleep },
298 { "udpinp", &lock_class_mtx_sleep },
299 { "so_snd", &lock_class_mtx_sleep },
304 { "tcp", &lock_class_mtx_sleep },
305 { "tcpinp", &lock_class_mtx_sleep },
306 { "so_snd", &lock_class_mtx_sleep },
311 { "slip_mtx", &lock_class_mtx_sleep },
312 { "slip sc_mtx", &lock_class_mtx_sleep },
317 { "ddp_list_mtx", &lock_class_mtx_sleep },
318 { "ddp_mtx", &lock_class_mtx_sleep },
323 { "bpf global lock", &lock_class_mtx_sleep },
324 { "bpf interface lock", &lock_class_mtx_sleep },
325 { "bpf cdev lock", &lock_class_mtx_sleep },
330 { "nfsd_mtx", &lock_class_mtx_sleep },
331 { "so_snd", &lock_class_mtx_sleep },
336 { "system map", &lock_class_mtx_sleep },
337 { "vm page queue mutex", &lock_class_mtx_sleep },
338 { "vnode interlock", &lock_class_mtx_sleep },
339 { "cdev", &lock_class_mtx_sleep },
345 { "ap boot", &lock_class_mtx_spin },
347 { "sio", &lock_class_mtx_spin },
349 { "cy", &lock_class_mtx_spin },
351 { "uart_hwmtx", &lock_class_mtx_spin },
352 { "sabtty", &lock_class_mtx_spin },
353 { "zstty", &lock_class_mtx_spin },
354 { "ng_node", &lock_class_mtx_spin },
355 { "ng_worklist", &lock_class_mtx_spin },
356 { "taskqueue_fast", &lock_class_mtx_spin },
357 { "intr table", &lock_class_mtx_spin },
358 { "ithread table lock", &lock_class_mtx_spin },
359 { "sleepq chain", &lock_class_mtx_spin },
360 { "sched lock", &lock_class_mtx_spin },
361 { "turnstile chain", &lock_class_mtx_spin },
362 { "td_contested", &lock_class_mtx_spin },
363 { "callout", &lock_class_mtx_spin },
364 { "entropy harvest mutex", &lock_class_mtx_spin },
368 { "allpmaps", &lock_class_mtx_spin },
369 { "vm page queue free mutex", &lock_class_mtx_spin },
370 { "icu", &lock_class_mtx_spin },
372 { "smp rendezvous", &lock_class_mtx_spin },
373 #if defined(__i386__) || defined(__amd64__)
374 { "tlb", &lock_class_mtx_spin },
377 { "ipi", &lock_class_mtx_spin },
378 { "rtc_mtx", &lock_class_mtx_spin },
381 { "clk", &lock_class_mtx_spin },
382 { "mutex profiling lock", &lock_class_mtx_spin },
383 { "kse zombie lock", &lock_class_mtx_spin },
384 { "ALD Queue", &lock_class_mtx_spin },
386 { "MCA spin lock", &lock_class_mtx_spin },
388 #if defined(__i386__) || defined(__amd64__)
389 { "pcicfg", &lock_class_mtx_spin },
390 { "NDIS thread lock", &lock_class_mtx_spin },
392 { "tw_osl_io_lock", &lock_class_mtx_spin },
393 { "tw_osl_q_lock", &lock_class_mtx_spin },
394 { "tw_cl_io_lock", &lock_class_mtx_spin },
395 { "tw_cl_intr_lock", &lock_class_mtx_spin },
396 { "tw_cl_gen_lock", &lock_class_mtx_spin },
403 * Pairs of locks which have been blessed
404 * Don't complain about order problems with blessed locks
406 static struct witness_blessed blessed_list[] = {
408 static int blessed_count =
409 sizeof(blessed_list) / sizeof(struct witness_blessed);
413 * List of all locks in the system.
415 TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks);
417 static struct mtx all_mtx = {
418 { &lock_class_mtx_sleep, /* mtx_object.lo_class */
419 "All locks list", /* mtx_object.lo_name */
420 "All locks list", /* mtx_object.lo_type */
421 LO_INITIALIZED, /* mtx_object.lo_flags */
422 { NULL, NULL }, /* mtx_object.lo_list */
423 NULL }, /* mtx_object.lo_witness */
424 MTX_UNOWNED, 0 /* mtx_lock, mtx_recurse */
428 * This global is set to 0 once it becomes safe to use the witness code.
430 static int witness_cold = 1;
433 * Global variables for book keeping.
435 static int lock_cur_cnt;
436 static int lock_max_cnt;
439 * The WITNESS-enabled diagnostic code.
442 witness_initialize(void *dummy __unused)
444 struct lock_object *lock;
445 struct witness_order_list_entry *order;
446 struct witness *w, *w1;
450 * We have to release Giant before initializing its witness
451 * structure so that WITNESS doesn't get confused.
454 mtx_assert(&Giant, MA_NOTOWNED);
456 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
457 TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
458 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
460 for (i = 0; i < WITNESS_COUNT; i++)
461 witness_free(&w_data[i]);
462 for (i = 0; i < WITNESS_CHILDCOUNT; i++)
463 witness_child_free(&w_childdata[i]);
464 for (i = 0; i < LOCK_CHILDCOUNT; i++)
465 witness_lock_list_free(&w_locklistdata[i]);
467 /* First add in all the specified order lists. */
468 for (order = order_lists; order->w_name != NULL; order++) {
469 w = enroll(order->w_name, order->w_class);
472 w->w_file = "order list";
473 for (order++; order->w_name != NULL; order++) {
474 w1 = enroll(order->w_name, order->w_class);
477 w1->w_file = "order list";
478 if (!itismychild(w, w1))
479 panic("Not enough memory for static orders!");
484 /* Iterate through all locks and add them to witness. */
486 TAILQ_FOREACH(lock, &all_locks, lo_list) {
487 if (lock->lo_flags & LO_WITNESS)
488 lock->lo_witness = enroll(lock->lo_type,
491 lock->lo_witness = NULL;
493 mtx_unlock(&all_mtx);
495 /* Mark the witness code as being ready for use. */
496 atomic_store_rel_int(&witness_cold, 0);
500 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
503 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
507 value = witness_watch;
508 error = sysctl_handle_int(oidp, &value, 0, req);
509 if (error != 0 || req->newptr == NULL)
511 error = suser(req->td);
514 if (value == witness_watch)
523 witness_init(struct lock_object *lock)
525 struct lock_class *class;
527 class = lock->lo_class;
528 if (lock->lo_flags & LO_INITIALIZED)
529 panic("%s: lock (%s) %s is already initialized", __func__,
530 class->lc_name, lock->lo_name);
531 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
532 (class->lc_flags & LC_RECURSABLE) == 0)
533 panic("%s: lock (%s) %s can not be recursable", __func__,
534 class->lc_name, lock->lo_name);
535 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
536 (class->lc_flags & LC_SLEEPABLE) == 0)
537 panic("%s: lock (%s) %s can not be sleepable", __func__,
538 class->lc_name, lock->lo_name);
539 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
540 (class->lc_flags & LC_UPGRADABLE) == 0)
541 panic("%s: lock (%s) %s can not be upgradable", __func__,
542 class->lc_name, lock->lo_name);
545 TAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
546 lock->lo_flags |= LO_INITIALIZED;
548 if (lock_cur_cnt > lock_max_cnt)
549 lock_max_cnt = lock_cur_cnt;
550 mtx_unlock(&all_mtx);
551 if (!witness_cold && witness_watch != 0 && panicstr == NULL &&
552 (lock->lo_flags & LO_WITNESS) != 0)
553 lock->lo_witness = enroll(lock->lo_type, class);
555 lock->lo_witness = NULL;
559 witness_destroy(struct lock_object *lock)
564 panic("lock (%s) %s destroyed while witness_cold",
565 lock->lo_class->lc_name, lock->lo_name);
566 if ((lock->lo_flags & LO_INITIALIZED) == 0)
567 panic("%s: lock (%s) %s is not initialized", __func__,
568 lock->lo_class->lc_name, lock->lo_name);
570 /* XXX: need to verify that no one holds the lock */
571 w = lock->lo_witness;
573 mtx_lock_spin(&w_mtx);
574 MPASS(w->w_refcount > 0);
578 * Lock is already released if we have an allocation failure
579 * and depart() fails.
581 if (w->w_refcount != 0 || depart(w))
582 mtx_unlock_spin(&w_mtx);
587 TAILQ_REMOVE(&all_locks, lock, lo_list);
588 lock->lo_flags &= ~LO_INITIALIZED;
589 mtx_unlock(&all_mtx);
594 witness_display_list(void(*prnt)(const char *fmt, ...),
595 struct witness_list *list)
599 STAILQ_FOREACH(w, list, w_typelist) {
600 if (w->w_file == NULL || w->w_level > 0)
603 * This lock has no anscestors, display its descendants.
605 witness_displaydescendants(prnt, w, 0);
610 witness_display(void(*prnt)(const char *fmt, ...))
614 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
617 /* Clear all the displayed flags. */
618 STAILQ_FOREACH(w, &w_all, w_list) {
623 * First, handle sleep locks which have been acquired at least
626 prnt("Sleep locks:\n");
627 witness_display_list(prnt, &w_sleep);
630 * Now do spin locks which have been acquired at least once.
632 prnt("\nSpin locks:\n");
633 witness_display_list(prnt, &w_spin);
636 * Finally, any locks which have not been acquired yet.
638 prnt("\nLocks which were never acquired:\n");
639 STAILQ_FOREACH(w, &w_all, w_list) {
640 if (w->w_file != NULL || w->w_refcount == 0)
642 prnt("%s\n", w->w_name);
647 /* Trim useless garbage from filenames. */
649 fixup_filename(const char *file)
654 while (strncmp(file, "../", 3) == 0)
660 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
663 if (witness_watch == 0 || panicstr != NULL)
666 /* Require locks that witness knows about. */
667 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
668 lock2->lo_witness == NULL)
671 MPASS(!mtx_owned(&w_mtx));
672 mtx_lock_spin(&w_mtx);
675 * If we already have either an explicit or implied lock order that
676 * is the other way around, then return an error.
678 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
679 mtx_unlock_spin(&w_mtx);
683 /* Try to add the new order. */
684 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
685 lock2->lo_type, lock1->lo_type);
686 if (!itismychild(lock1->lo_witness, lock2->lo_witness))
688 mtx_unlock_spin(&w_mtx);
693 witness_checkorder(struct lock_object *lock, int flags, const char *file,
696 struct lock_list_entry **lock_list, *lle;
697 struct lock_instance *lock1, *lock2;
698 struct lock_class *class;
699 struct witness *w, *w1;
703 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
708 * Try locks do not block if they fail to acquire the lock, thus
709 * there is no danger of deadlocks or of switching while holding a
710 * spin lock if we acquire a lock via a try operation. This
711 * function shouldn't even be called for try locks, so panic if
714 if (flags & LOP_TRYLOCK)
715 panic("%s should not be called for try lock operations",
718 w = lock->lo_witness;
719 class = lock->lo_class;
721 file = fixup_filename(file);
723 if (class->lc_flags & LC_SLEEPLOCK) {
725 * Since spin locks include a critical section, this check
726 * implicitly enforces a lock order of all sleep locks before
729 if (td->td_critnest != 0 && !kdb_active)
730 panic("blockable sleep lock (%s) %s @ %s:%d",
731 class->lc_name, lock->lo_name, file, line);
734 * If this is the first lock acquired then just return as
735 * no order checking is needed.
737 if (td->td_sleeplocks == NULL)
739 lock_list = &td->td_sleeplocks;
742 * If this is the first lock, just return as no order
743 * checking is needed. We check this in both if clauses
744 * here as unifying the check would require us to use a
745 * critical section to ensure we don't migrate while doing
746 * the check. Note that if this is not the first lock, we
747 * are already in a critical section and are safe for the
750 if (PCPU_GET(spinlocks) == NULL)
752 lock_list = PCPU_PTR(spinlocks);
756 * Check to see if we are recursing on a lock we already own. If
757 * so, make sure that we don't mismatch exclusive and shared lock
760 lock1 = find_instance(*lock_list, lock);
762 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
763 (flags & LOP_EXCLUSIVE) == 0) {
764 printf("shared lock of (%s) %s @ %s:%d\n",
765 class->lc_name, lock->lo_name, file, line);
766 printf("while exclusively locked from %s:%d\n",
767 lock1->li_file, lock1->li_line);
768 panic("share->excl");
770 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
771 (flags & LOP_EXCLUSIVE) != 0) {
772 printf("exclusive lock of (%s) %s @ %s:%d\n",
773 class->lc_name, lock->lo_name, file, line);
774 printf("while share locked from %s:%d\n",
775 lock1->li_file, lock1->li_line);
776 panic("excl->share");
782 * Try locks do not block if they fail to acquire the lock, thus
783 * there is no danger of deadlocks or of switching while holding a
784 * spin lock if we acquire a lock via a try operation.
786 if (flags & LOP_TRYLOCK)
790 * Check for duplicate locks of the same type. Note that we only
791 * have to check for this on the last lock we just acquired. Any
792 * other cases will be caught as lock order violations.
794 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
795 w1 = lock1->li_lock->lo_witness;
797 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) ||
800 w->w_same_squawked = 1;
801 printf("acquiring duplicate lock of same type: \"%s\"\n",
803 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
804 lock1->li_file, lock1->li_line);
805 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
812 MPASS(!mtx_owned(&w_mtx));
813 mtx_lock_spin(&w_mtx);
815 * If we have a known higher number just say ok
817 if (witness_watch > 1 && w->w_level > w1->w_level) {
818 mtx_unlock_spin(&w_mtx);
822 * If we know that the the lock we are acquiring comes after
823 * the lock we most recently acquired in the lock order tree,
824 * then there is no need for any further checks.
826 if (isitmydescendant(w1, w)) {
827 mtx_unlock_spin(&w_mtx);
830 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
831 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
833 MPASS(j < WITNESS_COUNT);
834 lock1 = &lle->ll_children[i];
835 w1 = lock1->li_lock->lo_witness;
838 * If this lock doesn't undergo witness checking,
842 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
843 ("lock missing witness structure"));
847 * If we are locking Giant and this is a sleepable
848 * lock, then skip it.
850 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
851 lock == &Giant.mtx_object)
854 * If we are locking a sleepable lock and this lock
855 * is Giant, then skip it.
857 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
858 lock1->li_lock == &Giant.mtx_object)
861 * If we are locking a sleepable lock and this lock
862 * isn't sleepable, we want to treat it as a lock
863 * order violation to enfore a general lock order of
864 * sleepable locks before non-sleepable locks.
866 if (!((lock->lo_flags & LO_SLEEPABLE) != 0 &&
867 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
869 * Check the lock order hierarchy for a reveresal.
871 if (!isitmydescendant(w, w1))
874 * We have a lock order violation, check to see if it
875 * is allowed or has already been yelled about.
877 mtx_unlock_spin(&w_mtx);
880 * If the lock order is blessed, just bail. We don't
881 * look for other lock order violations though, which
887 if (lock1->li_lock == &Giant.mtx_object) {
888 if (w1->w_Giant_squawked)
891 w1->w_Giant_squawked = 1;
893 if (w1->w_other_squawked)
896 w1->w_other_squawked = 1;
901 printf("lock order reversal\n");
903 * Try to locate an earlier lock with
904 * witness w in our list.
907 lock2 = &lle->ll_children[i];
908 MPASS(lock2->li_lock != NULL);
909 if (lock2->li_lock->lo_witness == w)
911 if (i == 0 && lle->ll_next != NULL) {
913 i = lle->ll_count - 1;
914 MPASS(i >= 0 && i < LOCK_NCHILDREN);
919 printf(" 1st %p %s (%s) @ %s:%d\n",
920 lock1->li_lock, lock1->li_lock->lo_name,
921 lock1->li_lock->lo_type, lock1->li_file,
923 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
924 lock->lo_name, lock->lo_type, file, line);
926 printf(" 1st %p %s (%s) @ %s:%d\n",
927 lock2->li_lock, lock2->li_lock->lo_name,
928 lock2->li_lock->lo_type, lock2->li_file,
930 printf(" 2nd %p %s (%s) @ %s:%d\n",
931 lock1->li_lock, lock1->li_lock->lo_name,
932 lock1->li_lock->lo_type, lock1->li_file,
934 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
935 lock->lo_name, lock->lo_type, file, line);
944 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
946 * If requested, build a new lock order. However, don't build a new
947 * relationship between a sleepable lock and Giant if it is in the
948 * wrong direction. The correct lock order is that sleepable locks
949 * always come before Giant.
951 if (flags & LOP_NEWORDER &&
952 !(lock1->li_lock == &Giant.mtx_object &&
953 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
954 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
955 lock->lo_type, lock1->li_lock->lo_type);
956 if (!itismychild(lock1->li_lock->lo_witness, w))
957 /* Witness is dead. */
960 mtx_unlock_spin(&w_mtx);
973 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
975 struct lock_list_entry **lock_list, *lle;
976 struct lock_instance *instance;
980 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
983 w = lock->lo_witness;
985 file = fixup_filename(file);
987 /* Determine lock list for this lock. */
988 if (lock->lo_class->lc_flags & LC_SLEEPLOCK)
989 lock_list = &td->td_sleeplocks;
991 lock_list = PCPU_PTR(spinlocks);
993 /* Check to see if we are recursing on a lock we already own. */
994 instance = find_instance(*lock_list, lock);
995 if (instance != NULL) {
996 instance->li_flags++;
997 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
998 td->td_proc->p_pid, lock->lo_name,
999 instance->li_flags & LI_RECURSEMASK);
1000 instance->li_file = file;
1001 instance->li_line = line;
1005 /* Update per-witness last file and line acquire. */
1009 /* Find the next open lock instance in the list and fill it. */
1011 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1012 lle = witness_lock_list_get();
1015 lle->ll_next = *lock_list;
1016 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1017 td->td_proc->p_pid, lle);
1020 instance = &lle->ll_children[lle->ll_count++];
1021 instance->li_lock = lock;
1022 instance->li_line = line;
1023 instance->li_file = file;
1024 if ((flags & LOP_EXCLUSIVE) != 0)
1025 instance->li_flags = LI_EXCLUSIVE;
1027 instance->li_flags = 0;
1028 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1029 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1033 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1035 struct lock_instance *instance;
1036 struct lock_class *class;
1038 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1039 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1041 class = lock->lo_class;
1042 file = fixup_filename(file);
1043 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1044 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1045 class->lc_name, lock->lo_name, file, line);
1046 if ((flags & LOP_TRYLOCK) == 0)
1047 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
1048 lock->lo_name, file, line);
1049 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1050 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1051 class->lc_name, lock->lo_name, file, line);
1052 instance = find_instance(curthread->td_sleeplocks, lock);
1053 if (instance == NULL)
1054 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1055 class->lc_name, lock->lo_name, file, line);
1056 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1057 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1058 class->lc_name, lock->lo_name, file, line);
1059 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1060 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1061 class->lc_name, lock->lo_name,
1062 instance->li_flags & LI_RECURSEMASK, file, line);
1063 instance->li_flags |= LI_EXCLUSIVE;
1067 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1070 struct lock_instance *instance;
1071 struct lock_class *class;
1073 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1074 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1076 class = lock->lo_class;
1077 file = fixup_filename(file);
1078 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1079 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1080 class->lc_name, lock->lo_name, file, line);
1081 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1082 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1083 class->lc_name, lock->lo_name, file, line);
1084 instance = find_instance(curthread->td_sleeplocks, lock);
1085 if (instance == NULL)
1086 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1087 class->lc_name, lock->lo_name, file, line);
1088 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1089 panic("downgrade of shared lock (%s) %s @ %s:%d",
1090 class->lc_name, lock->lo_name, file, line);
1091 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1092 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1093 class->lc_name, lock->lo_name,
1094 instance->li_flags & LI_RECURSEMASK, file, line);
1095 instance->li_flags &= ~LI_EXCLUSIVE;
1099 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1101 struct lock_list_entry **lock_list, *lle;
1102 struct lock_instance *instance;
1103 struct lock_class *class;
1108 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1112 class = lock->lo_class;
1113 file = fixup_filename(file);
1115 /* Find lock instance associated with this lock. */
1116 if (class->lc_flags & LC_SLEEPLOCK)
1117 lock_list = &td->td_sleeplocks;
1119 lock_list = PCPU_PTR(spinlocks);
1120 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1121 for (i = 0; i < (*lock_list)->ll_count; i++) {
1122 instance = &(*lock_list)->ll_children[i];
1123 if (instance->li_lock == lock)
1126 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
1130 /* First, check for shared/exclusive mismatches. */
1131 if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1132 (flags & LOP_EXCLUSIVE) == 0) {
1133 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1134 lock->lo_name, file, line);
1135 printf("while exclusively locked from %s:%d\n",
1136 instance->li_file, instance->li_line);
1137 panic("excl->ushare");
1139 if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1140 (flags & LOP_EXCLUSIVE) != 0) {
1141 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1142 lock->lo_name, file, line);
1143 printf("while share locked from %s:%d\n", instance->li_file,
1145 panic("share->uexcl");
1148 /* If we are recursed, unrecurse. */
1149 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1150 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1151 td->td_proc->p_pid, instance->li_lock->lo_name,
1152 instance->li_flags);
1153 instance->li_flags--;
1157 /* Otherwise, remove this item from the list. */
1159 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1160 td->td_proc->p_pid, instance->li_lock->lo_name,
1161 (*lock_list)->ll_count - 1);
1162 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1163 (*lock_list)->ll_children[j] =
1164 (*lock_list)->ll_children[j + 1];
1165 (*lock_list)->ll_count--;
1168 /* If this lock list entry is now empty, free it. */
1169 if ((*lock_list)->ll_count == 0) {
1171 *lock_list = lle->ll_next;
1172 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1173 td->td_proc->p_pid, lle);
1174 witness_lock_list_free(lle);
1179 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1180 * exempt Giant and sleepable locks from the checks as well. If any
1181 * non-exempt locks are held, then a supplied message is printed to the
1182 * console along with a list of the offending locks. If indicated in the
1183 * flags then a failure results in a panic as well.
1186 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1188 struct lock_list_entry *lle;
1189 struct lock_instance *lock1;
1194 if (witness_cold || witness_watch == 0 || panicstr != NULL)
1198 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1199 for (i = lle->ll_count - 1; i >= 0; i--) {
1200 lock1 = &lle->ll_children[i];
1201 if (lock1->li_lock == lock)
1203 if (flags & WARN_GIANTOK &&
1204 lock1->li_lock == &Giant.mtx_object)
1206 if (flags & WARN_SLEEPOK &&
1207 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1213 printf(" with the following");
1214 if (flags & WARN_SLEEPOK)
1215 printf(" non-sleepable");
1216 printf(" locks held:\n");
1219 witness_list_lock(lock1);
1221 if (PCPU_GET(spinlocks) != NULL) {
1223 * Since we already hold a spinlock preemption is
1230 printf(" with the following");
1231 if (flags & WARN_SLEEPOK)
1232 printf(" non-sleepable");
1233 printf(" locks held:\n");
1235 n += witness_list_locks(PCPU_PTR(spinlocks));
1237 if (flags & WARN_PANIC && n)
1238 panic("witness_warn");
1240 else if (witness_kdb && n)
1241 kdb_enter(__func__);
1242 else if (witness_trace && n)
1249 witness_file(struct lock_object *lock)
1253 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1255 w = lock->lo_witness;
1260 witness_line(struct lock_object *lock)
1264 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1266 w = lock->lo_witness;
1270 static struct witness *
1271 enroll(const char *description, struct lock_class *lock_class)
1275 if (witness_watch == 0 || panicstr != NULL)
1277 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
1279 mtx_lock_spin(&w_mtx);
1280 STAILQ_FOREACH(w, &w_all, w_list) {
1281 if (w->w_name == description || (w->w_refcount > 0 &&
1282 strcmp(description, w->w_name) == 0)) {
1284 mtx_unlock_spin(&w_mtx);
1285 if (lock_class != w->w_class)
1287 "lock (%s) %s does not match earlier (%s) lock",
1288 description, lock_class->lc_name,
1289 w->w_class->lc_name);
1294 * This isn't quite right, as witness_cold is still 0 while we
1295 * enroll all the locks initialized before witness_initialize().
1297 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
1298 mtx_unlock_spin(&w_mtx);
1299 panic("spin lock %s not in order list", description);
1301 if ((w = witness_get()) == NULL)
1303 w->w_name = description;
1304 w->w_class = lock_class;
1306 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1307 if (lock_class->lc_flags & LC_SPINLOCK)
1308 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1309 else if (lock_class->lc_flags & LC_SLEEPLOCK)
1310 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1312 mtx_unlock_spin(&w_mtx);
1313 panic("lock class %s is not sleep or spin",
1314 lock_class->lc_name);
1316 mtx_unlock_spin(&w_mtx);
1320 /* Don't let the door bang you on the way out... */
1322 depart(struct witness *w)
1324 struct witness_child_list_entry *wcl, *nwcl;
1325 struct witness_list *list;
1326 struct witness *parent;
1328 MPASS(w->w_refcount == 0);
1329 if (w->w_class->lc_flags & LC_SLEEPLOCK)
1334 * First, we run through the entire tree looking for any
1335 * witnesses that the outgoing witness is a child of. For
1336 * each parent that we find, we reparent all the direct
1337 * children of the outgoing witness to its parent.
1339 STAILQ_FOREACH(parent, list, w_typelist) {
1340 if (!isitmychild(parent, w))
1342 removechild(parent, w);
1343 if (!reparentchildren(parent, w))
1348 * Now we go through and free up the child list of the
1351 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1352 nwcl = wcl->wcl_next;
1353 witness_child_free(wcl);
1357 * Detach from various lists and free.
1359 STAILQ_REMOVE(list, w, witness, w_typelist);
1360 STAILQ_REMOVE(&w_all, w, witness, w_list);
1363 /* Finally, fixup the tree. */
1364 return (rebalancetree(list));
1368 * Prune an entire lock order tree. We look for cases where a lock
1369 * is now both a descendant and a direct child of a given lock. In
1370 * that case, we want to remove the direct child link from the tree.
1372 * Returns false if insertchild() fails.
1375 rebalancetree(struct witness_list *list)
1377 struct witness *child, *parent;
1379 STAILQ_FOREACH(child, list, w_typelist) {
1380 STAILQ_FOREACH(parent, list, w_typelist) {
1381 if (!isitmychild(parent, child))
1383 removechild(parent, child);
1384 if (isitmydescendant(parent, child))
1386 if (!insertchild(parent, child))
1395 * Add "child" as a direct child of "parent". Returns false if
1396 * we fail due to out of memory.
1399 insertchild(struct witness *parent, struct witness *child)
1401 struct witness_child_list_entry **wcl;
1403 MPASS(child != NULL && parent != NULL);
1406 * Insert "child" after "parent"
1408 wcl = &parent->w_children;
1409 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1410 wcl = &(*wcl)->wcl_next;
1412 *wcl = witness_child_get();
1416 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1422 * Make all the direct descendants of oldparent be direct descendants
1426 reparentchildren(struct witness *newparent, struct witness *oldparent)
1428 struct witness_child_list_entry *wcl;
1431 /* Avoid making a witness a child of itself. */
1432 MPASS(!isitmychild(oldparent, newparent));
1434 for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1435 for (i = 0; i < wcl->wcl_count; i++)
1436 if (!insertchild(newparent, wcl->wcl_children[i]))
1442 itismychild(struct witness *parent, struct witness *child)
1444 struct witness_list *list;
1446 MPASS(child != NULL && parent != NULL);
1447 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1448 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1450 "%s: parent (%s) and child (%s) are not the same lock type",
1451 __func__, parent->w_class->lc_name,
1452 child->w_class->lc_name);
1454 if (!insertchild(parent, child))
1457 if (parent->w_class->lc_flags & LC_SLEEPLOCK)
1461 return (rebalancetree(list));
1465 removechild(struct witness *parent, struct witness *child)
1467 struct witness_child_list_entry **wcl, *wcl1;
1470 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1471 for (i = 0; i < (*wcl)->wcl_count; i++)
1472 if ((*wcl)->wcl_children[i] == child)
1476 (*wcl)->wcl_count--;
1477 if ((*wcl)->wcl_count > i)
1478 (*wcl)->wcl_children[i] =
1479 (*wcl)->wcl_children[(*wcl)->wcl_count];
1480 MPASS((*wcl)->wcl_children[i] != NULL);
1481 if ((*wcl)->wcl_count != 0)
1484 *wcl = wcl1->wcl_next;
1485 witness_child_free(wcl1);
1489 isitmychild(struct witness *parent, struct witness *child)
1491 struct witness_child_list_entry *wcl;
1494 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1495 for (i = 0; i < wcl->wcl_count; i++) {
1496 if (wcl->wcl_children[i] == child)
1504 isitmydescendant(struct witness *parent, struct witness *child)
1506 struct witness_child_list_entry *wcl;
1509 if (isitmychild(parent, child))
1512 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1514 for (i = 0; i < wcl->wcl_count; i++) {
1515 if (isitmydescendant(wcl->wcl_children[i], child))
1524 witness_levelall (void)
1526 struct witness_list *list;
1527 struct witness *w, *w1;
1530 * First clear all levels.
1532 STAILQ_FOREACH(w, &w_all, w_list) {
1537 * Look for locks with no parent and level all their descendants.
1539 STAILQ_FOREACH(w, &w_all, w_list) {
1541 * This is just an optimization, technically we could get
1542 * away just walking the all list each time.
1544 if (w->w_class->lc_flags & LC_SLEEPLOCK)
1548 STAILQ_FOREACH(w1, list, w_typelist) {
1549 if (isitmychild(w1, w))
1552 witness_leveldescendents(w, 0);
1554 ; /* silence GCC 3.x */
1559 witness_leveldescendents(struct witness *parent, int level)
1561 struct witness_child_list_entry *wcl;
1564 if (parent->w_level < level)
1565 parent->w_level = level;
1567 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1568 for (i = 0; i < wcl->wcl_count; i++)
1569 witness_leveldescendents(wcl->wcl_children[i], level);
1573 witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1574 struct witness *parent, int indent)
1576 struct witness_child_list_entry *wcl;
1579 level = parent->w_level;
1580 prnt("%-2d", level);
1581 for (i = 0; i < indent; i++)
1583 if (parent->w_refcount > 0)
1584 prnt("%s", parent->w_name);
1587 if (parent->w_displayed) {
1588 prnt(" -- (already displayed)\n");
1591 parent->w_displayed = 1;
1592 if (parent->w_refcount > 0) {
1593 if (parent->w_file != NULL)
1594 prnt(" -- last acquired @ %s:%d", parent->w_file,
1598 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1599 for (i = 0; i < wcl->wcl_count; i++)
1600 witness_displaydescendants(prnt,
1601 wcl->wcl_children[i], indent + 1);
1606 blessed(struct witness *w1, struct witness *w2)
1609 struct witness_blessed *b;
1611 for (i = 0; i < blessed_count; i++) {
1612 b = &blessed_list[i];
1613 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1614 if (strcmp(w2->w_name, b->b_lock2) == 0)
1618 if (strcmp(w1->w_name, b->b_lock2) == 0)
1619 if (strcmp(w2->w_name, b->b_lock1) == 0)
1626 static struct witness *
1631 if (witness_watch == 0) {
1632 mtx_unlock_spin(&w_mtx);
1635 if (STAILQ_EMPTY(&w_free)) {
1637 mtx_unlock_spin(&w_mtx);
1638 printf("%s: witness exhausted\n", __func__);
1641 w = STAILQ_FIRST(&w_free);
1642 STAILQ_REMOVE_HEAD(&w_free, w_list);
1643 bzero(w, sizeof(*w));
1648 witness_free(struct witness *w)
1651 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1654 static struct witness_child_list_entry *
1655 witness_child_get(void)
1657 struct witness_child_list_entry *wcl;
1659 if (witness_watch == 0) {
1660 mtx_unlock_spin(&w_mtx);
1666 mtx_unlock_spin(&w_mtx);
1667 printf("%s: witness exhausted\n", __func__);
1670 w_child_free = wcl->wcl_next;
1671 bzero(wcl, sizeof(*wcl));
1676 witness_child_free(struct witness_child_list_entry *wcl)
1679 wcl->wcl_next = w_child_free;
1683 static struct lock_list_entry *
1684 witness_lock_list_get(void)
1686 struct lock_list_entry *lle;
1688 if (witness_watch == 0)
1690 mtx_lock_spin(&w_mtx);
1691 lle = w_lock_list_free;
1694 mtx_unlock_spin(&w_mtx);
1695 printf("%s: witness exhausted\n", __func__);
1698 w_lock_list_free = lle->ll_next;
1699 mtx_unlock_spin(&w_mtx);
1700 bzero(lle, sizeof(*lle));
1705 witness_lock_list_free(struct lock_list_entry *lle)
1708 mtx_lock_spin(&w_mtx);
1709 lle->ll_next = w_lock_list_free;
1710 w_lock_list_free = lle;
1711 mtx_unlock_spin(&w_mtx);
1714 static struct lock_instance *
1715 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1717 struct lock_list_entry *lle;
1718 struct lock_instance *instance;
1721 for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1722 for (i = lle->ll_count - 1; i >= 0; i--) {
1723 instance = &lle->ll_children[i];
1724 if (instance->li_lock == lock)
1731 witness_list_lock(struct lock_instance *instance)
1733 struct lock_object *lock;
1735 lock = instance->li_lock;
1736 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1737 "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name);
1738 if (lock->lo_type != lock->lo_name)
1739 printf(" (%s)", lock->lo_type);
1740 printf(" r = %d (%p) locked @ %s:%d\n",
1741 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1747 witness_thread_has_locks(struct thread *td)
1750 return (td->td_sleeplocks != NULL);
1754 witness_proc_has_locks(struct proc *p)
1758 FOREACH_THREAD_IN_PROC(p, td) {
1759 if (witness_thread_has_locks(td))
1767 witness_list_locks(struct lock_list_entry **lock_list)
1769 struct lock_list_entry *lle;
1773 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1774 for (i = lle->ll_count - 1; i >= 0; i--) {
1775 witness_list_lock(&lle->ll_children[i]);
1782 * This is a bit risky at best. We call this function when we have timed
1783 * out acquiring a spin lock, and we assume that the other CPU is stuck
1784 * with this lock held. So, we go groveling around in the other CPU's
1785 * per-cpu data to try to find the lock instance for this spin lock to
1786 * see when it was last acquired.
1789 witness_display_spinlock(struct lock_object *lock, struct thread *owner)
1791 struct lock_instance *instance;
1794 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
1796 pc = pcpu_find(owner->td_oncpu);
1797 instance = find_instance(pc->pc_spinlocks, lock);
1798 if (instance != NULL)
1799 witness_list_lock(instance);
1803 witness_save(struct lock_object *lock, const char **filep, int *linep)
1805 struct lock_instance *instance;
1807 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1808 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1810 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1811 panic("%s: lock (%s) %s is not a sleep lock", __func__,
1812 lock->lo_class->lc_name, lock->lo_name);
1813 instance = find_instance(curthread->td_sleeplocks, lock);
1814 if (instance == NULL)
1815 panic("%s: lock (%s) %s not locked", __func__,
1816 lock->lo_class->lc_name, lock->lo_name);
1817 *filep = instance->li_file;
1818 *linep = instance->li_line;
1822 witness_restore(struct lock_object *lock, const char *file, int line)
1824 struct lock_instance *instance;
1826 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1827 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1829 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1830 panic("%s: lock (%s) %s is not a sleep lock", __func__,
1831 lock->lo_class->lc_name, lock->lo_name);
1832 instance = find_instance(curthread->td_sleeplocks, lock);
1833 if (instance == NULL)
1834 panic("%s: lock (%s) %s not locked", __func__,
1835 lock->lo_class->lc_name, lock->lo_name);
1836 lock->lo_witness->w_file = file;
1837 lock->lo_witness->w_line = line;
1838 instance->li_file = file;
1839 instance->li_line = line;
1843 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1845 #ifdef INVARIANT_SUPPORT
1846 struct lock_instance *instance;
1848 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1850 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
1851 instance = find_instance(curthread->td_sleeplocks, lock);
1852 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
1853 instance = find_instance(PCPU_GET(spinlocks), lock);
1855 panic("Lock (%s) %s is not sleep or spin!",
1856 lock->lo_class->lc_name, lock->lo_name);
1858 file = fixup_filename(file);
1861 if (instance != NULL)
1862 panic("Lock (%s) %s locked @ %s:%d.",
1863 lock->lo_class->lc_name, lock->lo_name, file, line);
1866 case LA_LOCKED | LA_RECURSED:
1867 case LA_LOCKED | LA_NOTRECURSED:
1869 case LA_SLOCKED | LA_RECURSED:
1870 case LA_SLOCKED | LA_NOTRECURSED:
1872 case LA_XLOCKED | LA_RECURSED:
1873 case LA_XLOCKED | LA_NOTRECURSED:
1874 if (instance == NULL) {
1875 panic("Lock (%s) %s not locked @ %s:%d.",
1876 lock->lo_class->lc_name, lock->lo_name, file, line);
1879 if ((flags & LA_XLOCKED) != 0 &&
1880 (instance->li_flags & LI_EXCLUSIVE) == 0)
1881 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1882 lock->lo_class->lc_name, lock->lo_name, file, line);
1883 if ((flags & LA_SLOCKED) != 0 &&
1884 (instance->li_flags & LI_EXCLUSIVE) != 0)
1885 panic("Lock (%s) %s exclusively locked @ %s:%d.",
1886 lock->lo_class->lc_name, lock->lo_name, file, line);
1887 if ((flags & LA_RECURSED) != 0 &&
1888 (instance->li_flags & LI_RECURSEMASK) == 0)
1889 panic("Lock (%s) %s not recursed @ %s:%d.",
1890 lock->lo_class->lc_name, lock->lo_name, file, line);
1891 if ((flags & LA_NOTRECURSED) != 0 &&
1892 (instance->li_flags & LI_RECURSEMASK) != 0)
1893 panic("Lock (%s) %s recursed @ %s:%d.",
1894 lock->lo_class->lc_name, lock->lo_name, file, line);
1897 panic("Invalid lock assertion at %s:%d.", file, line);
1900 #endif /* INVARIANT_SUPPORT */
1905 witness_list(struct thread *td)
1908 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1909 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
1911 if (witness_watch == 0)
1914 witness_list_locks(&td->td_sleeplocks);
1917 * We only handle spinlocks if td == curthread. This is somewhat broken
1918 * if td is currently executing on some other CPU and holds spin locks
1919 * as we won't display those locks. If we had a MI way of getting
1920 * the per-cpu data for a given cpu then we could use
1921 * td->td_oncpu to get the list of spinlocks for this thread
1924 * That still wouldn't really fix this unless we locked sched_lock
1925 * or stopped the other CPU to make sure it wasn't changing the list
1926 * out from under us. It is probably best to just not try to handle
1927 * threads on other CPU's for now.
1929 if (td == curthread && PCPU_GET(spinlocks) != NULL)
1930 witness_list_locks(PCPU_PTR(spinlocks));
1933 DB_SHOW_COMMAND(locks, db_witness_list)
1940 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1941 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1942 ((addr >> 16) % 16) * 10000;
1943 /* sx_slock(&allproc_lock); */
1944 FOREACH_PROC_IN_SYSTEM(p) {
1945 if (p->p_pid == pid)
1948 /* sx_sunlock(&allproc_lock); */
1950 db_printf("pid %d not found\n", pid);
1953 FOREACH_THREAD_IN_PROC(p, td) {
1962 DB_SHOW_COMMAND(alllocks, db_witness_list_all)
1968 * It would be nice to list only threads and processes that actually
1969 * held sleep locks, but that information is currently not exported
1972 FOREACH_PROC_IN_SYSTEM(p) {
1973 if (!witness_proc_has_locks(p))
1975 FOREACH_THREAD_IN_PROC(p, td) {
1976 if (!witness_thread_has_locks(td))
1978 printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
1979 p->p_comm, td, td->td_tid);
1985 DB_SHOW_COMMAND(witness, db_witness_display)
1988 witness_display(db_printf);