summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_turnstile.c
blob: 96bbdf9d8dd13d831f19ec0030808d5a8639f54d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*-
 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Berkeley Software Design Inc's name may not be used to endorse or
 *    promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
 * $FreeBSD$
 */

/*
 *	Main Entry: witness
 *	Pronunciation: 'wit-n&s
 *	Function: noun
 *	Etymology: Middle English witnesse, from Old English witnes knowledge,
 *	    testimony, witness, from 2wit
 *	Date: before 12th century
 *	1 : attestation of a fact or event : TESTIMONY
 *	2 : one that gives evidence; specifically : one who testifies in
 *	    a cause or before a judicial tribunal
 *	3 : one asked to be present at a transaction so as to be able to
 *	    testify to its having taken place
 *	4 : one who has personal knowledge of something
 *	5 a : something serving as evidence or proof : SIGN
 *	  b : public affirmation by word or example of usually
 *	      religious faith or conviction <the heroic witness to divine
 *	      life -- Pilot>
 *	6 capitalized : a member of the Jehovah's Witnesses 
 */

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/ktr.h>

#include <machine/cpu.h>
#define _KERN_MUTEX_C_		/* Cause non-inlined mtx_*() to be compiled. */
#include <machine/mutex.h>

/*
 * The non-inlined versions of the mtx_*() functions are always built (above),
 * but the witness code depends on the SMP_DEBUG and WITNESS kernel options
 * being specified.
 */
#if (defined(SMP_DEBUG) && defined(WITNESS))

#define WITNESS_COUNT 200
#define	WITNESS_NCHILDREN 2

#ifndef WITNESS
#define	WITNESS		0	/* default off */
#endif

#ifndef SMP
extern int witness_spin_check;
#endif

int witness_watch;

struct witness {
	struct witness	*w_next;
	char		*w_description;
	const char	*w_file;
	int		 w_line;
	struct witness	*w_morechildren;
	u_char		 w_childcnt;
	u_char		 w_Giant_squawked:1;
	u_char		 w_other_squawked:1;
	u_char		 w_same_squawked:1;
	u_char		 w_sleep:1;
	u_char		 w_spin:1;	/* this is a spin mutex */
	u_int		 w_level;
	struct witness	*w_children[WITNESS_NCHILDREN];
};

struct witness_blessed {
	char 	*b_lock1;
	char	*b_lock2;
};

#ifdef KDEBUG
/*
 * When WITNESS_KDEBUG is set to 1, it will cause the system to
 * drop into kdebug() when:
 *	- a lock heirarchy violation occurs
 *	- locks are held when going to sleep.
 */
#ifndef WITNESS_KDEBUG
#define WITNESS_KDEBUG 0
#endif
int	witness_kdebug = WITNESS_KDEBUG;
#endif /* KDEBUG */

#ifndef WITNESS_SKIPSPIN
#define WITNESS_SKIPSPIN 0
#endif
int	witness_skipspin = WITNESS_SKIPSPIN;


static struct mtx	 w_mtx;
static struct witness	*w_free;
static struct witness	*w_all;
static int		 w_inited;
static int		 witness_dead;	/* fatal error, probably no memory */

static struct witness	 w_data[WITNESS_COUNT];

static struct witness	 *enroll __P((char *description, int flag));
static int itismychild __P((struct witness *parent, struct witness *child));
static void removechild __P((struct witness *parent, struct witness *child));
static int isitmychild __P((struct witness *parent, struct witness *child));
static int isitmydescendant __P((struct witness *parent, struct witness *child));
static int dup_ok __P((struct witness *));
static int blessed __P((struct witness *, struct witness *));
static void witness_displaydescendants
    __P((void(*)(const char *fmt, ...), struct witness *));
static void witness_leveldescendents __P((struct witness *parent, int level));
static void witness_levelall __P((void));
static struct witness * witness_get __P((void));
static void witness_free __P((struct witness *m));


static char *ignore_list[] = {
	"witness lock",
	"Kdebug",		/* breaks rules and may or may not work */
	"Page Alias",		/* sparc only, witness lock won't block intr */
	NULL
};

static char *spin_order_list[] = {
	"sched lock",
	"log mtx",
	"zslock",	/* sparc only above log, this one is a real hack */
	"time lock",	/* above callout */
	"callout mtx",	/* above wayout */
	/*
	 * leaf locks
	 */
	"wayout mtx",
	"kernel_pmap",  /* sparc only, logically equal "pmap" below */
	"pmap",		/* sparc only */
	NULL
};

