summaryrefslogtreecommitdiffstats
path: root/sys/kern/kern_mutex.c
blob: 64d397263d7365a361f5c3545942cf194bec2879 (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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
/*-
 * 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 $
 *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
 * $FreeBSD$
 */

/*
 * Machine independent bits of mutex implementation and implementation of
 * `witness' structure & related debugging routines.
 */

/*
 *	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 "opt_ddb.h"
#include "opt_witness.h"

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/vmmeter.h>
#include <sys/ktr.h>

#include <machine/atomic.h>
#include <machine/bus.h>
#include <machine/clock.h>
#include <machine/cpu.h>

#include <ddb/ddb.h>

#include <vm/vm.h>
#include <vm/vm_extern.h>

#include <sys/mutex.h>

/*
 * The WITNESS-enabled mutex debug structure.
 */
#ifdef WITNESS
struct mtx_debug {
	struct witness	*mtxd_witness;
	LIST_ENTRY(mtx)	mtxd_held;
	const char	*mtxd_file;
	int		mtxd_line;
};

#define mtx_held	mtx_debug->mtxd_held
#define	mtx_file	mtx_debug->mtxd_file
#define	mtx_line	mtx_debug->mtxd_line
#define	mtx_witness	mtx_debug->mtxd_witness
#endif	/* WITNESS */

/*
 * Internal utility macros.
 */
#define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)

#define mtx_owner(m)	(mtx_unowned((m)) ? NULL \
	: (struct proc *)((m)->mtx_lock & MTX_FLAGMASK))

#define RETIP(x)		*(((uintptr_t *)(&x)) - 1)
#define SET_PRIO(p, pri)	(p)->p_priority = (pri)

/*
 * Early WITNESS-enabled declarations.
 */
#ifdef WITNESS

/*
 * Internal WITNESS routines which must be prototyped early.
 *
 * XXX: When/if witness code is cleaned up, it would be wise to place all
 *	witness prototyping early in this file.
 */ 
static void witness_init(struct mtx *, int flag);
static void witness_destroy(struct mtx *);
static void witness_display(void(*)(const char *fmt, ...));

MALLOC_DEFINE(M_WITNESS, "witness", "witness mtx_debug structure");

/* All mutexes in system (used for debug/panic) */
static struct mtx_debug all_mtx_debug = { NULL, {NULL, NULL}, NULL, 0 };

/*
 * This global is set to 0 once it becomes safe to use the witness code.
 */
static int witness_cold = 1;

#else	/* WITNESS */

/* XXX XXX XXX
 * flag++ is sleazoid way of shuting up warning
 */
#define witness_init(m, flag) flag++
#define witness_destroy(m)
#define witness_try_enter(m, t, f, l)
#endif	/* WITNESS */

/*
 * All mutex locks in system are kept on the all_mtx list.
 */
static struct mtx all_mtx = { MTX_UNOWNED, 0, 0, 0, "All mutexes queue head",
	TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
	{ NULL, NULL }, &all_mtx, &all_mtx,
#ifdef WITNESS
	&all_mtx_debug
#else
	NULL
#endif
	 };

/*
 * Global variables for book keeping.
 */
static int	mtx_cur_cnt;
static int	mtx_max_cnt;

/*
 * Prototypes for non-exported routines.
 *
 * NOTE: Prototypes for witness routines are placed at the bottom of the file. 
 */
static void	propagate_priority(struct proc *);

