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