static char *order_list[] = {
	"tcb", "inp", "so_snd", "so_rcv", "Giant lock", NULL,
	"udb", "inp", NULL,
	"unp head", "unp", "so_snd", NULL,
	"de0", "Giant lock", NULL,
	"ifnet", "Giant lock", NULL,
	"fifo", "so_snd", NULL,
	"hme0", "Giant lock", NULL,
	"esp0", "Giant lock", NULL,
	"hfa0", "Giant lock", NULL,
	"so_rcv", "atm_global", NULL,
	"so_snd", "atm_global", NULL,
	"NFS", "Giant lock", NULL,
	NULL
};

static char *dup_list[] = {
	"inp",
	"process group",
	"session",
	"unp",
	"rtentry",
	"rawcb",
	NULL
};

static char *sleep_list[] = {
	"Giant lock",
	NULL
};

/*
 * Pairs of locks which have been blessed
 * Don't complain about order problems with blessed locks
 */
static struct witness_blessed blessed_list[] = {
};
static int blessed_count = sizeof(blessed_list) / sizeof(struct witness_blessed);

void
witness_init(struct mtx *m, int flag)
{
	m->mtx_witness = enroll(m->mtx_description, flag);
}

void
witness_destroy(struct mtx *m)
{
	struct mtx *m1;
	struct proc *p;
	p = CURPROC;
	for ((m1 = LIST_FIRST(&p->p_heldmtx)); m1 != NULL;
		m1 = LIST_NEXT(m1, mtx_held)) {
		if (m1 == m) {
			LIST_REMOVE(m, mtx_held);
			break;
		}
	}
	return;

}

void
witness_enter(struct mtx *m, int flags, const char *file, int line)
{
	struct witness *w, *w1;
	struct mtx *m1;
	struct proc *p;
	int i;
#ifdef KDEBUG
	int go_into_kdebug = 0;
#endif /* KDEBUG */

	w = m->mtx_witness;
	p = CURPROC;

	if (flags & MTX_SPIN) {
		if (!w->w_spin)
			panic("mutex_enter: MTX_SPIN on MTX_DEF mutex %s @"
			    " %s:%d", m->mtx_description, file, line);
		if (m->mtx_recurse != 0)
			return;
		mtx_enter(&w_mtx, MTX_SPIN);
		i = witness_spin_check;
		if (i != 0 && w->w_level < i) {
			mtx_exit(&w_mtx, MTX_SPIN);
			panic("mutex_enter(%s:%x, MTX_SPIN) out of order @"
			    " %s:%d already holding %s:%x",
			    m->mtx_description, w->w_level, file, line,
			    spin_order_list[ffs(i)-1], i);
		}
		PCPU_SET(witness_spin_check, i | w->w_level);
		mtx_exit(&w_mtx, MTX_SPIN);
		return;
	}
	if (w->w_spin)
		panic("mutex_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
		    m->mtx_description, file, line);

	if (m->mtx_recurse != 0)
		return;
	if (witness_dead)
		goto out;
	if (cold)
		goto out;

	if (!mtx_legal2block())
		panic("blockable mtx_enter() of %s when not legal @ %s:%d",
			    m->mtx_description, file, line);
	/*
	 * Is this the first mutex acquired 
	 */
	if ((m1 = LIST_FIRST(&p->p_heldmtx)) == NULL)
		goto out;

	if ((w1 = m1->mtx_witness) == w) {
		if (w->w_same_squawked || dup_ok(w))
			goto out;
		w->w_same_squawked = 1;
		printf("acquring duplicate lock of same type: \"%s\"\n", 
			m->mtx_description);
		printf(" 1st @ %s:%d\n", w->w_file, w->w_line);
		printf(" 2nd @ %s:%d\n", file, line);
#ifdef KDEBUG
		go_into_kdebug = 1;
#endif /* KDEBUG */
		goto out;
	}
	MPASS(!mtx_owned(&w_mtx));
	mtx_enter(&w_mtx, MTX_SPIN);
	/*
	 * If we have a known higher number just say ok
	 */
	if (witness_watch > 1 && w->w_level > w1->w_level) {
		mtx_exit(&w_mtx, MTX_SPIN);
		goto out;
	}
	if (isitmydescendant(m1->mtx_witness, w)) {
		mtx_exit(&w_mtx, MTX_SPIN);
		goto out;
	}
	for (i = 0; m1 != NULL; m1 = LIST_NEXT(m1, mtx_held), i++) {

		ASS(i < 200);
		w1 = m1->mtx_witness;
		if (isitmydescendant(w, w1)) {
			mtx_exit(&w_mtx, MTX_SPIN);
			if (blessed(w, w1))
				goto out;
			if (m1 == &Giant) {
				if (w1->w_Giant_squawked)
					goto out;
				else
					w1->w_Giant_squawked = 1;
			} else {
				if (w1->w_other_squawked)
					goto out;
				else
					w1->w_other_squawked = 1;
			}
			printf("lock order reversal\n");
			printf(" 1st %s last acquired @ %s:%d\n",
			    w->w_description, w->w_file, w->w_line);
			printf(" 2nd %p %s @ %s:%d\n",
			    m1, w1->w_description, w1->w_file, w1->w_line);
			printf(" 3rd %p %s @ %s:%d\n",
			    m, w->w_description, file, line);
#ifdef KDEBUG
			go_into_kdebug = 1;
#endif /* KDEBUG */
			goto out;
		}
	}
	m1 = LIST_FIRST(&p->p_heldmtx);
	if (!itismychild(m1->mtx_witness, w))
		mtx_exit(&w_mtx, MTX_SPIN);

out:
#ifdef KDEBUG
	if (witness_kdebug && go_into_kdebug)
		kdebug();
#endif /* KDEBUG */
	w->w_file = file;
	w->w_line = line;
	m->mtx_line = line;
	m->mtx_file = file;

	/*
	 * If this pays off it likely means that a mutex  being witnessed
	 * is acquired in hardclock. Put it in the ignore list. It is
	 * likely not the mutex this assert fails on.
	 */
	ASS(m->mtx_held.le_prev == NULL);
	LIST_INSERT_HEAD(&p->p_heldmtx, (struct mtx*)m, mtx_held);
}