static void
propagate_priority(struct proc *p)
{
	int pri = p->p_priority;
	struct mtx *m = p->p_blocked;

	mtx_assert(&sched_lock, MA_OWNED);
	for (;;) {
		struct proc *p1;

		p = mtx_owner(m);

		if (p == NULL) {
			/*
			 * This really isn't quite right. Really
			 * ought to bump priority of process that
			 * next acquires the mutex.
			 */
			MPASS(m->mtx_lock == MTX_CONTESTED);
			return;
		}

		MPASS(p->p_magic == P_MAGIC);
		KASSERT(p->p_stat != SSLEEP, ("sleeping process owns a mutex"));
		if (p->p_priority <= pri)
			return;

		/*
		 * Bump this process' priority.
		 */
		SET_PRIO(p, pri);

		/*
		 * If lock holder is actually running, just bump priority.
		 */
#ifdef SMP
		/*
		 * For SMP, we can check the p_oncpu field to see if we are
		 * running.
		 */
		if (p->p_oncpu != 0xff) {
			MPASS(p->p_stat == SRUN || p->p_stat == SZOMB);
			return;
		}
#else
		/*
		 * For UP, we check to see if p is curproc (this shouldn't
		 * ever happen however as it would mean we are in a deadlock.)
		 */
		if (p == curproc) {
			panic("Deadlock detected");
			return;
		}
#endif
		/*
		 * If on run queue move to new run queue, and
		 * quit.
		 */
		if (p->p_stat == SRUN) {
			printf("XXX: moving proc %d(%s) to a new run queue\n",
			       p->p_pid, p->p_comm);
			MPASS(p->p_blocked == NULL);
			remrunqueue(p);
			setrunqueue(p);
			return;
		}

		/*
		 * If we aren't blocked on a mutex, we should be.
		 */
		KASSERT(p->p_stat == SMTX, (
		    "process %d(%s):%d holds %s but isn't blocked on a mutex\n",
		    p->p_pid, p->p_comm, p->p_stat,
		    m->mtx_description));

		/*
		 * Pick up the mutex that p is blocked on.
		 */
		m = p->p_blocked;
		MPASS(m != NULL);

		printf("XXX: process %d(%s) is blocked on %s\n", p->p_pid,
		    p->p_comm, m->mtx_description);

		/*
		 * Check if the proc needs to be moved up on
		 * the blocked chain
		 */
		if (p == TAILQ_FIRST(&m->mtx_blocked)) {
			printf("XXX: process at head of run queue\n");
			continue;
		}

		p1 = TAILQ_PREV(p, rq, p_procq);
		if (p1->p_priority <= pri) {
			printf(
			   "XXX: previous process %d(%s) has higher priority\n",
	                    p->p_pid, p->p_comm);
			continue;
		}

		/*
		 * Remove proc from blocked chain and determine where
		 * it should be moved up to.  Since we know that p1 has
		 * a lower priority than p, we know that at least one
		 * process in the chain has a lower priority and that
		 * p1 will thus not be NULL after the loop.
		 */
		TAILQ_REMOVE(&m->mtx_blocked, p, p_procq);
		TAILQ_FOREACH(p1, &m->mtx_blocked, p_procq) {
			MPASS(p1->p_magic == P_MAGIC);
			if (p1->p_priority > pri)
				break;
		}

		MPASS(p1 != NULL);
		TAILQ_INSERT_BEFORE(p1, p, p_procq);
		CTR4(KTR_LOCK,
		    "propagate_priority: p %p moved before %p on [%p] %s",
		    p, p1, m, m->mtx_description);
	}
}

/*
 * The important part of mtx_trylock{,_flags}()
 * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that
 * if we're called, it's because we know we don't already own this lock.
 */
int
_mtx_trylock(struct mtx *m, int opts, const char *file, int line)
{
	int rval;

	KASSERT(CURPROC != NULL, ("curproc is NULL in _mtx_trylock"));

	/*
	 * _mtx_trylock does not accept MTX_NOSWITCH option.
	 */
	MPASS((opts & MTX_NOSWITCH) == 0);

	rval = _obtain_lock(m, CURTHD);

#ifdef WITNESS
	if (rval && m->mtx_witness != NULL) {
		/*
		 * We do not handle recursion in _mtx_trylock; see the
		 * note at the top of the routine.
		 */
		MPASS(!mtx_recursed(m));
		witness_try_enter(m, (opts | m->mtx_flags), file, line);
	}
#endif	/* WITNESS */

	if ((opts & MTX_QUIET) == 0)
		CTR5(KTR_LOCK, "TRY_ENTER %s [%p] result=%d at %s:%d",
		    m->mtx_description, m, rval, file, line);

	return rval;
}

/*
 * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
 *
 * We call this if the lock is either contested (i.e. we need to go to
 * sleep waiting for it), or if we need to recurse on it.
 */
