xref: /netbsd-src/sys/kern/subr_lockdebug.c (revision e7f0067cbe438f86b63b9abff581c10376f6c63e)
1*e7f0067cSchristos /*	$NetBSD: subr_lockdebug.c,v 1.58 2017/09/16 23:55:33 christos Exp $	*/
2b07ec3fcSad 
3b07ec3fcSad /*-
4057adba1Sad  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5b07ec3fcSad  * All rights reserved.
6b07ec3fcSad  *
7b07ec3fcSad  * This code is derived from software contributed to The NetBSD Foundation
8b07ec3fcSad  * by Andrew Doran.
9b07ec3fcSad  *
10b07ec3fcSad  * Redistribution and use in source and binary forms, with or without
11b07ec3fcSad  * modification, are permitted provided that the following conditions
12b07ec3fcSad  * are met:
13b07ec3fcSad  * 1. Redistributions of source code must retain the above copyright
14b07ec3fcSad  *    notice, this list of conditions and the following disclaimer.
15b07ec3fcSad  * 2. Redistributions in binary form must reproduce the above copyright
16b07ec3fcSad  *    notice, this list of conditions and the following disclaimer in the
17b07ec3fcSad  *    documentation and/or other materials provided with the distribution.
18b07ec3fcSad  *
19b07ec3fcSad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b07ec3fcSad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b07ec3fcSad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b07ec3fcSad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b07ec3fcSad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b07ec3fcSad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b07ec3fcSad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b07ec3fcSad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b07ec3fcSad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b07ec3fcSad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b07ec3fcSad  * POSSIBILITY OF SUCH DAMAGE.
30b07ec3fcSad  */
31b07ec3fcSad 
32b07ec3fcSad /*
33dde5d75eSad  * Basic lock debugging code shared among lock primitives.
34b07ec3fcSad  */
35b07ec3fcSad 
360ca3d21bSdsl #include <sys/cdefs.h>
37*e7f0067cSchristos __KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.58 2017/09/16 23:55:33 christos Exp $");
380ca3d21bSdsl 
3905cf8927Sozaki-r #ifdef _KERNEL_OPT
40b07ec3fcSad #include "opt_ddb.h"
4105cf8927Sozaki-r #endif
42b07ec3fcSad 
43b07ec3fcSad #include <sys/param.h>
44b07ec3fcSad #include <sys/proc.h>
45b07ec3fcSad #include <sys/systm.h>
4611dc6399Sad #include <sys/kernel.h>
47b07ec3fcSad #include <sys/kmem.h>
48b07ec3fcSad #include <sys/lockdebug.h>
49b07ec3fcSad #include <sys/sleepq.h>
5011dc6399Sad #include <sys/cpu.h>
51b470ab62Sad #include <sys/atomic.h>
52212c50ddSad #include <sys/lock.h>
5319e6c76bSmatt #include <sys/rbtree.h>
5438d5e341Syamt 
550664a045Sad #include <machine/lock.h>
560664a045Sad 
57057adba1Sad unsigned int		ld_panic;
58057adba1Sad 
59b07ec3fcSad #ifdef LOCKDEBUG
60b07ec3fcSad 
61b07ec3fcSad #define	LD_BATCH_SHIFT	9
62b07ec3fcSad #define	LD_BATCH	(1 << LD_BATCH_SHIFT)
63b07ec3fcSad #define	LD_BATCH_MASK	(LD_BATCH - 1)
64b07ec3fcSad #define	LD_MAX_LOCKS	1048576
65b07ec3fcSad #define	LD_SLOP		16
66b07ec3fcSad 
67b07ec3fcSad #define	LD_LOCKED	0x01
68b07ec3fcSad #define	LD_SLEEPER	0x02
69b07ec3fcSad 
70461cd942Sad #define	LD_WRITE_LOCK	0x80000000
71461cd942Sad 
72b07ec3fcSad typedef struct lockdebug {
73879d5dfbSrmind 	struct rb_node	ld_rb_node;
74a4e0004bSad 	__cpu_simple_lock_t ld_spinlock;
75b07ec3fcSad 	_TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
76b07ec3fcSad 	_TAILQ_ENTRY(struct lockdebug, volatile) ld_achain;
77b07ec3fcSad 	volatile void	*ld_lock;
78b07ec3fcSad 	lockops_t	*ld_lockops;
79b07ec3fcSad 	struct lwp	*ld_lwp;
80b07ec3fcSad 	uintptr_t	ld_locked;
81b07ec3fcSad 	uintptr_t	ld_unlocked;
8211dc6399Sad 	uintptr_t	ld_initaddr;
83b07ec3fcSad 	uint16_t	ld_shares;
84b07ec3fcSad 	uint16_t	ld_cpu;
85b07ec3fcSad 	uint8_t		ld_flags;
86b07ec3fcSad 	uint8_t		ld_shwant;	/* advisory */
87b07ec3fcSad 	uint8_t		ld_exwant;	/* advisory */
88b07ec3fcSad 	uint8_t		ld_unused;
89b07ec3fcSad } volatile lockdebug_t;
90b07ec3fcSad 
91b07ec3fcSad typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
92b07ec3fcSad 
93a4e0004bSad __cpu_simple_lock_t	ld_mod_lk;
9411910619Smatt lockdebuglist_t		ld_free = TAILQ_HEAD_INITIALIZER(ld_free);
9511910619Smatt lockdebuglist_t		ld_all = TAILQ_HEAD_INITIALIZER(ld_all);
96b07ec3fcSad int			ld_nfree;
97b07ec3fcSad int			ld_freeptr;
98b07ec3fcSad int			ld_recurse;
9964e54fbbSad bool			ld_nomore;
100b07ec3fcSad lockdebug_t		ld_prime[LD_BATCH];
101b07ec3fcSad 
1029be065fbSchristos static void	lockdebug_abort1(const char *, size_t, lockdebug_t *, int,
103a4e0004bSad     const char *, bool);
104a4e0004bSad static int	lockdebug_more(int);
10564e54fbbSad static void	lockdebug_init(void);
106a27531bcSchristos static void	lockdebug_dump(lockdebug_t *, void (*)(const char *, ...)
107a27531bcSchristos     __printflike(1, 2));
108b07ec3fcSad 
10938d5e341Syamt static signed int
110879d5dfbSrmind ld_rbto_compare_nodes(void *ctx, const void *n1, const void *n2)
11138d5e341Syamt {
112879d5dfbSrmind 	const lockdebug_t *ld1 = n1;
113879d5dfbSrmind 	const lockdebug_t *ld2 = n2;
11433e66db2Syamt 	const uintptr_t a = (uintptr_t)ld1->ld_lock;
11533e66db2Syamt 	const uintptr_t b = (uintptr_t)ld2->ld_lock;
11633e66db2Syamt 
11733e66db2Syamt 	if (a < b)
11833e66db2Syamt 		return -1;
119879d5dfbSrmind 	if (a > b)
120879d5dfbSrmind 		return 1;
12138d5e341Syamt 	return 0;
12238d5e341Syamt }
12338d5e341Syamt 
12438d5e341Syamt static signed int
125879d5dfbSrmind ld_rbto_compare_key(void *ctx, const void *n, const void *key)
12638d5e341Syamt {
127879d5dfbSrmind 	const lockdebug_t *ld = n;
12833e66db2Syamt 	const uintptr_t a = (uintptr_t)ld->ld_lock;
12933e66db2Syamt 	const uintptr_t b = (uintptr_t)key;
13033e66db2Syamt 
13133e66db2Syamt 	if (a < b)
13233e66db2Syamt 		return -1;
133879d5dfbSrmind 	if (a > b)
134879d5dfbSrmind 		return 1;
13538d5e341Syamt 	return 0;
13638d5e341Syamt }
13738d5e341Syamt 
138879d5dfbSrmind static rb_tree_t ld_rb_tree;
13938d5e341Syamt 
140879d5dfbSrmind static const rb_tree_ops_t ld_rb_tree_ops = {
1415a4f0c6bSmatt 	.rbto_compare_nodes = ld_rbto_compare_nodes,
1425a4f0c6bSmatt 	.rbto_compare_key = ld_rbto_compare_key,
143879d5dfbSrmind 	.rbto_node_offset = offsetof(lockdebug_t, ld_rb_node),
144879d5dfbSrmind 	.rbto_context = NULL
14538d5e341Syamt };
14638d5e341Syamt 
147671754ccSyamt static inline lockdebug_t *
148*e7f0067cSchristos lockdebug_lookup1(const volatile void *lock)
149671754ccSyamt {
150671754ccSyamt 	lockdebug_t *ld;
151a4e0004bSad 	struct cpu_info *ci;
152671754ccSyamt 
153a4e0004bSad 	ci = curcpu();
154a4e0004bSad 	__cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
155*e7f0067cSchristos 	ld = rb_tree_find_node(&ld_rb_tree, (void *)(intptr_t)lock);
156a4e0004bSad 	__cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
157a4e0004bSad 	if (ld == NULL) {
158671754ccSyamt 		return NULL;
159a4e0004bSad 	}
160a4e0004bSad 	__cpu_simple_lock(&ld->ld_spinlock);
161671754ccSyamt 
162671754ccSyamt 	return ld;
163671754ccSyamt }
164671754ccSyamt 
165a4e0004bSad static void
166a4e0004bSad lockdebug_lock_cpus(void)
167a4e0004bSad {
168a4e0004bSad 	CPU_INFO_ITERATOR cii;
169a4e0004bSad 	struct cpu_info *ci;
170a4e0004bSad 
171a4e0004bSad 	for (CPU_INFO_FOREACH(cii, ci)) {
172a4e0004bSad 		__cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
173a4e0004bSad 	}
174a4e0004bSad }
175a4e0004bSad 
176a4e0004bSad static void
177a4e0004bSad lockdebug_unlock_cpus(void)
178a4e0004bSad {
179a4e0004bSad 	CPU_INFO_ITERATOR cii;
180a4e0004bSad 	struct cpu_info *ci;
181a4e0004bSad 
182a4e0004bSad 	for (CPU_INFO_FOREACH(cii, ci)) {
183a4e0004bSad 		__cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
184a4e0004bSad 	}
185a4e0004bSad }
186a4e0004bSad 
187b07ec3fcSad /*
188b07ec3fcSad  * lockdebug_lookup:
189b07ec3fcSad  *
19038d5e341Syamt  *	Find a lockdebug structure by a pointer to a lock and return it locked.
191b07ec3fcSad  */
192b07ec3fcSad static inline lockdebug_t *
193*e7f0067cSchristos lockdebug_lookup(const char *func, size_t line, const volatile void *lock,
1949be065fbSchristos     uintptr_t where)
195b07ec3fcSad {
19638d5e341Syamt 	lockdebug_t *ld;
197b07ec3fcSad 
198a4e0004bSad 	ld = lockdebug_lookup1(lock);
199879d5dfbSrmind 	if (ld == NULL) {
2009be065fbSchristos 		panic("%s,%zu: uninitialized lock (lock=%p, from=%08"
2019be065fbSchristos 		    PRIxPTR ")", func, line, lock, where);
202879d5dfbSrmind 	}
203b07ec3fcSad 	return ld;
204b07ec3fcSad }
205b07ec3fcSad 
206b07ec3fcSad /*
207b07ec3fcSad  * lockdebug_init:
208b07ec3fcSad  *
209b07ec3fcSad  *	Initialize the lockdebug system.  Allocate an initial pool of
210b07ec3fcSad  *	lockdebug structures before the VM system is up and running.
211b07ec3fcSad  */
21264e54fbbSad static void
213b07ec3fcSad lockdebug_init(void)
214b07ec3fcSad {
215b07ec3fcSad 	lockdebug_t *ld;
216b07ec3fcSad 	int i;
217b07ec3fcSad 
218a4e0004bSad 	TAILQ_INIT(&curcpu()->ci_data.cpu_ld_locks);
219a4e0004bSad 	TAILQ_INIT(&curlwp->l_ld_locks);
220a4e0004bSad 	__cpu_simple_lock_init(&curcpu()->ci_data.cpu_ld_lock);
221a4e0004bSad 	__cpu_simple_lock_init(&ld_mod_lk);
2222cab8950Smatt 
22338d5e341Syamt 	rb_tree_init(&ld_rb_tree, &ld_rb_tree_ops);
22438d5e341Syamt 
225b07ec3fcSad 	ld = ld_prime;
226b07ec3fcSad 	for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
227a4e0004bSad 		__cpu_simple_lock_init(&ld->ld_spinlock);
228b07ec3fcSad 		TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
229b07ec3fcSad 		TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
230b07ec3fcSad 	}
231b07ec3fcSad 	ld_freeptr = 1;
232b07ec3fcSad 	ld_nfree = LD_BATCH - 1;
233b07ec3fcSad }
234b07ec3fcSad 
235b07ec3fcSad /*
236b07ec3fcSad  * lockdebug_alloc:
237b07ec3fcSad  *
238b07ec3fcSad  *	A lock is being initialized, so allocate an associated debug
239b07ec3fcSad  *	structure.
240b07ec3fcSad  */
24138d5e341Syamt bool
2429be065fbSchristos lockdebug_alloc(const char *func, size_t line, volatile void *lock,
2439be065fbSchristos     lockops_t *lo, uintptr_t initaddr)
244b07ec3fcSad {
245b07ec3fcSad 	struct cpu_info *ci;
246b07ec3fcSad 	lockdebug_t *ld;
247a4e0004bSad 	int s;
248b07ec3fcSad 
2497eb6056fSad 	if (lo == NULL || panicstr != NULL || ld_panic)
25038d5e341Syamt 		return false;
25164e54fbbSad 	if (ld_freeptr == 0)
25264e54fbbSad 		lockdebug_init();
253b07ec3fcSad 
254a4e0004bSad 	s = splhigh();
255a4e0004bSad 	__cpu_simple_lock(&ld_mod_lk);
256a4e0004bSad 	if ((ld = lockdebug_lookup1(lock)) != NULL) {
257a4e0004bSad 		__cpu_simple_unlock(&ld_mod_lk);
2589be065fbSchristos 		lockdebug_abort1(func, line, ld, s, "already initialized",
2599be065fbSchristos 		    true);
2607eb6056fSad 		return false;
261671754ccSyamt 	}
262671754ccSyamt 
263b07ec3fcSad 	/*
264b07ec3fcSad 	 * Pinch a new debug structure.  We may recurse because we call
265b07ec3fcSad 	 * kmem_alloc(), which may need to initialize new locks somewhere
2665492d866Sskrll 	 * down the path.  If not recursing, we try to maintain at least
267b07ec3fcSad 	 * LD_SLOP structures free, which should hopefully be enough to
268b07ec3fcSad 	 * satisfy kmem_alloc().  If we can't provide a structure, not to
269b07ec3fcSad 	 * worry: we'll just mark the lock as not having an ID.
270b07ec3fcSad 	 */
271461cd942Sad 	ci = curcpu();
272b07ec3fcSad 	ci->ci_lkdebug_recurse++;
273b07ec3fcSad 	if (TAILQ_EMPTY(&ld_free)) {
27464e54fbbSad 		if (ci->ci_lkdebug_recurse > 1 || ld_nomore) {
275b07ec3fcSad 			ci->ci_lkdebug_recurse--;
276a4e0004bSad 			__cpu_simple_unlock(&ld_mod_lk);
277a4e0004bSad 			splx(s);
27838d5e341Syamt 			return false;
279b07ec3fcSad 		}
280a4e0004bSad 		s = lockdebug_more(s);
281a4e0004bSad 	} else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP) {
282a4e0004bSad 		s = lockdebug_more(s);
283a4e0004bSad 	}
284b07ec3fcSad 	if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
285a4e0004bSad 		__cpu_simple_unlock(&ld_mod_lk);
286a4e0004bSad 		splx(s);
28738d5e341Syamt 		return false;
288b07ec3fcSad 	}
289b07ec3fcSad 	TAILQ_REMOVE(&ld_free, ld, ld_chain);
290b07ec3fcSad 	ld_nfree--;
291b07ec3fcSad 	ci->ci_lkdebug_recurse--;
292b07ec3fcSad 
293a4e0004bSad 	if (ld->ld_lock != NULL) {
2949be065fbSchristos 		panic("%s,%zu: corrupt table ld %p", func, line, ld);
295a4e0004bSad 	}
296b07ec3fcSad 
297b07ec3fcSad 	/* Initialise the structure. */
298b07ec3fcSad 	ld->ld_lock = lock;
299b07ec3fcSad 	ld->ld_lockops = lo;
300b07ec3fcSad 	ld->ld_locked = 0;
301b07ec3fcSad 	ld->ld_unlocked = 0;
302b07ec3fcSad 	ld->ld_lwp = NULL;
30311dc6399Sad 	ld->ld_initaddr = initaddr;
3047b8f5124Sad 	ld->ld_flags = (lo->lo_type == LOCKOPS_SLEEP ? LD_SLEEPER : 0);
305a4e0004bSad 	lockdebug_lock_cpus();
306879d5dfbSrmind 	(void)rb_tree_insert_node(&ld_rb_tree, __UNVOLATILE(ld));
307a4e0004bSad 	lockdebug_unlock_cpus();
308a4e0004bSad 	__cpu_simple_unlock(&ld_mod_lk);
30938d5e341Syamt 
310a4e0004bSad 	splx(s);
31138d5e341Syamt 	return true;
312b07ec3fcSad }
313b07ec3fcSad 
314b07ec3fcSad /*
315b07ec3fcSad  * lockdebug_free:
316b07ec3fcSad  *
317b07ec3fcSad  *	A lock is being destroyed, so release debugging resources.
318b07ec3fcSad  */
319b07ec3fcSad void
3209be065fbSchristos lockdebug_free(const char *func, size_t line, volatile void *lock)
321b07ec3fcSad {
322b07ec3fcSad 	lockdebug_t *ld;
323a4e0004bSad 	int s;
324b07ec3fcSad 
3257eb6056fSad 	if (panicstr != NULL || ld_panic)
326b07ec3fcSad 		return;
327b07ec3fcSad 
328a4e0004bSad 	s = splhigh();
329a4e0004bSad 	__cpu_simple_lock(&ld_mod_lk);
3309be065fbSchristos 	ld = lockdebug_lookup(func, line, lock,
3319be065fbSchristos 	    (uintptr_t) __builtin_return_address(0));
33238d5e341Syamt 	if (ld == NULL) {
333a4e0004bSad 		__cpu_simple_unlock(&ld_mod_lk);
3349be065fbSchristos 		panic("%s,%zu: destroying uninitialized object %p"
3359be065fbSchristos 		    "(ld_lock=%p)", func, line, lock, ld->ld_lock);
3367eb6056fSad 		return;
337b07ec3fcSad 	}
3387eb6056fSad 	if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
339a4e0004bSad 		__cpu_simple_unlock(&ld_mod_lk);
3409be065fbSchristos 		lockdebug_abort1(func, line, ld, s, "is locked or in use",
3419be065fbSchristos 		    true);
3427eb6056fSad 		return;
3437eb6056fSad 	}
344a4e0004bSad 	lockdebug_lock_cpus();
345879d5dfbSrmind 	rb_tree_remove_node(&ld_rb_tree, __UNVOLATILE(ld));
346a4e0004bSad 	lockdebug_unlock_cpus();
347b07ec3fcSad 	ld->ld_lock = NULL;
348b07ec3fcSad 	TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
349b07ec3fcSad 	ld_nfree++;
350a4e0004bSad 	__cpu_simple_unlock(&ld->ld_spinlock);
351a4e0004bSad 	__cpu_simple_unlock(&ld_mod_lk);
352a4e0004bSad 	splx(s);
353b07ec3fcSad }
354b07ec3fcSad 
355b07ec3fcSad /*
356b07ec3fcSad  * lockdebug_more:
357b07ec3fcSad  *
358b07ec3fcSad  *	Allocate a batch of debug structures and add to the free list.
359a4e0004bSad  *	Must be called with ld_mod_lk held.
360b07ec3fcSad  */
361a4e0004bSad static int
362a4e0004bSad lockdebug_more(int s)
363b07ec3fcSad {
364b07ec3fcSad 	lockdebug_t *ld;
365b07ec3fcSad 	void *block;
36664e54fbbSad 	int i, base, m;
367b07ec3fcSad 
3687b8f5124Sad 	/*
3697b8f5124Sad 	 * Can't call kmem_alloc() if in interrupt context.  XXX We could
3707b8f5124Sad 	 * deadlock, because we don't know which locks the caller holds.
3717b8f5124Sad 	 */
3727b8f5124Sad 	if (cpu_intr_p() || (curlwp->l_pflag & LP_INTR) != 0) {
3737b8f5124Sad 		return s;
3747b8f5124Sad 	}
3757b8f5124Sad 
376b07ec3fcSad 	while (ld_nfree < LD_SLOP) {
377a4e0004bSad 		__cpu_simple_unlock(&ld_mod_lk);
378a4e0004bSad 		splx(s);
379b07ec3fcSad 		block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
380a4e0004bSad 		s = splhigh();
381a4e0004bSad 		__cpu_simple_lock(&ld_mod_lk);
382b07ec3fcSad 
383b07ec3fcSad 		if (ld_nfree > LD_SLOP) {
384b07ec3fcSad 			/* Somebody beat us to it. */
385a4e0004bSad 			__cpu_simple_unlock(&ld_mod_lk);
386a4e0004bSad 			splx(s);
387b07ec3fcSad 			kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
388a4e0004bSad 			s = splhigh();
389a4e0004bSad 			__cpu_simple_lock(&ld_mod_lk);
390b07ec3fcSad 			continue;
391b07ec3fcSad 		}
392b07ec3fcSad 
393b07ec3fcSad 		base = ld_freeptr;
394b07ec3fcSad 		ld_nfree += LD_BATCH;
395b07ec3fcSad 		ld = block;
396b07ec3fcSad 		base <<= LD_BATCH_SHIFT;
39764e54fbbSad 		m = min(LD_MAX_LOCKS, base + LD_BATCH);
398b07ec3fcSad 
39964e54fbbSad 		if (m == LD_MAX_LOCKS)
40064e54fbbSad 			ld_nomore = true;
40164e54fbbSad 
40264e54fbbSad 		for (i = base; i < m; i++, ld++) {
403a4e0004bSad 			__cpu_simple_lock_init(&ld->ld_spinlock);
404b07ec3fcSad 			TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
405b07ec3fcSad 			TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
406b07ec3fcSad 		}
407b07ec3fcSad 
408b470ab62Sad 		membar_producer();
409b07ec3fcSad 	}
410a4e0004bSad 
411a4e0004bSad 	return s;
412b07ec3fcSad }
413b07ec3fcSad 
414b07ec3fcSad /*
415b07ec3fcSad  * lockdebug_wantlock:
416b07ec3fcSad  *
41748e395b1Spgoyette  *	Process the preamble to a lock acquire.  The "shared"
41848e395b1Spgoyette  *	parameter controls which ld_{ex,sh}want counter is
41948e395b1Spgoyette  *	updated; a negative value of shared updates neither.
420b07ec3fcSad  */
421b07ec3fcSad void
4229be065fbSchristos lockdebug_wantlock(const char *func, size_t line,
423*e7f0067cSchristos     const volatile void *lock, uintptr_t where, int shared)
424b07ec3fcSad {
425b07ec3fcSad 	struct lwp *l = curlwp;
426b07ec3fcSad 	lockdebug_t *ld;
427dd962f86Sthorpej 	bool recurse;
428a4e0004bSad 	int s;
429b07ec3fcSad 
430b07ec3fcSad 	(void)shared;
4314f3d5a9cSthorpej 	recurse = false;
432b07ec3fcSad 
4337eb6056fSad 	if (panicstr != NULL || ld_panic)
434b07ec3fcSad 		return;
435b07ec3fcSad 
436a4e0004bSad 	s = splhigh();
4379be065fbSchristos 	if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
438a4e0004bSad 		splx(s);
439b07ec3fcSad 		return;
440a4e0004bSad 	}
441839080f7Syamt 	if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
442b07ec3fcSad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
443060c06beSmlelstv 			if (ld->ld_lwp == l)
4444f3d5a9cSthorpej 				recurse = true;
445ac8f6353Srmind 		} else if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
4464f3d5a9cSthorpej 			recurse = true;
447b07ec3fcSad 	}
44811dc6399Sad 	if (cpu_intr_p()) {
4497eb6056fSad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
4509be065fbSchristos 			lockdebug_abort1(func, line, ld, s,
45111dc6399Sad 			    "acquiring sleep lock from interrupt context",
45211dc6399Sad 			    true);
4537eb6056fSad 			return;
4547eb6056fSad 		}
45511dc6399Sad 	}
45648e395b1Spgoyette 	if (shared > 0)
457b07ec3fcSad 		ld->ld_shwant++;
45848e395b1Spgoyette 	else if (shared == 0)
459b07ec3fcSad 		ld->ld_exwant++;
4607eb6056fSad 	if (recurse) {
4619be065fbSchristos 		lockdebug_abort1(func, line, ld, s, "locking against myself",
46211dc6399Sad 		    true);
4637eb6056fSad 		return;
4647eb6056fSad 	}
465a4e0004bSad 	__cpu_simple_unlock(&ld->ld_spinlock);
466a4e0004bSad 	splx(s);
467b07ec3fcSad }
468b07ec3fcSad 
469b07ec3fcSad /*
470b07ec3fcSad  * lockdebug_locked:
471b07ec3fcSad  *
472b07ec3fcSad  *	Process a lock acquire operation.
473b07ec3fcSad  */
474b07ec3fcSad void
4759be065fbSchristos lockdebug_locked(const char *func, size_t line,
4769be065fbSchristos     volatile void *lock, void *cvlock, uintptr_t where, int shared)
477b07ec3fcSad {
478b07ec3fcSad 	struct lwp *l = curlwp;
479b07ec3fcSad 	lockdebug_t *ld;
480a4e0004bSad 	int s;
481b07ec3fcSad 
4827eb6056fSad 	if (panicstr != NULL || ld_panic)
483b07ec3fcSad 		return;
484b07ec3fcSad 
485a4e0004bSad 	s = splhigh();
4869be065fbSchristos 	if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
487a4e0004bSad 		splx(s);
488b07ec3fcSad 		return;
489a4e0004bSad 	}
4907b8f5124Sad 	if (cvlock) {
4917b8f5124Sad 		KASSERT(ld->ld_lockops->lo_type == LOCKOPS_CV);
4927b8f5124Sad 		if (lock == (void *)&lbolt) {
4937b8f5124Sad 			/* nothing */
4947b8f5124Sad 		} else if (ld->ld_shares++ == 0) {
4957b8f5124Sad 			ld->ld_locked = (uintptr_t)cvlock;
4967b8f5124Sad 		} else if (cvlock != (void *)ld->ld_locked) {
4979be065fbSchristos 			lockdebug_abort1(func, line, ld, s,
4989be065fbSchristos 			    "multiple locks used with condition variable",
4999be065fbSchristos 			    true);
5007b8f5124Sad 			return;
5017b8f5124Sad 		}
5027b8f5124Sad 	} else if (shared) {
503b07ec3fcSad 		l->l_shlocks++;
5049d109b30Syamt 		ld->ld_locked = where;
505b07ec3fcSad 		ld->ld_shares++;
506b07ec3fcSad 		ld->ld_shwant--;
507b07ec3fcSad 	} else {
5087eb6056fSad 		if ((ld->ld_flags & LD_LOCKED) != 0) {
5099be065fbSchristos 			lockdebug_abort1(func, line, ld, s, "already locked",
510a4e0004bSad 			    true);
5117eb6056fSad 			return;
5127eb6056fSad 		}
513b07ec3fcSad 		ld->ld_flags |= LD_LOCKED;
514b07ec3fcSad 		ld->ld_locked = where;
515b07ec3fcSad 		ld->ld_exwant--;
516b07ec3fcSad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
517a4e0004bSad 			TAILQ_INSERT_TAIL(&l->l_ld_locks, ld, ld_chain);
518b07ec3fcSad 		} else {
519a4e0004bSad 			TAILQ_INSERT_TAIL(&curcpu()->ci_data.cpu_ld_locks,
520a4e0004bSad 			    ld, ld_chain);
521b07ec3fcSad 		}
522b07ec3fcSad 	}
523ac8f6353Srmind 	ld->ld_cpu = (uint16_t)cpu_index(curcpu());
524839080f7Syamt 	ld->ld_lwp = l;
525a4e0004bSad 	__cpu_simple_unlock(&ld->ld_spinlock);
526a4e0004bSad 	splx(s);
527b07ec3fcSad }
528b07ec3fcSad 
529b07ec3fcSad /*
530b07ec3fcSad  * lockdebug_unlocked:
531b07ec3fcSad  *
532b07ec3fcSad  *	Process a lock release operation.
533b07ec3fcSad  */
534b07ec3fcSad void
5359be065fbSchristos lockdebug_unlocked(const char *func, size_t line,
5369be065fbSchristos     volatile void *lock, uintptr_t where, int shared)
537b07ec3fcSad {
538b07ec3fcSad 	struct lwp *l = curlwp;
539b07ec3fcSad 	lockdebug_t *ld;
540a4e0004bSad 	int s;
541b07ec3fcSad 
5427eb6056fSad 	if (panicstr != NULL || ld_panic)
543b07ec3fcSad 		return;
544b07ec3fcSad 
545a4e0004bSad 	s = splhigh();
5469be065fbSchristos 	if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
547a4e0004bSad 		splx(s);
548b07ec3fcSad 		return;
549a4e0004bSad 	}
5507b8f5124Sad 	if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
5517b8f5124Sad 		if (lock == (void *)&lbolt) {
5527b8f5124Sad 			/* nothing */
5537b8f5124Sad 		} else {
5547b8f5124Sad 			ld->ld_shares--;
5557b8f5124Sad 		}
5567b8f5124Sad 	} else if (shared) {
5577eb6056fSad 		if (l->l_shlocks == 0) {
5589be065fbSchristos 			lockdebug_abort1(func, line, ld, s,
55911dc6399Sad 			    "no shared locks held by LWP", true);
5607eb6056fSad 			return;
5617eb6056fSad 		}
5627eb6056fSad 		if (ld->ld_shares == 0) {
5639be065fbSchristos 			lockdebug_abort1(func, line, ld, s,
56411dc6399Sad 			    "no shared holds on this lock", true);
5657eb6056fSad 			return;
5667eb6056fSad 		}
567b07ec3fcSad 		l->l_shlocks--;
568b07ec3fcSad 		ld->ld_shares--;
5699d109b30Syamt 		if (ld->ld_lwp == l) {
5709d109b30Syamt 			ld->ld_unlocked = where;
571839080f7Syamt 			ld->ld_lwp = NULL;
5729d109b30Syamt 		}
573ac8f6353Srmind 		if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
574839080f7Syamt 			ld->ld_cpu = (uint16_t)-1;
575b07ec3fcSad 	} else {
5767eb6056fSad 		if ((ld->ld_flags & LD_LOCKED) == 0) {
5779be065fbSchristos 			lockdebug_abort1(func, line, ld, s, "not locked", true);
5787eb6056fSad 			return;
5797eb6056fSad 		}
580b07ec3fcSad 
581b07ec3fcSad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
5827eb6056fSad 			if (ld->ld_lwp != curlwp) {
5839be065fbSchristos 				lockdebug_abort1(func, line, ld, s,
58411dc6399Sad 				    "not held by current LWP", true);
5857eb6056fSad 				return;
5867eb6056fSad 			}
587a4e0004bSad 			TAILQ_REMOVE(&l->l_ld_locks, ld, ld_chain);
588b07ec3fcSad 		} else {
589ac8f6353Srmind 			if (ld->ld_cpu != (uint16_t)cpu_index(curcpu())) {
5909be065fbSchristos 				lockdebug_abort1(func, line, ld, s,
59111dc6399Sad 				    "not held by current CPU", true);
5927eb6056fSad 				return;
5937eb6056fSad 			}
594a4e0004bSad 			TAILQ_REMOVE(&curcpu()->ci_data.cpu_ld_locks, ld,
595a4e0004bSad 			    ld_chain);
596b07ec3fcSad 		}
597521a86d5Smatt 		ld->ld_flags &= ~LD_LOCKED;
598521a86d5Smatt 		ld->ld_unlocked = where;
599521a86d5Smatt 		ld->ld_lwp = NULL;
600b07ec3fcSad 	}
601a4e0004bSad 	__cpu_simple_unlock(&ld->ld_spinlock);
602a4e0004bSad 	splx(s);
603b07ec3fcSad }
604b07ec3fcSad 
605b07ec3fcSad /*
6067b8f5124Sad  * lockdebug_wakeup:
6077b8f5124Sad  *
6087b8f5124Sad  *	Process a wakeup on a condition variable.
6097b8f5124Sad  */
6107b8f5124Sad void
6119be065fbSchristos lockdebug_wakeup(const char *func, size_t line, volatile void *lock,
6129be065fbSchristos     uintptr_t where)
6137b8f5124Sad {
6147b8f5124Sad 	lockdebug_t *ld;
6157b8f5124Sad 	int s;
6167b8f5124Sad 
6177b8f5124Sad 	if (panicstr != NULL || ld_panic || lock == (void *)&lbolt)
6187b8f5124Sad 		return;
6197b8f5124Sad 
6207b8f5124Sad 	s = splhigh();
6217b8f5124Sad 	/* Find the CV... */
6229be065fbSchristos 	if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
6237b8f5124Sad 		splx(s);
6247b8f5124Sad 		return;
6257b8f5124Sad 	}
6267b8f5124Sad 	/*
6277b8f5124Sad 	 * If it has any waiters, ensure that they are using the
6287b8f5124Sad 	 * same interlock.
6297b8f5124Sad 	 */
6307b8f5124Sad 	if (ld->ld_shares != 0 && !mutex_owned((kmutex_t *)ld->ld_locked)) {
6319be065fbSchristos 		lockdebug_abort1(func, line, ld, s, "interlocking mutex not "
6327b8f5124Sad 		    "held during wakeup", true);
6337b8f5124Sad 		return;
6347b8f5124Sad 	}
6357b8f5124Sad 	__cpu_simple_unlock(&ld->ld_spinlock);
6367b8f5124Sad 	splx(s);
6377b8f5124Sad }
6387b8f5124Sad 
6397b8f5124Sad /*
640b07ec3fcSad  * lockdebug_barrier:
641b07ec3fcSad  *
642b07ec3fcSad  *	Panic if we hold more than one specified spin lock, and optionally,
643b07ec3fcSad  *	if we hold sleep locks.
644b07ec3fcSad  */
645b07ec3fcSad void
6469be065fbSchristos lockdebug_barrier(const char *func, size_t line, volatile void *spinlock,
6479be065fbSchristos     int slplocks)
648b07ec3fcSad {
649b07ec3fcSad 	struct lwp *l = curlwp;
650b07ec3fcSad 	lockdebug_t *ld;
651a4e0004bSad 	int s;
652b07ec3fcSad 
6537eb6056fSad 	if (panicstr != NULL || ld_panic)
654b07ec3fcSad 		return;
655b07ec3fcSad 
656a4e0004bSad 	s = splhigh();
657a4e0004bSad 	if ((l->l_pflag & LP_INTR) == 0) {
658a4e0004bSad 		TAILQ_FOREACH(ld, &curcpu()->ci_data.cpu_ld_locks, ld_chain) {
659b07ec3fcSad 			if (ld->ld_lock == spinlock) {
660b07ec3fcSad 				continue;
661b07ec3fcSad 			}
662a4e0004bSad 			__cpu_simple_lock(&ld->ld_spinlock);
6639be065fbSchristos 			lockdebug_abort1(func, line, ld, s,
664a4e0004bSad 			    "spin lock held", true);
6657eb6056fSad 			return;
6667eb6056fSad 		}
667b07ec3fcSad 	}
668a4e0004bSad 	if (slplocks) {
669a4e0004bSad 		splx(s);
6707eb6056fSad 		return;
6717eb6056fSad 	}
672a4e0004bSad 	if ((ld = TAILQ_FIRST(&l->l_ld_locks)) != NULL) {
673a4e0004bSad 		__cpu_simple_lock(&ld->ld_spinlock);
6749be065fbSchristos 		lockdebug_abort1(func, line, ld, s, "sleep lock held", true);
675a4e0004bSad 		return;
676b07ec3fcSad 	}
677a4e0004bSad 	splx(s);
678a4e0004bSad 	if (l->l_shlocks != 0) {
679a27531bcSchristos 		TAILQ_FOREACH(ld, &ld_all, ld_achain) {
680a27531bcSchristos 			if (ld->ld_lockops->lo_type == LOCKOPS_CV)
681a27531bcSchristos 				continue;
682a27531bcSchristos 			if (ld->ld_lwp == l)
683a27531bcSchristos 				lockdebug_dump(ld, printf);
684a27531bcSchristos 		}
6859be065fbSchristos 		panic("%s,%zu: holding %d shared locks", func, line,
6869be065fbSchristos 		    l->l_shlocks);
687b07ec3fcSad 	}
688b07ec3fcSad }
689b07ec3fcSad 
690b07ec3fcSad /*
69111dc6399Sad  * lockdebug_mem_check:
69211dc6399Sad  *
69311dc6399Sad  *	Check for in-use locks within a memory region that is
69438d5e341Syamt  *	being freed.
69511dc6399Sad  */
69611dc6399Sad void
6979be065fbSchristos lockdebug_mem_check(const char *func, size_t line, void *base, size_t sz)
69811dc6399Sad {
69911dc6399Sad 	lockdebug_t *ld;
700a4e0004bSad 	struct cpu_info *ci;
701461cd942Sad 	int s;
70211dc6399Sad 
7037eb6056fSad 	if (panicstr != NULL || ld_panic)
704ea3f10f7Sad 		return;
705ea3f10f7Sad 
706a4e0004bSad 	s = splhigh();
707a4e0004bSad 	ci = curcpu();
708a4e0004bSad 	__cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
70938d5e341Syamt 	ld = (lockdebug_t *)rb_tree_find_node_geq(&ld_rb_tree, base);
710461cd942Sad 	if (ld != NULL) {
711461cd942Sad 		const uintptr_t lock = (uintptr_t)ld->ld_lock;
712461cd942Sad 
713461cd942Sad 		if ((uintptr_t)base > lock)
7149be065fbSchristos 			panic("%s,%zu: corrupt tree ld=%p, base=%p, sz=%zu",
7159be065fbSchristos 			    func, line, ld, base, sz);
716461cd942Sad 		if (lock >= (uintptr_t)base + sz)
717461cd942Sad 			ld = NULL;
718461cd942Sad 	}
719a4e0004bSad 	__cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
720a4e0004bSad 	if (ld != NULL) {
721a4e0004bSad 		__cpu_simple_lock(&ld->ld_spinlock);
7229be065fbSchristos 		lockdebug_abort1(func, line, ld, s,
72311dc6399Sad 		    "allocation contains active lock", !cold);
724a4e0004bSad 		return;
725a4e0004bSad 	}
726a4e0004bSad 	splx(s);
72711dc6399Sad }
72811dc6399Sad 
72911dc6399Sad /*
730b07ec3fcSad  * lockdebug_dump:
731b07ec3fcSad  *
732b07ec3fcSad  *	Dump information about a lock on panic, or for DDB.
733b07ec3fcSad  */
734b07ec3fcSad static void
735a67c3c89Schristos lockdebug_dump(lockdebug_t *ld, void (*pr)(const char *, ...)
736a67c3c89Schristos     __printflike(1, 2))
737b07ec3fcSad {
738b07ec3fcSad 	int sleeper = (ld->ld_flags & LD_SLEEPER);
739b07ec3fcSad 
740b07ec3fcSad 	(*pr)(
741b07ec3fcSad 	    "lock address : %#018lx type     : %18s\n"
7427b8f5124Sad 	    "initialized  : %#018lx",
7437b8f5124Sad 	    (long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
7447b8f5124Sad 	    (long)ld->ld_initaddr);
7457b8f5124Sad 
7467b8f5124Sad 	if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
7475fb876b9Snjoly 		(*pr)(" interlock: %#018lx\n", (long)ld->ld_locked);
7487b8f5124Sad 	} else {
7497b8f5124Sad 		(*pr)("\n"
750b07ec3fcSad 		    "shared holds : %18u exclusive: %18u\n"
751b07ec3fcSad 		    "shares wanted: %18u exclusive: %18u\n"
752b07ec3fcSad 		    "current cpu  : %18u last held: %18u\n"
753b07ec3fcSad 		    "current lwp  : %#018lx last held: %#018lx\n"
754521a86d5Smatt 		    "last locked%c : %#018lx unlocked%c: %#018lx\n",
755b07ec3fcSad 		    (unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
756b07ec3fcSad 		    (unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
757ac8f6353Srmind 		    (unsigned)cpu_index(curcpu()), (unsigned)ld->ld_cpu,
758b07ec3fcSad 		    (long)curlwp, (long)ld->ld_lwp,
759521a86d5Smatt 		    ((ld->ld_flags & LD_LOCKED) ? '*' : ' '),
760521a86d5Smatt 		    (long)ld->ld_locked,
761521a86d5Smatt 		    ((ld->ld_flags & LD_LOCKED) ? ' ' : '*'),
762521a86d5Smatt 		    (long)ld->ld_unlocked);
7637b8f5124Sad 	}
764b07ec3fcSad 
765b07ec3fcSad 	if (ld->ld_lockops->lo_dump != NULL)
766b07ec3fcSad 		(*ld->ld_lockops->lo_dump)(ld->ld_lock);
767b07ec3fcSad 
768b07ec3fcSad 	if (sleeper) {
769b07ec3fcSad 		(*pr)("\n");
770b07ec3fcSad 		turnstile_print(ld->ld_lock, pr);
771b07ec3fcSad 	}
772b07ec3fcSad }
773b07ec3fcSad 
774b07ec3fcSad /*
7757eb6056fSad  * lockdebug_abort1:
776b07ec3fcSad  *
7777eb6056fSad  *	An error has been trapped - dump lock info and panic.
778b07ec3fcSad  */
77964e54fbbSad static void
7809be065fbSchristos lockdebug_abort1(const char *func, size_t line, lockdebug_t *ld, int s,
78111dc6399Sad 		 const char *msg, bool dopanic)
782b07ec3fcSad {
783b07ec3fcSad 
7847eb6056fSad 	/*
785d9ddb522Schristos 	 * Don't make the situation worse if the system is already going
7867eb6056fSad 	 * down in flames.  Once a panic is triggered, lockdebug state
7877eb6056fSad 	 * becomes stale and cannot be trusted.
7887eb6056fSad 	 */
7897eb6056fSad 	if (atomic_inc_uint_nv(&ld_panic) != 1) {
790a4e0004bSad 		__cpu_simple_unlock(&ld->ld_spinlock);
791a4e0004bSad 		splx(s);
7927eb6056fSad 		return;
7937eb6056fSad 	}
7947eb6056fSad 
7959be065fbSchristos 	printf_nolog("%s error: %s,%zu: %s\n\n", ld->ld_lockops->lo_name,
7969be065fbSchristos 	    func, line, msg);
797b07ec3fcSad 	lockdebug_dump(ld, printf_nolog);
798a4e0004bSad 	__cpu_simple_unlock(&ld->ld_spinlock);
799a4e0004bSad 	splx(s);
800b07ec3fcSad 	printf_nolog("\n");
80111dc6399Sad 	if (dopanic)
8029be065fbSchristos 		panic("LOCKDEBUG: %s error: %s,%zu: %s",
8039be065fbSchristos 		    ld->ld_lockops->lo_name, func, line, msg);
804b07ec3fcSad }
805b07ec3fcSad 
806b07ec3fcSad #endif	/* LOCKDEBUG */
807b07ec3fcSad 
808b07ec3fcSad /*
809b07ec3fcSad  * lockdebug_lock_print:
810b07ec3fcSad  *
811b07ec3fcSad  *	Handle the DDB 'show lock' command.
812b07ec3fcSad  */
813b07ec3fcSad #ifdef DDB
814b07ec3fcSad void
815b07ec3fcSad lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
816b07ec3fcSad {
817b07ec3fcSad #ifdef LOCKDEBUG
818b07ec3fcSad 	lockdebug_t *ld;
819b07ec3fcSad 
820b07ec3fcSad 	TAILQ_FOREACH(ld, &ld_all, ld_achain) {
821648f423cSdyoung 		if (ld->ld_lock == NULL)
822648f423cSdyoung 			continue;
823648f423cSdyoung 		if (addr == NULL || ld->ld_lock == addr) {
824b07ec3fcSad 			lockdebug_dump(ld, pr);
825648f423cSdyoung 			if (addr != NULL)
826b07ec3fcSad 				return;
827b07ec3fcSad 		}
828b07ec3fcSad 	}
829648f423cSdyoung 	if (addr != NULL) {
830648f423cSdyoung 		(*pr)("Sorry, no record of a lock with address %p found.\n",
831648f423cSdyoung 		    addr);
832648f423cSdyoung 	}
833b07ec3fcSad #else
834b07ec3fcSad 	(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
835b07ec3fcSad #endif	/* LOCKDEBUG */
836b07ec3fcSad }
837b07ec3fcSad #endif	/* DDB */
838b07ec3fcSad 
839b07ec3fcSad /*
840b07ec3fcSad  * lockdebug_abort:
841b07ec3fcSad  *
842b07ec3fcSad  *	An error has been trapped - dump lock info and call panic().
843b07ec3fcSad  */
844b07ec3fcSad void
845*e7f0067cSchristos lockdebug_abort(const char *func, size_t line, const volatile void *lock,
8469be065fbSchristos     lockops_t *ops, const char *msg)
847b07ec3fcSad {
848b07ec3fcSad #ifdef LOCKDEBUG
849b07ec3fcSad 	lockdebug_t *ld;
850a4e0004bSad 	int s;
851b07ec3fcSad 
852a4e0004bSad 	s = splhigh();
8539be065fbSchristos 	if ((ld = lockdebug_lookup(func, line, lock,
854ca70a1c0Srafal 			(uintptr_t) __builtin_return_address(0))) != NULL) {
8559be065fbSchristos 		lockdebug_abort1(func, line, ld, s, msg, true);
856a4e0004bSad 		return;
857b07ec3fcSad 	}
858a4e0004bSad 	splx(s);
859b07ec3fcSad #endif	/* LOCKDEBUG */
860b07ec3fcSad 
8617eb6056fSad 	/*
8627eb6056fSad 	 * Complain first on the occurrance only.  Otherwise proceeed to
8637eb6056fSad 	 * panic where we will `rendezvous' with other CPUs if the machine
8647eb6056fSad 	 * is going down in flames.
8657eb6056fSad 	 */
8667eb6056fSad 	if (atomic_inc_uint_nv(&ld_panic) == 1) {
8679be065fbSchristos 		printf_nolog("%s error: %s,%zu: %s\n\n"
868b07ec3fcSad 		    "lock address : %#018lx\n"
869b07ec3fcSad 		    "current cpu  : %18d\n"
870b07ec3fcSad 		    "current lwp  : %#018lx\n",
8719be065fbSchristos 		    ops->lo_name, func, line, msg, (long)lock,
872ac8f6353Srmind 		    (int)cpu_index(curcpu()), (long)curlwp);
873b07ec3fcSad 		(*ops->lo_dump)(lock);
874b07ec3fcSad 		printf_nolog("\n");
8757eb6056fSad 	}
8767eb6056fSad 
8779be065fbSchristos 	panic("lock error: %s: %s,%zu: %s: lock %p cpu %d lwp %p",
8789be065fbSchristos 	    ops->lo_name, func, line, msg, lock, cpu_index(curcpu()), curlwp);
879b07ec3fcSad }
880