void
witness_exit(struct mtx *m, int flags, const char *file, int line)
{
	struct witness *w;

	w = m->mtx_witness;

	if (flags & MTX_SPIN) {
		if (!w->w_spin)
			panic("mutex_exit: MTX_SPIN on MTX_DEF mutex %s @"
			    " %s:%d", m->mtx_description, file, line);
		if (m->mtx_recurse != 0)
			return;
		mtx_enter(&w_mtx, MTX_SPIN);
		PCPU_SET(witness_spin_check, witness_spin_check & ~w->w_level);
		mtx_exit(&w_mtx, MTX_SPIN);
		return;
	}
	if (w->w_spin)
		panic("mutex_exit: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
		    m->mtx_description, file, line);

	if (m->mtx_recurse != 0)
		return;

	if ((flags & MTX_NOSWITCH) == 0 && !mtx_legal2block() && !cold)
		panic("switchable mtx_exit() of %s when not legal @ %s:%d",
			    m->mtx_description, file, line);
	LIST_REMOVE(m, mtx_held);
	m->mtx_held.le_prev = NULL;
}

void
witness_try_enter(struct mtx *m, int flags, const char *file, int line)
{
	struct proc *p;
	struct witness *w = m->mtx_witness;

	if (flags & MTX_SPIN) {
		if (!w->w_spin)
			panic("mutex_try_enter: "
			    "MTX_SPIN on MTX_DEF mutex %s @ %s:%d",
			    m->mtx_description, file, line);
		if (m->mtx_recurse != 0)
			return;
		mtx_enter(&w_mtx, MTX_SPIN);
		PCPU_SET(witness_spin_check, witness_spin_check | w->w_level);
		mtx_exit(&w_mtx, MTX_SPIN);
		return;
	}

	if (w->w_spin)
		panic("mutex_try_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
		    m->mtx_description, file, line);

	if (m->mtx_recurse != 0)
		return;

	w->w_file = file;
	w->w_line = line;
	m->mtx_line = line;
	m->mtx_file = file;
	p = CURPROC;
	ASS(m->mtx_held.le_prev == NULL);
	LIST_INSERT_HEAD(&p->p_heldmtx, (struct mtx*)m, mtx_held);
}

void
witness_display(void(*prnt)(const char *fmt, ...))
{
	struct witness *w, *w1;

	witness_levelall();

	for (w = w_all; w; w = w->w_next) {
		if (w->w_file == NULL)
			continue;
		for (w1 = w_all; w1; w1 = w1->w_next) {
			if (isitmychild(w1, w))
				break;
		}
		if (w1 != NULL)
			continue;
		/*
		 * This lock has no anscestors, display its descendants. 
		 */
		witness_displaydescendants(prnt, w);
	}
	prnt("\nMutex which were never acquired\n");
	for (w = w_all; w; w = w->w_next) {
		if (w->w_file != NULL)
			continue;
		prnt("%s\n", w->w_description);
	}
}

