Deleted Added
full compact
subr_witness.c (90361) subr_witness.c (91140)
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 $
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 * $FreeBSD: head/sys/kern/subr_witness.c 90361 2002-02-07 20:58:47Z julian $
30 * $FreeBSD: head/sys/kern/subr_witness.c 91140 2002-02-23 11:12:57Z tanimura $
31 */
32
33/*
34 * Implementation of the `witness' lock verifier. Originally implemented for
35 * mutexes in BSD/OS. Extended to handle generic lock objects and lock
36 * classes in FreeBSD.
37 */
38
39/*
40 * Main Entry: witness
41 * Pronunciation: 'wit-n&s
42 * Function: noun
43 * Etymology: Middle English witnesse, from Old English witnes knowledge,
44 * testimony, witness, from 2wit
45 * Date: before 12th century
46 * 1 : attestation of a fact or event : TESTIMONY
47 * 2 : one that gives evidence; specifically : one who testifies in
48 * a cause or before a judicial tribunal
49 * 3 : one asked to be present at a transaction so as to be able to
50 * testify to its having taken place
51 * 4 : one who has personal knowledge of something
52 * 5 a : something serving as evidence or proof : SIGN
53 * b : public affirmation by word or example of usually
54 * religious faith or conviction <the heroic witness to divine
55 * life -- Pilot>
56 * 6 capitalized : a member of the Jehovah's Witnesses
57 */
58
59#include "opt_ddb.h"
60#include "opt_witness.h"
61
62#include <sys/param.h>
63#include <sys/bus.h>
64#include <sys/kernel.h>
65#include <sys/ktr.h>
66#include <sys/lock.h>
67#include <sys/malloc.h>
68#include <sys/mutex.h>
69#include <sys/proc.h>
70#include <sys/sysctl.h>
71#include <sys/systm.h>
72
73#include <ddb/ddb.h>
74
75#define WITNESS_COUNT 200
76#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
77/*
78 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
79 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should
80 * probably be safe for the most part, but it's still a SWAG.
81 */
82#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
83
84#define WITNESS_NCHILDREN 6
85
86struct witness_child_list_entry;
87
88struct witness {
89 const char *w_name;
90 struct lock_class *w_class;
91 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */
92 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */
93 struct witness_child_list_entry *w_children; /* Great evilness... */
94 const char *w_file;
95 int w_line;
96 u_int w_level;
97 u_int w_refcount;
98 u_char w_Giant_squawked:1;
99 u_char w_other_squawked:1;
100 u_char w_same_squawked:1;
101};
102
103struct witness_child_list_entry {
104 struct witness_child_list_entry *wcl_next;
105 struct witness *wcl_children[WITNESS_NCHILDREN];
106 u_int wcl_count;
107};
108
109STAILQ_HEAD(witness_list, witness);
110
111struct witness_blessed {
112 const char *b_lock1;
113 const char *b_lock2;
114};
115
116struct witness_order_list_entry {
117 const char *w_name;
118 struct lock_class *w_class;
119};
120
121static struct witness *enroll(const char *description,
122 struct lock_class *lock_class);
123static int itismychild(struct witness *parent, struct witness *child);
124static void removechild(struct witness *parent, struct witness *child);
125static int isitmychild(struct witness *parent, struct witness *child);
126static int isitmydescendant(struct witness *parent, struct witness *child);
127static int dup_ok(struct witness *);
128static int blessed(struct witness *, struct witness *);
129static void witness_display_list(void(*prnt)(const char *fmt, ...),
130 struct witness_list *list);
131static void witness_displaydescendants(void(*)(const char *fmt, ...),
132 struct witness *);
133static void witness_leveldescendents(struct witness *parent, int level);
134static void witness_levelall(void);
135static struct witness *witness_get(void);
136static void witness_free(struct witness *m);
137static struct witness_child_list_entry *witness_child_get(void);
138static void witness_child_free(struct witness_child_list_entry *wcl);
139static struct lock_list_entry *witness_lock_list_get(void);
140static void witness_lock_list_free(struct lock_list_entry *lle);
141static void witness_display(void(*)(const char *fmt, ...));
142static struct lock_instance *find_instance(struct lock_list_entry *lock_list,
143 struct lock_object *lock);
144
145MALLOC_DEFINE(M_WITNESS, "witness", "witness structure");
146
147static int witness_watch = 1;
148TUNABLE_INT("debug.witness_watch", &witness_watch);
149SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, "");
150
151#ifdef DDB
152/*
153 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to
154 * drop into kdebug() when:
155 * - a lock heirarchy violation occurs
156 * - locks are held when going to sleep.
157 */
158#ifdef WITNESS_DDB
159int witness_ddb = 1;
160#else
161int witness_ddb = 0;
162#endif
163TUNABLE_INT("debug.witness_ddb", &witness_ddb);
164SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
165#endif /* DDB */
166
167#ifdef WITNESS_SKIPSPIN
168int witness_skipspin = 1;
169#else
170int witness_skipspin = 0;
171#endif
172TUNABLE_INT("debug.witness_skipspin", &witness_skipspin);
173SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0,
174 "");
175
176static struct mtx w_mtx;
177static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
178static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
179static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
180static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
181static struct witness_child_list_entry *w_child_free = NULL;
182static struct lock_list_entry *w_lock_list_free = NULL;
183static int witness_dead; /* fatal error, probably no memory */
184
185static struct witness w_data[WITNESS_COUNT];
186static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
187static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
188
189static struct witness_order_list_entry order_lists[] = {
190 { "Giant", &lock_class_mtx_sleep },
31 */
32
33/*
34 * Implementation of the `witness' lock verifier. Originally implemented for
35 * mutexes in BSD/OS. Extended to handle generic lock objects and lock
36 * classes in FreeBSD.
37 */
38
39/*
40 * Main Entry: witness
41 * Pronunciation: 'wit-n&s
42 * Function: noun
43 * Etymology: Middle English witnesse, from Old English witnes knowledge,
44 * testimony, witness, from 2wit
45 * Date: before 12th century
46 * 1 : attestation of a fact or event : TESTIMONY
47 * 2 : one that gives evidence; specifically : one who testifies in
48 * a cause or before a judicial tribunal
49 * 3 : one asked to be present at a transaction so as to be able to
50 * testify to its having taken place
51 * 4 : one who has personal knowledge of something
52 * 5 a : something serving as evidence or proof : SIGN
53 * b : public affirmation by word or example of usually
54 * religious faith or conviction <the heroic witness to divine
55 * life -- Pilot>
56 * 6 capitalized : a member of the Jehovah's Witnesses
57 */
58
59#include "opt_ddb.h"
60#include "opt_witness.h"
61
62#include <sys/param.h>
63#include <sys/bus.h>
64#include <sys/kernel.h>
65#include <sys/ktr.h>
66#include <sys/lock.h>
67#include <sys/malloc.h>
68#include <sys/mutex.h>
69#include <sys/proc.h>
70#include <sys/sysctl.h>
71#include <sys/systm.h>
72
73#include <ddb/ddb.h>
74
75#define WITNESS_COUNT 200
76#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
77/*
78 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
79 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should
80 * probably be safe for the most part, but it's still a SWAG.
81 */
82#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
83
84#define WITNESS_NCHILDREN 6
85
86struct witness_child_list_entry;
87
88struct witness {
89 const char *w_name;
90 struct lock_class *w_class;
91 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */
92 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */
93 struct witness_child_list_entry *w_children; /* Great evilness... */
94 const char *w_file;
95 int w_line;
96 u_int w_level;
97 u_int w_refcount;
98 u_char w_Giant_squawked:1;
99 u_char w_other_squawked:1;
100 u_char w_same_squawked:1;
101};
102
103struct witness_child_list_entry {
104 struct witness_child_list_entry *wcl_next;
105 struct witness *wcl_children[WITNESS_NCHILDREN];
106 u_int wcl_count;
107};
108
109STAILQ_HEAD(witness_list, witness);
110
111struct witness_blessed {
112 const char *b_lock1;
113 const char *b_lock2;
114};
115
116struct witness_order_list_entry {
117 const char *w_name;
118 struct lock_class *w_class;
119};
120
121static struct witness *enroll(const char *description,
122 struct lock_class *lock_class);
123static int itismychild(struct witness *parent, struct witness *child);
124static void removechild(struct witness *parent, struct witness *child);
125static int isitmychild(struct witness *parent, struct witness *child);
126static int isitmydescendant(struct witness *parent, struct witness *child);
127static int dup_ok(struct witness *);
128static int blessed(struct witness *, struct witness *);
129static void witness_display_list(void(*prnt)(const char *fmt, ...),
130 struct witness_list *list);
131static void witness_displaydescendants(void(*)(const char *fmt, ...),
132 struct witness *);
133static void witness_leveldescendents(struct witness *parent, int level);
134static void witness_levelall(void);
135static struct witness *witness_get(void);
136static void witness_free(struct witness *m);
137static struct witness_child_list_entry *witness_child_get(void);
138static void witness_child_free(struct witness_child_list_entry *wcl);
139static struct lock_list_entry *witness_lock_list_get(void);
140static void witness_lock_list_free(struct lock_list_entry *lle);
141static void witness_display(void(*)(const char *fmt, ...));
142static struct lock_instance *find_instance(struct lock_list_entry *lock_list,
143 struct lock_object *lock);
144
145MALLOC_DEFINE(M_WITNESS, "witness", "witness structure");
146
147static int witness_watch = 1;
148TUNABLE_INT("debug.witness_watch", &witness_watch);
149SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, "");
150
151#ifdef DDB
152/*
153 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to
154 * drop into kdebug() when:
155 * - a lock heirarchy violation occurs
156 * - locks are held when going to sleep.
157 */
158#ifdef WITNESS_DDB
159int witness_ddb = 1;
160#else
161int witness_ddb = 0;
162#endif
163TUNABLE_INT("debug.witness_ddb", &witness_ddb);
164SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
165#endif /* DDB */
166
167#ifdef WITNESS_SKIPSPIN
168int witness_skipspin = 1;
169#else
170int witness_skipspin = 0;
171#endif
172TUNABLE_INT("debug.witness_skipspin", &witness_skipspin);
173SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0,
174 "");
175
176static struct mtx w_mtx;
177static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
178static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
179static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
180static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
181static struct witness_child_list_entry *w_child_free = NULL;
182static struct lock_list_entry *w_lock_list_free = NULL;
183static int witness_dead; /* fatal error, probably no memory */
184
185static struct witness w_data[WITNESS_COUNT];
186static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
187static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
188
189static struct witness_order_list_entry order_lists[] = {
190 { "Giant", &lock_class_mtx_sleep },
191 { "pgrpsess", &lock_class_sx },
191 { "proctree", &lock_class_sx },
192 { "allproc", &lock_class_sx },
192 { "proctree", &lock_class_sx },
193 { "allproc", &lock_class_sx },
194 { "process group", &lock_class_mtx_sleep },
193 { "process lock", &lock_class_mtx_sleep },
195 { "process lock", &lock_class_mtx_sleep },
196 { "session", &lock_class_mtx_sleep },
194 { "uidinfo hash", &lock_class_mtx_sleep },
195 { "uidinfo struct", &lock_class_mtx_sleep },
196 { NULL, NULL },
197 /*
198 * spin locks
199 */
200#ifdef SMP
201 { "ap boot", &lock_class_mtx_spin },
202#ifdef __i386__
203 { "com", &lock_class_mtx_spin },
204#endif
205#endif
206 { "sio", &lock_class_mtx_spin },
207#ifdef __i386__
208 { "cy", &lock_class_mtx_spin },
209#endif
210 { "ng_node", &lock_class_mtx_spin },
211 { "ng_worklist", &lock_class_mtx_spin },
212 { "ithread table lock", &lock_class_mtx_spin },
213 { "sched lock", &lock_class_mtx_spin },
214 { "callout", &lock_class_mtx_spin },
215 /*
216 * leaf locks
217 */
218 { "allpmaps", &lock_class_mtx_spin },
219 { "icu", &lock_class_mtx_spin },
220#ifdef SMP
221 { "smp rendezvous", &lock_class_mtx_spin },
222#endif
223 { "clk", &lock_class_mtx_spin },
224 { NULL, NULL },
225 { NULL, NULL }
226};
227
228static const char *dup_list[] = {
229 "process lock",
197 { "uidinfo hash", &lock_class_mtx_sleep },
198 { "uidinfo struct", &lock_class_mtx_sleep },
199 { NULL, NULL },
200 /*
201 * spin locks
202 */
203#ifdef SMP
204 { "ap boot", &lock_class_mtx_spin },
205#ifdef __i386__
206 { "com", &lock_class_mtx_spin },
207#endif
208#endif
209 { "sio", &lock_class_mtx_spin },
210#ifdef __i386__
211 { "cy", &lock_class_mtx_spin },
212#endif
213 { "ng_node", &lock_class_mtx_spin },
214 { "ng_worklist", &lock_class_mtx_spin },
215 { "ithread table lock", &lock_class_mtx_spin },
216 { "sched lock", &lock_class_mtx_spin },
217 { "callout", &lock_class_mtx_spin },
218 /*
219 * leaf locks
220 */
221 { "allpmaps", &lock_class_mtx_spin },
222 { "icu", &lock_class_mtx_spin },
223#ifdef SMP
224 { "smp rendezvous", &lock_class_mtx_spin },
225#endif
226 { "clk", &lock_class_mtx_spin },
227 { NULL, NULL },
228 { NULL, NULL }
229};
230
231static const char *dup_list[] = {
232 "process lock",
233 "process group",
230 NULL
231};
232
233/*
234 * Pairs of locks which have been blessed
235 * Don't complain about order problems with blessed locks
236 */
237static struct witness_blessed blessed_list[] = {
238};
239static int blessed_count =
240 sizeof(blessed_list) / sizeof(struct witness_blessed);
241
242/*
243 * List of all locks in the system.
244 */
245STAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks);
246
247static struct mtx all_mtx = {
248 { &lock_class_mtx_sleep, /* mtx_object.lo_class */
249 "All locks list", /* mtx_object.lo_name */
250 LO_INITIALIZED, /* mtx_object.lo_flags */
251 { NULL }, /* mtx_object.lo_list */
252 NULL }, /* mtx_object.lo_witness */
253 MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */
254 TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
255 { NULL, NULL } /* mtx_contested */
256};
257
258/*
259 * This global is set to 0 once it becomes safe to use the witness code.
260 */
261static int witness_cold = 1;
262
263/*
264 * Global variables for book keeping.
265 */
266static int lock_cur_cnt;
267static int lock_max_cnt;
268
269/*
270 * The WITNESS-enabled diagnostic code.
271 */
272static void
273witness_initialize(void *dummy __unused)
274{
275 struct lock_object *lock;
276 struct witness_order_list_entry *order;
277 struct witness *w, *w1;
278 int i;
279
280 /*
281 * We have to release Giant before initializing its witness
282 * structure so that WITNESS doesn't get confused.
283 */
284 mtx_unlock(&Giant);
285 mtx_assert(&Giant, MA_NOTOWNED);
286
287 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
288 STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
289 mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
290 for (i = 0; i < WITNESS_COUNT; i++)
291 witness_free(&w_data[i]);
292 for (i = 0; i < WITNESS_CHILDCOUNT; i++)
293 witness_child_free(&w_childdata[i]);
294 for (i = 0; i < LOCK_CHILDCOUNT; i++)
295 witness_lock_list_free(&w_locklistdata[i]);
296
297 /* First add in all the specified order lists. */
298 for (order = order_lists; order->w_name != NULL; order++) {
299 w = enroll(order->w_name, order->w_class);
300 if (w == NULL)
301 continue;
302 w->w_file = "order list";
303 for (order++; order->w_name != NULL; order++) {
304 w1 = enroll(order->w_name, order->w_class);
305 if (w1 == NULL)
306 continue;
307 w1->w_file = "order list";
308 itismychild(w, w1);
309 w = w1;
310 }
311 }
312
313 /* Iterate through all locks and add them to witness. */
314 mtx_lock(&all_mtx);
315 STAILQ_FOREACH(lock, &all_locks, lo_list) {
316 if (lock->lo_flags & LO_WITNESS)
317 lock->lo_witness = enroll(lock->lo_name,
318 lock->lo_class);
319 else
320 lock->lo_witness = NULL;
321 }
322 mtx_unlock(&all_mtx);
323
324 /* Mark the witness code as being ready for use. */
325 atomic_store_rel_int(&witness_cold, 0);
326
327 mtx_lock(&Giant);
328}
329SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
330
331void
332witness_init(struct lock_object *lock)
333{
334 struct lock_class *class;
335
336 class = lock->lo_class;
337 if (lock->lo_flags & LO_INITIALIZED)
338 panic("%s: lock (%s) %s is already initialized", __func__,
339 class->lc_name, lock->lo_name);
340 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
341 (class->lc_flags & LC_RECURSABLE) == 0)
342 panic("%s: lock (%s) %s can not be recursable", __func__,
343 class->lc_name, lock->lo_name);
344 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
345 (class->lc_flags & LC_SLEEPABLE) == 0)
346 panic("%s: lock (%s) %s can not be sleepable", __func__,
347 class->lc_name, lock->lo_name);
348 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
349 (class->lc_flags & LC_UPGRADABLE) == 0)
350 panic("%s: lock (%s) %s can not be upgradable", __func__,
351 class->lc_name, lock->lo_name);
352
353 mtx_lock(&all_mtx);
354 STAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
355 lock->lo_flags |= LO_INITIALIZED;
356 lock_cur_cnt++;
357 if (lock_cur_cnt > lock_max_cnt)
358 lock_max_cnt = lock_cur_cnt;
359 mtx_unlock(&all_mtx);
360 if (!witness_cold && !witness_dead && panicstr == NULL &&
361 (lock->lo_flags & LO_WITNESS) != 0)
362 lock->lo_witness = enroll(lock->lo_name, class);
363 else
364 lock->lo_witness = NULL;
365}
366
367void
368witness_destroy(struct lock_object *lock)
369{
370 struct witness *w;
371
372 if (witness_cold)
373 panic("lock (%s) %s destroyed while witness_cold",
374 lock->lo_class->lc_name, lock->lo_name);
375 if ((lock->lo_flags & LO_INITIALIZED) == 0)
376 panic("%s: lock (%s) %s is not initialized", __func__,
377 lock->lo_class->lc_name, lock->lo_name);
378
379 /* XXX: need to verify that no one holds the lock */
380 w = lock->lo_witness;
381 if (w != NULL) {
382 mtx_lock_spin(&w_mtx);
383 w->w_refcount--;
384 if (w->w_refcount == 0) {
385 CTR2(KTR_WITNESS,
386 "%s: marking witness %s as dead", __func__, w->w_name);
387 w->w_name = "(dead)";
388 w->w_file = "(dead)";
389 w->w_line = 0;
390 }
391 mtx_unlock_spin(&w_mtx);
392 }
393
394 mtx_lock(&all_mtx);
395 lock_cur_cnt--;
396 STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list);
397 lock->lo_flags &= ~LO_INITIALIZED;
398 mtx_unlock(&all_mtx);
399}
400
401static void
402witness_display_list(void(*prnt)(const char *fmt, ...),
403 struct witness_list *list)
404{
405 struct witness *w, *w1;
406 int found;
407
408 STAILQ_FOREACH(w, list, w_typelist) {
409 if (w->w_file == NULL)
410 continue;
411 found = 0;
412 STAILQ_FOREACH(w1, list, w_typelist) {
413 if (isitmychild(w1, w)) {
414 found++;
415 break;
416 }
417 }
418 if (found)
419 continue;
420 /*
421 * This lock has no anscestors, display its descendants.
422 */
423 witness_displaydescendants(prnt, w);
424 }
425}
426
427static void
428witness_display(void(*prnt)(const char *fmt, ...))
429{
430 struct witness *w;
431
432 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
433 witness_levelall();
434
435 /*
436 * First, handle sleep locks which have been acquired at least
437 * once.
438 */
439 prnt("Sleep locks:\n");
440 witness_display_list(prnt, &w_sleep);
441
442 /*
443 * Now do spin locks which have been acquired at least once.
444 */
445 prnt("\nSpin locks:\n");
446 witness_display_list(prnt, &w_spin);
447
448 /*
449 * Finally, any locks which have not been acquired yet.
450 */
451 prnt("\nLocks which were never acquired:\n");
452 STAILQ_FOREACH(w, &w_all, w_list) {
453 if (w->w_file != NULL)
454 continue;
455 prnt("%s\n", w->w_name);
456 }
457}
458
459void
460witness_lock(struct lock_object *lock, int flags, const char *file, int line)
461{
462 struct lock_list_entry **lock_list, *lle;
463 struct lock_instance *lock1, *lock2;
464 struct lock_class *class;
465 struct witness *w, *w1;
466 struct thread *td;
467 int i, j;
468#ifdef DDB
469 int go_into_ddb = 0;
470#endif /* DDB */
471
472 if (witness_cold || witness_dead || lock->lo_witness == NULL ||
473 panicstr != NULL)
474 return;
475 w = lock->lo_witness;
476 class = lock->lo_class;
477 td = curthread;
478
479 if (class->lc_flags & LC_SLEEPLOCK) {
480 if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0)
481 panic("blockable sleep lock (%s) %s @ %s:%d",
482 class->lc_name, lock->lo_name, file, line);
483 lock_list = &td->td_sleeplocks;
484 } else
485 lock_list = PCPU_PTR(spinlocks);
486
487 /*
488 * Try locks do not block if they fail to acquire the lock, thus
489 * there is no danger of deadlocks or of switching while holding a
490 * spin lock if we acquire a lock via a try operation.
491 */
492 if (flags & LOP_TRYLOCK)
493 goto out;
494
495 /*
496 * Is this the first lock acquired? If so, then no order checking
497 * is needed.
498 */
499 if (*lock_list == NULL)
500 goto out;
501
502 /*
503 * Check to see if we are recursing on a lock we already own.
504 */
505 lock1 = find_instance(*lock_list, lock);
506 if (lock1 != NULL) {
507 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
508 (flags & LOP_EXCLUSIVE) == 0) {
509 printf("shared lock of (%s) %s @ %s:%d\n",
510 class->lc_name, lock->lo_name, file, line);
511 printf("while exclusively locked from %s:%d\n",
512 lock1->li_file, lock1->li_line);
513 panic("share->excl");
514 }
515 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
516 (flags & LOP_EXCLUSIVE) != 0) {
517 printf("exclusive lock of (%s) %s @ %s:%d\n",
518 class->lc_name, lock->lo_name, file, line);
519 printf("while share locked from %s:%d\n",
520 lock1->li_file, lock1->li_line);
521 panic("excl->share");
522 }
523 lock1->li_flags++;
524 if ((lock->lo_flags & LO_RECURSABLE) == 0) {
525 printf(
526 "recursed on non-recursive lock (%s) %s @ %s:%d\n",
527 class->lc_name, lock->lo_name, file, line);
528 printf("first acquired @ %s:%d\n", lock1->li_file,
529 lock1->li_line);
530 panic("recurse");
531 }
532 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
533 td->td_proc->p_pid, lock->lo_name,
534 lock1->li_flags & LI_RECURSEMASK);
535 lock1->li_file = file;
536 lock1->li_line = line;
537 return;
538 }
539
540 /*
541 * Check for duplicate locks of the same type. Note that we only
542 * have to check for this on the last lock we just acquired. Any
543 * other cases will be caught as lock order violations.
544 */
545 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
546 w1 = lock1->li_lock->lo_witness;
547 if (w1 == w) {
548 if (w->w_same_squawked || dup_ok(w))
549 goto out;
550 w->w_same_squawked = 1;
551 printf("acquiring duplicate lock of same type: \"%s\"\n",
552 lock->lo_name);
553 printf(" 1st @ %s:%d\n", lock1->li_file, lock1->li_line);
554 printf(" 2nd @ %s:%d\n", file, line);
555#ifdef DDB
556 go_into_ddb = 1;
557#endif /* DDB */
558 goto out;
559 }
560 MPASS(!mtx_owned(&w_mtx));
561 mtx_lock_spin(&w_mtx);
562 /*
563 * If we have a known higher number just say ok
564 */
565 if (witness_watch > 1 && w->w_level > w1->w_level) {
566 mtx_unlock_spin(&w_mtx);
567 goto out;
568 }
569 if (isitmydescendant(w1, w)) {
570 mtx_unlock_spin(&w_mtx);
571 goto out;
572 }
573 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
574 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
575
576 MPASS(j < WITNESS_COUNT);
577 lock1 = &lle->ll_children[i];
578 w1 = lock1->li_lock->lo_witness;
579
580 /*
581 * If this lock doesn't undergo witness checking,
582 * then skip it.
583 */
584 if (w1 == NULL) {
585 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
586 ("lock missing witness structure"));
587 continue;
588 }
589 /*
590 * If we are locking Giant and we slept with this
591 * lock, then skip it.
592 */
593 if ((lock1->li_flags & LI_SLEPT) != 0 &&
594 lock == &Giant.mtx_object)
595 continue;
596 if (!isitmydescendant(w, w1))
597 continue;
598 /*
599 * We have a lock order violation, check to see if it
600 * is allowed or has already been yelled about.
601 */
602 mtx_unlock_spin(&w_mtx);
603 if (blessed(w, w1))
604 goto out;
605 if (lock1->li_lock == &Giant.mtx_object) {
606 if (w1->w_Giant_squawked)
607 goto out;
608 else
609 w1->w_Giant_squawked = 1;
610 } else {
611 if (w1->w_other_squawked)
612 goto out;
613 else
614 w1->w_other_squawked = 1;
615 }
616 /*
617 * Ok, yell about it.
618 */
619 printf("lock order reversal\n");
620 /*
621 * Try to locate an earlier lock with
622 * witness w in our list.
623 */
624 do {
625 lock2 = &lle->ll_children[i];
626 MPASS(lock2->li_lock != NULL);
627 if (lock2->li_lock->lo_witness == w)
628 break;
629 i--;
630 if (i == 0 && lle->ll_next != NULL) {
631 lle = lle->ll_next;
632 i = lle->ll_count - 1;
633 MPASS(i != 0);
634 }
635 } while (i >= 0);
636 if (i < 0) {
637 printf(" 1st %p %s @ %s:%d\n", lock1->li_lock,
638 lock1->li_lock->lo_name, lock1->li_file,
639 lock1->li_line);
640 printf(" 2nd %p %s @ %s:%d\n", lock,
641 lock->lo_name, file, line);
642 } else {
643 printf(" 1st %p %s @ %s:%d\n", lock2->li_lock,
644 lock2->li_lock->lo_name, lock2->li_file,
645 lock2->li_line);
646 printf(" 2nd %p %s @ %s:%d\n", lock1->li_lock,
647 lock1->li_lock->lo_name, lock1->li_file,
648 lock1->li_line);
649 printf(" 3rd %p %s @ %s:%d\n", lock,
650 lock->lo_name, file, line);
651 }
652#ifdef DDB
653 go_into_ddb = 1;
654#endif /* DDB */
655 goto out;
656 }
657 }
658 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
659 /*
660 * Don't build a new relationship if we are locking Giant just
661 * after waking up and the previous lock in the list was acquired
662 * prior to blocking.
663 */
664 if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0)
665 mtx_unlock_spin(&w_mtx);
666 else {
667 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
668 lock->lo_name, lock1->li_lock->lo_name);
669 if (!itismychild(lock1->li_lock->lo_witness, w))
670 mtx_unlock_spin(&w_mtx);
671 }
672
673out:
674#ifdef DDB
675 if (witness_ddb && go_into_ddb)
676 Debugger(__func__);
677#endif /* DDB */
678 w->w_file = file;
679 w->w_line = line;
680
681 lle = *lock_list;
682 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
683 lle = witness_lock_list_get();
684 if (lle == NULL)
685 return;
686 lle->ll_next = *lock_list;
687 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
688 td->td_proc->p_pid, lle);
689 *lock_list = lle;
690 }
691 lock1 = &lle->ll_children[lle->ll_count++];
692 lock1->li_lock = lock;
693 lock1->li_line = line;
694 lock1->li_file = file;
695 if ((flags & LOP_EXCLUSIVE) != 0)
696 lock1->li_flags = LI_EXCLUSIVE;
697 else
698 lock1->li_flags = 0;
699 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
700 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
701}
702
703void
704witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
705{
706 struct lock_instance *instance;
707 struct lock_class *class;
708
709 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
710 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
711 return;
712 class = lock->lo_class;
713 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
714 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
715 class->lc_name, lock->lo_name, file, line);
716 if ((flags & LOP_TRYLOCK) == 0)
717 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
718 lock->lo_name, file, line);
719 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
720 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
721 class->lc_name, lock->lo_name, file, line);
722 instance = find_instance(curthread->td_sleeplocks, lock);
723 if (instance == NULL)
724 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
725 class->lc_name, lock->lo_name, file, line);
726 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
727 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
728 class->lc_name, lock->lo_name, file, line);
729 if ((instance->li_flags & LI_RECURSEMASK) != 0)
730 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
731 class->lc_name, lock->lo_name,
732 instance->li_flags & LI_RECURSEMASK, file, line);
733 instance->li_flags |= LI_EXCLUSIVE;
734}
735
736void
737witness_downgrade(struct lock_object *lock, int flags, const char *file,
738 int line)
739{
740 struct lock_instance *instance;
741 struct lock_class *class;
742
743 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
744 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
745 return;
746 class = lock->lo_class;
747 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
748 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
749 class->lc_name, lock->lo_name, file, line);
750 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
751 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
752 class->lc_name, lock->lo_name, file, line);
753 instance = find_instance(curthread->td_sleeplocks, lock);
754 if (instance == NULL)
755 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
756 class->lc_name, lock->lo_name, file, line);
757 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
758 panic("downgrade of shared lock (%s) %s @ %s:%d",
759 class->lc_name, lock->lo_name, file, line);
760 if ((instance->li_flags & LI_RECURSEMASK) != 0)
761 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
762 class->lc_name, lock->lo_name,
763 instance->li_flags & LI_RECURSEMASK, file, line);
764 instance->li_flags &= ~LI_EXCLUSIVE;
765}
766
767void
768witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
769{
770 struct lock_list_entry **lock_list, *lle;
771 struct lock_instance *instance;
772 struct lock_class *class;
773 struct thread *td;
774 critical_t s;
775 int i, j;
776
777 if (witness_cold || witness_dead || lock->lo_witness == NULL ||
778 panicstr != NULL)
779 return;
780 td = curthread;
781 class = lock->lo_class;
782 if (class->lc_flags & LC_SLEEPLOCK)
783 lock_list = &td->td_sleeplocks;
784 else
785 lock_list = PCPU_PTR(spinlocks);
786 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
787 for (i = 0; i < (*lock_list)->ll_count; i++) {
788 instance = &(*lock_list)->ll_children[i];
789 if (instance->li_lock == lock) {
790 if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
791 (flags & LOP_EXCLUSIVE) == 0) {
792 printf(
793 "shared unlock of (%s) %s @ %s:%d\n",
794 class->lc_name, lock->lo_name,
795 file, line);
796 printf(
797 "while exclusively locked from %s:%d\n",
798 instance->li_file,
799 instance->li_line);
800 panic("excl->ushare");
801 }
802 if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
803 (flags & LOP_EXCLUSIVE) != 0) {
804 printf(
805 "exclusive unlock of (%s) %s @ %s:%d\n",
806 class->lc_name, lock->lo_name,
807 file, line);
808 printf(
809 "while share locked from %s:%d\n",
810 instance->li_file,
811 instance->li_line);
812 panic("share->uexcl");
813 }
814 /* If we are recursed, unrecurse. */
815 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
816 CTR4(KTR_WITNESS,
817 "%s: pid %d unrecursed on %s r=%d", __func__,
818 td->td_proc->p_pid,
819 instance->li_lock->lo_name,
820 instance->li_flags);
821 instance->li_flags--;
822 return;
823 }
824 s = cpu_critical_enter();
825 CTR4(KTR_WITNESS,
826 "%s: pid %d removed %s from lle[%d]", __func__,
827 td->td_proc->p_pid,
828 instance->li_lock->lo_name,
829 (*lock_list)->ll_count - 1);
830 (*lock_list)->ll_count--;
831 for (j = i; j < (*lock_list)->ll_count; j++)
832 (*lock_list)->ll_children[j] =
833 (*lock_list)->ll_children[j + 1];
834 cpu_critical_exit(s);
835 if ((*lock_list)->ll_count == 0) {
836 lle = *lock_list;
837 *lock_list = lle->ll_next;
838 CTR3(KTR_WITNESS,
839 "%s: pid %d removed lle %p", __func__,
840 td->td_proc->p_pid, lle);
841 witness_lock_list_free(lle);
842 }
843 return;
844 }
845 }
846 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
847 file, line);
848}
849
850/*
851 * Warn if any held locks are not sleepable. Note that Giant and the lock
852 * passed in are both special cases since they are both released during the
853 * sleep process and aren't actually held while the thread is asleep.
854 */
855int
856witness_sleep(int check_only, struct lock_object *lock, const char *file,
857 int line)
858{
859 struct lock_list_entry **lock_list, *lle;
860 struct lock_instance *lock1;
861 struct thread *td;
862 critical_t savecrit;
863 int i, n;
864
865 if (witness_dead || panicstr != NULL)
866 return (0);
867 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
868 n = 0;
869 /*
870 * Preemption bad because we need PCPU_PTR(spinlocks) to not change.
871 */
872 savecrit = cpu_critical_enter();
873 td = curthread;
874 lock_list = &td->td_sleeplocks;
875again:
876 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
877 for (i = lle->ll_count - 1; i >= 0; i--) {
878 lock1 = &lle->ll_children[i];
879 if (lock1->li_lock == lock ||
880 lock1->li_lock == &Giant.mtx_object)
881 continue;
882 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) {
883 if (check_only == 0) {
884 CTR3(KTR_WITNESS,
885 "pid %d: sleeping with lock (%s) %s held",
886 td->td_proc->p_pid,
887 lock1->li_lock->lo_class->lc_name,
888 lock1->li_lock->lo_name);
889 lock1->li_flags |= LI_SLEPT;
890 }
891 continue;
892 }
893 n++;
894 printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
895 file, line, check_only ? "could sleep" : "sleeping",
896 lock1->li_lock->lo_name, lock1->li_file,
897 lock1->li_line);
898 }
899 if (lock_list == &td->td_sleeplocks) {
900 lock_list = PCPU_PTR(spinlocks);
901 goto again;
902 }
903#ifdef DDB
904 if (witness_ddb && n)
905 Debugger(__func__);
906#endif /* DDB */
907 cpu_critical_exit(savecrit);
908 return (n);
909}
910
911static struct witness *
912enroll(const char *description, struct lock_class *lock_class)
913{
914 struct witness *w;
915
916 if (!witness_watch || witness_dead || panicstr != NULL)
917 return (NULL);
918 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
919 return (NULL);
920 mtx_lock_spin(&w_mtx);
921 STAILQ_FOREACH(w, &w_all, w_list) {
922 if (strcmp(description, w->w_name) == 0) {
923 w->w_refcount++;
924 mtx_unlock_spin(&w_mtx);
925 if (lock_class != w->w_class)
926 panic(
927 "lock (%s) %s does not match earlier (%s) lock",
928 description, lock_class->lc_name,
929 w->w_class->lc_name);
930 return (w);
931 }
932 }
933 /*
934 * This isn't quite right, as witness_cold is still 0 while we
935 * enroll all the locks initialized before witness_initialize().
936 */
937 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
938 mtx_unlock_spin(&w_mtx);
939 panic("spin lock %s not in order list", description);
940 }
941 if ((w = witness_get()) == NULL)
942 return (NULL);
943 w->w_name = description;
944 w->w_class = lock_class;
945 w->w_refcount = 1;
946 STAILQ_INSERT_HEAD(&w_all, w, w_list);
947 if (lock_class->lc_flags & LC_SPINLOCK)
948 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
949 else if (lock_class->lc_flags & LC_SLEEPLOCK)
950 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
951 else {
952 mtx_unlock_spin(&w_mtx);
953 panic("lock class %s is not sleep or spin",
954 lock_class->lc_name);
955 }
956 mtx_unlock_spin(&w_mtx);
957 return (w);
958}
959
960static int
961itismychild(struct witness *parent, struct witness *child)
962{
963 static int recursed;
964 struct witness_child_list_entry **wcl;
965 struct witness_list *list;
966
967 MPASS(child != NULL && parent != NULL);
968 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
969 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
970 panic(
971 "%s: parent (%s) and child (%s) are not the same lock type",
972 __func__, parent->w_class->lc_name,
973 child->w_class->lc_name);
974
975 /*
976 * Insert "child" after "parent"
977 */
978 wcl = &parent->w_children;
979 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
980 wcl = &(*wcl)->wcl_next;
981 if (*wcl == NULL) {
982 *wcl = witness_child_get();
983 if (*wcl == NULL)
984 return (1);
985 }
986 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
987
988 /*
989 * Now prune whole tree. We look for cases where a lock is now
990 * both a descendant and a direct child of a given lock. In that
991 * case, we want to remove the direct child link from the tree.
992 */
993 if (recursed)
994 return (0);
995 recursed = 1;
996 if (parent->w_class->lc_flags & LC_SLEEPLOCK)
997 list = &w_sleep;
998 else
999 list = &w_spin;
1000 STAILQ_FOREACH(child, list, w_typelist) {
1001 STAILQ_FOREACH(parent, list, w_typelist) {
1002 if (!isitmychild(parent, child))
1003 continue;
1004 removechild(parent, child);
1005 if (isitmydescendant(parent, child))
1006 continue;
1007 itismychild(parent, child);
1008 }
1009 }
1010 recursed = 0;
1011 witness_levelall();
1012 return (0);
1013}
1014
1015static void
1016removechild(struct witness *parent, struct witness *child)
1017{
1018 struct witness_child_list_entry **wcl, *wcl1;
1019 int i;
1020
1021 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1022 for (i = 0; i < (*wcl)->wcl_count; i++)
1023 if ((*wcl)->wcl_children[i] == child)
1024 goto found;
1025 return;
1026found:
1027 (*wcl)->wcl_count--;
1028 if ((*wcl)->wcl_count > i)
1029 (*wcl)->wcl_children[i] =
1030 (*wcl)->wcl_children[(*wcl)->wcl_count];
1031 MPASS((*wcl)->wcl_children[i] != NULL);
1032 if ((*wcl)->wcl_count != 0)
1033 return;
1034 wcl1 = *wcl;
1035 *wcl = wcl1->wcl_next;
1036 witness_child_free(wcl1);
1037}
1038
1039static int
1040isitmychild(struct witness *parent, struct witness *child)
1041{
1042 struct witness_child_list_entry *wcl;
1043 int i;
1044
1045 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1046 for (i = 0; i < wcl->wcl_count; i++) {
1047 if (wcl->wcl_children[i] == child)
1048 return (1);
1049 }
1050 }
1051 return (0);
1052}
1053
1054static int
1055isitmydescendant(struct witness *parent, struct witness *child)
1056{
1057 struct witness_child_list_entry *wcl;
1058 int i, j;
1059
1060 if (isitmychild(parent, child))
1061 return (1);
1062 j = 0;
1063 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1064 MPASS(j < 1000);
1065 for (i = 0; i < wcl->wcl_count; i++) {
1066 if (isitmydescendant(wcl->wcl_children[i], child))
1067 return (1);
1068 }
1069 j++;
1070 }
1071 return (0);
1072}
1073
1074void
1075witness_levelall (void)
1076{
1077 struct witness_list *list;
1078 struct witness *w, *w1;
1079
1080 /*
1081 * First clear all levels.
1082 */
1083 STAILQ_FOREACH(w, &w_all, w_list) {
1084 w->w_level = 0;
1085 }
1086
1087 /*
1088 * Look for locks with no parent and level all their descendants.
1089 */
1090 STAILQ_FOREACH(w, &w_all, w_list) {
1091 /*
1092 * This is just an optimization, technically we could get
1093 * away just walking the all list each time.
1094 */
1095 if (w->w_class->lc_flags & LC_SLEEPLOCK)
1096 list = &w_sleep;
1097 else
1098 list = &w_spin;
1099 STAILQ_FOREACH(w1, list, w_typelist) {
1100 if (isitmychild(w1, w))
1101 goto skip;
1102 }
1103 witness_leveldescendents(w, 0);
1104 skip:
1105 }
1106}
1107
1108static void
1109witness_leveldescendents(struct witness *parent, int level)
1110{
1111 struct witness_child_list_entry *wcl;
1112 int i;
1113
1114 if (parent->w_level < level)
1115 parent->w_level = level;
1116 level++;
1117 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1118 for (i = 0; i < wcl->wcl_count; i++)
1119 witness_leveldescendents(wcl->wcl_children[i], level);
1120}
1121
1122static void
1123witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1124 struct witness *parent)
1125{
1126 struct witness_child_list_entry *wcl;
1127 int i, level;
1128
1129 level = parent->w_level;
1130 prnt("%-2d", level);
1131 for (i = 0; i < level; i++)
1132 prnt(" ");
1133 prnt("%s", parent->w_name);
1134 if (parent->w_file != NULL)
1135 prnt(" -- last acquired @ %s:%d\n", parent->w_file,
1136 parent->w_line);
1137 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1138 for (i = 0; i < wcl->wcl_count; i++)
1139 witness_displaydescendants(prnt,
1140 wcl->wcl_children[i]);
1141}
1142
1143static int
1144dup_ok(struct witness *w)
1145{
1146 const char **dup;
1147
1148 for (dup = dup_list; *dup != NULL; dup++)
1149 if (strcmp(w->w_name, *dup) == 0)
1150 return (1);
1151 return (0);
1152}
1153
1154static int
1155blessed(struct witness *w1, struct witness *w2)
1156{
1157 int i;
1158 struct witness_blessed *b;
1159
1160 for (i = 0; i < blessed_count; i++) {
1161 b = &blessed_list[i];
1162 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1163 if (strcmp(w2->w_name, b->b_lock2) == 0)
1164 return (1);
1165 continue;
1166 }
1167 if (strcmp(w1->w_name, b->b_lock2) == 0)
1168 if (strcmp(w2->w_name, b->b_lock1) == 0)
1169 return (1);
1170 }
1171 return (0);
1172}
1173
1174static struct witness *
1175witness_get(void)
1176{
1177 struct witness *w;
1178
1179 if (witness_dead) {
1180 mtx_unlock_spin(&w_mtx);
1181 return (NULL);
1182 }
1183 if (STAILQ_EMPTY(&w_free)) {
1184 witness_dead = 1;
1185 mtx_unlock_spin(&w_mtx);
1186 printf("%s: witness exhausted\n", __func__);
1187 return (NULL);
1188 }
1189 w = STAILQ_FIRST(&w_free);
1190 STAILQ_REMOVE_HEAD(&w_free, w_list);
1191 bzero(w, sizeof(*w));
1192 return (w);
1193}
1194
1195static void
1196witness_free(struct witness *w)
1197{
1198
1199 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1200}
1201
1202static struct witness_child_list_entry *
1203witness_child_get(void)
1204{
1205 struct witness_child_list_entry *wcl;
1206
1207 if (witness_dead) {
1208 mtx_unlock_spin(&w_mtx);
1209 return (NULL);
1210 }
1211 wcl = w_child_free;
1212 if (wcl == NULL) {
1213 witness_dead = 1;
1214 mtx_unlock_spin(&w_mtx);
1215 printf("%s: witness exhausted\n", __func__);
1216 return (NULL);
1217 }
1218 w_child_free = wcl->wcl_next;
1219 bzero(wcl, sizeof(*wcl));
1220 return (wcl);
1221}
1222
1223static void
1224witness_child_free(struct witness_child_list_entry *wcl)
1225{
1226
1227 wcl->wcl_next = w_child_free;
1228 w_child_free = wcl;
1229}
1230
1231static struct lock_list_entry *
1232witness_lock_list_get(void)
1233{
1234 struct lock_list_entry *lle;
1235
1236 if (witness_dead)
1237 return (NULL);
1238 mtx_lock_spin(&w_mtx);
1239 lle = w_lock_list_free;
1240 if (lle == NULL) {
1241 witness_dead = 1;
1242 mtx_unlock_spin(&w_mtx);
1243 printf("%s: witness exhausted\n", __func__);
1244 return (NULL);
1245 }
1246 w_lock_list_free = lle->ll_next;
1247 mtx_unlock_spin(&w_mtx);
1248 bzero(lle, sizeof(*lle));
1249 return (lle);
1250}
1251
1252static void
1253witness_lock_list_free(struct lock_list_entry *lle)
1254{
1255
1256 mtx_lock_spin(&w_mtx);
1257 lle->ll_next = w_lock_list_free;
1258 w_lock_list_free = lle;
1259 mtx_unlock_spin(&w_mtx);
1260}
1261
1262static struct lock_instance *
1263find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1264{
1265 struct lock_list_entry *lle;
1266 struct lock_instance *instance;
1267 int i;
1268
1269 for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1270 for (i = lle->ll_count - 1; i >= 0; i--) {
1271 instance = &lle->ll_children[i];
1272 if (instance->li_lock == lock)
1273 return (instance);
1274 }
1275 return (NULL);
1276}
1277
1278int
1279witness_list_locks(struct lock_list_entry **lock_list)
1280{
1281 struct lock_list_entry *lle;
1282 struct lock_instance *instance;
1283 struct lock_object *lock;
1284 int i, nheld;
1285
1286 nheld = 0;
1287 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1288 for (i = lle->ll_count - 1; i >= 0; i--) {
1289 instance = &lle->ll_children[i];
1290 lock = instance->li_lock;
1291 printf("%s (%s) %s (%p) locked @ %s:%d\n",
1292 (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1293 "exclusive" : "shared",
1294 lock->lo_class->lc_name, lock->lo_name, lock,
1295 instance->li_file, instance->li_line);
1296 nheld++;
1297 }
1298 return (nheld);
1299}
1300
1301/*
1302 * Calling this on td != curthread is bad unless we are in ddb.
1303 */
1304int
1305witness_list(struct thread *td)
1306{
1307 critical_t savecrit;
1308 int nheld;
1309
1310 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1311#ifdef DDB
1312 KASSERT(td == curthread || db_active,
1313 ("%s: td != curthread and we aren't in the debugger", __func__));
1314 if (!db_active && witness_dead)
1315 return (0);
1316#else
1317 KASSERT(td == curthread, ("%s: p != curthread", __func__));
1318 if (witness_dead)
1319 return (0);
1320#endif
1321 nheld = witness_list_locks(&td->td_sleeplocks);
1322
1323 /*
1324 * We only handle spinlocks if td == curthread. This is somewhat broken
1325 * if td is currently executing on some other CPU and holds spin locks
1326 * as we won't display those locks. If we had a MI way of getting
1327 * the per-cpu data for a given cpu then we could use
1328 * td->td_kse->ke_oncpu to get the list of spinlocks for this thread
1329 * and "fix" this.
1330 */
1331 if (td == curthread) {
1332 /*
1333 * Preemption bad because we need PCPU_PTR(spinlocks) to not
1334 * change.
1335 */
1336 savecrit = cpu_critical_enter();
1337 nheld += witness_list_locks(PCPU_PTR(spinlocks));
1338 cpu_critical_exit(savecrit);
1339 }
1340 return (nheld);
1341}
1342
1343void
1344witness_save(struct lock_object *lock, const char **filep, int *linep)
1345{
1346 struct lock_instance *instance;
1347
1348 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1349 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1350 return;
1351 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1352 panic("%s: lock (%s) %s is not a sleep lock", __func__,
1353 lock->lo_class->lc_name, lock->lo_name);
1354 instance = find_instance(curthread->td_sleeplocks, lock);
1355 if (instance == NULL)
1356 panic("%s: lock (%s) %s not locked", __func__,
1357 lock->lo_class->lc_name, lock->lo_name);
1358 *filep = instance->li_file;
1359 *linep = instance->li_line;
1360}
1361
1362void
1363witness_restore(struct lock_object *lock, const char *file, int line)
1364{
1365 struct lock_instance *instance;
1366
1367 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1368 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1369 return;
1370 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1371 panic("%s: lock (%s) %s is not a sleep lock", __func__,
1372 lock->lo_class->lc_name, lock->lo_name);
1373 instance = find_instance(curthread->td_sleeplocks, lock);
1374 if (instance == NULL)
1375 panic("%s: lock (%s) %s not locked", __func__,
1376 lock->lo_class->lc_name, lock->lo_name);
1377 lock->lo_witness->w_file = file;
1378 lock->lo_witness->w_line = line;
1379 instance->li_file = file;
1380 instance->li_line = line;
1381}
1382
1383void
1384witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1385{
1386#ifdef INVARIANT_SUPPORT
1387 struct lock_instance *instance;
1388
1389 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1390 return;
1391 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
1392 instance = find_instance(curthread->td_sleeplocks, lock);
1393 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
1394 instance = find_instance(PCPU_GET(spinlocks), lock);
1395 else {
1396 panic("Lock (%s) %s is not sleep or spin!",
1397 lock->lo_class->lc_name, lock->lo_name);
1398 return;
1399 }
1400 switch (flags) {
1401 case LA_UNLOCKED:
1402 if (instance != NULL)
1403 panic("Lock (%s) %s locked @ %s:%d.",
1404 lock->lo_class->lc_name, lock->lo_name, file, line);
1405 break;
1406 case LA_LOCKED:
1407 case LA_LOCKED | LA_RECURSED:
1408 case LA_LOCKED | LA_NOTRECURSED:
1409 case LA_SLOCKED:
1410 case LA_SLOCKED | LA_RECURSED:
1411 case LA_SLOCKED | LA_NOTRECURSED:
1412 case LA_XLOCKED:
1413 case LA_XLOCKED | LA_RECURSED:
1414 case LA_XLOCKED | LA_NOTRECURSED:
1415 if (instance == NULL) {
1416 panic("Lock (%s) %s not locked @ %s:%d.",
1417 lock->lo_class->lc_name, lock->lo_name, file, line);
1418 break;
1419 }
1420 if ((flags & LA_XLOCKED) != 0 &&
1421 (instance->li_flags & LI_EXCLUSIVE) == 0)
1422 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1423 lock->lo_class->lc_name, lock->lo_name, file, line);
1424 if ((flags & LA_SLOCKED) != 0 &&
1425 (instance->li_flags & LI_EXCLUSIVE) != 0)
1426 panic("Lock (%s) %s exclusively locked @ %s:%d.",
1427 lock->lo_class->lc_name, lock->lo_name, file, line);
1428 if ((flags & LA_RECURSED) != 0 &&
1429 (instance->li_flags & LI_RECURSEMASK) == 0)
1430 panic("Lock (%s) %s not recursed @ %s:%d.",
1431 lock->lo_class->lc_name, lock->lo_name, file, line);
1432 if ((flags & LA_NOTRECURSED) != 0 &&
1433 (instance->li_flags & LI_RECURSEMASK) != 0)
1434 panic("Lock (%s) %s recursed @ %s:%d.",
1435 lock->lo_class->lc_name, lock->lo_name, file, line);
1436 break;
1437 default:
1438 panic("Invalid lock assertion at %s:%d.", file, line);
1439
1440 }
1441#endif /* INVARIANT_SUPPORT */
1442}
1443
1444#ifdef DDB
1445
1446DB_SHOW_COMMAND(locks, db_witness_list)
1447{
1448 struct thread *td;
1449 pid_t pid;
1450 struct proc *p;
1451
1452 if (have_addr) {
1453 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1454 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1455 ((addr >> 16) % 16) * 10000;
1456 /* sx_slock(&allproc_lock); */
1457 FOREACH_PROC_IN_SYSTEM(p) {
1458 if (p->p_pid == pid)
1459 break;
1460 }
1461 /* sx_sunlock(&allproc_lock); */
1462 if (p == NULL) {
1463 db_printf("pid %d not found\n", pid);
1464 return;
1465 }
1466 FOREACH_THREAD_IN_PROC(p, td) {
1467 witness_list(td);
1468 }
1469 } else {
1470 td = curthread;
1471 witness_list(td);
1472 }
1473}
1474
1475DB_SHOW_COMMAND(witness, db_witness_display)
1476{
1477
1478 witness_display(db_printf);
1479}
1480#endif
234 NULL
235};
236
237/*
238 * Pairs of locks which have been blessed
239 * Don't complain about order problems with blessed locks
240 */
241static struct witness_blessed blessed_list[] = {
242};
243static int blessed_count =
244 sizeof(blessed_list) / sizeof(struct witness_blessed);
245
246/*
247 * List of all locks in the system.
248 */
249STAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks);
250
251static struct mtx all_mtx = {
252 { &lock_class_mtx_sleep, /* mtx_object.lo_class */
253 "All locks list", /* mtx_object.lo_name */
254 LO_INITIALIZED, /* mtx_object.lo_flags */
255 { NULL }, /* mtx_object.lo_list */
256 NULL }, /* mtx_object.lo_witness */
257 MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */
258 TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
259 { NULL, NULL } /* mtx_contested */
260};
261
262/*
263 * This global is set to 0 once it becomes safe to use the witness code.
264 */
265static int witness_cold = 1;
266
267/*
268 * Global variables for book keeping.
269 */
270static int lock_cur_cnt;
271static int lock_max_cnt;
272
273/*
274 * The WITNESS-enabled diagnostic code.
275 */
276static void
277witness_initialize(void *dummy __unused)
278{
279 struct lock_object *lock;
280 struct witness_order_list_entry *order;
281 struct witness *w, *w1;
282 int i;
283
284 /*
285 * We have to release Giant before initializing its witness
286 * structure so that WITNESS doesn't get confused.
287 */
288 mtx_unlock(&Giant);
289 mtx_assert(&Giant, MA_NOTOWNED);
290
291 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
292 STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
293 mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
294 for (i = 0; i < WITNESS_COUNT; i++)
295 witness_free(&w_data[i]);
296 for (i = 0; i < WITNESS_CHILDCOUNT; i++)
297 witness_child_free(&w_childdata[i]);
298 for (i = 0; i < LOCK_CHILDCOUNT; i++)
299 witness_lock_list_free(&w_locklistdata[i]);
300
301 /* First add in all the specified order lists. */
302 for (order = order_lists; order->w_name != NULL; order++) {
303 w = enroll(order->w_name, order->w_class);
304 if (w == NULL)
305 continue;
306 w->w_file = "order list";
307 for (order++; order->w_name != NULL; order++) {
308 w1 = enroll(order->w_name, order->w_class);
309 if (w1 == NULL)
310 continue;
311 w1->w_file = "order list";
312 itismychild(w, w1);
313 w = w1;
314 }
315 }
316
317 /* Iterate through all locks and add them to witness. */
318 mtx_lock(&all_mtx);
319 STAILQ_FOREACH(lock, &all_locks, lo_list) {
320 if (lock->lo_flags & LO_WITNESS)
321 lock->lo_witness = enroll(lock->lo_name,
322 lock->lo_class);
323 else
324 lock->lo_witness = NULL;
325 }
326 mtx_unlock(&all_mtx);
327
328 /* Mark the witness code as being ready for use. */
329 atomic_store_rel_int(&witness_cold, 0);
330
331 mtx_lock(&Giant);
332}
333SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
334
335void
336witness_init(struct lock_object *lock)
337{
338 struct lock_class *class;
339
340 class = lock->lo_class;
341 if (lock->lo_flags & LO_INITIALIZED)
342 panic("%s: lock (%s) %s is already initialized", __func__,
343 class->lc_name, lock->lo_name);
344 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
345 (class->lc_flags & LC_RECURSABLE) == 0)
346 panic("%s: lock (%s) %s can not be recursable", __func__,
347 class->lc_name, lock->lo_name);
348 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
349 (class->lc_flags & LC_SLEEPABLE) == 0)
350 panic("%s: lock (%s) %s can not be sleepable", __func__,
351 class->lc_name, lock->lo_name);
352 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
353 (class->lc_flags & LC_UPGRADABLE) == 0)
354 panic("%s: lock (%s) %s can not be upgradable", __func__,
355 class->lc_name, lock->lo_name);
356
357 mtx_lock(&all_mtx);
358 STAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
359 lock->lo_flags |= LO_INITIALIZED;
360 lock_cur_cnt++;
361 if (lock_cur_cnt > lock_max_cnt)
362 lock_max_cnt = lock_cur_cnt;
363 mtx_unlock(&all_mtx);
364 if (!witness_cold && !witness_dead && panicstr == NULL &&
365 (lock->lo_flags & LO_WITNESS) != 0)
366 lock->lo_witness = enroll(lock->lo_name, class);
367 else
368 lock->lo_witness = NULL;
369}
370
371void
372witness_destroy(struct lock_object *lock)
373{
374 struct witness *w;
375
376 if (witness_cold)
377 panic("lock (%s) %s destroyed while witness_cold",
378 lock->lo_class->lc_name, lock->lo_name);
379 if ((lock->lo_flags & LO_INITIALIZED) == 0)
380 panic("%s: lock (%s) %s is not initialized", __func__,
381 lock->lo_class->lc_name, lock->lo_name);
382
383 /* XXX: need to verify that no one holds the lock */
384 w = lock->lo_witness;
385 if (w != NULL) {
386 mtx_lock_spin(&w_mtx);
387 w->w_refcount--;
388 if (w->w_refcount == 0) {
389 CTR2(KTR_WITNESS,
390 "%s: marking witness %s as dead", __func__, w->w_name);
391 w->w_name = "(dead)";
392 w->w_file = "(dead)";
393 w->w_line = 0;
394 }
395 mtx_unlock_spin(&w_mtx);
396 }
397
398 mtx_lock(&all_mtx);
399 lock_cur_cnt--;
400 STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list);
401 lock->lo_flags &= ~LO_INITIALIZED;
402 mtx_unlock(&all_mtx);
403}
404
405static void
406witness_display_list(void(*prnt)(const char *fmt, ...),
407 struct witness_list *list)
408{
409 struct witness *w, *w1;
410 int found;
411
412 STAILQ_FOREACH(w, list, w_typelist) {
413 if (w->w_file == NULL)
414 continue;
415 found = 0;
416 STAILQ_FOREACH(w1, list, w_typelist) {
417 if (isitmychild(w1, w)) {
418 found++;
419 break;
420 }
421 }
422 if (found)
423 continue;
424 /*
425 * This lock has no anscestors, display its descendants.
426 */
427 witness_displaydescendants(prnt, w);
428 }
429}
430
431static void
432witness_display(void(*prnt)(const char *fmt, ...))
433{
434 struct witness *w;
435
436 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
437 witness_levelall();
438
439 /*
440 * First, handle sleep locks which have been acquired at least
441 * once.
442 */
443 prnt("Sleep locks:\n");
444 witness_display_list(prnt, &w_sleep);
445
446 /*
447 * Now do spin locks which have been acquired at least once.
448 */
449 prnt("\nSpin locks:\n");
450 witness_display_list(prnt, &w_spin);
451
452 /*
453 * Finally, any locks which have not been acquired yet.
454 */
455 prnt("\nLocks which were never acquired:\n");
456 STAILQ_FOREACH(w, &w_all, w_list) {
457 if (w->w_file != NULL)
458 continue;
459 prnt("%s\n", w->w_name);
460 }
461}
462
463void
464witness_lock(struct lock_object *lock, int flags, const char *file, int line)
465{
466 struct lock_list_entry **lock_list, *lle;
467 struct lock_instance *lock1, *lock2;
468 struct lock_class *class;
469 struct witness *w, *w1;
470 struct thread *td;
471 int i, j;
472#ifdef DDB
473 int go_into_ddb = 0;
474#endif /* DDB */
475
476 if (witness_cold || witness_dead || lock->lo_witness == NULL ||
477 panicstr != NULL)
478 return;
479 w = lock->lo_witness;
480 class = lock->lo_class;
481 td = curthread;
482
483 if (class->lc_flags & LC_SLEEPLOCK) {
484 if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0)
485 panic("blockable sleep lock (%s) %s @ %s:%d",
486 class->lc_name, lock->lo_name, file, line);
487 lock_list = &td->td_sleeplocks;
488 } else
489 lock_list = PCPU_PTR(spinlocks);
490
491 /*
492 * Try locks do not block if they fail to acquire the lock, thus
493 * there is no danger of deadlocks or of switching while holding a
494 * spin lock if we acquire a lock via a try operation.
495 */
496 if (flags & LOP_TRYLOCK)
497 goto out;
498
499 /*
500 * Is this the first lock acquired? If so, then no order checking
501 * is needed.
502 */
503 if (*lock_list == NULL)
504 goto out;
505
506 /*
507 * Check to see if we are recursing on a lock we already own.
508 */
509 lock1 = find_instance(*lock_list, lock);
510 if (lock1 != NULL) {
511 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
512 (flags & LOP_EXCLUSIVE) == 0) {
513 printf("shared lock of (%s) %s @ %s:%d\n",
514 class->lc_name, lock->lo_name, file, line);
515 printf("while exclusively locked from %s:%d\n",
516 lock1->li_file, lock1->li_line);
517 panic("share->excl");
518 }
519 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
520 (flags & LOP_EXCLUSIVE) != 0) {
521 printf("exclusive lock of (%s) %s @ %s:%d\n",
522 class->lc_name, lock->lo_name, file, line);
523 printf("while share locked from %s:%d\n",
524 lock1->li_file, lock1->li_line);
525 panic("excl->share");
526 }
527 lock1->li_flags++;
528 if ((lock->lo_flags & LO_RECURSABLE) == 0) {
529 printf(
530 "recursed on non-recursive lock (%s) %s @ %s:%d\n",
531 class->lc_name, lock->lo_name, file, line);
532 printf("first acquired @ %s:%d\n", lock1->li_file,
533 lock1->li_line);
534 panic("recurse");
535 }
536 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
537 td->td_proc->p_pid, lock->lo_name,
538 lock1->li_flags & LI_RECURSEMASK);
539 lock1->li_file = file;
540 lock1->li_line = line;
541 return;
542 }
543
544 /*
545 * Check for duplicate locks of the same type. Note that we only
546 * have to check for this on the last lock we just acquired. Any
547 * other cases will be caught as lock order violations.
548 */
549 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
550 w1 = lock1->li_lock->lo_witness;
551 if (w1 == w) {
552 if (w->w_same_squawked || dup_ok(w))
553 goto out;
554 w->w_same_squawked = 1;
555 printf("acquiring duplicate lock of same type: \"%s\"\n",
556 lock->lo_name);
557 printf(" 1st @ %s:%d\n", lock1->li_file, lock1->li_line);
558 printf(" 2nd @ %s:%d\n", file, line);
559#ifdef DDB
560 go_into_ddb = 1;
561#endif /* DDB */
562 goto out;
563 }
564 MPASS(!mtx_owned(&w_mtx));
565 mtx_lock_spin(&w_mtx);
566 /*
567 * If we have a known higher number just say ok
568 */
569 if (witness_watch > 1 && w->w_level > w1->w_level) {
570 mtx_unlock_spin(&w_mtx);
571 goto out;
572 }
573 if (isitmydescendant(w1, w)) {
574 mtx_unlock_spin(&w_mtx);
575 goto out;
576 }
577 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
578 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
579
580 MPASS(j < WITNESS_COUNT);
581 lock1 = &lle->ll_children[i];
582 w1 = lock1->li_lock->lo_witness;
583
584 /*
585 * If this lock doesn't undergo witness checking,
586 * then skip it.
587 */
588 if (w1 == NULL) {
589 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
590 ("lock missing witness structure"));
591 continue;
592 }
593 /*
594 * If we are locking Giant and we slept with this
595 * lock, then skip it.
596 */
597 if ((lock1->li_flags & LI_SLEPT) != 0 &&
598 lock == &Giant.mtx_object)
599 continue;
600 if (!isitmydescendant(w, w1))
601 continue;
602 /*
603 * We have a lock order violation, check to see if it
604 * is allowed or has already been yelled about.
605 */
606 mtx_unlock_spin(&w_mtx);
607 if (blessed(w, w1))
608 goto out;
609 if (lock1->li_lock == &Giant.mtx_object) {
610 if (w1->w_Giant_squawked)
611 goto out;
612 else
613 w1->w_Giant_squawked = 1;
614 } else {
615 if (w1->w_other_squawked)
616 goto out;
617 else
618 w1->w_other_squawked = 1;
619 }
620 /*
621 * Ok, yell about it.
622 */
623 printf("lock order reversal\n");
624 /*
625 * Try to locate an earlier lock with
626 * witness w in our list.
627 */
628 do {
629 lock2 = &lle->ll_children[i];
630 MPASS(lock2->li_lock != NULL);
631 if (lock2->li_lock->lo_witness == w)
632 break;
633 i--;
634 if (i == 0 && lle->ll_next != NULL) {
635 lle = lle->ll_next;
636 i = lle->ll_count - 1;
637 MPASS(i != 0);
638 }
639 } while (i >= 0);
640 if (i < 0) {
641 printf(" 1st %p %s @ %s:%d\n", lock1->li_lock,
642 lock1->li_lock->lo_name, lock1->li_file,
643 lock1->li_line);
644 printf(" 2nd %p %s @ %s:%d\n", lock,
645 lock->lo_name, file, line);
646 } else {
647 printf(" 1st %p %s @ %s:%d\n", lock2->li_lock,
648 lock2->li_lock->lo_name, lock2->li_file,
649 lock2->li_line);
650 printf(" 2nd %p %s @ %s:%d\n", lock1->li_lock,
651 lock1->li_lock->lo_name, lock1->li_file,
652 lock1->li_line);
653 printf(" 3rd %p %s @ %s:%d\n", lock,
654 lock->lo_name, file, line);
655 }
656#ifdef DDB
657 go_into_ddb = 1;
658#endif /* DDB */
659 goto out;
660 }
661 }
662 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
663 /*
664 * Don't build a new relationship if we are locking Giant just
665 * after waking up and the previous lock in the list was acquired
666 * prior to blocking.
667 */
668 if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0)
669 mtx_unlock_spin(&w_mtx);
670 else {
671 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
672 lock->lo_name, lock1->li_lock->lo_name);
673 if (!itismychild(lock1->li_lock->lo_witness, w))
674 mtx_unlock_spin(&w_mtx);
675 }
676
677out:
678#ifdef DDB
679 if (witness_ddb && go_into_ddb)
680 Debugger(__func__);
681#endif /* DDB */
682 w->w_file = file;
683 w->w_line = line;
684
685 lle = *lock_list;
686 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
687 lle = witness_lock_list_get();
688 if (lle == NULL)
689 return;
690 lle->ll_next = *lock_list;
691 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
692 td->td_proc->p_pid, lle);
693 *lock_list = lle;
694 }
695 lock1 = &lle->ll_children[lle->ll_count++];
696 lock1->li_lock = lock;
697 lock1->li_line = line;
698 lock1->li_file = file;
699 if ((flags & LOP_EXCLUSIVE) != 0)
700 lock1->li_flags = LI_EXCLUSIVE;
701 else
702 lock1->li_flags = 0;
703 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
704 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
705}
706
707void
708witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
709{
710 struct lock_instance *instance;
711 struct lock_class *class;
712
713 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
714 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
715 return;
716 class = lock->lo_class;
717 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
718 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
719 class->lc_name, lock->lo_name, file, line);
720 if ((flags & LOP_TRYLOCK) == 0)
721 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
722 lock->lo_name, file, line);
723 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
724 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
725 class->lc_name, lock->lo_name, file, line);
726 instance = find_instance(curthread->td_sleeplocks, lock);
727 if (instance == NULL)
728 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
729 class->lc_name, lock->lo_name, file, line);
730 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
731 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
732 class->lc_name, lock->lo_name, file, line);
733 if ((instance->li_flags & LI_RECURSEMASK) != 0)
734 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
735 class->lc_name, lock->lo_name,
736 instance->li_flags & LI_RECURSEMASK, file, line);
737 instance->li_flags |= LI_EXCLUSIVE;
738}
739
740void
741witness_downgrade(struct lock_object *lock, int flags, const char *file,
742 int line)
743{
744 struct lock_instance *instance;
745 struct lock_class *class;
746
747 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
748 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
749 return;
750 class = lock->lo_class;
751 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
752 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
753 class->lc_name, lock->lo_name, file, line);
754 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
755 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
756 class->lc_name, lock->lo_name, file, line);
757 instance = find_instance(curthread->td_sleeplocks, lock);
758 if (instance == NULL)
759 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
760 class->lc_name, lock->lo_name, file, line);
761 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
762 panic("downgrade of shared lock (%s) %s @ %s:%d",
763 class->lc_name, lock->lo_name, file, line);
764 if ((instance->li_flags & LI_RECURSEMASK) != 0)
765 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
766 class->lc_name, lock->lo_name,
767 instance->li_flags & LI_RECURSEMASK, file, line);
768 instance->li_flags &= ~LI_EXCLUSIVE;
769}
770
771void
772witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
773{
774 struct lock_list_entry **lock_list, *lle;
775 struct lock_instance *instance;
776 struct lock_class *class;
777 struct thread *td;
778 critical_t s;
779 int i, j;
780
781 if (witness_cold || witness_dead || lock->lo_witness == NULL ||
782 panicstr != NULL)
783 return;
784 td = curthread;
785 class = lock->lo_class;
786 if (class->lc_flags & LC_SLEEPLOCK)
787 lock_list = &td->td_sleeplocks;
788 else
789 lock_list = PCPU_PTR(spinlocks);
790 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
791 for (i = 0; i < (*lock_list)->ll_count; i++) {
792 instance = &(*lock_list)->ll_children[i];
793 if (instance->li_lock == lock) {
794 if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
795 (flags & LOP_EXCLUSIVE) == 0) {
796 printf(
797 "shared unlock of (%s) %s @ %s:%d\n",
798 class->lc_name, lock->lo_name,
799 file, line);
800 printf(
801 "while exclusively locked from %s:%d\n",
802 instance->li_file,
803 instance->li_line);
804 panic("excl->ushare");
805 }
806 if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
807 (flags & LOP_EXCLUSIVE) != 0) {
808 printf(
809 "exclusive unlock of (%s) %s @ %s:%d\n",
810 class->lc_name, lock->lo_name,
811 file, line);
812 printf(
813 "while share locked from %s:%d\n",
814 instance->li_file,
815 instance->li_line);
816 panic("share->uexcl");
817 }
818 /* If we are recursed, unrecurse. */
819 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
820 CTR4(KTR_WITNESS,
821 "%s: pid %d unrecursed on %s r=%d", __func__,
822 td->td_proc->p_pid,
823 instance->li_lock->lo_name,
824 instance->li_flags);
825 instance->li_flags--;
826 return;
827 }
828 s = cpu_critical_enter();
829 CTR4(KTR_WITNESS,
830 "%s: pid %d removed %s from lle[%d]", __func__,
831 td->td_proc->p_pid,
832 instance->li_lock->lo_name,
833 (*lock_list)->ll_count - 1);
834 (*lock_list)->ll_count--;
835 for (j = i; j < (*lock_list)->ll_count; j++)
836 (*lock_list)->ll_children[j] =
837 (*lock_list)->ll_children[j + 1];
838 cpu_critical_exit(s);
839 if ((*lock_list)->ll_count == 0) {
840 lle = *lock_list;
841 *lock_list = lle->ll_next;
842 CTR3(KTR_WITNESS,
843 "%s: pid %d removed lle %p", __func__,
844 td->td_proc->p_pid, lle);
845 witness_lock_list_free(lle);
846 }
847 return;
848 }
849 }
850 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
851 file, line);
852}
853
854/*
855 * Warn if any held locks are not sleepable. Note that Giant and the lock
856 * passed in are both special cases since they are both released during the
857 * sleep process and aren't actually held while the thread is asleep.
858 */
859int
860witness_sleep(int check_only, struct lock_object *lock, const char *file,
861 int line)
862{
863 struct lock_list_entry **lock_list, *lle;
864 struct lock_instance *lock1;
865 struct thread *td;
866 critical_t savecrit;
867 int i, n;
868
869 if (witness_dead || panicstr != NULL)
870 return (0);
871 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
872 n = 0;
873 /*
874 * Preemption bad because we need PCPU_PTR(spinlocks) to not change.
875 */
876 savecrit = cpu_critical_enter();
877 td = curthread;
878 lock_list = &td->td_sleeplocks;
879again:
880 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
881 for (i = lle->ll_count - 1; i >= 0; i--) {
882 lock1 = &lle->ll_children[i];
883 if (lock1->li_lock == lock ||
884 lock1->li_lock == &Giant.mtx_object)
885 continue;
886 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) {
887 if (check_only == 0) {
888 CTR3(KTR_WITNESS,
889 "pid %d: sleeping with lock (%s) %s held",
890 td->td_proc->p_pid,
891 lock1->li_lock->lo_class->lc_name,
892 lock1->li_lock->lo_name);
893 lock1->li_flags |= LI_SLEPT;
894 }
895 continue;
896 }
897 n++;
898 printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
899 file, line, check_only ? "could sleep" : "sleeping",
900 lock1->li_lock->lo_name, lock1->li_file,
901 lock1->li_line);
902 }
903 if (lock_list == &td->td_sleeplocks) {
904 lock_list = PCPU_PTR(spinlocks);
905 goto again;
906 }
907#ifdef DDB
908 if (witness_ddb && n)
909 Debugger(__func__);
910#endif /* DDB */
911 cpu_critical_exit(savecrit);
912 return (n);
913}
914
915static struct witness *
916enroll(const char *description, struct lock_class *lock_class)
917{
918 struct witness *w;
919
920 if (!witness_watch || witness_dead || panicstr != NULL)
921 return (NULL);
922 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
923 return (NULL);
924 mtx_lock_spin(&w_mtx);
925 STAILQ_FOREACH(w, &w_all, w_list) {
926 if (strcmp(description, w->w_name) == 0) {
927 w->w_refcount++;
928 mtx_unlock_spin(&w_mtx);
929 if (lock_class != w->w_class)
930 panic(
931 "lock (%s) %s does not match earlier (%s) lock",
932 description, lock_class->lc_name,
933 w->w_class->lc_name);
934 return (w);
935 }
936 }
937 /*
938 * This isn't quite right, as witness_cold is still 0 while we
939 * enroll all the locks initialized before witness_initialize().
940 */
941 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
942 mtx_unlock_spin(&w_mtx);
943 panic("spin lock %s not in order list", description);
944 }
945 if ((w = witness_get()) == NULL)
946 return (NULL);
947 w->w_name = description;
948 w->w_class = lock_class;
949 w->w_refcount = 1;
950 STAILQ_INSERT_HEAD(&w_all, w, w_list);
951 if (lock_class->lc_flags & LC_SPINLOCK)
952 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
953 else if (lock_class->lc_flags & LC_SLEEPLOCK)
954 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
955 else {
956 mtx_unlock_spin(&w_mtx);
957 panic("lock class %s is not sleep or spin",
958 lock_class->lc_name);
959 }
960 mtx_unlock_spin(&w_mtx);
961 return (w);
962}
963
964static int
965itismychild(struct witness *parent, struct witness *child)
966{
967 static int recursed;
968 struct witness_child_list_entry **wcl;
969 struct witness_list *list;
970
971 MPASS(child != NULL && parent != NULL);
972 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
973 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
974 panic(
975 "%s: parent (%s) and child (%s) are not the same lock type",
976 __func__, parent->w_class->lc_name,
977 child->w_class->lc_name);
978
979 /*
980 * Insert "child" after "parent"
981 */
982 wcl = &parent->w_children;
983 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
984 wcl = &(*wcl)->wcl_next;
985 if (*wcl == NULL) {
986 *wcl = witness_child_get();
987 if (*wcl == NULL)
988 return (1);
989 }
990 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
991
992 /*
993 * Now prune whole tree. We look for cases where a lock is now
994 * both a descendant and a direct child of a given lock. In that
995 * case, we want to remove the direct child link from the tree.
996 */
997 if (recursed)
998 return (0);
999 recursed = 1;
1000 if (parent->w_class->lc_flags & LC_SLEEPLOCK)
1001 list = &w_sleep;
1002 else
1003 list = &w_spin;
1004 STAILQ_FOREACH(child, list, w_typelist) {
1005 STAILQ_FOREACH(parent, list, w_typelist) {
1006 if (!isitmychild(parent, child))
1007 continue;
1008 removechild(parent, child);
1009 if (isitmydescendant(parent, child))
1010 continue;
1011 itismychild(parent, child);
1012 }
1013 }
1014 recursed = 0;
1015 witness_levelall();
1016 return (0);
1017}
1018
1019static void
1020removechild(struct witness *parent, struct witness *child)
1021{
1022 struct witness_child_list_entry **wcl, *wcl1;
1023 int i;
1024
1025 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1026 for (i = 0; i < (*wcl)->wcl_count; i++)
1027 if ((*wcl)->wcl_children[i] == child)
1028 goto found;
1029 return;
1030found:
1031 (*wcl)->wcl_count--;
1032 if ((*wcl)->wcl_count > i)
1033 (*wcl)->wcl_children[i] =
1034 (*wcl)->wcl_children[(*wcl)->wcl_count];
1035 MPASS((*wcl)->wcl_children[i] != NULL);
1036 if ((*wcl)->wcl_count != 0)
1037 return;
1038 wcl1 = *wcl;
1039 *wcl = wcl1->wcl_next;
1040 witness_child_free(wcl1);
1041}
1042
1043static int
1044isitmychild(struct witness *parent, struct witness *child)
1045{
1046 struct witness_child_list_entry *wcl;
1047 int i;
1048
1049 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1050 for (i = 0; i < wcl->wcl_count; i++) {
1051 if (wcl->wcl_children[i] == child)
1052 return (1);
1053 }
1054 }
1055 return (0);
1056}
1057
1058static int
1059isitmydescendant(struct witness *parent, struct witness *child)
1060{
1061 struct witness_child_list_entry *wcl;
1062 int i, j;
1063
1064 if (isitmychild(parent, child))
1065 return (1);
1066 j = 0;
1067 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1068 MPASS(j < 1000);
1069 for (i = 0; i < wcl->wcl_count; i++) {
1070 if (isitmydescendant(wcl->wcl_children[i], child))
1071 return (1);
1072 }
1073 j++;
1074 }
1075 return (0);
1076}
1077
1078void
1079witness_levelall (void)
1080{
1081 struct witness_list *list;
1082 struct witness *w, *w1;
1083
1084 /*
1085 * First clear all levels.
1086 */
1087 STAILQ_FOREACH(w, &w_all, w_list) {
1088 w->w_level = 0;
1089 }
1090
1091 /*
1092 * Look for locks with no parent and level all their descendants.
1093 */
1094 STAILQ_FOREACH(w, &w_all, w_list) {
1095 /*
1096 * This is just an optimization, technically we could get
1097 * away just walking the all list each time.
1098 */
1099 if (w->w_class->lc_flags & LC_SLEEPLOCK)
1100 list = &w_sleep;
1101 else
1102 list = &w_spin;
1103 STAILQ_FOREACH(w1, list, w_typelist) {
1104 if (isitmychild(w1, w))
1105 goto skip;
1106 }
1107 witness_leveldescendents(w, 0);
1108 skip:
1109 }
1110}
1111
1112static void
1113witness_leveldescendents(struct witness *parent, int level)
1114{
1115 struct witness_child_list_entry *wcl;
1116 int i;
1117
1118 if (parent->w_level < level)
1119 parent->w_level = level;
1120 level++;
1121 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1122 for (i = 0; i < wcl->wcl_count; i++)
1123 witness_leveldescendents(wcl->wcl_children[i], level);
1124}
1125
1126static void
1127witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1128 struct witness *parent)
1129{
1130 struct witness_child_list_entry *wcl;
1131 int i, level;
1132
1133 level = parent->w_level;
1134 prnt("%-2d", level);
1135 for (i = 0; i < level; i++)
1136 prnt(" ");
1137 prnt("%s", parent->w_name);
1138 if (parent->w_file != NULL)
1139 prnt(" -- last acquired @ %s:%d\n", parent->w_file,
1140 parent->w_line);
1141 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1142 for (i = 0; i < wcl->wcl_count; i++)
1143 witness_displaydescendants(prnt,
1144 wcl->wcl_children[i]);
1145}
1146
1147static int
1148dup_ok(struct witness *w)
1149{
1150 const char **dup;
1151
1152 for (dup = dup_list; *dup != NULL; dup++)
1153 if (strcmp(w->w_name, *dup) == 0)
1154 return (1);
1155 return (0);
1156}
1157
1158static int
1159blessed(struct witness *w1, struct witness *w2)
1160{
1161 int i;
1162 struct witness_blessed *b;
1163
1164 for (i = 0; i < blessed_count; i++) {
1165 b = &blessed_list[i];
1166 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1167 if (strcmp(w2->w_name, b->b_lock2) == 0)
1168 return (1);
1169 continue;
1170 }
1171 if (strcmp(w1->w_name, b->b_lock2) == 0)
1172 if (strcmp(w2->w_name, b->b_lock1) == 0)
1173 return (1);
1174 }
1175 return (0);
1176}
1177
1178static struct witness *
1179witness_get(void)
1180{
1181 struct witness *w;
1182
1183 if (witness_dead) {
1184 mtx_unlock_spin(&w_mtx);
1185 return (NULL);
1186 }
1187 if (STAILQ_EMPTY(&w_free)) {
1188 witness_dead = 1;
1189 mtx_unlock_spin(&w_mtx);
1190 printf("%s: witness exhausted\n", __func__);
1191 return (NULL);
1192 }
1193 w = STAILQ_FIRST(&w_free);
1194 STAILQ_REMOVE_HEAD(&w_free, w_list);
1195 bzero(w, sizeof(*w));
1196 return (w);
1197}
1198
1199static void
1200witness_free(struct witness *w)
1201{
1202
1203 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1204}
1205
1206static struct witness_child_list_entry *
1207witness_child_get(void)
1208{
1209 struct witness_child_list_entry *wcl;
1210
1211 if (witness_dead) {
1212 mtx_unlock_spin(&w_mtx);
1213 return (NULL);
1214 }
1215 wcl = w_child_free;
1216 if (wcl == NULL) {
1217 witness_dead = 1;
1218 mtx_unlock_spin(&w_mtx);
1219 printf("%s: witness exhausted\n", __func__);
1220 return (NULL);
1221 }
1222 w_child_free = wcl->wcl_next;
1223 bzero(wcl, sizeof(*wcl));
1224 return (wcl);
1225}
1226
1227static void
1228witness_child_free(struct witness_child_list_entry *wcl)
1229{
1230
1231 wcl->wcl_next = w_child_free;
1232 w_child_free = wcl;
1233}
1234
1235static struct lock_list_entry *
1236witness_lock_list_get(void)
1237{
1238 struct lock_list_entry *lle;
1239
1240 if (witness_dead)
1241 return (NULL);
1242 mtx_lock_spin(&w_mtx);
1243 lle = w_lock_list_free;
1244 if (lle == NULL) {
1245 witness_dead = 1;
1246 mtx_unlock_spin(&w_mtx);
1247 printf("%s: witness exhausted\n", __func__);
1248 return (NULL);
1249 }
1250 w_lock_list_free = lle->ll_next;
1251 mtx_unlock_spin(&w_mtx);
1252 bzero(lle, sizeof(*lle));
1253 return (lle);
1254}
1255
1256static void
1257witness_lock_list_free(struct lock_list_entry *lle)
1258{
1259
1260 mtx_lock_spin(&w_mtx);
1261 lle->ll_next = w_lock_list_free;
1262 w_lock_list_free = lle;
1263 mtx_unlock_spin(&w_mtx);
1264}
1265
1266static struct lock_instance *
1267find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1268{
1269 struct lock_list_entry *lle;
1270 struct lock_instance *instance;
1271 int i;
1272
1273 for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1274 for (i = lle->ll_count - 1; i >= 0; i--) {
1275 instance = &lle->ll_children[i];
1276 if (instance->li_lock == lock)
1277 return (instance);
1278 }
1279 return (NULL);
1280}
1281
1282int
1283witness_list_locks(struct lock_list_entry **lock_list)
1284{
1285 struct lock_list_entry *lle;
1286 struct lock_instance *instance;
1287 struct lock_object *lock;
1288 int i, nheld;
1289
1290 nheld = 0;
1291 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1292 for (i = lle->ll_count - 1; i >= 0; i--) {
1293 instance = &lle->ll_children[i];
1294 lock = instance->li_lock;
1295 printf("%s (%s) %s (%p) locked @ %s:%d\n",
1296 (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1297 "exclusive" : "shared",
1298 lock->lo_class->lc_name, lock->lo_name, lock,
1299 instance->li_file, instance->li_line);
1300 nheld++;
1301 }
1302 return (nheld);
1303}
1304
1305/*
1306 * Calling this on td != curthread is bad unless we are in ddb.
1307 */
1308int
1309witness_list(struct thread *td)
1310{
1311 critical_t savecrit;
1312 int nheld;
1313
1314 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1315#ifdef DDB
1316 KASSERT(td == curthread || db_active,
1317 ("%s: td != curthread and we aren't in the debugger", __func__));
1318 if (!db_active && witness_dead)
1319 return (0);
1320#else
1321 KASSERT(td == curthread, ("%s: p != curthread", __func__));
1322 if (witness_dead)
1323 return (0);
1324#endif
1325 nheld = witness_list_locks(&td->td_sleeplocks);
1326
1327 /*
1328 * We only handle spinlocks if td == curthread. This is somewhat broken
1329 * if td is currently executing on some other CPU and holds spin locks
1330 * as we won't display those locks. If we had a MI way of getting
1331 * the per-cpu data for a given cpu then we could use
1332 * td->td_kse->ke_oncpu to get the list of spinlocks for this thread
1333 * and "fix" this.
1334 */
1335 if (td == curthread) {
1336 /*
1337 * Preemption bad because we need PCPU_PTR(spinlocks) to not
1338 * change.
1339 */
1340 savecrit = cpu_critical_enter();
1341 nheld += witness_list_locks(PCPU_PTR(spinlocks));
1342 cpu_critical_exit(savecrit);
1343 }
1344 return (nheld);
1345}
1346
1347void
1348witness_save(struct lock_object *lock, const char **filep, int *linep)
1349{
1350 struct lock_instance *instance;
1351
1352 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1353 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1354 return;
1355 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1356 panic("%s: lock (%s) %s is not a sleep lock", __func__,
1357 lock->lo_class->lc_name, lock->lo_name);
1358 instance = find_instance(curthread->td_sleeplocks, lock);
1359 if (instance == NULL)
1360 panic("%s: lock (%s) %s not locked", __func__,
1361 lock->lo_class->lc_name, lock->lo_name);
1362 *filep = instance->li_file;
1363 *linep = instance->li_line;
1364}
1365
1366void
1367witness_restore(struct lock_object *lock, const char *file, int line)
1368{
1369 struct lock_instance *instance;
1370
1371 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1372 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1373 return;
1374 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1375 panic("%s: lock (%s) %s is not a sleep lock", __func__,
1376 lock->lo_class->lc_name, lock->lo_name);
1377 instance = find_instance(curthread->td_sleeplocks, lock);
1378 if (instance == NULL)
1379 panic("%s: lock (%s) %s not locked", __func__,
1380 lock->lo_class->lc_name, lock->lo_name);
1381 lock->lo_witness->w_file = file;
1382 lock->lo_witness->w_line = line;
1383 instance->li_file = file;
1384 instance->li_line = line;
1385}
1386
1387void
1388witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1389{
1390#ifdef INVARIANT_SUPPORT
1391 struct lock_instance *instance;
1392
1393 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1394 return;
1395 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
1396 instance = find_instance(curthread->td_sleeplocks, lock);
1397 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
1398 instance = find_instance(PCPU_GET(spinlocks), lock);
1399 else {
1400 panic("Lock (%s) %s is not sleep or spin!",
1401 lock->lo_class->lc_name, lock->lo_name);
1402 return;
1403 }
1404 switch (flags) {
1405 case LA_UNLOCKED:
1406 if (instance != NULL)
1407 panic("Lock (%s) %s locked @ %s:%d.",
1408 lock->lo_class->lc_name, lock->lo_name, file, line);
1409 break;
1410 case LA_LOCKED:
1411 case LA_LOCKED | LA_RECURSED:
1412 case LA_LOCKED | LA_NOTRECURSED:
1413 case LA_SLOCKED:
1414 case LA_SLOCKED | LA_RECURSED:
1415 case LA_SLOCKED | LA_NOTRECURSED:
1416 case LA_XLOCKED:
1417 case LA_XLOCKED | LA_RECURSED:
1418 case LA_XLOCKED | LA_NOTRECURSED:
1419 if (instance == NULL) {
1420 panic("Lock (%s) %s not locked @ %s:%d.",
1421 lock->lo_class->lc_name, lock->lo_name, file, line);
1422 break;
1423 }
1424 if ((flags & LA_XLOCKED) != 0 &&
1425 (instance->li_flags & LI_EXCLUSIVE) == 0)
1426 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1427 lock->lo_class->lc_name, lock->lo_name, file, line);
1428 if ((flags & LA_SLOCKED) != 0 &&
1429 (instance->li_flags & LI_EXCLUSIVE) != 0)
1430 panic("Lock (%s) %s exclusively locked @ %s:%d.",
1431 lock->lo_class->lc_name, lock->lo_name, file, line);
1432 if ((flags & LA_RECURSED) != 0 &&
1433 (instance->li_flags & LI_RECURSEMASK) == 0)
1434 panic("Lock (%s) %s not recursed @ %s:%d.",
1435 lock->lo_class->lc_name, lock->lo_name, file, line);
1436 if ((flags & LA_NOTRECURSED) != 0 &&
1437 (instance->li_flags & LI_RECURSEMASK) != 0)
1438 panic("Lock (%s) %s recursed @ %s:%d.",
1439 lock->lo_class->lc_name, lock->lo_name, file, line);
1440 break;
1441 default:
1442 panic("Invalid lock assertion at %s:%d.", file, line);
1443
1444 }
1445#endif /* INVARIANT_SUPPORT */
1446}
1447
1448#ifdef DDB
1449
1450DB_SHOW_COMMAND(locks, db_witness_list)
1451{
1452 struct thread *td;
1453 pid_t pid;
1454 struct proc *p;
1455
1456 if (have_addr) {
1457 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1458 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1459 ((addr >> 16) % 16) * 10000;
1460 /* sx_slock(&allproc_lock); */
1461 FOREACH_PROC_IN_SYSTEM(p) {
1462 if (p->p_pid == pid)
1463 break;
1464 }
1465 /* sx_sunlock(&allproc_lock); */
1466 if (p == NULL) {
1467 db_printf("pid %d not found\n", pid);
1468 return;
1469 }
1470 FOREACH_THREAD_IN_PROC(p, td) {
1471 witness_list(td);
1472 }
1473 } else {
1474 td = curthread;
1475 witness_list(td);
1476 }
1477}
1478
1479DB_SHOW_COMMAND(witness, db_witness_display)
1480{
1481
1482 witness_display(db_printf);
1483}
1484#endif