subr_lockdebug.c revision 1.55.4.1 1 /* $NetBSD: subr_lockdebug.c,v 1.55.4.1 2017/05/02 03:19:22 pgoyette 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.55.4.1 2017/05/02 03:19:22 pgoyette 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
55 #include <machine/lock.h>
56
57 unsigned int ld_panic;
58
59 #ifdef LOCKDEBUG
60
61 #define LD_BATCH_SHIFT 9
62 #define LD_BATCH (1 << LD_BATCH_SHIFT)
63 #define LD_BATCH_MASK (LD_BATCH - 1)
64 #define LD_MAX_LOCKS 1048576
65 #define LD_SLOP 16
66
67 #define LD_LOCKED 0x01
68 #define LD_SLEEPER 0x02
69
70 #define LD_WRITE_LOCK 0x80000000
71
72 typedef struct lockdebug {
73 struct rb_node ld_rb_node;
74 __cpu_simple_lock_t ld_spinlock;
75 _TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
76 _TAILQ_ENTRY(struct lockdebug, volatile) ld_achain;
77 volatile void *ld_lock;
78 lockops_t *ld_lockops;
79 struct lwp *ld_lwp;
80 uintptr_t ld_locked;
81 uintptr_t ld_unlocked;
82 uintptr_t ld_initaddr;
83 uint16_t ld_shares;
84 uint16_t ld_cpu;
85 uint8_t ld_flags;
86 uint8_t ld_shwant; /* advisory */
87 uint8_t ld_exwant; /* advisory */
88 uint8_t ld_unused;
89 } volatile lockdebug_t;
90
91 typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
92
93 __cpu_simple_lock_t ld_mod_lk;
94 lockdebuglist_t ld_free = TAILQ_HEAD_INITIALIZER(ld_free);
95 lockdebuglist_t ld_all = TAILQ_HEAD_INITIALIZER(ld_all);
96 int ld_nfree;
97 int ld_freeptr;
98 int ld_recurse;
99 bool ld_nomore;
100 lockdebug_t ld_prime[LD_BATCH];
101
102 static void lockdebug_abort1(const char *, size_t, lockdebug_t *, int,
103 const char *, bool);
104 static int lockdebug_more(int);
105 static void lockdebug_init(void);
106 static void lockdebug_dump(lockdebug_t *, void (*)(const char *, ...)
107 __printflike(1, 2));
108
109 static signed int
110 ld_rbto_compare_nodes(void *ctx, const void *n1, const void *n2)
111 {
112 const lockdebug_t *ld1 = n1;
113 const lockdebug_t *ld2 = n2;
114 const uintptr_t a = (uintptr_t)ld1->ld_lock;
115 const uintptr_t b = (uintptr_t)ld2->ld_lock;
116
117 if (a < b)
118 return -1;
119 if (a > b)
120 return 1;
121 return 0;
122 }
123
124 static signed int
125 ld_rbto_compare_key(void *ctx, const void *n, const void *key)
126 {
127 const lockdebug_t *ld = n;
128 const uintptr_t a = (uintptr_t)ld->ld_lock;
129 const uintptr_t b = (uintptr_t)key;
130
131 if (a < b)
132 return -1;
133 if (a > b)
134 return 1;
135 return 0;
136 }
137
138 static rb_tree_t ld_rb_tree;
139
140 static const rb_tree_ops_t ld_rb_tree_ops = {
141 .rbto_compare_nodes = ld_rbto_compare_nodes,
142 .rbto_compare_key = ld_rbto_compare_key,
143 .rbto_node_offset = offsetof(lockdebug_t, ld_rb_node),
144 .rbto_context = NULL
145 };
146
147 static inline lockdebug_t *
148 lockdebug_lookup1(volatile void *lock)
149 {
150 lockdebug_t *ld;
151 struct cpu_info *ci;
152
153 ci = curcpu();
154 __cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
155 ld = (lockdebug_t *)rb_tree_find_node(&ld_rb_tree, __UNVOLATILE(lock));
156 __cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
157 if (ld == NULL) {
158 return NULL;
159 }
160 __cpu_simple_lock(&ld->ld_spinlock);
161
162 return ld;
163 }
164
165 static void
166 lockdebug_lock_cpus(void)
167 {
168 CPU_INFO_ITERATOR cii;
169 struct cpu_info *ci;
170
171 for (CPU_INFO_FOREACH(cii, ci)) {
172 __cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
173 }
174 }
175
176 static void
177 lockdebug_unlock_cpus(void)
178 {
179 CPU_INFO_ITERATOR cii;
180 struct cpu_info *ci;
181
182 for (CPU_INFO_FOREACH(cii, ci)) {
183 __cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
184 }
185 }
186
187 /*
188 * lockdebug_lookup:
189 *
190 * Find a lockdebug structure by a pointer to a lock and return it locked.
191 */
192 static inline lockdebug_t *
193 lockdebug_lookup(const char *func, size_t line, volatile void *lock,
194 uintptr_t where)
195 {
196 lockdebug_t *ld;
197
198 ld = lockdebug_lookup1(lock);
199 if (ld == NULL) {
200 panic("%s,%zu: uninitialized lock (lock=%p, from=%08"
201 PRIxPTR ")", func, line, lock, where);
202 }
203 return ld;
204 }
205
206 /*
207 * lockdebug_init:
208 *
209 * Initialize the lockdebug system. Allocate an initial pool of
210 * lockdebug structures before the VM system is up and running.
211 */
212 static void
213 lockdebug_init(void)
214 {
215 lockdebug_t *ld;
216 int i;
217
218 TAILQ_INIT(&curcpu()->ci_data.cpu_ld_locks);
219 TAILQ_INIT(&curlwp->l_ld_locks);
220 __cpu_simple_lock_init(&curcpu()->ci_data.cpu_ld_lock);
221 __cpu_simple_lock_init(&ld_mod_lk);
222
223 rb_tree_init(&ld_rb_tree, &ld_rb_tree_ops);
224
225 ld = ld_prime;
226 for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
227 __cpu_simple_lock_init(&ld->ld_spinlock);
228 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
229 TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
230 }
231 ld_freeptr = 1;
232 ld_nfree = LD_BATCH - 1;
233 }
234
235 /*
236 * lockdebug_alloc:
237 *
238 * A lock is being initialized, so allocate an associated debug
239 * structure.
240 */
241 bool
242 lockdebug_alloc(const char *func, size_t line, volatile void *lock,
243 lockops_t *lo, uintptr_t initaddr)
244 {
245 struct cpu_info *ci;
246 lockdebug_t *ld;
247 int s;
248
249 if (lo == NULL || panicstr != NULL || ld_panic)
250 return false;
251 if (ld_freeptr == 0)
252 lockdebug_init();
253
254 s = splhigh();
255 __cpu_simple_lock(&ld_mod_lk);
256 if ((ld = lockdebug_lookup1(lock)) != NULL) {
257 __cpu_simple_unlock(&ld_mod_lk);
258 lockdebug_abort1(func, line, ld, s, "already initialized",
259 true);
260 return false;
261 }
262
263 /*
264 * Pinch a new debug structure. We may recurse because we call
265 * kmem_alloc(), which may need to initialize new locks somewhere
266 * down the path. If not recursing, we try to maintain at least
267 * LD_SLOP structures free, which should hopefully be enough to
268 * satisfy kmem_alloc(). If we can't provide a structure, not to
269 * worry: we'll just mark the lock as not having an ID.
270 */
271 ci = curcpu();
272 ci->ci_lkdebug_recurse++;
273 if (TAILQ_EMPTY(&ld_free)) {
274 if (ci->ci_lkdebug_recurse > 1 || ld_nomore) {
275 ci->ci_lkdebug_recurse--;
276 __cpu_simple_unlock(&ld_mod_lk);
277 splx(s);
278 return false;
279 }
280 s = lockdebug_more(s);
281 } else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP) {
282 s = lockdebug_more(s);
283 }
284 if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
285 __cpu_simple_unlock(&ld_mod_lk);
286 splx(s);
287 return false;
288 }
289 TAILQ_REMOVE(&ld_free, ld, ld_chain);
290 ld_nfree--;
291 ci->ci_lkdebug_recurse--;
292
293 if (ld->ld_lock != NULL) {
294 panic("%s,%zu: corrupt table ld %p", func, line, ld);
295 }
296
297 /* Initialise the structure. */
298 ld->ld_lock = lock;
299 ld->ld_lockops = lo;
300 ld->ld_locked = 0;
301 ld->ld_unlocked = 0;
302 ld->ld_lwp = NULL;
303 ld->ld_initaddr = initaddr;
304 ld->ld_flags = (lo->lo_type == LOCKOPS_SLEEP ? LD_SLEEPER : 0);
305 lockdebug_lock_cpus();
306 (void)rb_tree_insert_node(&ld_rb_tree, __UNVOLATILE(ld));
307 lockdebug_unlock_cpus();
308 __cpu_simple_unlock(&ld_mod_lk);
309
310 splx(s);
311 return true;
312 }
313
314 /*
315 * lockdebug_free:
316 *
317 * A lock is being destroyed, so release debugging resources.
318 */
319 void
320 lockdebug_free(const char *func, size_t line, volatile void *lock)
321 {
322 lockdebug_t *ld;
323 int s;
324
325 if (panicstr != NULL || ld_panic)
326 return;
327
328 s = splhigh();
329 __cpu_simple_lock(&ld_mod_lk);
330 ld = lockdebug_lookup(func, line, lock,
331 (uintptr_t) __builtin_return_address(0));
332 if (ld == NULL) {
333 __cpu_simple_unlock(&ld_mod_lk);
334 panic("%s,%zu: destroying uninitialized object %p"
335 "(ld_lock=%p)", func, line, lock, ld->ld_lock);
336 return;
337 }
338 if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
339 __cpu_simple_unlock(&ld_mod_lk);
340 lockdebug_abort1(func, line, ld, s, "is locked or in use",
341 true);
342 return;
343 }
344 lockdebug_lock_cpus();
345 rb_tree_remove_node(&ld_rb_tree, __UNVOLATILE(ld));
346 lockdebug_unlock_cpus();
347 ld->ld_lock = NULL;
348 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
349 ld_nfree++;
350 __cpu_simple_unlock(&ld->ld_spinlock);
351 __cpu_simple_unlock(&ld_mod_lk);
352 splx(s);
353 }
354
355 /*
356 * lockdebug_more:
357 *
358 * Allocate a batch of debug structures and add to the free list.
359 * Must be called with ld_mod_lk held.
360 */
361 static int
362 lockdebug_more(int s)
363 {
364 lockdebug_t *ld;
365 void *block;
366 int i, base, m;
367
368 /*
369 * Can't call kmem_alloc() if in interrupt context. XXX We could
370 * deadlock, because we don't know which locks the caller holds.
371 */
372 if (cpu_intr_p() || (curlwp->l_pflag & LP_INTR) != 0) {
373 return s;
374 }
375
376 while (ld_nfree < LD_SLOP) {
377 __cpu_simple_unlock(&ld_mod_lk);
378 splx(s);
379 block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
380 s = splhigh();
381 __cpu_simple_lock(&ld_mod_lk);
382
383 if (block == NULL)
384 return s;
385
386 if (ld_nfree > LD_SLOP) {
387 /* Somebody beat us to it. */
388 __cpu_simple_unlock(&ld_mod_lk);
389 splx(s);
390 kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
391 s = splhigh();
392 __cpu_simple_lock(&ld_mod_lk);
393 continue;
394 }
395
396 base = ld_freeptr;
397 ld_nfree += LD_BATCH;
398 ld = block;
399 base <<= LD_BATCH_SHIFT;
400 m = min(LD_MAX_LOCKS, base + LD_BATCH);
401
402 if (m == LD_MAX_LOCKS)
403 ld_nomore = true;
404
405 for (i = base; i < m; i++, ld++) {
406 __cpu_simple_lock_init(&ld->ld_spinlock);
407 TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
408 TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
409 }
410
411 membar_producer();
412 }
413
414 return s;
415 }
416
417 /*
418 * lockdebug_wantlock:
419 *
420 * Process the preamble to a lock acquire. The "shared"
421 * parameter controls which ld_{ex,sh}want counter is
422 * updated; a negative value of shared updates neither.
423 */
424 void
425 lockdebug_wantlock(const char *func, size_t line,
426 volatile void *lock, uintptr_t where, int shared)
427 {
428 struct lwp *l = curlwp;
429 lockdebug_t *ld;
430 bool recurse;
431 int s;
432
433 (void)shared;
434 recurse = false;
435
436 if (panicstr != NULL || ld_panic)
437 return;
438
439 s = splhigh();
440 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
441 splx(s);
442 return;
443 }
444 if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
445 if ((ld->ld_flags & LD_SLEEPER) != 0) {
446 if (ld->ld_lwp == l)
447 recurse = true;
448 } else if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
449 recurse = true;
450 }
451 if (cpu_intr_p()) {
452 if ((ld->ld_flags & LD_SLEEPER) != 0) {
453 lockdebug_abort1(func, line, ld, s,
454 "acquiring sleep lock from interrupt context",
455 true);
456 return;
457 }
458 }
459 if (shared > 0)
460 ld->ld_shwant++;
461 else if (shared == 0)
462 ld->ld_exwant++;
463 if (recurse) {
464 lockdebug_abort1(func, line, ld, s, "locking against myself",
465 true);
466 return;
467 }
468 __cpu_simple_unlock(&ld->ld_spinlock);
469 splx(s);
470 }
471
472 /*
473 * lockdebug_locked:
474 *
475 * Process a lock acquire operation.
476 */
477 void
478 lockdebug_locked(const char *func, size_t line,
479 volatile void *lock, void *cvlock, uintptr_t where, int shared)
480 {
481 struct lwp *l = curlwp;
482 lockdebug_t *ld;
483 int s;
484
485 if (panicstr != NULL || ld_panic)
486 return;
487
488 s = splhigh();
489 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
490 splx(s);
491 return;
492 }
493 if (cvlock) {
494 KASSERT(ld->ld_lockops->lo_type == LOCKOPS_CV);
495 if (lock == (void *)&lbolt) {
496 /* nothing */
497 } else if (ld->ld_shares++ == 0) {
498 ld->ld_locked = (uintptr_t)cvlock;
499 } else if (cvlock != (void *)ld->ld_locked) {
500 lockdebug_abort1(func, line, ld, s,
501 "multiple locks used with condition variable",
502 true);
503 return;
504 }
505 } else if (shared) {
506 l->l_shlocks++;
507 ld->ld_locked = where;
508 ld->ld_shares++;
509 ld->ld_shwant--;
510 } else {
511 if ((ld->ld_flags & LD_LOCKED) != 0) {
512 lockdebug_abort1(func, line, ld, s, "already locked",
513 true);
514 return;
515 }
516 ld->ld_flags |= LD_LOCKED;
517 ld->ld_locked = where;
518 ld->ld_exwant--;
519 if ((ld->ld_flags & LD_SLEEPER) != 0) {
520 TAILQ_INSERT_TAIL(&l->l_ld_locks, ld, ld_chain);
521 } else {
522 TAILQ_INSERT_TAIL(&curcpu()->ci_data.cpu_ld_locks,
523 ld, ld_chain);
524 }
525 }
526 ld->ld_cpu = (uint16_t)cpu_index(curcpu());
527 ld->ld_lwp = l;
528 __cpu_simple_unlock(&ld->ld_spinlock);
529 splx(s);
530 }
531
532 /*
533 * lockdebug_unlocked:
534 *
535 * Process a lock release operation.
536 */
537 void
538 lockdebug_unlocked(const char *func, size_t line,
539 volatile void *lock, uintptr_t where, int shared)
540 {
541 struct lwp *l = curlwp;
542 lockdebug_t *ld;
543 int s;
544
545 if (panicstr != NULL || ld_panic)
546 return;
547
548 s = splhigh();
549 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
550 splx(s);
551 return;
552 }
553 if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
554 if (lock == (void *)&lbolt) {
555 /* nothing */
556 } else {
557 ld->ld_shares--;
558 }
559 } else if (shared) {
560 if (l->l_shlocks == 0) {
561 lockdebug_abort1(func, line, ld, s,
562 "no shared locks held by LWP", true);
563 return;
564 }
565 if (ld->ld_shares == 0) {
566 lockdebug_abort1(func, line, ld, s,
567 "no shared holds on this lock", true);
568 return;
569 }
570 l->l_shlocks--;
571 ld->ld_shares--;
572 if (ld->ld_lwp == l) {
573 ld->ld_unlocked = where;
574 ld->ld_lwp = NULL;
575 }
576 if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
577 ld->ld_cpu = (uint16_t)-1;
578 } else {
579 if ((ld->ld_flags & LD_LOCKED) == 0) {
580 lockdebug_abort1(func, line, ld, s, "not locked", true);
581 return;
582 }
583
584 if ((ld->ld_flags & LD_SLEEPER) != 0) {
585 if (ld->ld_lwp != curlwp) {
586 lockdebug_abort1(func, line, ld, s,
587 "not held by current LWP", true);
588 return;
589 }
590 TAILQ_REMOVE(&l->l_ld_locks, ld, ld_chain);
591 } else {
592 if (ld->ld_cpu != (uint16_t)cpu_index(curcpu())) {
593 lockdebug_abort1(func, line, ld, s,
594 "not held by current CPU", true);
595 return;
596 }
597 TAILQ_REMOVE(&curcpu()->ci_data.cpu_ld_locks, ld,
598 ld_chain);
599 }
600 ld->ld_flags &= ~LD_LOCKED;
601 ld->ld_unlocked = where;
602 ld->ld_lwp = NULL;
603 }
604 __cpu_simple_unlock(&ld->ld_spinlock);
605 splx(s);
606 }
607
608 /*
609 * lockdebug_wakeup:
610 *
611 * Process a wakeup on a condition variable.
612 */
613 void
614 lockdebug_wakeup(const char *func, size_t line, volatile void *lock,
615 uintptr_t where)
616 {
617 lockdebug_t *ld;
618 int s;
619
620 if (panicstr != NULL || ld_panic || lock == (void *)&lbolt)
621 return;
622
623 s = splhigh();
624 /* Find the CV... */
625 if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
626 splx(s);
627 return;
628 }
629 /*
630 * If it has any waiters, ensure that they are using the
631 * same interlock.
632 */
633 if (ld->ld_shares != 0 && !mutex_owned((kmutex_t *)ld->ld_locked)) {
634 lockdebug_abort1(func, line, ld, s, "interlocking mutex not "
635 "held during wakeup", true);
636 return;
637 }
638 __cpu_simple_unlock(&ld->ld_spinlock);
639 splx(s);
640 }
641
642 /*
643 * lockdebug_barrier:
644 *
645 * Panic if we hold more than one specified spin lock, and optionally,
646 * if we hold sleep locks.
647 */
648 void
649 lockdebug_barrier(const char *func, size_t line, volatile void *spinlock,
650 int slplocks)
651 {
652 struct lwp *l = curlwp;
653 lockdebug_t *ld;
654 int s;
655
656 if (panicstr != NULL || ld_panic)
657 return;
658
659 s = splhigh();
660 if ((l->l_pflag & LP_INTR) == 0) {
661 TAILQ_FOREACH(ld, &curcpu()->ci_data.cpu_ld_locks, ld_chain) {
662 if (ld->ld_lock == spinlock) {
663 continue;
664 }
665 __cpu_simple_lock(&ld->ld_spinlock);
666 lockdebug_abort1(func, line, ld, s,
667 "spin lock held", true);
668 return;
669 }
670 }
671 if (slplocks) {
672 splx(s);
673 return;
674 }
675 if ((ld = TAILQ_FIRST(&l->l_ld_locks)) != NULL) {
676 __cpu_simple_lock(&ld->ld_spinlock);
677 lockdebug_abort1(func, line, ld, s, "sleep lock held", true);
678 return;
679 }
680 splx(s);
681 if (l->l_shlocks != 0) {
682 TAILQ_FOREACH(ld, &ld_all, ld_achain) {
683 if (ld->ld_lockops->lo_type == LOCKOPS_CV)
684 continue;
685 if (ld->ld_lwp == l)
686 lockdebug_dump(ld, printf);
687 }
688 panic("%s,%zu: holding %d shared locks", func, line,
689 l->l_shlocks);
690 }
691 }
692
693 /*
694 * lockdebug_mem_check:
695 *
696 * Check for in-use locks within a memory region that is
697 * being freed.
698 */
699 void
700 lockdebug_mem_check(const char *func, size_t line, void *base, size_t sz)
701 {
702 lockdebug_t *ld;
703 struct cpu_info *ci;
704 int s;
705
706 if (panicstr != NULL || ld_panic)
707 return;
708
709 s = splhigh();
710 ci = curcpu();
711 __cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
712 ld = (lockdebug_t *)rb_tree_find_node_geq(&ld_rb_tree, base);
713 if (ld != NULL) {
714 const uintptr_t lock = (uintptr_t)ld->ld_lock;
715
716 if ((uintptr_t)base > lock)
717 panic("%s,%zu: corrupt tree ld=%p, base=%p, sz=%zu",
718 func, line, ld, base, sz);
719 if (lock >= (uintptr_t)base + sz)
720 ld = NULL;
721 }
722 __cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
723 if (ld != NULL) {
724 __cpu_simple_lock(&ld->ld_spinlock);
725 lockdebug_abort1(func, line, ld, s,
726 "allocation contains active lock", !cold);
727 return;
728 }
729 splx(s);
730 }
731
732 /*
733 * lockdebug_dump:
734 *
735 * Dump information about a lock on panic, or for DDB.
736 */
737 static void
738 lockdebug_dump(lockdebug_t *ld, void (*pr)(const char *, ...)
739 __printflike(1, 2))
740 {
741 int sleeper = (ld->ld_flags & LD_SLEEPER);
742
743 (*pr)(
744 "lock address : %#018lx type : %18s\n"
745 "initialized : %#018lx",
746 (long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
747 (long)ld->ld_initaddr);
748
749 if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
750 (*pr)(" interlock: %#018lx\n", (long)ld->ld_locked);
751 } else {
752 (*pr)("\n"
753 "shared holds : %18u exclusive: %18u\n"
754 "shares wanted: %18u exclusive: %18u\n"
755 "current cpu : %18u last held: %18u\n"
756 "current lwp : %#018lx last held: %#018lx\n"
757 "last locked%c : %#018lx unlocked%c: %#018lx\n",
758 (unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
759 (unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
760 (unsigned)cpu_index(curcpu()), (unsigned)ld->ld_cpu,
761 (long)curlwp, (long)ld->ld_lwp,
762 ((ld->ld_flags & LD_LOCKED) ? '*' : ' '),
763 (long)ld->ld_locked,
764 ((ld->ld_flags & LD_LOCKED) ? ' ' : '*'),
765 (long)ld->ld_unlocked);
766 }
767
768 if (ld->ld_lockops->lo_dump != NULL)
769 (*ld->ld_lockops->lo_dump)(ld->ld_lock);
770
771 if (sleeper) {
772 (*pr)("\n");
773 turnstile_print(ld->ld_lock, pr);
774 }
775 }
776
777 /*
778 * lockdebug_abort1:
779 *
780 * An error has been trapped - dump lock info and panic.
781 */
782 static void
783 lockdebug_abort1(const char *func, size_t line, lockdebug_t *ld, int s,
784 const char *msg, bool dopanic)
785 {
786
787 /*
788 * Don't make the situation worse if the system is already going
789 * down in flames. Once a panic is triggered, lockdebug state
790 * becomes stale and cannot be trusted.
791 */
792 if (atomic_inc_uint_nv(&ld_panic) != 1) {
793 __cpu_simple_unlock(&ld->ld_spinlock);
794 splx(s);
795 return;
796 }
797
798 printf_nolog("%s error: %s,%zu: %s\n\n", ld->ld_lockops->lo_name,
799 func, line, msg);
800 lockdebug_dump(ld, printf_nolog);
801 __cpu_simple_unlock(&ld->ld_spinlock);
802 splx(s);
803 printf_nolog("\n");
804 if (dopanic)
805 panic("LOCKDEBUG: %s error: %s,%zu: %s",
806 ld->ld_lockops->lo_name, func, line, msg);
807 }
808
809 #endif /* LOCKDEBUG */
810
811 /*
812 * lockdebug_lock_print:
813 *
814 * Handle the DDB 'show lock' command.
815 */
816 #ifdef DDB
817 void
818 lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
819 {
820 #ifdef LOCKDEBUG
821 lockdebug_t *ld;
822
823 TAILQ_FOREACH(ld, &ld_all, ld_achain) {
824 if (ld->ld_lock == NULL)
825 continue;
826 if (addr == NULL || ld->ld_lock == addr) {
827 lockdebug_dump(ld, pr);
828 if (addr != NULL)
829 return;
830 }
831 }
832 if (addr != NULL) {
833 (*pr)("Sorry, no record of a lock with address %p found.\n",
834 addr);
835 }
836 #else
837 (*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
838 #endif /* LOCKDEBUG */
839 }
840 #endif /* DDB */
841
842 /*
843 * lockdebug_abort:
844 *
845 * An error has been trapped - dump lock info and call panic().
846 */
847 void
848 lockdebug_abort(const char *func, size_t line, volatile void *lock,
849 lockops_t *ops, const char *msg)
850 {
851 #ifdef LOCKDEBUG
852 lockdebug_t *ld;
853 int s;
854
855 s = splhigh();
856 if ((ld = lockdebug_lookup(func, line, lock,
857 (uintptr_t) __builtin_return_address(0))) != NULL) {
858 lockdebug_abort1(func, line, ld, s, msg, true);
859 return;
860 }
861 splx(s);
862 #endif /* LOCKDEBUG */
863
864 /*
865 * Complain first on the occurrance only. Otherwise proceeed to
866 * panic where we will `rendezvous' with other CPUs if the machine
867 * is going down in flames.
868 */
869 if (atomic_inc_uint_nv(&ld_panic) == 1) {
870 printf_nolog("%s error: %s,%zu: %s\n\n"
871 "lock address : %#018lx\n"
872 "current cpu : %18d\n"
873 "current lwp : %#018lx\n",
874 ops->lo_name, func, line, msg, (long)lock,
875 (int)cpu_index(curcpu()), (long)curlwp);
876 (*ops->lo_dump)(lock);
877 printf_nolog("\n");
878 }
879
880 panic("lock error: %s: %s,%zu: %s: lock %p cpu %d lwp %p",
881 ops->lo_name, func, line, msg, lock, cpu_index(curcpu()), curlwp);
882 }
883