void
_mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line)
{
	struct proc *p = CURPROC;

	if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)p) {
		m->mtx_recurse++;
		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
		if ((opts & MTX_QUIET) == 0)
			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recurse", m);
		return;
	}

	if ((opts & MTX_QUIET) == 0)
		CTR3(KTR_LOCK, "mtx_lock: %p contested (lock=%p) [%p]", m,
		    (void *)m->mtx_lock, (void *)RETIP(m));

	/*
	 * Save our priority. Even though p_nativepri is protected by
	 * sched_lock, we don't obtain it here as it can be expensive.
	 * Since this is the only place p_nativepri is set, and since two
	 * CPUs will not be executing the same process concurrently, we know
	 * that no other CPU is going to be messing with this. Also,
	 * p_nativepri is only read when we are blocked on a mutex, so that
	 * can't be happening right now either.
	 */
	p->p_nativepri = p->p_priority;

	while (!_obtain_lock(m, p)) {
		uintptr_t v;
		struct proc *p1;

		mtx_lock_spin(&sched_lock);
		/*
		 * Check if the lock has been released while spinning for
		 * the sched_lock.
		 */
		if ((v = m->mtx_lock) == MTX_UNOWNED) {
			mtx_unlock_spin(&sched_lock);
			continue;
		}

		/*
		 * The mutex was marked contested on release. This means that
		 * there are processes blocked on it.
		 */
		if (v == MTX_CONTESTED) {
			p1 = TAILQ_FIRST(&m->mtx_blocked);
			KASSERT(p1 != NULL,
			    ("contested mutex has no contesters"));
			m->mtx_lock = (uintptr_t)p | MTX_CONTESTED;

			if (p1->p_priority < p->p_priority)
				SET_PRIO(p, p1->p_priority); 
			mtx_unlock_spin(&sched_lock);
			return;
		}

		/*
		 * If the mutex isn't already contested and a failure occurs
		 * setting the contested bit, the mutex was either released
		 * or the state of the MTX_RECURSED bit changed.
		 */
		if ((v & MTX_CONTESTED) == 0 &&
		    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
			(void *)(v | MTX_CONTESTED))) {
			mtx_unlock_spin(&sched_lock);
			continue;
		}

		/*
		 * We deffinately must sleep for this lock.
		 */
		mtx_assert(m, MA_NOTOWNED);

#ifdef notyet
		/*
		 * If we're borrowing an interrupted thread's VM context, we
		 * must clean up before going to sleep.
		 */
		if (p->p_flag & (P_ITHD | P_SITHD)) {
			ithd_t *it = (ithd_t *)p;

			if (it->it_interrupted) {
				if ((opts & MTX_QUIET) == 0)
					CTR2(KTR_LOCK,
					    "mtx_lock: 0x%x interrupted 0x%x",
					    it, it->it_interrupted);
				intr_thd_fixup(it);
			}
		}
#endif

		/*
		 * Put us on the list of threads blocked on this mutex.
		 */
		if (TAILQ_EMPTY(&m->mtx_blocked)) {
			p1 = (struct proc *)(m->mtx_lock & MTX_FLAGMASK);
			LIST_INSERT_HEAD(&p1->p_contested, m, mtx_contested);
			TAILQ_INSERT_TAIL(&m->mtx_blocked, p, p_procq);
		} else {
			TAILQ_FOREACH(p1, &m->mtx_blocked, p_procq)
				if (p1->p_priority > p->p_priority)
					break;
			if (p1)
				TAILQ_INSERT_BEFORE(p1, p, p_procq);
			else
				TAILQ_INSERT_TAIL(&m->mtx_blocked, p, p_procq);
		}

		/*
		 * Save who we're blocked on.
		 */
		p->p_blocked = m;
		p->p_mtxname = m->mtx_description;
		p->p_stat = SMTX;
#if 0
		propagate_priority(p);
#endif

		if ((opts & MTX_QUIET) == 0)
			CTR3(KTR_LOCK,
			    "_mtx_lock_sleep: p %p blocked on [%p] %s", p, m,
			    m->mtx_description);

		mi_switch();

		if ((opts & MTX_QUIET) == 0)
			CTR3(KTR_LOCK,
			  "_mtx_lock_sleep: p %p free from blocked on [%p] %s",
			  p, m, m->mtx_description);

		mtx_unlock_spin(&sched_lock);
	}

	return;
}

