1 /*-
2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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
14 * written permission.
15 *
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
26 * SUCH DAMAGE.
27 *
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 $
30 */
31
32 /*
33 * Implementation of the `witness' lock verifier. Originally implemented for
34 * mutexes in BSD/OS. Extended to handle generic lock objects and lock
35 * classes in FreeBSD.
36 */
37
38 /*
39 * Main Entry: witness
40 * Pronunciation: 'wit-n&s
41 * Function: noun
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
54 * life -- Pilot>
55 * 6 capitalized : a member of the Jehovah's Witnesses
56 */
57
58 /*
59 * Special rules concerning Giant and lock orders:
60 *
61 * 1) Giant must be acquired before any other mutexes. Stated another way,
62 * no other mutex may be held when Giant is acquired.
63 *
64 * 2) Giant must be released when blocking on a sleepable lock.
65 *
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
69 * 2).
70 *
71 * 3) Giant may be acquired before or after sleepable locks.
72 *
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.
82 */
83
84 #include <sys/cdefs.h>
85 __FBSDID("$FreeBSD: releng/6.3/sys/kern/subr_witness.c 173886 2007-11-24 19:45:58Z cvs2svn $");
86
87 #include "opt_ddb.h"
88 #include "opt_witness.h"
89
90 #include <sys/param.h>
91 #include <sys/bus.h>
92 #include <sys/kdb.h>
93 #include <sys/kernel.h>
94 #include <sys/ktr.h>
95 #include <sys/lock.h>
96 #include <sys/malloc.h>
97 #include <sys/mutex.h>
98 #include <sys/proc.h>
99 #include <sys/sysctl.h>
100 #include <sys/systm.h>
101
102 #include <ddb/ddb.h>
103
104 #include <machine/stdarg.h>
105
106 /* Define this to check for blessed mutexes */
107 #undef BLESSING
108
109 #define WITNESS_COUNT 1024
110 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
111 /*
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.
115 */
116 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
117
118 #define WITNESS_NCHILDREN 6
119
120 struct witness_child_list_entry;
121
122 struct witness {
123 const char *w_name;
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... */
128 const char *w_file;
129 int w_line;
130 u_int w_level;
131 u_int w_refcount;
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;
136 };
137
138 struct witness_child_list_entry {
139 struct witness_child_list_entry *wcl_next;
140 struct witness *wcl_children[WITNESS_NCHILDREN];
141 u_int wcl_count;
142 };
143
144 STAILQ_HEAD(witness_list, witness);
145
146 #ifdef BLESSING
147 struct witness_blessed {
148 const char *b_lock1;
149 const char *b_lock2;
150 };
151 #endif
152
153 struct witness_order_list_entry {
154 const char *w_name;
155 struct lock_class *w_class;
156 };
157
158 #ifdef BLESSING
159 static int blessed(struct witness *, struct witness *);
160 #endif
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 void removechild(struct witness *parent, struct witness *child);
169 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
170 static const char *fixup_filename(const char *file);
171 static struct witness *witness_get(void);
172 static void witness_free(struct witness *m);
173 static struct witness_child_list_entry *witness_child_get(void);
174 static void witness_child_free(struct witness_child_list_entry *wcl);
175 static struct lock_list_entry *witness_lock_list_get(void);
176 static void witness_lock_list_free(struct lock_list_entry *lle);
177 static struct lock_instance *find_instance(struct lock_list_entry *lock_list,
178 struct lock_object *lock);
179 static void witness_list_lock(struct lock_instance *instance);
180 #ifdef DDB
181 static void witness_leveldescendents(struct witness *parent, int level);
182 static void witness_levelall(void);
183 static void witness_displaydescendants(void(*)(const char *fmt, ...),
184 struct witness *, int indent);
185 static void witness_display_list(void(*prnt)(const char *fmt, ...),
186 struct witness_list *list);
187 static void witness_display(void(*)(const char *fmt, ...));
188 static void witness_list(struct thread *td);
189 #endif
190
191 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking");
192
193 /*
194 * If set to 0, witness is disabled. If set to a non-zero value, witness
195 * performs full lock order checking for all locks. At runtime, this
196 * value may be set to 0 to turn off witness. witness is not allowed be
197 * turned on once it is turned off, however.
198 */
199 static int witness_watch = 1;
200 TUNABLE_INT("debug.witness.watch", &witness_watch);
201 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
202 sysctl_debug_witness_watch, "I", "witness is watching lock operations");
203
204 #ifdef KDB
205 /*
206 * When KDB is enabled and witness_kdb is set to 1, it will cause the system
207 * to drop into kdebug() when:
208 * - a lock heirarchy violation occurs
209 * - locks are held when going to sleep.
210 */
211 #ifdef WITNESS_KDB
212 int witness_kdb = 1;
213 #else
214 int witness_kdb = 0;
215 #endif
216 TUNABLE_INT("debug.witness.kdb", &witness_kdb);
217 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
218
219 /*
220 * When KDB is enabled and witness_trace is set to 1, it will cause the system
221 * to print a stack trace:
222 * - a lock heirarchy violation occurs
223 * - locks are held when going to sleep.
224 */
225 int witness_trace = 1;
226 TUNABLE_INT("debug.witness.trace", &witness_trace);
227 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
228 #endif /* KDB */
229
230 #ifdef WITNESS_SKIPSPIN
231 int witness_skipspin = 1;
232 #else
233 int witness_skipspin = 0;
234 #endif
235 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
236 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN,
237 &witness_skipspin, 0, "");
238
239 static struct mtx w_mtx;
240 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
241 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
242 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
243 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
244 static struct witness_child_list_entry *w_child_free = NULL;
245 static struct lock_list_entry *w_lock_list_free = NULL;
246
247 static int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt;
248 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
249 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
250 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
251 "");
252 SYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD,
253 &w_child_free_cnt, 0, "");
254 SYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0,
255 "");
256
257 static struct witness w_data[WITNESS_COUNT];
258 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
259 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
260
261 static struct witness_order_list_entry order_lists[] = {
262 /*
263 * sx locks
264 */
265 { "proctree", &lock_class_sx },
266 { "allproc", &lock_class_sx },
267 { NULL, NULL },
268 /*
269 * Various mutexes
270 */
271 { "Giant", &lock_class_mtx_sleep },
272 { "filedesc structure", &lock_class_mtx_sleep },
273 { "pipe mutex", &lock_class_mtx_sleep },
274 { "sigio lock", &lock_class_mtx_sleep },
275 { "process group", &lock_class_mtx_sleep },
276 { "process lock", &lock_class_mtx_sleep },
277 { "session", &lock_class_mtx_sleep },
278 { "uidinfo hash", &lock_class_mtx_sleep },
279 { "uidinfo struct", &lock_class_mtx_sleep },
280 { "allprison", &lock_class_mtx_sleep },
281 { NULL, NULL },
282 /*
283 * Sockets
284 */
285 { "filedesc structure", &lock_class_mtx_sleep },
286 { "accept", &lock_class_mtx_sleep },
287 { "so_snd", &lock_class_mtx_sleep },
288 { "so_rcv", &lock_class_mtx_sleep },
289 { "sellck", &lock_class_mtx_sleep },
290 { NULL, NULL },
291 /*
292 * Routing
293 */
294 { "so_rcv", &lock_class_mtx_sleep },
295 { "radix node head", &lock_class_mtx_sleep },
296 { "rtentry", &lock_class_mtx_sleep },
297 { "ifaddr", &lock_class_mtx_sleep },
298 { NULL, NULL },
299 /*
300 * Multicast - protocol locks before interface locks.
301 */
302 { "in_multi_mtx", &lock_class_mtx_sleep },
303 { "igmp_mtx", &lock_class_mtx_sleep },
304 { "if_addr_mtx", &lock_class_mtx_sleep },
305 { NULL, NULL },
306 /*
307 * UNIX Domain Sockets
308 */
309 { "unp", &lock_class_mtx_sleep },
310 { "so_snd", &lock_class_mtx_sleep },
311 { NULL, NULL },
312 /*
313 * UDP/IP
314 */
315 { "udp", &lock_class_mtx_sleep },
316 { "udpinp", &lock_class_mtx_sleep },
317 { "so_snd", &lock_class_mtx_sleep },
318 { NULL, NULL },
319 /*
320 * TCP/IP
321 */
322 { "tcp", &lock_class_mtx_sleep },
323 { "tcpinp", &lock_class_mtx_sleep },
324 { "so_snd", &lock_class_mtx_sleep },
325 { NULL, NULL },
326 /*
327 * SLIP
328 */
329 { "slip_mtx", &lock_class_mtx_sleep },
330 { "slip sc_mtx", &lock_class_mtx_sleep },
331 { NULL, NULL },
332 /*
333 * netatalk
334 */
335 { "ddp_list_mtx", &lock_class_mtx_sleep },
336 { "ddp_mtx", &lock_class_mtx_sleep },
337 { NULL, NULL },
338 /*
339 * BPF
340 */
341 { "bpf global lock", &lock_class_mtx_sleep },
342 { "bpf interface lock", &lock_class_mtx_sleep },
343 { "bpf cdev lock", &lock_class_mtx_sleep },
344 { NULL, NULL },
345 /*
346 * NFS server
347 */
348 { "nfsd_mtx", &lock_class_mtx_sleep },
349 { "so_snd", &lock_class_mtx_sleep },
350 { NULL, NULL },
351 /*
352 * CDEV
353 */
354 { "system map", &lock_class_mtx_sleep },
355 { "vm page queue mutex", &lock_class_mtx_sleep },
356 { "vnode interlock", &lock_class_mtx_sleep },
357 { "cdev", &lock_class_mtx_sleep },
358 { NULL, NULL },
359 /*
360 * spin locks
361 */
362 #ifdef SMP
363 { "ap boot", &lock_class_mtx_spin },
364 #endif
365 { "rm.mutex_mtx", &lock_class_mtx_spin },
366 { "sio", &lock_class_mtx_spin },
367 #ifdef __i386__
368 { "cy", &lock_class_mtx_spin },
369 #endif
370 { "uart_hwmtx", &lock_class_mtx_spin },
371 { "sabtty", &lock_class_mtx_spin },
372 { "zstty", &lock_class_mtx_spin },
373 { "ng_node", &lock_class_mtx_spin },
374 { "ng_worklist", &lock_class_mtx_spin },
375 { "fast_taskqueue", &lock_class_mtx_spin },
376 { "intr table", &lock_class_mtx_spin },
377 { "sleepq chain", &lock_class_mtx_spin },
378 { "sched lock", &lock_class_mtx_spin },
379 { "turnstile chain", &lock_class_mtx_spin },
380 { "td_contested", &lock_class_mtx_spin },
381 { "callout", &lock_class_mtx_spin },
382 { "entropy harvest mutex", &lock_class_mtx_spin },
383 /*
384 * leaf locks
385 */
386 { "allpmaps", &lock_class_mtx_spin },
387 { "vm page queue free mutex", &lock_class_mtx_spin },
388 { "icu", &lock_class_mtx_spin },
389 #ifdef SMP
390 { "smp rendezvous", &lock_class_mtx_spin },
391 #if defined(__i386__) || defined(__amd64__)
392 { "tlb", &lock_class_mtx_spin },
393 #endif
394 #ifdef __sparc64__
395 { "ipi", &lock_class_mtx_spin },
396 { "rtc_mtx", &lock_class_mtx_spin },
397 #endif
398 #endif
399 { "clk", &lock_class_mtx_spin },
400 { "mutex profiling lock", &lock_class_mtx_spin },
401 { "kse zombie lock", &lock_class_mtx_spin },
402 { "ALD Queue", &lock_class_mtx_spin },
403 #ifdef __ia64__
404 { "MCA spin lock", &lock_class_mtx_spin },
405 #endif
406 #if defined(__i386__) || defined(__amd64__)
407 { "pcicfg", &lock_class_mtx_spin },
408 { "NDIS thread lock", &lock_class_mtx_spin },
409 #endif
410 { "tw_osl_io_lock", &lock_class_mtx_spin },
411 { "tw_osl_q_lock", &lock_class_mtx_spin },
412 { "tw_cl_io_lock", &lock_class_mtx_spin },
413 { "tw_cl_intr_lock", &lock_class_mtx_spin },
414 { "tw_cl_gen_lock", &lock_class_mtx_spin },
415 { NULL, NULL },
416 { NULL, NULL }
417 };
418
419 #ifdef BLESSING
420 /*
421 * Pairs of locks which have been blessed
422 * Don't complain about order problems with blessed locks
423 */
424 static struct witness_blessed blessed_list[] = {
425 };
426 static int blessed_count =
427 sizeof(blessed_list) / sizeof(struct witness_blessed);
428 #endif
429
430 /*
431 * List of all locks in the system.
432 */
433 TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks);
434
435 static struct mtx all_mtx = {
436 { &lock_class_mtx_sleep, /* mtx_object.lo_class */
437 "All locks list", /* mtx_object.lo_name */
438 "All locks list", /* mtx_object.lo_type */
439 LO_INITIALIZED, /* mtx_object.lo_flags */
440 { NULL, NULL }, /* mtx_object.lo_list */
441 NULL }, /* mtx_object.lo_witness */
442 MTX_UNOWNED, 0 /* mtx_lock, mtx_recurse */
443 };
444
445 /*
446 * This global is set to 0 once it becomes safe to use the witness code.
447 */
448 static int witness_cold = 1;
449
450 /*
451 * This global is set to 1 once the static lock orders have been enrolled
452 * so that a warning can be issued for any spin locks enrolled later.
453 */
454 static int witness_spin_warn = 0;
455
456 /*
457 * Global variables for book keeping.
458 */
459 static int lock_cur_cnt;
460 static int lock_max_cnt;
461
462 /*
463 * The WITNESS-enabled diagnostic code.
464 */
465 static void
466 witness_initialize(void *dummy __unused)
467 {
468 struct lock_object *lock;
469 struct witness_order_list_entry *order;
470 struct witness *w, *w1;
471 int i;
472
473 /*
474 * We have to release Giant before initializing its witness
475 * structure so that WITNESS doesn't get confused.
476 */
477 mtx_unlock(&Giant);
478 mtx_assert(&Giant, MA_NOTOWNED);
479
480 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
481 TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
482 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
483 MTX_NOWITNESS);
484 for (i = 0; i < WITNESS_COUNT; i++)
485 witness_free(&w_data[i]);
486 for (i = 0; i < WITNESS_CHILDCOUNT; i++)
487 witness_child_free(&w_childdata[i]);
488 for (i = 0; i < LOCK_CHILDCOUNT; i++)
489 witness_lock_list_free(&w_locklistdata[i]);
490
491 /* First add in all the specified order lists. */
492 for (order = order_lists; order->w_name != NULL; order++) {
493 w = enroll(order->w_name, order->w_class);
494 if (w == NULL)
495 continue;
496 w->w_file = "order list";
497 for (order++; order->w_name != NULL; order++) {
498 w1 = enroll(order->w_name, order->w_class);
499 if (w1 == NULL)
500 continue;
501 w1->w_file = "order list";
502 if (!itismychild(w, w1))
503 panic("Not enough memory for static orders!");
504 w = w1;
505 }
506 }
507 witness_spin_warn = 1;
508
509 /* Iterate through all locks and add them to witness. */
510 mtx_lock(&all_mtx);
511 TAILQ_FOREACH(lock, &all_locks, lo_list) {
512 if (lock->lo_flags & LO_WITNESS)
513 lock->lo_witness = enroll(lock->lo_type,
514 LOCK_CLASS(lock));
515 else
516 lock->lo_witness = NULL;
517 }
518 mtx_unlock(&all_mtx);
519
520 /* Mark the witness code as being ready for use. */
521 witness_cold = 0;
522
523 mtx_lock(&Giant);
524 }
525 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
526
527 static int
528 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
529 {
530 int error, value;
531
532 value = witness_watch;
533 error = sysctl_handle_int(oidp, &value, 0, req);
534 if (error != 0 || req->newptr == NULL)
535 return (error);
536 error = suser(req->td);
537 if (error != 0)
538 return (error);
539 if (value == witness_watch)
540 return (0);
541 if (value != 0)
542 return (EINVAL);
543 witness_watch = 0;
544 return (0);
545 }
546
547 void
548 witness_init(struct lock_object *lock)
549 {
550 struct lock_class *class;
551
552 class = LOCK_CLASS(lock);
553 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
554 (class->lc_flags & LC_RECURSABLE) == 0)
555 panic("%s: lock (%s) %s can not be recursable", __func__,
556 class->lc_name, lock->lo_name);
557 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
558 (class->lc_flags & LC_SLEEPABLE) == 0)
559 panic("%s: lock (%s) %s can not be sleepable", __func__,
560 class->lc_name, lock->lo_name);
561 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
562 (class->lc_flags & LC_UPGRADABLE) == 0)
563 panic("%s: lock (%s) %s can not be upgradable", __func__,
564 class->lc_name, lock->lo_name);
565
566 mtx_lock(&all_mtx);
567 TAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
568 lock_cur_cnt++;
569 if (lock_cur_cnt > lock_max_cnt)
570 lock_max_cnt = lock_cur_cnt;
571 mtx_unlock(&all_mtx);
572 if (!witness_cold && witness_watch != 0 && panicstr == NULL &&
573 (lock->lo_flags & LO_WITNESS) != 0)
574 lock->lo_witness = enroll(lock->lo_type, class);
575 else
576 lock->lo_witness = NULL;
577 }
578
579 void
580 witness_destroy(struct lock_object *lock)
581 {
582 struct lock_class *class;
583 struct witness *w;
584
585 class = LOCK_CLASS(lock);
586 if (witness_cold)
587 panic("lock (%s) %s destroyed while witness_cold",
588 class->lc_name, lock->lo_name);
589
590 /* XXX: need to verify that no one holds the lock */
591 w = lock->lo_witness;
592 if (w != NULL) {
593 mtx_lock_spin(&w_mtx);
594 MPASS(w->w_refcount > 0);
595 w->w_refcount--;
596
597 /*
598 * Lock is already released if we have an allocation failure
599 * and depart() fails.
600 */
601 if (w->w_refcount != 0 || depart(w))
602 mtx_unlock_spin(&w_mtx);
603 }
604
605 mtx_lock(&all_mtx);
606 lock_cur_cnt--;
607 TAILQ_REMOVE(&all_locks, lock, lo_list);
608 mtx_unlock(&all_mtx);
609 }
610
611 #ifdef DDB
612 static void
613 witness_levelall (void)
614 {
615 struct witness_list *list;
616 struct witness *w, *w1;
617
618 /*
619 * First clear all levels.
620 */
621 STAILQ_FOREACH(w, &w_all, w_list) {
622 w->w_level = 0;
623 }
624
625 /*
626 * Look for locks with no parent and level all their descendants.
627 */
628 STAILQ_FOREACH(w, &w_all, w_list) {
629 /*
630 * This is just an optimization, technically we could get
631 * away just walking the all list each time.
632 */
633 if (w->w_class->lc_flags & LC_SLEEPLOCK)
634 list = &w_sleep;
635 else
636 list = &w_spin;
637 STAILQ_FOREACH(w1, list, w_typelist) {
638 if (isitmychild(w1, w))
639 goto skip;
640 }
641 witness_leveldescendents(w, 0);
642 skip:
643 ; /* silence GCC 3.x */
644 }
645 }
646
647 static void
648 witness_leveldescendents(struct witness *parent, int level)
649 {
650 struct witness_child_list_entry *wcl;
651 int i;
652
653 if (parent->w_level < level)
654 parent->w_level = level;
655 level++;
656 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
657 for (i = 0; i < wcl->wcl_count; i++)
658 witness_leveldescendents(wcl->wcl_children[i], level);
659 }
660
661 static void
662 witness_displaydescendants(void(*prnt)(const char *fmt, ...),
663 struct witness *parent, int indent)
664 {
665 struct witness_child_list_entry *wcl;
666 int i, level;
667
668 level = parent->w_level;
669 prnt("%-2d", level);
670 for (i = 0; i < indent; i++)
671 prnt(" ");
672 if (parent->w_refcount > 0)
673 prnt("%s", parent->w_name);
674 else
675 prnt("(dead)");
676 if (parent->w_displayed) {
677 prnt(" -- (already displayed)\n");
678 return;
679 }
680 parent->w_displayed = 1;
681 if (parent->w_refcount > 0) {
682 if (parent->w_file != NULL)
683 prnt(" -- last acquired @ %s:%d", parent->w_file,
684 parent->w_line);
685 }
686 prnt("\n");
687 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
688 for (i = 0; i < wcl->wcl_count; i++)
689 witness_displaydescendants(prnt,
690 wcl->wcl_children[i], indent + 1);
691 }
692
693 static void
694 witness_display_list(void(*prnt)(const char *fmt, ...),
695 struct witness_list *list)
696 {
697 struct witness *w;
698
699 STAILQ_FOREACH(w, list, w_typelist) {
700 if (w->w_file == NULL || w->w_level > 0)
701 continue;
702 /*
703 * This lock has no anscestors, display its descendants.
704 */
705 witness_displaydescendants(prnt, w, 0);
706 }
707 }
708
709 static void
710 witness_display(void(*prnt)(const char *fmt, ...))
711 {
712 struct witness *w;
713
714 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
715 witness_levelall();
716
717 /* Clear all the displayed flags. */
718 STAILQ_FOREACH(w, &w_all, w_list) {
719 w->w_displayed = 0;
720 }
721
722 /*
723 * First, handle sleep locks which have been acquired at least
724 * once.
725 */
726 prnt("Sleep locks:\n");
727 witness_display_list(prnt, &w_sleep);
728
729 /*
730 * Now do spin locks which have been acquired at least once.
731 */
732 prnt("\nSpin locks:\n");
733 witness_display_list(prnt, &w_spin);
734
735 /*
736 * Finally, any locks which have not been acquired yet.
737 */
738 prnt("\nLocks which were never acquired:\n");
739 STAILQ_FOREACH(w, &w_all, w_list) {
740 if (w->w_file != NULL || w->w_refcount == 0)
741 continue;
742 prnt("%s\n", w->w_name);
743 }
744 }
745 #endif /* DDB */
746
747 /* Trim useless garbage from filenames. */
748 static const char *
749 fixup_filename(const char *file)
750 {
751
752 if (file == NULL)
753 return (NULL);
754 while (strncmp(file, "../", 3) == 0)
755 file += 3;
756 return (file);
757 }
758
759 int
760 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
761 {
762
763 if (witness_watch == 0 || panicstr != NULL)
764 return (0);
765
766 /* Require locks that witness knows about. */
767 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
768 lock2->lo_witness == NULL)
769 return (EINVAL);
770
771 MPASS(!mtx_owned(&w_mtx));
772 mtx_lock_spin(&w_mtx);
773
774 /*
775 * If we already have either an explicit or implied lock order that
776 * is the other way around, then return an error.
777 */
778 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
779 mtx_unlock_spin(&w_mtx);
780 return (EDOOFUS);
781 }
782
783 /* Try to add the new order. */
784 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
785 lock2->lo_type, lock1->lo_type);
786 if (!itismychild(lock1->lo_witness, lock2->lo_witness))
787 return (ENOMEM);
788 mtx_unlock_spin(&w_mtx);
789 return (0);
790 }
791
792 void
793 witness_checkorder(struct lock_object *lock, int flags, const char *file,
794 int line)
795 {
796 struct lock_list_entry **lock_list, *lle;
797 struct lock_instance *lock1, *lock2;
798 struct lock_class *class;
799 struct witness *w, *w1;
800 struct thread *td;
801 int i, j;
802
803 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
804 panicstr != NULL)
805 return;
806
807 /*
808 * Try locks do not block if they fail to acquire the lock, thus
809 * there is no danger of deadlocks or of switching while holding a
810 * spin lock if we acquire a lock via a try operation. This
811 * function shouldn't even be called for try locks, so panic if
812 * that happens.
813 */
814 if (flags & LOP_TRYLOCK)
815 panic("%s should not be called for try lock operations",
816 __func__);
817
818 w = lock->lo_witness;
819 class = LOCK_CLASS(lock);
820 td = curthread;
821 file = fixup_filename(file);
822
823 if (class->lc_flags & LC_SLEEPLOCK) {
824 /*
825 * Since spin locks include a critical section, this check
826 * implicitly enforces a lock order of all sleep locks before
827 * all spin locks.
828 */
829 if (td->td_critnest != 0 && !kdb_active)
830 panic("blockable sleep lock (%s) %s @ %s:%d",
831 class->lc_name, lock->lo_name, file, line);
832
833 /*
834 * If this is the first lock acquired then just return as
835 * no order checking is needed.
836 */
837 if (td->td_sleeplocks == NULL)
838 return;
839 lock_list = &td->td_sleeplocks;
840 } else {
841 /*
842 * If this is the first lock, just return as no order
843 * checking is needed. We check this in both if clauses
844 * here as unifying the check would require us to use a
845 * critical section to ensure we don't migrate while doing
846 * the check. Note that if this is not the first lock, we
847 * are already in a critical section and are safe for the
848 * rest of the check.
849 */
850 if (PCPU_GET(spinlocks) == NULL)
851 return;
852 lock_list = PCPU_PTR(spinlocks);
853 }
854
855 /*
856 * Check to see if we are recursing on a lock we already own. If
857 * so, make sure that we don't mismatch exclusive and shared lock
858 * acquires.
859 */
860 lock1 = find_instance(*lock_list, lock);
861 if (lock1 != NULL) {
862 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
863 (flags & LOP_EXCLUSIVE) == 0) {
864 printf("shared lock of (%s) %s @ %s:%d\n",
865 class->lc_name, lock->lo_name, file, line);
866 printf("while exclusively locked from %s:%d\n",
867 lock1->li_file, lock1->li_line);
868 panic("share->excl");
869 }
870 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
871 (flags & LOP_EXCLUSIVE) != 0) {
872 printf("exclusive lock of (%s) %s @ %s:%d\n",
873 class->lc_name, lock->lo_name, file, line);
874 printf("while share locked from %s:%d\n",
875 lock1->li_file, lock1->li_line);
876 panic("excl->share");
877 }
878 return;
879 }
880
881 /*
882 * Try locks do not block if they fail to acquire the lock, thus
883 * there is no danger of deadlocks or of switching while holding a
884 * spin lock if we acquire a lock via a try operation.
885 */
886 if (flags & LOP_TRYLOCK)
887 return;
888
889 /*
890 * Check for duplicate locks of the same type. Note that we only
891 * have to check for this on the last lock we just acquired. Any
892 * other cases will be caught as lock order violations.
893 */
894 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
895 w1 = lock1->li_lock->lo_witness;
896 if (w1 == w) {
897 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) ||
898 (flags & LOP_DUPOK))
899 return;
900 w->w_same_squawked = 1;
901 printf("acquiring duplicate lock of same type: \"%s\"\n",
902 lock->lo_type);
903 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
904 lock1->li_file, lock1->li_line);
905 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
906 #ifdef KDB
907 goto debugger;
908 #else
909 return;
910 #endif
911 }
912 MPASS(!mtx_owned(&w_mtx));
913 mtx_lock_spin(&w_mtx);
914 /*
915 * If we know that the the lock we are acquiring comes after
916 * the lock we most recently acquired in the lock order tree,
917 * then there is no need for any further checks.
918 */
919 if (isitmychild(w1, w)) {
920 mtx_unlock_spin(&w_mtx);
921 return;
922 }
923 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
924 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
925
926 MPASS(j < WITNESS_COUNT);
927 lock1 = &lle->ll_children[i];
928 w1 = lock1->li_lock->lo_witness;
929
930 /*
931 * If this lock doesn't undergo witness checking,
932 * then skip it.
933 */
934 if (w1 == NULL) {
935 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
936 ("lock missing witness structure"));
937 continue;
938 }
939 /*
940 * If we are locking Giant and this is a sleepable
941 * lock, then skip it.
942 */
943 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
944 lock == &Giant.mtx_object)
945 continue;
946 /*
947 * If we are locking a sleepable lock and this lock
948 * is Giant, then skip it.
949 */
950 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
951 lock1->li_lock == &Giant.mtx_object)
952 continue;
953 /*
954 * If we are locking a sleepable lock and this lock
955 * isn't sleepable, we want to treat it as a lock
956 * order violation to enfore a general lock order of
957 * sleepable locks before non-sleepable locks.
958 */
959 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
960 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
961 goto reversal;
962 /*
963 * If we are locking Giant and this is a non-sleepable
964 * lock, then treat it as a reversal.
965 */
966 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
967 lock == &Giant.mtx_object)
968 goto reversal;
969 /*
970 * Check the lock order hierarchy for a reveresal.
971 */
972 if (!isitmydescendant(w, w1))
973 continue;
974 reversal:
975 /*
976 * We have a lock order violation, check to see if it
977 * is allowed or has already been yelled about.
978 */
979 mtx_unlock_spin(&w_mtx);
980 #ifdef BLESSING
981 /*
982 * If the lock order is blessed, just bail. We don't
983 * look for other lock order violations though, which
984 * may be a bug.
985 */
986 if (blessed(w, w1))
987 return;
988 #endif
989 if (lock1->li_lock == &Giant.mtx_object) {
990 if (w1->w_Giant_squawked)
991 return;
992 else
993 w1->w_Giant_squawked = 1;
994 } else {
995 if (w1->w_other_squawked)
996 return;
997 else
998 w1->w_other_squawked = 1;
999 }
1000 /*
1001 * Ok, yell about it.
1002 */
1003 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1004 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1005 printf(
1006 "lock order reversal: (sleepable after non-sleepable)\n");
1007 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1008 && lock == &Giant.mtx_object)
1009 printf(
1010 "lock order reversal: (Giant after non-sleepable)\n");
1011 else
1012 printf("lock order reversal:\n");
1013 /*
1014 * Try to locate an earlier lock with
1015 * witness w in our list.
1016 */
1017 do {
1018 lock2 = &lle->ll_children[i];
1019 MPASS(lock2->li_lock != NULL);
1020 if (lock2->li_lock->lo_witness == w)
1021 break;
1022 if (i == 0 && lle->ll_next != NULL) {
1023 lle = lle->ll_next;
1024 i = lle->ll_count - 1;
1025 MPASS(i >= 0 && i < LOCK_NCHILDREN);
1026 } else
1027 i--;
1028 } while (i >= 0);
1029 if (i < 0) {
1030 printf(" 1st %p %s (%s) @ %s:%d\n",
1031 lock1->li_lock, lock1->li_lock->lo_name,
1032 lock1->li_lock->lo_type, lock1->li_file,
1033 lock1->li_line);
1034 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1035 lock->lo_name, lock->lo_type, file, line);
1036 } else {
1037 printf(" 1st %p %s (%s) @ %s:%d\n",
1038 lock2->li_lock, lock2->li_lock->lo_name,
1039 lock2->li_lock->lo_type, lock2->li_file,
1040 lock2->li_line);
1041 printf(" 2nd %p %s (%s) @ %s:%d\n",
1042 lock1->li_lock, lock1->li_lock->lo_name,
1043 lock1->li_lock->lo_type, lock1->li_file,
1044 lock1->li_line);
1045 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1046 lock->lo_name, lock->lo_type, file, line);
1047 }
1048 #ifdef KDB
1049 goto debugger;
1050 #else
1051 return;
1052 #endif
1053 }
1054 }
1055 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
1056 /*
1057 * If requested, build a new lock order. However, don't build a new
1058 * relationship between a sleepable lock and Giant if it is in the
1059 * wrong direction. The correct lock order is that sleepable locks
1060 * always come before Giant.
1061 */
1062 if (flags & LOP_NEWORDER &&
1063 !(lock1->li_lock == &Giant.mtx_object &&
1064 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1065 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1066 lock->lo_type, lock1->li_lock->lo_type);
1067 if (!itismychild(lock1->li_lock->lo_witness, w))
1068 /* Witness is dead. */
1069 return;
1070 }
1071 mtx_unlock_spin(&w_mtx);
1072 return;
1073
1074 #ifdef KDB
1075 debugger:
1076 if (witness_trace)
1077 kdb_backtrace();
1078 if (witness_kdb)
1079 kdb_enter(__func__);
1080 #endif
1081 }
1082
1083 void
1084 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1085 {
1086 struct lock_list_entry **lock_list, *lle;
1087 struct lock_instance *instance;
1088 struct witness *w;
1089 struct thread *td;
1090
1091 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1092 panicstr != NULL)
1093 return;
1094 w = lock->lo_witness;
1095 td = curthread;
1096 file = fixup_filename(file);
1097
1098 /* Determine lock list for this lock. */
1099 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1100 lock_list = &td->td_sleeplocks;
1101 else
1102 lock_list = PCPU_PTR(spinlocks);
1103
1104 /* Check to see if we are recursing on a lock we already own. */
1105 instance = find_instance(*lock_list, lock);
1106 if (instance != NULL) {
1107 instance->li_flags++;
1108 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1109 td->td_proc->p_pid, lock->lo_name,
1110 instance->li_flags & LI_RECURSEMASK);
1111 instance->li_file = file;
1112 instance->li_line = line;
1113 return;
1114 }
1115
1116 /* Update per-witness last file and line acquire. */
1117 w->w_file = file;
1118 w->w_line = line;
1119
1120 /* Find the next open lock instance in the list and fill it. */
1121 lle = *lock_list;
1122 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1123 lle = witness_lock_list_get();
1124 if (lle == NULL)
1125 return;
1126 lle->ll_next = *lock_list;
1127 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1128 td->td_proc->p_pid, lle);
1129 *lock_list = lle;
1130 }
1131 instance = &lle->ll_children[lle->ll_count++];
1132 instance->li_lock = lock;
1133 instance->li_line = line;
1134 instance->li_file = file;
1135 if ((flags & LOP_EXCLUSIVE) != 0)
1136 instance->li_flags = LI_EXCLUSIVE;
1137 else
1138 instance->li_flags = 0;
1139 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1140 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1141 }
1142
1143 void
1144 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1145 {
1146 struct lock_instance *instance;
1147 struct lock_class *class;
1148
1149 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1150 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1151 return;
1152 class = LOCK_CLASS(lock);
1153 file = fixup_filename(file);
1154 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1155 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1156 class->lc_name, lock->lo_name, file, line);
1157 if ((flags & LOP_TRYLOCK) == 0)
1158 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
1159 lock->lo_name, file, line);
1160 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1161 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1162 class->lc_name, lock->lo_name, file, line);
1163 instance = find_instance(curthread->td_sleeplocks, lock);
1164 if (instance == NULL)
1165 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1166 class->lc_name, lock->lo_name, file, line);
1167 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1168 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1169 class->lc_name, lock->lo_name, file, line);
1170 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1171 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1172 class->lc_name, lock->lo_name,
1173 instance->li_flags & LI_RECURSEMASK, file, line);
1174 instance->li_flags |= LI_EXCLUSIVE;
1175 }
1176
1177 void
1178 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1179 int line)
1180 {
1181 struct lock_instance *instance;
1182 struct lock_class *class;
1183
1184 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1185 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1186 return;
1187 class = LOCK_CLASS(lock);
1188 file = fixup_filename(file);
1189 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1190 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1191 class->lc_name, lock->lo_name, file, line);
1192 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1193 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1194 class->lc_name, lock->lo_name, file, line);
1195 instance = find_instance(curthread->td_sleeplocks, lock);
1196 if (instance == NULL)
1197 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1198 class->lc_name, lock->lo_name, file, line);
1199 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1200 panic("downgrade of shared lock (%s) %s @ %s:%d",
1201 class->lc_name, lock->lo_name, file, line);
1202 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1203 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1204 class->lc_name, lock->lo_name,
1205 instance->li_flags & LI_RECURSEMASK, file, line);
1206 instance->li_flags &= ~LI_EXCLUSIVE;
1207 }
1208
1209 void
1210 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1211 {
1212 struct lock_list_entry **lock_list, *lle;
1213 struct lock_instance *instance;
1214 struct lock_class *class;
1215 struct thread *td;
1216 register_t s;
1217 int i, j;
1218
1219 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1220 panicstr != NULL)
1221 return;
1222 td = curthread;
1223 class = LOCK_CLASS(lock);
1224 file = fixup_filename(file);
1225
1226 /* Find lock instance associated with this lock. */
1227 if (class->lc_flags & LC_SLEEPLOCK)
1228 lock_list = &td->td_sleeplocks;
1229 else
1230 lock_list = PCPU_PTR(spinlocks);
1231 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1232 for (i = 0; i < (*lock_list)->ll_count; i++) {
1233 instance = &(*lock_list)->ll_children[i];
1234 if (instance->li_lock == lock)
1235 goto found;
1236 }
1237 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
1238 file, line);
1239 found:
1240
1241 /* First, check for shared/exclusive mismatches. */
1242 if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1243 (flags & LOP_EXCLUSIVE) == 0) {
1244 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1245 lock->lo_name, file, line);
1246 printf("while exclusively locked from %s:%d\n",
1247 instance->li_file, instance->li_line);
1248 panic("excl->ushare");
1249 }
1250 if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1251 (flags & LOP_EXCLUSIVE) != 0) {
1252 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1253 lock->lo_name, file, line);
1254 printf("while share locked from %s:%d\n", instance->li_file,
1255 instance->li_line);
1256 panic("share->uexcl");
1257 }
1258
1259 /* If we are recursed, unrecurse. */
1260 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1261 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1262 td->td_proc->p_pid, instance->li_lock->lo_name,
1263 instance->li_flags);
1264 instance->li_flags--;
1265 return;
1266 }
1267
1268 /* Otherwise, remove this item from the list. */
1269 s = intr_disable();
1270 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1271 td->td_proc->p_pid, instance->li_lock->lo_name,
1272 (*lock_list)->ll_count - 1);
1273 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1274 (*lock_list)->ll_children[j] =
1275 (*lock_list)->ll_children[j + 1];
1276 (*lock_list)->ll_count--;
1277 intr_restore(s);
1278
1279 /* If this lock list entry is now empty, free it. */
1280 if ((*lock_list)->ll_count == 0) {
1281 lle = *lock_list;
1282 *lock_list = lle->ll_next;
1283 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1284 td->td_proc->p_pid, lle);
1285 witness_lock_list_free(lle);
1286 }
1287 }
1288
1289 /*
1290 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1291 * exempt Giant and sleepable locks from the checks as well. If any
1292 * non-exempt locks are held, then a supplied message is printed to the
1293 * console along with a list of the offending locks. If indicated in the
1294 * flags then a failure results in a panic as well.
1295 */
1296 int
1297 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1298 {
1299 struct lock_list_entry *lle;
1300 struct lock_instance *lock1;
1301 struct thread *td;
1302 va_list ap;
1303 int i, n;
1304
1305 if (witness_cold || witness_watch == 0 || panicstr != NULL)
1306 return (0);
1307 n = 0;
1308 td = curthread;
1309 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1310 for (i = lle->ll_count - 1; i >= 0; i--) {
1311 lock1 = &lle->ll_children[i];
1312 if (lock1->li_lock == lock)
1313 continue;
1314 if (flags & WARN_GIANTOK &&
1315 lock1->li_lock == &Giant.mtx_object)
1316 continue;
1317 if (flags & WARN_SLEEPOK &&
1318 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1319 continue;
1320 if (n == 0) {
1321 va_start(ap, fmt);
1322 vprintf(fmt, ap);
1323 va_end(ap);
1324 printf(" with the following");
1325 if (flags & WARN_SLEEPOK)
1326 printf(" non-sleepable");
1327 printf(" locks held:\n");
1328 }
1329 n++;
1330 witness_list_lock(lock1);
1331 }
1332 if (PCPU_GET(spinlocks) != NULL) {
1333 /*
1334 * Since we already hold a spinlock preemption is
1335 * already blocked.
1336 */
1337 if (n == 0) {
1338 va_start(ap, fmt);
1339 vprintf(fmt, ap);
1340 va_end(ap);
1341 printf(" with the following");
1342 if (flags & WARN_SLEEPOK)
1343 printf(" non-sleepable");
1344 printf(" locks held:\n");
1345 }
1346 n += witness_list_locks(PCPU_PTR(spinlocks));
1347 }
1348 if (flags & WARN_PANIC && n)
1349 panic("witness_warn");
1350 #ifdef KDB
1351 else if (witness_kdb && n)
1352 kdb_enter(__func__);
1353 else if (witness_trace && n)
1354 kdb_backtrace();
1355 #endif
1356 return (n);
1357 }
1358
1359 const char *
1360 witness_file(struct lock_object *lock)
1361 {
1362 struct witness *w;
1363
1364 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1365 return ("?");
1366 w = lock->lo_witness;
1367 return (w->w_file);
1368 }
1369
1370 int
1371 witness_line(struct lock_object *lock)
1372 {
1373 struct witness *w;
1374
1375 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1376 return (0);
1377 w = lock->lo_witness;
1378 return (w->w_line);
1379 }
1380
1381 static struct witness *
1382 enroll(const char *description, struct lock_class *lock_class)
1383 {
1384 struct witness *w;
1385
1386 if (witness_watch == 0 || panicstr != NULL)
1387 return (NULL);
1388 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
1389 return (NULL);
1390 mtx_lock_spin(&w_mtx);
1391 STAILQ_FOREACH(w, &w_all, w_list) {
1392 if (w->w_name == description || (w->w_refcount > 0 &&
1393 strcmp(description, w->w_name) == 0)) {
1394 w->w_refcount++;
1395 mtx_unlock_spin(&w_mtx);
1396 if (lock_class != w->w_class)
1397 panic(
1398 "lock (%s) %s does not match earlier (%s) lock",
1399 description, lock_class->lc_name,
1400 w->w_class->lc_name);
1401 return (w);
1402 }
1403 }
1404 if ((w = witness_get()) == NULL)
1405 goto out;
1406 w->w_name = description;
1407 w->w_class = lock_class;
1408 w->w_refcount = 1;
1409 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1410 if (lock_class->lc_flags & LC_SPINLOCK) {
1411 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1412 w_spin_cnt++;
1413 } else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1414 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1415 w_sleep_cnt++;
1416 } else {
1417 mtx_unlock_spin(&w_mtx);
1418 panic("lock class %s is not sleep or spin",
1419 lock_class->lc_name);
1420 }
1421 mtx_unlock_spin(&w_mtx);
1422 out:
1423 /*
1424 * We issue a warning for any spin locks not defined in the static
1425 * order list as a way to discourage their use (folks should really
1426 * be using non-spin mutexes most of the time). However, several
1427 * 3rd part device drivers use spin locks because that is all they
1428 * have available on Windows and Linux and they think that normal
1429 * mutexes are insufficient.
1430 */
1431 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn)
1432 printf("WITNESS: spin lock %s not in order list\n",
1433 description);
1434 return (w);
1435 }
1436
1437 /* Don't let the door bang you on the way out... */
1438 static int
1439 depart(struct witness *w)
1440 {
1441 struct witness_child_list_entry *wcl, *nwcl;
1442 struct witness_list *list;
1443 struct witness *parent;
1444
1445 MPASS(w->w_refcount == 0);
1446 if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1447 list = &w_sleep;
1448 w_sleep_cnt--;
1449 } else {
1450 list = &w_spin;
1451 w_spin_cnt--;
1452 }
1453 /*
1454 * First, we run through the entire tree looking for any
1455 * witnesses that the outgoing witness is a child of. For
1456 * each parent that we find, we reparent all the direct
1457 * children of the outgoing witness to its parent.
1458 */
1459 STAILQ_FOREACH(parent, list, w_typelist) {
1460 if (!isitmychild(parent, w))
1461 continue;
1462 removechild(parent, w);
1463 }
1464
1465 /*
1466 * Now we go through and free up the child list of the
1467 * outgoing witness.
1468 */
1469 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1470 nwcl = wcl->wcl_next;
1471 w_child_cnt--;
1472 witness_child_free(wcl);
1473 }
1474
1475 /*
1476 * Detach from various lists and free.
1477 */
1478 STAILQ_REMOVE(list, w, witness, w_typelist);
1479 STAILQ_REMOVE(&w_all, w, witness, w_list);
1480 witness_free(w);
1481
1482 return (1);
1483 }
1484
1485 /*
1486 * Add "child" as a direct child of "parent". Returns false if
1487 * we fail due to out of memory.
1488 */
1489 static int
1490 insertchild(struct witness *parent, struct witness *child)
1491 {
1492 struct witness_child_list_entry **wcl;
1493
1494 MPASS(child != NULL && parent != NULL);
1495
1496 /*
1497 * Insert "child" after "parent"
1498 */
1499 wcl = &parent->w_children;
1500 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1501 wcl = &(*wcl)->wcl_next;
1502 if (*wcl == NULL) {
1503 *wcl = witness_child_get();
1504 if (*wcl == NULL)
1505 return (0);
1506 w_child_cnt++;
1507 }
1508 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1509
1510 return (1);
1511 }
1512
1513
1514 static int
1515 itismychild(struct witness *parent, struct witness *child)
1516 {
1517 struct witness_list *list;
1518
1519 MPASS(child != NULL && parent != NULL);
1520 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1521 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1522 panic(
1523 "%s: parent (%s) and child (%s) are not the same lock type",
1524 __func__, parent->w_class->lc_name,
1525 child->w_class->lc_name);
1526
1527 if (!insertchild(parent, child))
1528 return (0);
1529
1530 if (parent->w_class->lc_flags & LC_SLEEPLOCK)
1531 list = &w_sleep;
1532 else
1533 list = &w_spin;
1534 return (1);
1535 }
1536
1537 static void
1538 removechild(struct witness *parent, struct witness *child)
1539 {
1540 struct witness_child_list_entry **wcl, *wcl1;
1541 int i;
1542
1543 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1544 for (i = 0; i < (*wcl)->wcl_count; i++)
1545 if ((*wcl)->wcl_children[i] == child)
1546 goto found;
1547 return;
1548 found:
1549 (*wcl)->wcl_count--;
1550 if ((*wcl)->wcl_count > i)
1551 (*wcl)->wcl_children[i] =
1552 (*wcl)->wcl_children[(*wcl)->wcl_count];
1553 MPASS((*wcl)->wcl_children[i] != NULL);
1554 if ((*wcl)->wcl_count != 0)
1555 return;
1556 wcl1 = *wcl;
1557 *wcl = wcl1->wcl_next;
1558 w_child_cnt--;
1559 witness_child_free(wcl1);
1560 }
1561
1562 static int
1563 isitmychild(struct witness *parent, struct witness *child)
1564 {
1565 struct witness_child_list_entry *wcl;
1566 int i;
1567
1568 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1569 for (i = 0; i < wcl->wcl_count; i++) {
1570 if (wcl->wcl_children[i] == child)
1571 return (1);
1572 }
1573 }
1574 return (0);
1575 }
1576
1577 static int
1578 isitmydescendant(struct witness *parent, struct witness *child)
1579 {
1580 struct witness_child_list_entry *wcl;
1581 int i, j;
1582
1583 if (isitmychild(parent, child))
1584 return (1);
1585 j = 0;
1586 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1587 MPASS(j < 1000);
1588 for (i = 0; i < wcl->wcl_count; i++) {
1589 if (isitmydescendant(wcl->wcl_children[i], child))
1590 return (1);
1591 }
1592 j++;
1593 }
1594 return (0);
1595 }
1596
1597 #ifdef BLESSING
1598 static int
1599 blessed(struct witness *w1, struct witness *w2)
1600 {
1601 int i;
1602 struct witness_blessed *b;
1603
1604 for (i = 0; i < blessed_count; i++) {
1605 b = &blessed_list[i];
1606 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1607 if (strcmp(w2->w_name, b->b_lock2) == 0)
1608 return (1);
1609 continue;
1610 }
1611 if (strcmp(w1->w_name, b->b_lock2) == 0)
1612 if (strcmp(w2->w_name, b->b_lock1) == 0)
1613 return (1);
1614 }
1615 return (0);
1616 }
1617 #endif
1618
1619 static struct witness *
1620 witness_get(void)
1621 {
1622 struct witness *w;
1623
1624 if (witness_watch == 0) {
1625 mtx_unlock_spin(&w_mtx);
1626 return (NULL);
1627 }
1628 if (STAILQ_EMPTY(&w_free)) {
1629 witness_watch = 0;
1630 mtx_unlock_spin(&w_mtx);
1631 printf("%s: witness exhausted\n", __func__);
1632 return (NULL);
1633 }
1634 w = STAILQ_FIRST(&w_free);
1635 STAILQ_REMOVE_HEAD(&w_free, w_list);
1636 w_free_cnt--;
1637 bzero(w, sizeof(*w));
1638 return (w);
1639 }
1640
1641 static void
1642 witness_free(struct witness *w)
1643 {
1644
1645 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1646 w_free_cnt++;
1647 }
1648
1649 static struct witness_child_list_entry *
1650 witness_child_get(void)
1651 {
1652 struct witness_child_list_entry *wcl;
1653
1654 if (witness_watch == 0) {
1655 mtx_unlock_spin(&w_mtx);
1656 return (NULL);
1657 }
1658 wcl = w_child_free;
1659 if (wcl == NULL) {
1660 witness_watch = 0;
1661 mtx_unlock_spin(&w_mtx);
1662 printf("%s: witness exhausted\n", __func__);
1663 return (NULL);
1664 }
1665 w_child_free = wcl->wcl_next;
1666 w_child_free_cnt--;
1667 bzero(wcl, sizeof(*wcl));
1668 return (wcl);
1669 }
1670
1671 static void
1672 witness_child_free(struct witness_child_list_entry *wcl)
1673 {
1674
1675 wcl->wcl_next = w_child_free;
1676 w_child_free = wcl;
1677 w_child_free_cnt++;
1678 }
1679
1680 static struct lock_list_entry *
1681 witness_lock_list_get(void)
1682 {
1683 struct lock_list_entry *lle;
1684
1685 if (witness_watch == 0)
1686 return (NULL);
1687 mtx_lock_spin(&w_mtx);
1688 lle = w_lock_list_free;
1689 if (lle == NULL) {
1690 witness_watch = 0;
1691 mtx_unlock_spin(&w_mtx);
1692 printf("%s: witness exhausted\n", __func__);
1693 return (NULL);
1694 }
1695 w_lock_list_free = lle->ll_next;
1696 mtx_unlock_spin(&w_mtx);
1697 bzero(lle, sizeof(*lle));
1698 return (lle);
1699 }
1700
1701 static void
1702 witness_lock_list_free(struct lock_list_entry *lle)
1703 {
1704
1705 mtx_lock_spin(&w_mtx);
1706 lle->ll_next = w_lock_list_free;
1707 w_lock_list_free = lle;
1708 mtx_unlock_spin(&w_mtx);
1709 }
1710
1711 static struct lock_instance *
1712 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1713 {
1714 struct lock_list_entry *lle;
1715 struct lock_instance *instance;
1716 int i;
1717
1718 for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1719 for (i = lle->ll_count - 1; i >= 0; i--) {
1720 instance = &lle->ll_children[i];
1721 if (instance->li_lock == lock)
1722 return (instance);
1723 }
1724 return (NULL);
1725 }
1726
1727 static void
1728 witness_list_lock(struct lock_instance *instance)
1729 {
1730 struct lock_object *lock;
1731
1732 lock = instance->li_lock;
1733 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1734 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
1735 if (lock->lo_type != lock->lo_name)
1736 printf(" (%s)", lock->lo_type);
1737 printf(" r = %d (%p) locked @ %s:%d\n",
1738 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1739 instance->li_line);
1740 }
1741
1742 #ifdef DDB
1743 static int
1744 witness_thread_has_locks(struct thread *td)
1745 {
1746
1747 return (td->td_sleeplocks != NULL);
1748 }
1749
1750 static int
1751 witness_proc_has_locks(struct proc *p)
1752 {
1753 struct thread *td;
1754
1755 FOREACH_THREAD_IN_PROC(p, td) {
1756 if (witness_thread_has_locks(td))
1757 return (1);
1758 }
1759 return (0);
1760 }
1761 #endif
1762
1763 int
1764 witness_list_locks(struct lock_list_entry **lock_list)
1765 {
1766 struct lock_list_entry *lle;
1767 int i, nheld;
1768
1769 nheld = 0;
1770 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1771 for (i = lle->ll_count - 1; i >= 0; i--) {
1772 witness_list_lock(&lle->ll_children[i]);
1773 nheld++;
1774 }
1775 return (nheld);
1776 }
1777
1778 /*
1779 * This is a bit risky at best. We call this function when we have timed
1780 * out acquiring a spin lock, and we assume that the other CPU is stuck
1781 * with this lock held. So, we go groveling around in the other CPU's
1782 * per-cpu data to try to find the lock instance for this spin lock to
1783 * see when it was last acquired.
1784 */
1785 void
1786 witness_display_spinlock(struct lock_object *lock, struct thread *owner)
1787 {
1788 struct lock_instance *instance;
1789 struct pcpu *pc;
1790
1791 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
1792 return;
1793 pc = pcpu_find(owner->td_oncpu);
1794 instance = find_instance(pc->pc_spinlocks, lock);
1795 if (instance != NULL)
1796 witness_list_lock(instance);
1797 }
1798
1799 void
1800 witness_save(struct lock_object *lock, const char **filep, int *linep)
1801 {
1802 struct lock_list_entry *lock_list;
1803 struct lock_instance *instance;
1804 struct lock_class *class;
1805
1806 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1807 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1808 return;
1809 class = LOCK_CLASS(lock);
1810 if (class->lc_flags & LC_SLEEPLOCK)
1811 lock_list = curthread->td_sleeplocks;
1812 else {
1813 if (witness_skipspin)
1814 return;
1815 lock_list = PCPU_GET(spinlocks);
1816 }
1817 instance = find_instance(lock_list, lock);
1818 if (instance == NULL)
1819 panic("%s: lock (%s) %s not locked", __func__,
1820 class->lc_name, lock->lo_name);
1821 *filep = instance->li_file;
1822 *linep = instance->li_line;
1823 }
1824
1825 void
1826 witness_restore(struct lock_object *lock, const char *file, int line)
1827 {
1828 struct lock_list_entry *lock_list;
1829 struct lock_instance *instance;
1830 struct lock_class *class;
1831
1832 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1833 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1834 return;
1835 class = LOCK_CLASS(lock);
1836 if (class->lc_flags & LC_SLEEPLOCK)
1837 lock_list = curthread->td_sleeplocks;
1838 else {
1839 if (witness_skipspin)
1840 return;
1841 lock_list = PCPU_GET(spinlocks);
1842 }
1843 instance = find_instance(lock_list, lock);
1844 if (instance == NULL)
1845 panic("%s: lock (%s) %s not locked", __func__,
1846 class->lc_name, lock->lo_name);
1847 lock->lo_witness->w_file = file;
1848 lock->lo_witness->w_line = line;
1849 instance->li_file = file;
1850 instance->li_line = line;
1851 }
1852
1853 void
1854 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1855 {
1856 #ifdef INVARIANT_SUPPORT
1857 struct lock_instance *instance;
1858 struct lock_class *class;
1859
1860 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1861 return;
1862 class = LOCK_CLASS(lock);
1863 if ((class->lc_flags & LC_SLEEPLOCK) != 0)
1864 instance = find_instance(curthread->td_sleeplocks, lock);
1865 else if ((class->lc_flags & LC_SPINLOCK) != 0)
1866 instance = find_instance(PCPU_GET(spinlocks), lock);
1867 else {
1868 panic("Lock (%s) %s is not sleep or spin!",
1869 class->lc_name, lock->lo_name);
1870 }
1871 file = fixup_filename(file);
1872 switch (flags) {
1873 case LA_UNLOCKED:
1874 if (instance != NULL)
1875 panic("Lock (%s) %s locked @ %s:%d.",
1876 class->lc_name, lock->lo_name, file, line);
1877 break;
1878 case LA_LOCKED:
1879 case LA_LOCKED | LA_RECURSED:
1880 case LA_LOCKED | LA_NOTRECURSED:
1881 case LA_SLOCKED:
1882 case LA_SLOCKED | LA_RECURSED:
1883 case LA_SLOCKED | LA_NOTRECURSED:
1884 case LA_XLOCKED:
1885 case LA_XLOCKED | LA_RECURSED:
1886 case LA_XLOCKED | LA_NOTRECURSED:
1887 if (instance == NULL) {
1888 panic("Lock (%s) %s not locked @ %s:%d.",
1889 class->lc_name, lock->lo_name, file, line);
1890 break;
1891 }
1892 if ((flags & LA_XLOCKED) != 0 &&
1893 (instance->li_flags & LI_EXCLUSIVE) == 0)
1894 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1895 class->lc_name, lock->lo_name, file, line);
1896 if ((flags & LA_SLOCKED) != 0 &&
1897 (instance->li_flags & LI_EXCLUSIVE) != 0)
1898 panic("Lock (%s) %s exclusively locked @ %s:%d.",
1899 class->lc_name, lock->lo_name, file, line);
1900 if ((flags & LA_RECURSED) != 0 &&
1901 (instance->li_flags & LI_RECURSEMASK) == 0)
1902 panic("Lock (%s) %s not recursed @ %s:%d.",
1903 class->lc_name, lock->lo_name, file, line);
1904 if ((flags & LA_NOTRECURSED) != 0 &&
1905 (instance->li_flags & LI_RECURSEMASK) != 0)
1906 panic("Lock (%s) %s recursed @ %s:%d.",
1907 class->lc_name, lock->lo_name, file, line);
1908 break;
1909 default:
1910 panic("Invalid lock assertion at %s:%d.", file, line);
1911
1912 }
1913 #endif /* INVARIANT_SUPPORT */
1914 }
1915
1916 #ifdef DDB
1917 static void
1918 witness_list(struct thread *td)
1919 {
1920
1921 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1922 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
1923
1924 if (witness_watch == 0)
1925 return;
1926
1927 witness_list_locks(&td->td_sleeplocks);
1928
1929 /*
1930 * We only handle spinlocks if td == curthread. This is somewhat broken
1931 * if td is currently executing on some other CPU and holds spin locks
1932 * as we won't display those locks. If we had a MI way of getting
1933 * the per-cpu data for a given cpu then we could use
1934 * td->td_oncpu to get the list of spinlocks for this thread
1935 * and "fix" this.
1936 *
1937 * That still wouldn't really fix this unless we locked sched_lock
1938 * or stopped the other CPU to make sure it wasn't changing the list
1939 * out from under us. It is probably best to just not try to handle
1940 * threads on other CPU's for now.
1941 */
1942 if (td == curthread && PCPU_GET(spinlocks) != NULL)
1943 witness_list_locks(PCPU_PTR(spinlocks));
1944 }
1945
1946 DB_SHOW_COMMAND(locks, db_witness_list)
1947 {
1948 struct thread *td;
1949 pid_t pid;
1950 struct proc *p;
1951
1952 if (have_addr) {
1953 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1954 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1955 ((addr >> 16) % 16) * 10000;
1956 /* sx_slock(&allproc_lock); */
1957 FOREACH_PROC_IN_SYSTEM(p) {
1958 if (p->p_pid == pid)
1959 break;
1960 }
1961 /* sx_sunlock(&allproc_lock); */
1962 if (p == NULL) {
1963 db_printf("pid %d not found\n", pid);
1964 return;
1965 }
1966 FOREACH_THREAD_IN_PROC(p, td) {
1967 witness_list(td);
1968 }
1969 } else {
1970 td = curthread;
1971 witness_list(td);
1972 }
1973 }
1974
1975 DB_SHOW_COMMAND(alllocks, db_witness_list_all)
1976 {
1977 struct thread *td;
1978 struct proc *p;
1979
1980 /*
1981 * It would be nice to list only threads and processes that actually
1982 * held sleep locks, but that information is currently not exported
1983 * by WITNESS.
1984 */
1985 FOREACH_PROC_IN_SYSTEM(p) {
1986 if (!witness_proc_has_locks(p))
1987 continue;
1988 FOREACH_THREAD_IN_PROC(p, td) {
1989 if (!witness_thread_has_locks(td))
1990 continue;
1991 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
1992 p->p_comm, td, td->td_tid);
1993 witness_list(td);
1994 }
1995 }
1996 }
1997
1998 DB_SHOW_COMMAND(witness, db_witness_display)
1999 {
2000
2001 witness_display(db_printf);
2002 }
2003 #endif
Cache object: 93174edb9af687031e33067f2f0556da
|