int
witness_sleep(int check_only, struct mtx *mtx, const char *file, int line)
{
	struct mtx *m;
	struct proc *p;
	char **sleep;
	int n = 0;

	p = CURPROC;
	for ((m = LIST_FIRST(&p->p_heldmtx)); m != NULL;
	    m = LIST_NEXT(m, mtx_held)) {
		if (m == mtx)
			continue;
		for (sleep = sleep_list; *sleep!= NULL; sleep++)
			if (strcmp(m->mtx_description, *sleep) == 0)
				goto next;
		printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
			file, line, check_only ? "could sleep" : "sleeping",
			m->mtx_description,
			m->mtx_witness->w_file, m->mtx_witness->w_line);
		n++;
	next:
	}
#ifdef KDEBUG
	if (witness_kdebug && n)
		kdebug();
#endif /* KDEBUG */
	return (n);
}

static struct witness *
enroll(char *description, int flag)
{
	int i;
	struct witness *w, *w1;
	char **ignore;
	char **order;

	if (!witness_watch)
		return (NULL);
	for (ignore = ignore_list; *ignore != NULL; ignore++)
		if (strcmp(description, *ignore) == 0)
			return (NULL);

	if (w_inited == 0) {
		mtx_init(&w_mtx, "witness lock", MTX_DEF);
		for (i = 0; i < WITNESS_COUNT; i++) {
			w = &w_data[i];
			witness_free(w);
		}
		w_inited = 1;
		for (order = order_list; *order != NULL; order++) {
			w = enroll(*order, MTX_DEF);
			w->w_file = "order list";
			for (order++; *order != NULL; order++) {
				w1 = enroll(*order, MTX_DEF);
				w1->w_file = "order list";
				itismychild(w, w1);
				w = w1;
    	    	    	}
		}
	}
	if ((flag & MTX_SPIN) && witness_skipspin)
		return (NULL);
	mtx_enter(&w_mtx, MTX_SPIN);
	for (w = w_all; w; w = w->w_next) {
		if (strcmp(description, w->w_description) == 0) {
			mtx_exit(&w_mtx, MTX_SPIN);
			return (w);
		}
	}
	if ((w = witness_get()) == NULL)
		return (NULL);
	w->w_next = w_all;
	w_all = w;
	w->w_description = description;
	mtx_exit(&w_mtx, MTX_SPIN);
	if (flag & MTX_SPIN) {
		w->w_spin = 1;
	
		i = 1;
		for (order = spin_order_list; *order != NULL; order++) {
			if (strcmp(description, *order) == 0)
				break;
			i <<= 1;
		}
		if (*order == NULL)
			panic("spin lock %s not in order list", description);
		w->w_level = i; 
	}
	return (w);
}

static int
itismychild(struct witness *parent, struct witness *child)
{
	static int recursed;

	/*
	 * Insert "child" after "parent"
	 */
	while (parent->w_morechildren)
		parent = parent->w_morechildren;

	if (parent->w_childcnt == WITNESS_NCHILDREN) {
		if ((parent->w_morechildren = witness_get()) == NULL)
			return (1);
		parent = parent->w_morechildren;
	}
	ASS(child != NULL);
	parent->w_children[parent->w_childcnt++] = child;
	/*
	 * now prune whole tree
	 */
	if (recursed)
		return (0);
	recursed = 1;
	for (child = w_all; child != NULL; child = child->w_next) {
		for (parent = w_all; parent != NULL;
		    parent = parent->w_next) {
			if (!isitmychild(parent, child))
				continue;
			removechild(parent, child);
			if (isitmydescendant(parent, child))
				continue;
			itismychild(parent, child);
		}
	}
	recursed = 0;
	witness_levelall();
	return (0);
}

static void
removechild(struct witness *parent, struct witness *child)
{
	struct witness *w, *w1;
	int i;

	for (w = parent; w != NULL; w = w->w_morechildren)
		for (i = 0; i < w->w_childcnt; i++)
			if (w->w_children[i] == child)
				goto found;
	return;
found:
	for (w1 = w; w1->w_morechildren != NULL; w1 = w1->w_morechildren)
		continue;
	w->w_children[i] = w1->w_children[--w1->w_childcnt];
	ASS(w->w_children[i] != NULL);

	if (w1->w_childcnt != 0)
		return;

	if (w1 == parent)
		return;
	for (w = parent; w->w_morechildren != w1; w = w->w_morechildren)
		continue;
	w->w_morechildren = 0;
	witness_free(w1);
}