/*
 * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
 *
 * This is only called if we need to actually spin for the lock. Recursion
 * is handled inline.
 */
void
_mtx_lock_spin(struct mtx *m, int opts, u_int mtx_intr, const char *file,
	       int line)
{
	int i = 0;

	if ((opts & MTX_QUIET) == 0)
		CTR1(KTR_LOCK, "mtx_lock_spin: %p spinning", m);

	for (;;) {
		if (_obtain_lock(m, CURPROC))
			break;

		while (m->mtx_lock != MTX_UNOWNED) {
			if (i++ < 1000000)
				continue;
			if (i++ < 6000000)
				DELAY(1);
#ifdef DDB
			else if (!db_active)
#else
			else
#endif
			panic("spin lock %s held by %p for > 5 seconds",
			    m->mtx_description, (void *)m->mtx_lock);
		}
	}

	m->mtx_saveintr = mtx_intr;
	if ((opts & MTX_QUIET) == 0)
		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);

	return;
}

/*
 * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
 *
 * We are only called here if the lock is recursed or contested (i.e. we
 * need to wake up a blocked thread).
 */
void
_mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
{
	struct proc *p, *p1;
	struct mtx *m1;
	int pri;

	p = CURPROC;
	MPASS4(mtx_owned(m), "mtx_owned(mpp)", file, line);

	if ((opts & MTX_QUIET) == 0)
		CTR5(KTR_LOCK, "REL %s [%p] r=%d at %s:%d", m->mtx_description,
		    m, m->mtx_recurse, file, line);

	if (mtx_recursed(m)) {
		if (--(m->mtx_recurse) == 0)
			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
		if ((opts & MTX_QUIET) == 0)
			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
		return;
	}

	mtx_lock_spin(&sched_lock);
	if ((opts & MTX_QUIET) == 0)
		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);

	p1 = TAILQ_FIRST(&m->mtx_blocked);
	MPASS(p->p_magic == P_MAGIC);
	MPASS(p1->p_magic == P_MAGIC);

	TAILQ_REMOVE(&m->mtx_blocked, p1, p_procq);

	if (TAILQ_EMPTY(&m->mtx_blocked)) {
		LIST_REMOVE(m, mtx_contested);
		_release_lock_quick(m);
		if ((opts & MTX_QUIET) == 0)
			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
	} else
		atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED);

	pri = MAXPRI;
	LIST_FOREACH(m1, &p->p_contested, mtx_contested) {
		int cp = TAILQ_FIRST(&m1->mtx_blocked)->p_priority;
		if (cp < pri)
			pri = cp;
	}

	if (pri > p->p_nativepri)
		pri = p->p_nativepri;
	SET_PRIO(p, pri);

	if ((opts & MTX_QUIET) == 0)
		CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p",
		    m, p1);

	p1->p_blocked = NULL;
	p1->p_mtxname = NULL;
	p1->p_stat = SRUN;
	setrunqueue(p1);

	if ((opts & MTX_NOSWITCH) == 0 && p1->p_priority < pri) {
#ifdef notyet
		if (p->p_flag & (P_ITHD | P_SITHD)) {
			ithd_t *it = (ithd_t *)p;

			if (it->it_interrupted) {
				if ((opts & MTX_QUIET) == 0)
					CTR2(KTR_LOCK,
				    "_mtx_unlock_sleep: 0x%x interrupted 0x%x",
					    it, it->it_interrupted);
				intr_thd_fixup(it);
			}
		}
#endif
		setrunqueue(p);
		if ((opts & MTX_QUIET) == 0)
			CTR2(KTR_LOCK,
			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
			    (void *)m->mtx_lock);

		mi_switch();
		if ((opts & MTX_QUIET) == 0)
			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
			    m, (void *)m->mtx_lock);
	}

	mtx_unlock_spin(&sched_lock);

	return;
}

/*
 * All the unlocking of MTX_SPIN locks is done inline.
 * See the _rel_spin_lock() macro for the details. 
 */

/*
 * The INVARIANTS-enabled mtx_assert()
 */
