xref: /netbsd-src/sys/kern/subr_lockdebug.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: subr_lockdebug.c,v 1.55 2017/01/26 04:11:56 christos 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 2017/01/26 04:11:56 christos 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.
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)
458 		ld->ld_shwant++;
459 	else
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 void
816 lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
817 {
818 #ifdef LOCKDEBUG
819 	lockdebug_t *ld;
820 
821 	TAILQ_FOREACH(ld, &ld_all, ld_achain) {
822 		if (ld->ld_lock == NULL)
823 			continue;
824 		if (addr == NULL || ld->ld_lock == addr) {
825 			lockdebug_dump(ld, pr);
826 			if (addr != NULL)
827 				return;
828 		}
829 	}
830 	if (addr != NULL) {
831 		(*pr)("Sorry, no record of a lock with address %p found.\n",
832 		    addr);
833 	}
834 #else
835 	(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
836 #endif	/* LOCKDEBUG */
837 }
838 #endif	/* DDB */
839 
840 /*
841  * lockdebug_abort:
842  *
843  *	An error has been trapped - dump lock info and call panic().
844  */
845 void
846 lockdebug_abort(const char *func, size_t line, volatile void *lock,
847     lockops_t *ops, const char *msg)
848 {
849 #ifdef LOCKDEBUG
850 	lockdebug_t *ld;
851 	int s;
852 
853 	s = splhigh();
854 	if ((ld = lockdebug_lookup(func, line, lock,
855 			(uintptr_t) __builtin_return_address(0))) != NULL) {
856 		lockdebug_abort1(func, line, ld, s, msg, true);
857 		return;
858 	}
859 	splx(s);
860 #endif	/* LOCKDEBUG */
861 
862 	/*
863 	 * Complain first on the occurrance only.  Otherwise proceeed to
864 	 * panic where we will `rendezvous' with other CPUs if the machine
865 	 * is going down in flames.
866 	 */
867 	if (atomic_inc_uint_nv(&ld_panic) == 1) {
868 		printf_nolog("%s error: %s,%zu: %s\n\n"
869 		    "lock address : %#018lx\n"
870 		    "current cpu  : %18d\n"
871 		    "current lwp  : %#018lx\n",
872 		    ops->lo_name, func, line, msg, (long)lock,
873 		    (int)cpu_index(curcpu()), (long)curlwp);
874 		(*ops->lo_dump)(lock);
875 		printf_nolog("\n");
876 	}
877 
878 	panic("lock error: %s: %s,%zu: %s: lock %p cpu %d lwp %p",
879 	    ops->lo_name, func, line, msg, lock, cpu_index(curcpu()), curlwp);
880 }
881