subr_lockdebug.c revision 1.57.2.2 1 /* $NetBSD: subr_lockdebug.c,v 1.57.2.2 2018/09/07 12:34:18 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Basic lock debugging code shared among lock primitives.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.57.2.2 2018/09/07 12:34:18 martin Exp $");
38
39 #ifdef _KERNEL_OPT
40 #include "opt_ddb.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/kmem.h>
48 #include <sys/lockdebug.h>
49 #include <sys/sleepq.h>
50 #include <sys/cpu.h>
51 #include <sys/atomic.h>
52 #include <sys/lock.h>
53 #include <sys/rbtree.h>
54 #include <sys/ksyms.h>
55
56 #include <machine/lock.h>
57
58 unsigned int ld_panic;
59
60 #ifdef LOCKDEBUG
61
62 #define LD_BATCH_SHIFT 9
63 #define LD_BATCH (1 << LD_BATCH_SHIFT)
64 #define LD_BATCH_MASK (LD_BATCH - 1)
65 #define LD_MAX_LOCKS 1048576
66 #define LD_SLOP 16
67
68 #define LD_LOCKED 0x01
69 #define LD_SLEEPER 0x02
70
71 #define LD_WRITE_LOCK 0x80000000
72
73 typedef struct lockdebug {
74 struct rb_node ld_rb_node;
75 __cpu_simple_lock_t ld_spinlock;
76 _TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
77 _TAILQ_ENTRY(struct lockdebug, volatile) ld_achain;
78 volatile void *ld_lock;
79 lockops_t *ld_lockops;
80 struct lwp *ld_lwp;
81 uintptr_t ld_locked;
82 uintptr_t ld_unlocked;
83 uintptr_t ld_initaddr;
84 uint16_t ld_shares;
85 uint16_t ld_cpu;
86 uint8_t ld_flags;
87 uint8_t ld_shwant; /* advisory */
88 uint8_t ld_exwant; /* advisory */
89 uint8_t ld_unused;
90 } volatile lockdebug_t;
91
92 typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
93
94 __cpu_simple_lock_t ld_mod_lk;
95 lockdebuglist_t ld_free = TAILQ_HEAD_INITIALIZER(ld_free);
96 lockdebuglist_t ld_all = TAILQ_HEAD_INITIALIZER(ld_all);
97 int ld_nfree;
98 int ld_freeptr;
99 int ld_recurse;
100 bool ld_nomore;
101 lockdebug_t ld_prime[LD_BATCH];
102
103 static void lockdebug_abort1(const char *, size_t, lockdebug_t *, int,
104 const char *, bool);
105 static int lockdebug_more(int);
106 static void lockdebug_init(void);
107 static void lockdebug_dump(lockdebug_t *, void (*)(const char *, ...)
108 __printflike(1, 2));
109
110 static signed int
111 ld_rbto_compare_nodes(void *ctx, const void *n1, const void *n2)
112 {
113 const lockdebug_t *ld1 = n1;
114 const lockdebug_t *ld2 = n2;
115 const uintptr_t a = (uintptr_t)ld1->ld_lock;
116 const uintptr_t b = (uintptr_t)ld2->ld_lock;
117
118 if (a < b)
119 return -1;
120 if (a > b)
121 return 1;
122 return 0;
123 }
124
125 static signed int
126 ld_rbto_compare_key(void *ctx, const void *n, const void *key)
127 {
128 const lockdebug_t *ld = n;
129 const uintptr_t a = (uintptr_t)ld->ld_lock;
130 const uintptr_t b = (uintptr_t)key;
131
132 if (a < b)
133 return -1;
134 if (a > b)
135 return 1;
136 return 0;
137 }
138
139 static rb_tree_t ld_rb_tree;
140
141 static const rb_tree_ops_t ld_rb_tree_ops = {
142 .rbto_compare_nodes = ld_rbto_compare_nodes,
143 .rbto_compare_key = ld_rbto_compare_key,
144 .rbto_node_offset = offsetof(lockdebug_t, ld_rb_node),
145 .rbto_context = NULL
146 };
147
148 static inline lockdebug_t *
149 lockdebug_lookup1(volatile void *lock)
150 {
151 lockdebug_t *ld;
152 struct cpu_info *ci;
153
154 ci = curcpu();
155 __cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
156 ld = (lockdebug_t *)rb_tree_find_node(&ld_rb_tree, __UNVOLATILE(lock));
157 __cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
158 if (ld == NULL) {
159 return NULL;
160 }
161 __cpu_simple_lock(&ld->ld_spinlock);
162
163 return ld;
164 }
165
166 static void
167 lockdebug_lock_cpus(void)
168 {
169 CPU_INFO_ITERATOR cii;
170 struct cpu_info *ci;
171
172 for (CPU_INFO_FOREACH(cii, ci)) {
173 __cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
174 }
175 }
176
177 static void
178 lockdebug_unlock_cpus(void)
179 {
180 CPU_INFO_ITERATOR cii;
181 struct cpu_info *ci;
182
183 for (CPU_INFO_FOREACH(cii, ci)) {
184 __cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
185 }
186 }
187
188 /*
189 * lockdebug_lookup:
190 *
191 * Find a lockdebug structure by a pointer to a lock and return it locked.
192 */
193 static inline lockdebug_t *
194 lockdebug_lookup(const char *func, size_t line, volatile void *lock,
195 uintptr_t where)
196 {
197 lockdebug_t *ld;
198
199 ld = lockdebug_lookup1(lock);
200 if (ld == NULL) {
201 panic("%s,%zu: uninitialized lock (lock=%p, from=%08"
202 PRIxPTR ")", func, line, lock, where);
203 }
204 return ld;
205 }
206
207 /*
208 * lockdebug_init:
209 *
210 * Initialize the lockdebug system. Allocate an initial pool of
211 * lockdebug structures before the VM system is up and running.
212 */
213 static void
214 lockdebug_init(void)
215 {
216 lockdebug_t *ld;
217 int i;
218
219 TAILQ_INIT(&curcpu()->ci_data.cpu_ld_locks);
220 TAILQ_INIT(&curlwp->l_ld_locks);
221 __cpu_simple_lock_init(&curcpu()->ci_data.cpu_ld_lock);
222 __cpu_simple_lock_init(&ld_mod_lk);
223
224 rb_tree_init(&ld_rb_tree, &ld_rb_tree_ops);
225
226 ld = ld_prime;
227 for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
228 __cpu_simple_lock_init(&ld->ld_spinlock);
229 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
230 TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
231 }
232 ld_freeptr = 1;
233 ld_nfree = LD_BATCH - 1;
234 }
235
236 /*
237 * lockdebug_alloc:
238 *
239 * A lock is being initialized, so allocate an associated debug
240 * structure.
241 */
242 bool
243 lockdebug_alloc(const char *func, size_t line, volatile void *lock,
244 lockops_t *lo, uintptr_t initaddr)
245 {
246 struct cpu_info *ci;
247 lockdebug_t *ld;
248 int s;
249
250 if (lo == NULL || panicstr != NULL || ld_panic)
251 return false;
252 if (ld_freeptr == 0)
253 lockdebug_init();
254
255 s = splhigh();
256 __cpu_simple_lock(&ld_mod_lk);
257 if ((ld = lockdebug_lookup1(lock)) != NULL) {
258 __cpu_simple_unlock(&ld_mod_lk);
259 lockdebug_abort1(func, line, ld, s, "already initialized",
260 true);
261 return false;
262 }
263
264 /*
265 * Pinch a new debug structure. We may recurse because we call
266 * kmem_alloc(), which may need to initialize new locks somewhere
267 * down the path. If not recursing, we try to maintain at least
268 * LD_SLOP structures free, which should hopefully be enough to
269 * satisfy kmem_alloc(). If we can't provide a structure, not to
270 * worry: we'll just mark the lock as not having an ID.
271 */
272 ci = curcpu();
273 ci->ci_lkdebug_recurse++;
274 if (TAILQ_EMPTY(&ld_free)) {
275 if (ci->ci_lkdebug_recurse > 1 || ld_nomore) {
276 ci->ci_lkdebug_recurse--;
277 __cpu_simple_unlock(&ld_mod_lk);
278 splx(s);
279 return false;
280 }
281 s = lockdebug_more(s);
282 } else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP) {
283 s = lockdebug_more(s);
284 }
285 if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
286 __cpu_simple_unlock(&ld_mod_lk);
287 splx(s);
288 return false;
289 }
290 TAILQ_REMOVE(&ld_free, ld, ld_chain);
291 ld_nfree--;
292 ci->ci_lkdebug_recurse--;
293
294 if (ld->ld_lock != NULL) {
295 panic("%s,%zu: corrupt table ld %p", func, line, ld);
296 }
297
298 /* Initialise the structure. */
299 ld->ld_lock = lock;
300 ld->ld_lockops = lo;
301 ld->ld_locked = 0;
302 ld->ld_unlocked = 0;
303 ld->ld_lwp = NULL;
304 ld->ld_initaddr = initaddr;
305 ld->ld_flags = (lo->lo_type == LOCKOPS_SLEEP ? LD_SLEEPER : 0);
306 lockdebug_lock_cpus();
307 (void)rb_tree_insert_node(&ld_rb_tree, __UNVOLATILE(ld));
308 lockdebug_unlock_cpus();
309 __cpu_simple_unlock(&ld_mod_lk);
310
311 splx(s);
312 return true;
313 }
314
315 /*
316 * lockdebug_free:
317 *
318 * A lock is being destroyed, so release debugging resources.
319 */
320 void
321 lockdebug_free(const char *func, size_t line, volatile void *lock)
322 {
323 lockdebug_t *ld;
324 int s;
325
326 if (panicstr != NULL || ld_panic)
327 return;
328
329 s = splhigh();
330 __cpu_simple_lock(&ld_mod_lk);
331 ld = lockdebug_lookup(func, line, lock,
332 (uintptr_t) __builtin_return_address(0));
333 if (ld == NULL) {
334 __cpu_simple_unlock(&ld_mod_lk);
335 panic("%s,%zu: destroying uninitialized object %p"
336 "(ld_lock=%p)", func, line, lock, ld->ld_lock);
337 return;
338 }
339 if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
340 __cpu_simple_unlock(&ld_mod_lk);
341 lockdebug_abort1(func, line, ld, s, "is locked or in use",
342 true);
343 return;
344 }
345 lockdebug_lock_cpus();
346 rb_tree_remove_node(&ld_rb_tree, __UNVOLATILE(ld));
347 lockdebug_unlock_cpus();
348 ld->ld_lock = NULL;
349 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
350 ld_nfree++;
351 __cpu_simple_unlock(&ld->ld_spinlock);
352 __cpu_simple_unlock(&ld_mod_lk);
353 splx(s);
354 }
355
356 /*
357 * lockdebug_more:
358 *
359 * Allocate a batch of debug structures and add to the free list.
360 * Must be called with ld_mod_lk held.
361 */
362 static int
363 lockdebug_more(int s)
364 {
365 lockdebug_t *ld;
366 void *block;
367 int i, base, m;
368
369 /*
370 * Can't call kmem_alloc() if in interrupt context. XXX We could
371 * deadlock, because we don't know which locks the caller holds.
372 */
373 if (cpu_intr_p() || (curlwp->l_pflag & LP_INTR) != 0) {
374 return s;
375 }
376
377 while (ld_nfree < LD_SLOP) {
378 __cpu_simple_unlock(&ld_mod_lk);
379 splx(s);
380 block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
381 s = splhigh();
382 __cpu_simple_lock(&ld_mod_lk);
383
384 if (ld_nfree > LD_SLOP) {
385 /* Somebody beat us to it. */
386 __cpu_simple_unlock(&ld_mod_lk);
387 splx(s);
388 kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
389 s = splhigh();
390 __cpu_simple_lock(&ld_mod_lk);
391 continue;
392 }
393
394 base = ld_freeptr;
395 ld_nfree += LD_BATCH;
396 ld = block;
397 base <<= LD_BATCH_SHIFT;
398 m = min(LD_MAX_LOCKS, base + LD_BATCH);
399
400 if (m == LD_MAX_LOCKS)
401 ld_nomore = true;
402
403 for (i = base; i < m; i++, ld++) {
404 __cpu_simple_lock_init(&ld->ld_spinlock);
405 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
406 TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
407 }
408
409 membar_producer();
410 }
411
412 return s;
413 }
414
415 /*
416 * lockdebug_wantlock:
417 *
418 * Process the preamble to a lock acquire. The "shared"
419 * parameter controls which ld_{ex,sh}want counter is
420 * updated; a negative value of shared updates neither.
421 */
422 void
423 lockdebug_wantlock(const char *func, size_t line,
424 volatile void *lock, uintptr_t where, int shared)
425 {
426 struct lwp *l = curlwp;
427 lockdebug_t *ld;
428 bool recurse;
429 int s;
430
431 (void)shared;
432 recurse = false;
433
434 if (panicstr != NULL || ld_panic)
435 return;
436
437 s = splhigh();
438 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
439 splx(s);
440 return;
441 }
442 if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
443 if ((ld->ld_flags & LD_SLEEPER) != 0) {
444 if (ld->ld_lwp == l)
445 recurse = true;
446 } else if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
447 recurse = true;
448 }
449 if (cpu_intr_p()) {
450 if ((ld->ld_flags & LD_SLEEPER) != 0) {
451 lockdebug_abort1(func, line, ld, s,
452 "acquiring sleep lock from interrupt context",
453 true);
454 return;
455 }
456 }
457 if (shared > 0)
458 ld->ld_shwant++;
459 else if (shared == 0)
460 ld->ld_exwant++;
461 if (recurse) {
462 lockdebug_abort1(func, line, ld, s, "locking against myself",
463 true);
464 return;
465 }
466 __cpu_simple_unlock(&ld->ld_spinlock);
467 splx(s);
468 }
469
470 /*
471 * lockdebug_locked:
472 *
473 * Process a lock acquire operation.
474 */
475 void
476 lockdebug_locked(const char *func, size_t line,
477 volatile void *lock, void *cvlock, uintptr_t where, int shared)
478 {
479 struct lwp *l = curlwp;
480 lockdebug_t *ld;
481 int s;
482
483 if (panicstr != NULL || ld_panic)
484 return;
485
486 s = splhigh();
487 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
488 splx(s);
489 return;
490 }
491 if (cvlock) {
492 KASSERT(ld->ld_lockops->lo_type == LOCKOPS_CV);
493 if (lock == (void *)&lbolt) {
494 /* nothing */
495 } else if (ld->ld_shares++ == 0) {
496 ld->ld_locked = (uintptr_t)cvlock;
497 } else if (cvlock != (void *)ld->ld_locked) {
498 lockdebug_abort1(func, line, ld, s,
499 "multiple locks used with condition variable",
500 true);
501 return;
502 }
503 } else if (shared) {
504 l->l_shlocks++;
505 ld->ld_locked = where;
506 ld->ld_shares++;
507 ld->ld_shwant--;
508 } else {
509 if ((ld->ld_flags & LD_LOCKED) != 0) {
510 lockdebug_abort1(func, line, ld, s, "already locked",
511 true);
512 return;
513 }
514 ld->ld_flags |= LD_LOCKED;
515 ld->ld_locked = where;
516 ld->ld_exwant--;
517 if ((ld->ld_flags & LD_SLEEPER) != 0) {
518 TAILQ_INSERT_TAIL(&l->l_ld_locks, ld, ld_chain);
519 } else {
520 TAILQ_INSERT_TAIL(&curcpu()->ci_data.cpu_ld_locks,
521 ld, ld_chain);
522 }
523 }
524 ld->ld_cpu = (uint16_t)cpu_index(curcpu());
525 ld->ld_lwp = l;
526 __cpu_simple_unlock(&ld->ld_spinlock);
527 splx(s);
528 }
529
530 /*
531 * lockdebug_unlocked:
532 *
533 * Process a lock release operation.
534 */
535 void
536 lockdebug_unlocked(const char *func, size_t line,
537 volatile void *lock, uintptr_t where, int shared)
538 {
539 struct lwp *l = curlwp;
540 lockdebug_t *ld;
541 int s;
542
543 if (panicstr != NULL || ld_panic)
544 return;
545
546 s = splhigh();
547 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
548 splx(s);
549 return;
550 }
551 if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
552 if (lock == (void *)&lbolt) {
553 /* nothing */
554 } else {
555 ld->ld_shares--;
556 }
557 } else if (shared) {
558 if (l->l_shlocks == 0) {
559 lockdebug_abort1(func, line, ld, s,
560 "no shared locks held by LWP", true);
561 return;
562 }
563 if (ld->ld_shares == 0) {
564 lockdebug_abort1(func, line, ld, s,
565 "no shared holds on this lock", true);
566 return;
567 }
568 l->l_shlocks--;
569 ld->ld_shares--;
570 if (ld->ld_lwp == l) {
571 ld->ld_unlocked = where;
572 ld->ld_lwp = NULL;
573 }
574 if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
575 ld->ld_cpu = (uint16_t)-1;
576 } else {
577 if ((ld->ld_flags & LD_LOCKED) == 0) {
578 lockdebug_abort1(func, line, ld, s, "not locked", true);
579 return;
580 }
581
582 if ((ld->ld_flags & LD_SLEEPER) != 0) {
583 if (ld->ld_lwp != curlwp) {
584 lockdebug_abort1(func, line, ld, s,
585 "not held by current LWP", true);
586 return;
587 }
588 TAILQ_REMOVE(&l->l_ld_locks, ld, ld_chain);
589 } else {
590 if (ld->ld_cpu != (uint16_t)cpu_index(curcpu())) {
591 lockdebug_abort1(func, line, ld, s,
592 "not held by current CPU", true);
593 return;
594 }
595 TAILQ_REMOVE(&curcpu()->ci_data.cpu_ld_locks, ld,
596 ld_chain);
597 }
598 ld->ld_flags &= ~LD_LOCKED;
599 ld->ld_unlocked = where;
600 ld->ld_lwp = NULL;
601 }
602 __cpu_simple_unlock(&ld->ld_spinlock);
603 splx(s);
604 }
605
606 /*
607 * lockdebug_wakeup:
608 *
609 * Process a wakeup on a condition variable.
610 */
611 void
612 lockdebug_wakeup(const char *func, size_t line, volatile void *lock,
613 uintptr_t where)
614 {
615 lockdebug_t *ld;
616 int s;
617
618 if (panicstr != NULL || ld_panic || lock == (void *)&lbolt)
619 return;
620
621 s = splhigh();
622 /* Find the CV... */
623 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
624 splx(s);
625 return;
626 }
627 /*
628 * If it has any waiters, ensure that they are using the
629 * same interlock.
630 */
631 if (ld->ld_shares != 0 && !mutex_owned((kmutex_t *)ld->ld_locked)) {
632 lockdebug_abort1(func, line, ld, s, "interlocking mutex not "
633 "held during wakeup", true);
634 return;
635 }
636 __cpu_simple_unlock(&ld->ld_spinlock);
637 splx(s);
638 }
639
640 /*
641 * lockdebug_barrier:
642 *
643 * Panic if we hold more than one specified spin lock, and optionally,
644 * if we hold sleep locks.
645 */
646 void
647 lockdebug_barrier(const char *func, size_t line, volatile void *spinlock,
648 int slplocks)
649 {
650 struct lwp *l = curlwp;
651 lockdebug_t *ld;
652 int s;
653
654 if (panicstr != NULL || ld_panic)
655 return;
656
657 s = splhigh();
658 if ((l->l_pflag & LP_INTR) == 0) {
659 TAILQ_FOREACH(ld, &curcpu()->ci_data.cpu_ld_locks, ld_chain) {
660 if (ld->ld_lock == spinlock) {
661 continue;
662 }
663 __cpu_simple_lock(&ld->ld_spinlock);
664 lockdebug_abort1(func, line, ld, s,
665 "spin lock held", true);
666 return;
667 }
668 }
669 if (slplocks) {
670 splx(s);
671 return;
672 }
673 if ((ld = TAILQ_FIRST(&l->l_ld_locks)) != NULL) {
674 __cpu_simple_lock(&ld->ld_spinlock);
675 lockdebug_abort1(func, line, ld, s, "sleep lock held", true);
676 return;
677 }
678 splx(s);
679 if (l->l_shlocks != 0) {
680 TAILQ_FOREACH(ld, &ld_all, ld_achain) {
681 if (ld->ld_lockops->lo_type == LOCKOPS_CV)
682 continue;
683 if (ld->ld_lwp == l)
684 lockdebug_dump(ld, printf);
685 }
686 panic("%s,%zu: holding %d shared locks", func, line,
687 l->l_shlocks);
688 }
689 }
690
691 /*
692 * lockdebug_mem_check:
693 *
694 * Check for in-use locks within a memory region that is
695 * being freed.
696 */
697 void
698 lockdebug_mem_check(const char *func, size_t line, void *base, size_t sz)
699 {
700 lockdebug_t *ld;
701 struct cpu_info *ci;
702 int s;
703
704 if (panicstr != NULL || ld_panic)
705 return;
706
707 s = splhigh();
708 ci = curcpu();
709 __cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
710 ld = (lockdebug_t *)rb_tree_find_node_geq(&ld_rb_tree, base);
711 if (ld != NULL) {
712 const uintptr_t lock = (uintptr_t)ld->ld_lock;
713
714 if ((uintptr_t)base > lock)
715 panic("%s,%zu: corrupt tree ld=%p, base=%p, sz=%zu",
716 func, line, ld, base, sz);
717 if (lock >= (uintptr_t)base + sz)
718 ld = NULL;
719 }
720 __cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
721 if (ld != NULL) {
722 __cpu_simple_lock(&ld->ld_spinlock);
723 lockdebug_abort1(func, line, ld, s,
724 "allocation contains active lock", !cold);
725 return;
726 }
727 splx(s);
728 }
729
730 /*
731 * lockdebug_dump:
732 *
733 * Dump information about a lock on panic, or for DDB.
734 */
735 static void
736 lockdebug_dump(lockdebug_t *ld, void (*pr)(const char *, ...)
737 __printflike(1, 2))
738 {
739 int sleeper = (ld->ld_flags & LD_SLEEPER);
740
741 (*pr)(
742 "lock address : %#018lx type : %18s\n"
743 "initialized : %#018lx",
744 (long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
745 (long)ld->ld_initaddr);
746
747 if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
748 (*pr)(" interlock: %#018lx\n", (long)ld->ld_locked);
749 } else {
750 (*pr)("\n"
751 "shared holds : %18u exclusive: %18u\n"
752 "shares wanted: %18u exclusive: %18u\n"
753 "current cpu : %18u last held: %18u\n"
754 "current lwp : %#018lx last held: %#018lx\n"
755 "last locked%c : %#018lx unlocked%c: %#018lx\n",
756 (unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
757 (unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
758 (unsigned)cpu_index(curcpu()), (unsigned)ld->ld_cpu,
759 (long)curlwp, (long)ld->ld_lwp,
760 ((ld->ld_flags & LD_LOCKED) ? '*' : ' '),
761 (long)ld->ld_locked,
762 ((ld->ld_flags & LD_LOCKED) ? ' ' : '*'),
763 (long)ld->ld_unlocked);
764 }
765
766 if (ld->ld_lockops->lo_dump != NULL)
767 (*ld->ld_lockops->lo_dump)(ld->ld_lock);
768
769 if (sleeper) {
770 (*pr)("\n");
771 turnstile_print(ld->ld_lock, pr);
772 }
773 }
774
775 /*
776 * lockdebug_abort1:
777 *
778 * An error has been trapped - dump lock info and panic.
779 */
780 static void
781 lockdebug_abort1(const char *func, size_t line, lockdebug_t *ld, int s,
782 const char *msg, bool dopanic)
783 {
784
785 /*
786 * Don't make the situation worse if the system is already going
787 * down in flames. Once a panic is triggered, lockdebug state
788 * becomes stale and cannot be trusted.
789 */
790 if (atomic_inc_uint_nv(&ld_panic) != 1) {
791 __cpu_simple_unlock(&ld->ld_spinlock);
792 splx(s);
793 return;
794 }
795
796 printf_nolog("%s error: %s,%zu: %s\n\n", ld->ld_lockops->lo_name,
797 func, line, msg);
798 lockdebug_dump(ld, printf_nolog);
799 __cpu_simple_unlock(&ld->ld_spinlock);
800 splx(s);
801 printf_nolog("\n");
802 if (dopanic)
803 panic("LOCKDEBUG: %s error: %s,%zu: %s",
804 ld->ld_lockops->lo_name, func, line, msg);
805 }
806
807 #endif /* LOCKDEBUG */
808
809 /*
810 * lockdebug_lock_print:
811 *
812 * Handle the DDB 'show lock' command.
813 */
814 #ifdef DDB
815 #include <machine/db_machdep.h>
816 #include <ddb/db_interface.h>
817
818 void
819 lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
820 {
821 #ifdef LOCKDEBUG
822 lockdebug_t *ld;
823
824 TAILQ_FOREACH(ld, &ld_all, ld_achain) {
825 if (ld->ld_lock == NULL)
826 continue;
827 if (addr == NULL || ld->ld_lock == addr) {
828 lockdebug_dump(ld, pr);
829 if (addr != NULL)
830 return;
831 }
832 }
833 if (addr != NULL) {
834 (*pr)("Sorry, no record of a lock with address %p found.\n",
835 addr);
836 }
837 #else
838 (*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
839 #endif /* LOCKDEBUG */
840 }
841
842 #ifdef LOCKDEBUG
843 static void
844 lockdebug_show_all_locks_lwp(void (*pr)(const char *, ...), bool show_trace)
845 {
846 struct proc *p;
847
848 LIST_FOREACH(p, &allproc, p_list) {
849 struct lwp *l;
850 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
851 lockdebug_t *ld;
852 const char *sym;
853 int i = 0;
854 if (TAILQ_EMPTY(&l->l_ld_locks))
855 continue;
856 (*pr)("Locks held by an LWP (%s):\n",
857 l->l_name ? l->l_name : p->p_comm);
858 TAILQ_FOREACH(ld, &l->l_ld_locks, ld_chain) {
859 ksyms_getname(NULL, &sym,
860 (vaddr_t)ld->ld_initaddr,
861 KSYMS_CLOSEST|KSYMS_PROC|KSYMS_ANY);
862 (*pr)("Lock %d (initialized at %s)\n", i++, sym);
863 lockdebug_dump(ld, pr);
864 }
865 if (show_trace) {
866 db_stack_trace_print((db_expr_t)(intptr_t)l,
867 true,
868 32 /* Limit just in case */,
869 "a", pr);
870 }
871 (*pr)("\n");
872 }
873 }
874 }
875
876 static void
877 lockdebug_show_all_locks_cpu(void (*pr)(const char *, ...), bool show_trace)
878 {
879 lockdebug_t *ld;
880 CPU_INFO_ITERATOR cii;
881 struct cpu_info *ci;
882 const char *sym;
883
884 for (CPU_INFO_FOREACH(cii, ci)) {
885 int i = 0;
886 if (TAILQ_EMPTY(&ci->ci_data.cpu_ld_locks))
887 continue;
888 (*pr)("Locks held on CPU %u:\n", ci->ci_index);
889 TAILQ_FOREACH(ld, &ci->ci_data.cpu_ld_locks, ld_chain) {
890 ksyms_getname(NULL, &sym,
891 (vaddr_t)ld->ld_initaddr,
892 KSYMS_CLOSEST|KSYMS_PROC|KSYMS_ANY);
893 (*pr)("Lock %d (initialized at %s)\n", i++, sym);
894 lockdebug_dump(ld, pr);
895 if (show_trace) {
896 db_stack_trace_print(
897 (db_expr_t)(intptr_t)ci->ci_curlwp,
898 true,
899 32 /* Limit just in case */,
900 "a", pr);
901 }
902 (*pr)("\n");
903 }
904 }
905 }
906 #endif /* LOCKDEBUG */
907
908 void
909 lockdebug_show_all_locks(void (*pr)(const char *, ...), const char *modif)
910 {
911 #ifdef LOCKDEBUG
912 bool show_trace = false;
913 if (modif[0] == 't')
914 show_trace = true;
915
916 (*pr)("[Locks tracked through LWPs]\n");
917 lockdebug_show_all_locks_lwp(pr, show_trace);
918 (*pr)("\n");
919
920 (*pr)("[Locks tracked through CPUs]\n");
921 lockdebug_show_all_locks_cpu(pr, show_trace);
922 (*pr)("\n");
923 #else
924 (*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
925 #endif /* LOCKDEBUG */
926 }
927
928 void
929 lockdebug_show_lockstats(void (*pr)(const char *, ...))
930 {
931 #ifdef LOCKDEBUG
932 lockdebug_t *ld;
933 void *_ld;
934 uint32_t n_null = 0;
935 uint32_t n_spin_mutex = 0;
936 uint32_t n_adaptive_mutex = 0;
937 uint32_t n_rwlock = 0;
938 uint32_t n_cv = 0;
939 uint32_t n_others = 0;
940
941 RB_TREE_FOREACH(_ld, &ld_rb_tree) {
942 ld = _ld;
943 if (ld->ld_lock == NULL) {
944 n_null++;
945 continue;
946 }
947 if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
948 n_cv++;
949 continue;
950 }
951 if (ld->ld_lockops->lo_name[0] == 'M') {
952 if (ld->ld_lockops->lo_type == LOCKOPS_SLEEP)
953 n_adaptive_mutex++;
954 else
955 n_spin_mutex++;
956 continue;
957 }
958 if (ld->ld_lockops->lo_name[0] == 'R') {
959 n_rwlock++;
960 continue;
961 }
962 n_others++;
963 }
964 (*pr)(
965 "condvar: %u\n"
966 "spin mutex: %u\n"
967 "adaptive mutex: %u\n"
968 "rwlock: %u\n"
969 "null locks: %u\n"
970 "others: %u\n",
971 n_cv, n_spin_mutex, n_adaptive_mutex, n_rwlock,
972 n_null, n_others);
973 #else
974 (*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
975 #endif /* LOCKDEBUG */
976 }
977 #endif /* DDB */
978
979 /*
980 * lockdebug_dismiss:
981 *
982 * The system is rebooting, and potentially from an unsafe
983 * place so avoid any future aborts.
984 */
985 void
986 lockdebug_dismiss(void)
987 {
988
989 atomic_inc_uint_nv(&ld_panic);
990 }
991
992 /*
993 * lockdebug_abort:
994 *
995 * An error has been trapped - dump lock info and call panic().
996 */
997 void
998 lockdebug_abort(const char *func, size_t line, volatile void *lock,
999 lockops_t *ops, const char *msg)
1000 {
1001 #ifdef LOCKDEBUG
1002 lockdebug_t *ld;
1003 int s;
1004
1005 s = splhigh();
1006 if ((ld = lockdebug_lookup(func, line, lock,
1007 (uintptr_t) __builtin_return_address(0))) != NULL) {
1008 lockdebug_abort1(func, line, ld, s, msg, true);
1009 return;
1010 }
1011 splx(s);
1012 #endif /* LOCKDEBUG */
1013
1014 /*
1015 * Complain first on the occurrance only. Otherwise proceeed to
1016 * panic where we will `rendezvous' with other CPUs if the machine
1017 * is going down in flames.
1018 */
1019 if (atomic_inc_uint_nv(&ld_panic) == 1) {
1020 printf_nolog("%s error: %s,%zu: %s\n\n"
1021 "lock address : %#018lx\n"
1022 "current cpu : %18d\n"
1023 "current lwp : %#018lx\n",
1024 ops->lo_name, func, line, msg, (long)lock,
1025 (int)cpu_index(curcpu()), (long)curlwp);
1026 (*ops->lo_dump)(lock);
1027 printf_nolog("\n");
1028 }
1029
1030 panic("lock error: %s: %s,%zu: %s: lock %p cpu %d lwp %p",
1031 ops->lo_name, func, line, msg, lock, cpu_index(curcpu()), curlwp);
1032 }
1033