#ifdef INVARIANTS
void
_mtx_assert(struct mtx *m, int what, const char *file, int line)
{
	switch ((what)) {
	case MA_OWNED:
	case MA_OWNED | MA_RECURSED:
	case MA_OWNED | MA_NOTRECURSED:
		if (!mtx_owned((m)))
			panic("mutex %s not owned at %s:%d",
			    (m)->mtx_description, file, line);
		if (mtx_recursed((m))) {
			if (((what) & MA_NOTRECURSED) != 0)
				panic("mutex %s recursed at %s:%d",
				    (m)->mtx_description, file, line);
		} else if (((what) & MA_RECURSED) != 0) {
			panic("mutex %s unrecursed at %s:%d",
			    (m)->mtx_description, file, line);
		}
		break;
	case MA_NOTOWNED:
		if (mtx_owned((m)))
			panic("mutex %s owned at %s:%d",
			    (m)->mtx_description, file, line);
		break;
	default:
		panic("unknown mtx_assert at %s:%d", file, line);
	}
}
#endif

/*
 * The MUTEX_DEBUG-enabled mtx_validate()
 */
#define MV_DESTROY	0	/* validate before destory */
#define MV_INIT		1	/* validate before init */

#ifdef MUTEX_DEBUG

int mtx_validate __P((struct mtx *, int));

int
mtx_validate(struct mtx *m, int when)
{
	struct mtx *mp;
	int i;
	int retval = 0;

#ifdef WITNESS
	if (witness_cold)
		return 0;
#endif
	if (m == &all_mtx || cold)
		return 0;

	mtx_lock(&all_mtx);
/*
 * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
 * we can re-enable the kernacc() checks.
 */
#ifndef __alpha__
	MPASS(kernacc((caddr_t)all_mtx.mtx_next, sizeof(uintptr_t),
	    VM_PROT_READ) == 1);
#endif
	MPASS(all_mtx.mtx_next->mtx_prev == &all_mtx);
	for (i = 0, mp = all_mtx.mtx_next; mp != &all_mtx; mp = mp->mtx_next) {
#ifndef __alpha__
		if (kernacc((caddr_t)mp->mtx_next, sizeof(uintptr_t),
		    VM_PROT_READ) != 1) {
			panic("mtx_validate: mp=%p mp->mtx_next=%p",
			    mp, mp->mtx_next);
		}
#endif
		i++;
		if (i > mtx_cur_cnt) {
			panic("mtx_validate: too many in chain, known=%d\n",
			    mtx_cur_cnt);
		}
	}
	MPASS(i == mtx_cur_cnt); 
	switch (when) {
	case MV_DESTROY:
		for (mp = all_mtx.mtx_next; mp != &all_mtx; mp = mp->mtx_next)
			if (mp == m)
				break;
		MPASS(mp == m);
		break;
	case MV_INIT:
		for (mp = all_mtx.mtx_next; mp != &all_mtx; mp = mp->mtx_next)
		if (mp == m) {
			/*
			 * Not good. This mutex already exists.
			 */
			printf("re-initing existing mutex %s\n",
			    m->mtx_description);
			MPASS(m->mtx_lock == MTX_UNOWNED);
			retval = 1;
		}
	}
	mtx_unlock(&all_mtx);
	return (retval);
}
#endif

/*
 * Mutex initialization routine; initialize lock `m' of type contained in
 * `opts' with options contained in `opts' and description `description.'
 * Place on "all_mtx" queue.
 */ 
void
mtx_init(struct mtx *m, const char *description, int opts)
{

	if ((opts & MTX_QUIET) == 0)
		CTR2(KTR_LOCK, "mtx_init %p (%s)", m, description);

#ifdef MUTEX_DEBUG
	/* Diagnostic and error correction */
	if (mtx_validate(m, MV_INIT))
		return;
#endif

	bzero((void *)m, sizeof *m);
	TAILQ_INIT(&m->mtx_blocked);

#ifdef WITNESS
	if (!witness_cold) {
		m->mtx_debug = malloc(sizeof(struct mtx_debug),
		    M_WITNESS, M_NOWAIT | M_ZERO);
		MPASS(m->mtx_debug != NULL);
	}
#endif

	m->mtx_description = description;
	m->mtx_flags = opts;
	m->mtx_lock = MTX_UNOWNED;

	/* Put on all mutex queue */
	mtx_lock(&all_mtx);
	m->mtx_next = &all_mtx;
	m->mtx_prev = all_mtx.mtx_prev;
	m->mtx_prev->mtx_next = m;
	all_mtx.mtx_prev = m;
	if (++mtx_cur_cnt > mtx_max_cnt)
		mtx_max_cnt = mtx_cur_cnt;
	mtx_unlock(&all_mtx);

#ifdef WITNESS
	if (!witness_cold)
		witness_init(m, opts);
#endif
}

