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 $
34 * Pronunciation: 'wit-n&s
36 * Etymology: Middle English witnesse, from Old English witnes knowledge,
37 * testimony, witness, from 2wit
38 * Date: before 12th century
39 * 1 : attestation of a fact or event : TESTIMONY
40 * 2 : one that gives evidence; specifically : one who testifies in
41 * a cause or before a judicial tribunal
42 * 3 : one asked to be present at a transaction so as to be able to
43 * testify to its having taken place
44 * 4 : one who has personal knowledge of something
45 * 5 a : something serving as evidence or proof : SIGN
46 * b : public affirmation by word or example of usually
47 * religious faith or conviction <the heroic witness to divine
49 * 6 capitalized : a member of the Jehovah's Witnesses
52 #include <sys/param.h>
54 #include <sys/systm.h>
57 #include <machine/cpu.h>
58 #define _KERN_MUTEX_C_ /* Cause non-inlined mtx_*() to be compiled. */
59 #include <machine/mutex.h>
62 * The non-inlined versions of the mtx_*() functions are always built (above),
63 * but the witness code depends on the SMP_DEBUG and WITNESS kernel options
66 #if (defined(SMP_DEBUG) && defined(WITNESS))
68 #define WITNESS_COUNT 200
69 #define WITNESS_NCHILDREN 2
72 #define WITNESS 0 /* default off */
76 extern int witness_spin_check;
81 typedef struct witness {
82 struct witness *w_next;
86 struct witness *w_morechildren;
88 u_char w_Giant_squawked:1;
89 u_char w_other_squawked:1;
90 u_char w_same_squawked:1;
92 u_char w_spin:1; /* this is a spin mutex */
94 struct witness *w_children[WITNESS_NCHILDREN];
97 typedef struct witness_blessed {
104 * When WITNESS_KDEBUG is set to 1, it will cause the system to
105 * drop into kdebug() when:
106 * - a lock heirarchy violation occurs
107 * - locks are held when going to sleep.
109 #ifndef WITNESS_KDEBUG
110 #define WITNESS_KDEBUG 0
112 int witness_kdebug = WITNESS_KDEBUG;
115 #ifndef WITNESS_SKIPSPIN
116 #define WITNESS_SKIPSPIN 0
118 int witness_skipspin = WITNESS_SKIPSPIN;
122 static witness_t *w_free;
123 static witness_t *w_all;
125 static int witness_dead; /* fatal error, probably no memory */
127 static witness_t w_data[WITNESS_COUNT];
129 static witness_t *enroll __P((char *description, int flag));
130 static int itismychild __P((witness_t *parent, witness_t *child));
131 static void removechild __P((witness_t *parent, witness_t *child));
132 static int isitmychild __P((witness_t *parent, witness_t *child));
133 static int isitmydescendant __P((witness_t *parent, witness_t *child));
134 static int dup_ok __P((witness_t *));
135 static int blessed __P((witness_t *, witness_t *));
136 static void witness_displaydescendants
137 __P((void(*)(const char *fmt, ...), witness_t *));
138 static void witness_leveldescendents __P((witness_t *parent, int level));
139 static void witness_levelall __P((void));
140 static witness_t * witness_get __P((void));
141 static void witness_free __P((witness_t *m));
144 static char *ignore_list[] = {
146 "Kdebug", /* breaks rules and may or may not work */
147 "Page Alias", /* sparc only, witness lock won't block intr */
151 static char *spin_order_list[] = {
154 "zslock", /* sparc only above log, this one is a real hack */
155 "time lock", /* above callout */
156 "callout mtx", /* above wayout */
161 "kernel_pmap", /* sparc only, logically equal "pmap" below */
162 "pmap", /* sparc only */
166 static char *order_list[] = {
167 "tcb", "inp", "so_snd", "so_rcv", "Giant lock", NULL,
169 "unp head", "unp", "so_snd", NULL,
170 "de0", "Giant lock", NULL,
171 "ifnet", "Giant lock", NULL,
172 "fifo", "so_snd", NULL,
173 "hme0", "Giant lock", NULL,
174 "esp0", "Giant lock", NULL,
175 "hfa0", "Giant lock", NULL,
176 "so_rcv", "atm_global", NULL,
177 "so_snd", "atm_global", NULL,
178 "NFS", "Giant lock", NULL,
182 static char *dup_list[] = {
192 static char *sleep_list[] = {
198 * Pairs of locks which have been blessed
199 * Don't complain about order problems with blessed locks
201 static witness_blessed_t blessed_list[] = {
203 static int blessed_count = sizeof (blessed_list) / sizeof (witness_blessed_t);
206 witness_init(mtx_t *m, int flag)
208 m->mtx_witness = enroll(m->mtx_description, flag);
212 witness_destroy(mtx_t *m)
217 for ((m1 = LIST_FIRST(&p->p_heldmtx)); m1 != NULL;
218 m1 = LIST_NEXT(m1, mtx_held)) {
220 LIST_REMOVE(m, mtx_held);
229 witness_enter(mtx_t *m, int flags, char *file, int line)
236 int go_into_kdebug = 0;
242 if (flags & MTX_SPIN) {
244 panic("mutex_enter: MTX_SPIN on MTX_DEF mutex %s @ %s:%d",
245 m->mtx_description, file, line);
246 if (m->mtx_recurse != 0)
248 mtx_enter(&w_mtx, MTX_SPIN);
249 i = witness_spin_check;
250 if (i != 0 && w->w_level < i) {
251 mtx_exit(&w_mtx, MTX_SPIN);
252 panic("mutex_enter(%s:%x, MTX_SPIN) out of order @ %s:%d"
253 " already holding %s:%x",
254 m->mtx_description, w->w_level, file, line,
255 spin_order_list[ffs(i)-1], i);
257 PCPU_SET(witness_spin_check, i | w->w_level);
258 mtx_exit(&w_mtx, MTX_SPIN);
262 panic("mutex_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
263 m->mtx_description, file, line);
265 if (m->mtx_recurse != 0)
272 if (!mtx_legal2block())
273 panic("blockable mtx_enter() of %s when not legal @ %s:%d",
274 m->mtx_description, file, line);
276 * Is this the first mutex acquired
278 if ((m1 = LIST_FIRST(&p->p_heldmtx)) == NULL)
282 if ((w1 = m1->mtx_witness) == w) {
283 if (w->w_same_squawked || dup_ok(w))
285 w->w_same_squawked = 1;
286 printf("acquring duplicate lock of same type: \"%s\"\n",
288 printf(" 1st @ %s:%d\n", w->w_file, w->w_line);
289 printf(" 2nd @ %s:%d\n", file, line);
295 MPASS(!mtx_owned(&w_mtx));
296 mtx_enter(&w_mtx, MTX_SPIN);
298 * If we have a known higher number just say ok
300 if (witness_watch > 1 && w->w_level > w1->w_level) {
301 mtx_exit(&w_mtx, MTX_SPIN);
304 if (isitmydescendant(m1->mtx_witness, w)) {
305 mtx_exit(&w_mtx, MTX_SPIN);
308 for (i = 0; m1 != NULL; m1 = LIST_NEXT(m1, mtx_held), i++) {
311 w1 = m1->mtx_witness;
312 if (isitmydescendant(w, w1)) {
313 mtx_exit(&w_mtx, MTX_SPIN);
317 if (w1->w_Giant_squawked)
320 w1->w_Giant_squawked = 1;
322 if (w1->w_other_squawked)
325 w1->w_other_squawked = 1;
327 printf("lock order reversal\n");
328 printf(" 1st %s last acquired @ %s:%d\n",
329 w->w_description, w->w_file, w->w_line);
330 printf(" 2nd %p %s @ %s:%d\n",
331 m1, w1->w_description, w1->w_file, w1->w_line);
332 printf(" 3rd %p %s @ %s:%d\n",
333 m, w->w_description, file, line);
340 m1 = LIST_FIRST(&p->p_heldmtx);
341 if (!itismychild(m1->mtx_witness, w))
342 mtx_exit(&w_mtx, MTX_SPIN);
346 if (witness_kdebug && go_into_kdebug)
355 * If this pays off it likely means that a mutex being witnessed
356 * is acquired in hardclock. Put it in the ignore list. It is
357 * likely not the mutex this assert fails on.
359 ASS(m->mtx_held.le_prev == NULL);
360 LIST_INSERT_HEAD(&p->p_heldmtx, (struct mtx*)m, mtx_held);
364 witness_exit(mtx_t *m, int flags, char *file, int line)
370 if (flags & MTX_SPIN) {
372 panic("mutex_exit: MTX_SPIN on MTX_DEF mutex %s @ %s:%d",
373 m->mtx_description, file, line);
374 if (m->mtx_recurse != 0)
376 mtx_enter(&w_mtx, MTX_SPIN);
377 PCPU_SET(witness_spin_check, witness_spin_check & ~w->w_level);
378 mtx_exit(&w_mtx, MTX_SPIN);
382 panic("mutex_exit: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
383 m->mtx_description, file, line);
385 if (m->mtx_recurse != 0)
388 if ((flags & MTX_NOSWITCH) == 0 && !mtx_legal2block() && !cold)
389 panic("switchable mtx_exit() of %s when not legal @ %s:%d",
390 m->mtx_description, file, line);
391 LIST_REMOVE(m, mtx_held);
392 m->mtx_held.le_prev = NULL;
396 witness_try_enter(mtx_t *m, int flags, char *file, int line)
399 witness_t *w = m->mtx_witness;
402 if (flags & MTX_SPIN) {
404 panic("mutex_try_enter: "
405 "MTX_SPIN on MTX_DEF mutex %s @ %s:%d",
406 m->mtx_description, file, line);
407 if (m->mtx_recurse != 0)
409 mtx_enter(&w_mtx, MTX_SPIN);
410 PCPU_SET(witness_spin_check, witness_spin_check | w->w_level);
411 mtx_exit(&w_mtx, MTX_SPIN);
416 panic("mutex_try_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
417 m->mtx_description, file, line);
419 if (m->mtx_recurse != 0)
427 ASS(m->mtx_held.le_prev == NULL);
428 LIST_INSERT_HEAD(&p->p_heldmtx, (struct mtx*)m, mtx_held);
432 witness_display(void(*prnt)(const char *fmt, ...))
438 for (w = w_all; w; w = w->w_next) {
439 if (w->w_file == NULL)
441 for (w1 = w_all; w1; w1 = w1->w_next) {
442 if (isitmychild(w1, w))
448 * This lock has no anscestors, display its descendants.
450 witness_displaydescendants(prnt, w);
452 prnt("\nMutex which were never acquired\n");
453 for (w = w_all; w; w = w->w_next) {
454 if (w->w_file != NULL)
456 prnt("%s\n", w->w_description);
461 witness_sleep(int check_only, mtx_t *mtx, char *file, int line)
469 for ((m = LIST_FIRST(&p->p_heldmtx)); m != NULL;
470 m = LIST_NEXT(m, mtx_held)) {
473 for (sleep = sleep_list; *sleep!= NULL; sleep++)
474 if (strcmp(m->mtx_description, *sleep) == 0)
476 printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
477 file, line, check_only ? "could sleep" : "sleeping",
479 m->mtx_witness->w_file, m->mtx_witness->w_line);
484 if (witness_kdebug && n)
491 enroll(char *description, int flag)
500 for (ignore = ignore_list; *ignore != NULL; ignore++)
501 if (strcmp(description, *ignore) == 0)
505 mtx_init(&w_mtx, "witness lock", MTX_DEF);
506 for (i = 0; i < WITNESS_COUNT; i++) {
511 for (order = order_list; *order != NULL; order++) {
512 w = enroll(*order, MTX_DEF);
513 w->w_file = "order list";
514 for (order++; *order != NULL; order++) {
515 w1 = enroll(*order, MTX_DEF);
516 w1->w_file = "order list";
522 if ((flag & MTX_SPIN) && witness_skipspin)
524 mtx_enter(&w_mtx, MTX_SPIN);
525 for (w = w_all; w; w = w->w_next) {
526 if (strcmp(description, w->w_description) == 0) {
527 mtx_exit(&w_mtx, MTX_SPIN);
531 if ((w = witness_get()) == NULL)
535 w->w_description = description;
536 mtx_exit(&w_mtx, MTX_SPIN);
537 if (flag & MTX_SPIN) {
541 for (order = spin_order_list; *order != NULL; order++) {
542 if (strcmp(description, *order) == 0)
547 panic("spin lock %s not in order list", description);
554 itismychild(witness_t *parent, witness_t *child)
559 * Insert "child" after "parent"
561 while (parent->w_morechildren)
562 parent = parent->w_morechildren;
564 if (parent->w_childcnt == WITNESS_NCHILDREN) {
565 if ((parent->w_morechildren = witness_get()) == NULL)
567 parent = parent->w_morechildren;
570 parent->w_children[parent->w_childcnt++] = child;
572 * now prune whole tree
577 for (child = w_all; child != NULL; child = child->w_next) {
578 for (parent = w_all; parent != NULL;
579 parent = parent->w_next) {
580 if (!isitmychild(parent, child))
582 removechild(parent, child);
583 if (isitmydescendant(parent, child))
585 itismychild(parent, child);
594 removechild(witness_t *parent, witness_t *child)
599 for (w = parent; w != NULL; w = w->w_morechildren)
600 for (i = 0; i < w->w_childcnt; i++)
601 if (w->w_children[i] == child)
605 for (w1 = w; w1->w_morechildren != NULL; w1 = w1->w_morechildren)
607 w->w_children[i] = w1->w_children[--w1->w_childcnt];
608 ASS(w->w_children[i] != NULL);
610 if (w1->w_childcnt != 0)
615 for (w = parent; w->w_morechildren != w1; w = w->w_morechildren)
617 w->w_morechildren = 0;
622 isitmychild(witness_t *parent, witness_t *child)
627 for (w = parent; w != NULL; w = w->w_morechildren) {
628 for (i = 0; i < w->w_childcnt; i++) {
629 if (w->w_children[i] == child)
637 isitmydescendant(witness_t *parent, witness_t *child)
643 for (j = 0, w = parent; w != NULL; w = w->w_morechildren, j++) {
645 for (i = 0; i < w->w_childcnt; i++) {
646 if (w->w_children[i] == child)
649 for (i = 0; i < w->w_childcnt; i++) {
650 if (isitmydescendant(w->w_children[i], child))
658 witness_levelall (void)
662 for (w = w_all; w; w = w->w_next)
665 for (w = w_all; w; w = w->w_next) {
668 for (w1 = w_all; w1; w1 = w1->w_next) {
669 if (isitmychild(w1, w))
674 witness_leveldescendents(w, 0);
679 witness_leveldescendents(witness_t *parent, int level)
684 if (parent->w_level < level)
685 parent->w_level = level;
687 for (w = parent; w != NULL; w = w->w_morechildren)
688 for (i = 0; i < w->w_childcnt; i++)
689 witness_leveldescendents(w->w_children[i], level);
693 witness_displaydescendants(void(*prnt)(const char *fmt, ...), witness_t *parent)
697 int level = parent->w_level;
702 for (i = 0; i < level; i++)
704 prnt("%s", parent->w_description);
705 if (parent->w_file != NULL) {
706 prnt(" -- last acquired @ %s", parent->w_file);
708 prnt(":%d", parent->w_line);
713 for (w = parent; w != NULL; w = w->w_morechildren)
714 for (i = 0; i < w->w_childcnt; i++)
715 witness_displaydescendants(prnt, w->w_children[i]);
723 for (dup = dup_list; *dup!= NULL; dup++)
724 if (strcmp(w->w_description, *dup) == 0)
730 blessed(witness_t *w1, witness_t *w2)
733 witness_blessed_t *b;
735 for (i = 0; i < blessed_count; i++) {
736 b = &blessed_list[i];
737 if (strcmp(w1->w_description, b->b_lock1) == 0) {
738 if (strcmp(w2->w_description, b->b_lock2) == 0)
742 if (strcmp(w1->w_description, b->b_lock2) == 0)
743 if (strcmp(w2->w_description, b->b_lock1) == 0)
754 if ((w = w_free) == NULL) {
756 mtx_exit(&w_mtx, MTX_SPIN);
757 printf("witness exhausted\n");
761 bzero(w, sizeof (*w));
766 witness_free(witness_t *w)
773 witness_list(struct proc *p)
777 for ((m = LIST_FIRST(&p->p_heldmtx)); m != NULL;
778 m = LIST_NEXT(m, mtx_held)) {
779 printf("\t\"%s\" (%p) locked at %s:%d\n",
780 m->mtx_description, m,
781 m->mtx_witness->w_file, m->mtx_witness->w_line);
786 witness_save(mtx_t *m, char **filep, int *linep)
788 *filep = m->mtx_witness->w_file;
789 *linep = m->mtx_witness->w_line;
793 witness_restore(mtx_t *m, char *file, int line)
795 m->mtx_witness->w_file = file;
796 m->mtx_witness->w_line = line;
799 #endif /* (defined(SMP_DEBUG) && defined(WITNESS)) */