static int
isitmychild(struct witness *parent, struct witness *child)
{
	struct witness *w;
	int i;

	for (w = parent; w != NULL; w = w->w_morechildren) {
		for (i = 0; i < w->w_childcnt; i++) {
			if (w->w_children[i] == child)
				return (1);
		}
	}
	return (0);
}

static int
isitmydescendant(struct witness *parent, struct witness *child)
{
	struct witness *w;
	int i;
	int j;

	for (j = 0, w = parent; w != NULL; w = w->w_morechildren, j++) {
		ASS(j < 1000);
		for (i = 0; i < w->w_childcnt; i++) {
			if (w->w_children[i] == child)
				return (1);
		}
		for (i = 0; i < w->w_childcnt; i++) {
			if (isitmydescendant(w->w_children[i], child))
				return (1);
		}
	}
	return (0);
}

void
witness_levelall (void)
{
	struct witness *w, *w1;

	for (w = w_all; w; w = w->w_next)
		if (!w->w_spin)
			w->w_level = 0;
	for (w = w_all; w; w = w->w_next) {
		if (w->w_spin)
			continue;
		for (w1 = w_all; w1; w1 = w1->w_next) {
			if (isitmychild(w1, w))
				break;
		}
		if (w1 != NULL)
			continue;
		witness_leveldescendents(w, 0);
	}
}

static void
witness_leveldescendents(struct witness *parent, int level)
{
	int i;
	struct witness *w;

	if (parent->w_level < level)
		parent->w_level = level;
	level++;
	for (w = parent; w != NULL; w = w->w_morechildren)
		for (i = 0; i < w->w_childcnt; i++)
			witness_leveldescendents(w->w_children[i], level);
}

static void
witness_displaydescendants(void(*prnt)(const char *fmt, ...),
			   struct witness *parent)
{
	struct witness *w;
	int i;
	int level = parent->w_level;

	prnt("%d", level);
	if (level < 10)
		prnt(" ");
	for (i = 0; i < level; i++)
		prnt(" ");
	prnt("%s", parent->w_description);
	if (parent->w_file != NULL) {
		prnt(" -- last acquired @ %s", parent->w_file);
#ifndef W_USE_WHERE
		prnt(":%d", parent->w_line);
#endif
		prnt("\n");
	}

	for (w = parent; w != NULL; w = w->w_morechildren)
		for (i = 0; i < w->w_childcnt; i++)
			    witness_displaydescendants(prnt, w->w_children[i]);
    }

static int
dup_ok(struct witness *w)
{
	char **dup;
	
	for (dup = dup_list; *dup!= NULL; dup++)
		if (strcmp(w->w_description, *dup) == 0)
			return (1);
	return (0);
}

static int
blessed(struct witness *w1, struct witness *w2)
{
	int i;
	struct witness_blessed *b;

	for (i = 0; i < blessed_count; i++) {
		b = &blessed_list[i];
		if (strcmp(w1->w_description, b->b_lock1) == 0) {
			if (strcmp(w2->w_description, b->b_lock2) == 0)
				return (1);
			continue;
		}
		if (strcmp(w1->w_description, b->b_lock2) == 0)
			if (strcmp(w2->w_description, b->b_lock1) == 0)
				return (1);
	}
	return (0);
}

static struct witness *
witness_get()
{
	struct witness *w;

	if ((w = w_free) == NULL) {
		witness_dead = 1;
		mtx_exit(&w_mtx, MTX_SPIN);
		printf("witness exhausted\n");
		return (NULL);
	}
	w_free = w->w_next;
	bzero(w, sizeof(*w));
	return (w);
}

static void
witness_free(struct witness *w)
{
	w->w_next = w_free;
	w_free = w;
}

void
witness_list(struct proc *p)
{
	struct mtx *m;

	for ((m = LIST_FIRST(&p->p_heldmtx)); m != NULL;
	    m = LIST_NEXT(m, mtx_held)) {
		printf("\t\"%s\" (%p) locked at %s:%d\n",
		    m->mtx_description, m,
		    m->mtx_witness->w_file, m->mtx_witness->w_line);
	}
}

void
witness_save(struct mtx *m, const char **filep, int *linep)
{
	*filep = m->mtx_witness->w_file;
	*linep = m->mtx_witness->w_line;
}

void
witness_restore(struct mtx *m, const char *file, int line)
{
	m->mtx_witness->w_file = file;
	m->mtx_witness->w_line = line;
}

#endif	/* (defined(SMP_DEBUG) && defined(WITNESS)) */
OpenPOWER on IntegriCloud