/*
 * Remove lock `m' from all_mtx queue.
 */
void
mtx_destroy(struct mtx *m)
{

#ifdef WITNESS
	KASSERT(!witness_cold, ("%s: Cannot destroy while still cold\n",
	    __FUNCTION__));
#endif

	CTR2(KTR_LOCK, "mtx_destroy %p (%s)", m, m->mtx_description);

#ifdef MUTEX_DEBUG
	if (m->mtx_next == NULL)
		panic("mtx_destroy: %p (%s) already destroyed",
		    m, m->mtx_description);

	if (!mtx_owned(m)) {
		MPASS(m->mtx_lock == MTX_UNOWNED);
	} else {
		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
	}

	/* diagnostic */
	mtx_validate(m, MV_DESTROY);
#endif

#ifdef WITNESS
	if (m->mtx_witness)
		witness_destroy(m);
#endif /* WITNESS */

	/* Remove from the all mutex queue */
	mtx_lock(&all_mtx);
	m->mtx_next->mtx_prev = m->mtx_prev;
	m->mtx_prev->mtx_next = m->mtx_next;

#ifdef MUTEX_DEBUG
	m->mtx_next = m->mtx_prev = NULL;
#endif

#ifdef WITNESS
	free(m->mtx_debug, M_WITNESS);
	m->mtx_debug = NULL;
#endif

	mtx_cur_cnt--;
	mtx_unlock(&all_mtx);
}


/*
 * The WITNESS-enabled diagnostic code.
 */
#ifdef WITNESS
static void
witness_fixup(void *dummy __unused)
{
	struct mtx *mp;

	/*
	 * We have to release Giant before initializing its witness
	 * structure so that WITNESS doesn't get confused.
	 */
	mtx_unlock(&Giant);
	mtx_assert(&Giant, MA_NOTOWNED);

	mtx_lock(&all_mtx);

	/* Iterate through all mutexes and finish up mutex initialization. */
	for (mp = all_mtx.mtx_next; mp != &all_mtx; mp = mp->mtx_next) {

		mp->mtx_debug = malloc(sizeof(struct mtx_debug),
		    M_WITNESS, M_NOWAIT | M_ZERO);
		MPASS(mp->mtx_debug != NULL);

		witness_init(mp, mp->mtx_flags);
	}
	mtx_unlock(&all_mtx);

	/* Mark the witness code as being ready for use. */
	atomic_store_rel_int(&witness_cold, 0);

	mtx_lock(&Giant);
}
SYSINIT(wtnsfxup, SI_SUB_MUTEX, SI_ORDER_FIRST, witness_fixup, NULL)

#define WITNESS_COUNT 200
#define	WITNESS_NCHILDREN 2

int witness_watch = 1;

struct witness {
	struct witness	*w_next;
	const 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_spin:1;	/* MTX_SPIN type mutex. */
	u_int		 w_level;
	struct witness	*w_children[WITNESS_NCHILDREN];
};

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

#ifdef DDB
/*
 * When DDB is enabled and witness_ddb 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.
 */
int	witness_ddb;
#ifdef WITNESS_DDB
TUNABLE_INT_DECL("debug.witness_ddb", 1, witness_ddb);
#else
TUNABLE_INT_DECL("debug.witness_ddb", 0, witness_ddb);
#endif
SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
#endif /* DDB */

int	witness_skipspin;
#ifdef WITNESS_SKIPSPIN
TUNABLE_INT_DECL("debug.witness_skipspin", 1, witness_skipspin);
#else
TUNABLE_INT_DECL("debug.witness_skipspin", 0, witness_skipspin);
#endif
SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0,
    "");

/*
 * Witness-enabled globals
 */
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];

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

static char *ignore_list[] = {
	"witness lock",
	NULL
};

static char *spin_order_list[] = {
	"sio",
	"sched lock",
#ifdef __i386__
	"clk",
#endif
	"callout",
	/*
	 * leaf locks
	 */
#ifdef __i386__
	"ap boot",
	"imen",
#endif
	"com",
	"smp rendezvous",
	NULL
};

static char *order_list[] = {
	"Giant", "uidinfo hash", "uidinfo struct", NULL,
	"Giant", "proctree", "allproc", "process lock", NULL,
	NULL
};

static char *dup_list[] = {
	NULL
};

static char *sleep_list[] = {
	"Giant",
	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);

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

static 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;

}

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

	KASSERT(!witness_cold, ("%s: witness_cold\n", __FUNCTION__));
	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);
	}
}

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 DDB
	int go_into_ddb = 0;
#endif /* DDB */

	if (witness_cold || m->mtx_witness == NULL || panicstr)
		return;
	w = m->mtx_witness;
	p = CURPROC;

	if (flags & MTX_SPIN) {
		if ((m->mtx_flags & MTX_SPIN) == 0)
			panic("mutex_enter: MTX_SPIN on MTX_DEF mutex %s @"
			    " %s:%d", m->mtx_description, file, line);
		if (mtx_recursed(m)) {
			if ((m->mtx_flags & MTX_RECURSE) == 0)
				panic("mutex_enter: recursion on non-recursive"
				    " mutex %s @ %s:%d", m->mtx_description,
				    file, line);
			return;
		}
		mtx_lock_spin_flags(&w_mtx, MTX_QUIET);
		i = PCPU_GET(witness_spin_check);
		if (i != 0 && w->w_level < i) {
			mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
			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_unlock_spin_flags(&w_mtx, MTX_QUIET);
		w->w_file = file;
		w->w_line = line;
		m->mtx_line = line;
		m->mtx_file = file;
		return;
	}
	if ((m->mtx_flags & MTX_SPIN) != 0)
		panic("mutex_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
		    m->mtx_description, file, line);

	if (mtx_recursed(m)) {
		if ((m->mtx_flags & MTX_RECURSE) == 0)
			panic("mutex_enter: recursion on non-recursive"
			    " mutex %s @ %s:%d", m->mtx_description,
			    file, line);
		return;
	}
	if (witness_dead)
		goto out;
	if (cold)
		goto out;

	if (!mtx_legal2block())
		panic("blockable mtx_lock() 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 DDB
		go_into_ddb = 1;
#endif /* DDB */
		goto out;
	}
	MPASS(!mtx_owned(&w_mtx));
	mtx_lock_spin_flags(&w_mtx, MTX_QUIET);
	/*
	 * If we have a known higher number just say ok
	 */
	if (witness_watch > 1 && w->w_level > w1->w_level) {
		mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
		goto out;
	}
	if (isitmydescendant(m1->mtx_witness, w)) {
		mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
		goto out;
	}
	for (i = 0; m1 != NULL; m1 = LIST_NEXT(m1, mtx_held), i++) {

		MPASS(i < 200);
		w1 = m1->mtx_witness;
		if (isitmydescendant(w, w1)) {
			mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
			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 DDB
			go_into_ddb = 1;
#endif /* DDB */
			goto out;
		}
	}
	m1 = LIST_FIRST(&p->p_heldmtx);
	if (!itismychild(m1->mtx_witness, w))
		mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);

out:
#ifdef DDB
	if (witness_ddb && go_into_ddb)
		Debugger("witness_enter");
#endif /* DDB */
	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.
	 */
	MPASS(m->mtx_held.le_prev == NULL);
	LIST_INSERT_HEAD(&p->p_heldmtx, (struct mtx*)m, mtx_held);
}

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

	if (witness_cold)
		return;
	if (panicstr)
		return;
	if (flags & MTX_SPIN) {
		if ((m->mtx_flags & MTX_SPIN) == 0)
			panic("mutex_try_enter: "
			    "MTX_SPIN on MTX_DEF mutex %s @ %s:%d",
			    m->mtx_description, file, line);
		if (mtx_recursed(m)) {
			if ((m->mtx_flags & MTX_RECURSE) == 0)
				panic("mutex_try_enter: recursion on"
				    " non-recursive mutex %s @ %s:%d",
				    m->mtx_description, file, line);
			return;
		}
		mtx_lock_spin_flags(&w_mtx, MTX_QUIET);
		PCPU_SET(witness_spin_check,
		    PCPU_GET(witness_spin_check) | w->w_level);
		mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
		w->w_file = file;
		w->w_line = line;
		m->mtx_line = line;
		m->mtx_file = file;
		return;
	}

	if ((m->mtx_flags & MTX_SPIN) != 0)
		panic("mutex_try_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
		    m->mtx_description, file, line);

	if (mtx_recursed(m)) {
		if ((m->mtx_flags & MTX_RECURSE) == 0)
			panic("mutex_try_enter: recursion on non-recursive"
			    " mutex %s @ %s:%d", m->mtx_description, file,
			    line);
		return;
	}
	w->w_file = file;
	w->w_line = line;
	m->mtx_line = line;
	m->mtx_file = file;
	p = CURPROC;
	MPASS(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;

	if (witness_cold || m->mtx_witness == NULL || panicstr)
		return;
	w = m->mtx_witness;

	if (flags & MTX_SPIN) {
		if ((m->mtx_flags & MTX_SPIN) == 0)
			panic("mutex_exit: MTX_SPIN on MTX_DEF mutex %s @"
			    " %s:%d", m->mtx_description, file, line);
		if (mtx_recursed(m)) {
			if ((m->mtx_flags & MTX_RECURSE) == 0)
				panic("mutex_exit: recursion on non-recursive"
				    " mutex %s @ %s:%d", m->mtx_description,
				    file, line); 
			return;
		}
		mtx_lock_spin_flags(&w_mtx, MTX_QUIET);
		PCPU_SET(witness_spin_check,
		    PCPU_GET(witness_spin_check) & ~w->w_level);
		mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
		return;
	}
	if ((m->mtx_flags & MTX_SPIN) != 0)
		panic("mutex_exit: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
		    m->mtx_description, file, line);

	if (mtx_recursed(m)) {
		if ((m->mtx_flags & MTX_RECURSE) == 0)
			panic("mutex_exit: recursion on non-recursive"
			    " mutex %s @ %s:%d", m->mtx_description,
			    file, line); 
		return;
	}

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

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;

	KASSERT(!witness_cold, ("%s: witness_cold\n", __FUNCTION__));
	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 DDB
	if (witness_ddb && n)
		Debugger("witness_sleep");
#endif /* DDB */
	return (n);
}

static struct witness *
enroll(const 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_SPIN);
		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_lock_spin_flags(&w_mtx, MTX_QUIET);
	for (w = w_all; w; w = w->w_next) {
		if (strcmp(description, w->w_description) == 0) {
			mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
			return (w);
		}
	}
	if ((w = witness_get()) == NULL)
		return (NULL);
	w->w_next = w_all;
	w_all = w;
	w->w_description = description;
	mtx_unlock_spin_flags(&w_mtx, MTX_QUIET);
	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;
	}
	MPASS(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];
	MPASS(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++) {
		MPASS(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_unlock_spin_flags(&w_mtx, MTX_QUIET);
		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;
}

int
witness_list(struct proc *p)
{
	struct mtx *m;
	int nheld;

	KASSERT(!witness_cold, ("%s: witness_cold\n", __FUNCTION__));
	nheld = 0;
	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);
		nheld++;
	}

	return (nheld);
}

#ifdef DDB

DB_COMMAND(witness_list, db_witness_list)
{

	witness_list(CURPROC);
}

#endif

void
witness_save(struct mtx *m, const char **filep, int *linep)
{

	KASSERT(!witness_cold, ("%s: witness_cold\n", __FUNCTION__));
	if (m->mtx_witness == NULL)
		return;

	*filep = m->mtx_witness->w_file;
	*linep = m->mtx_witness->w_line;
}

void
witness_restore(struct mtx *m, const char *file, int line)
{

	KASSERT(!witness_cold, ("%s: witness_cold\n", __FUNCTION__));
	if (m->mtx_witness == NULL)
		return;

	m->mtx_witness->w_file = file;
	m->mtx_witness->w_line = line;
}

#endif	/* WITNESS */
OpenPOWER on IntegriCloud