Home | History | Annotate | Line # | Download | only in kern
subr_lockdebug.c revision 1.8.6.9
      1  1.8.6.9  jmcneill /*	$NetBSD: subr_lockdebug.c,v 1.8.6.9 2007/12/09 19:38:23 jmcneill Exp $	*/
      2      1.2        ad 
      3      1.2        ad /*-
      4      1.2        ad  * Copyright (c) 2006, 2007 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  * 3. All advertising materials mentioning features or use of this software
     19      1.2        ad  *    must display the following acknowledgement:
     20      1.2        ad  *	This product includes software developed by the NetBSD
     21      1.2        ad  *	Foundation, Inc. and its contributors.
     22      1.2        ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.2        ad  *    contributors may be used to endorse or promote products derived
     24      1.2        ad  *    from this software without specific prior written permission.
     25      1.2        ad  *
     26      1.2        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.2        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.2        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.2        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.2        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.2        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.2        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.2        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.2        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.2        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.2        ad  * POSSIBILITY OF SUCH DAMAGE.
     37      1.2        ad  */
     38      1.2        ad 
     39      1.2        ad /*
     40  1.8.6.3     joerg  * Basic lock debugging code shared among lock primitives.
     41      1.2        ad  */
     42      1.2        ad 
     43  1.8.6.1     joerg #include <sys/cdefs.h>
     44  1.8.6.9  jmcneill __KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.8.6.9 2007/12/09 19:38:23 jmcneill Exp $");
     45  1.8.6.1     joerg 
     46      1.2        ad #include "opt_ddb.h"
     47      1.2        ad 
     48      1.2        ad #include <sys/param.h>
     49      1.2        ad #include <sys/proc.h>
     50      1.2        ad #include <sys/systm.h>
     51  1.8.6.2     joerg #include <sys/kernel.h>
     52      1.2        ad #include <sys/kmem.h>
     53      1.2        ad #include <sys/lock.h>
     54      1.2        ad #include <sys/lockdebug.h>
     55      1.2        ad #include <sys/sleepq.h>
     56  1.8.6.2     joerg #include <sys/cpu.h>
     57  1.8.6.8     joerg #include <sys/atomic.h>
     58      1.2        ad 
     59  1.8.6.6     joerg #include <lib/libkern/rb.h>
     60  1.8.6.6     joerg 
     61      1.2        ad #ifdef LOCKDEBUG
     62      1.2        ad 
     63      1.2        ad #define	LD_BATCH_SHIFT	9
     64      1.2        ad #define	LD_BATCH	(1 << LD_BATCH_SHIFT)
     65      1.2        ad #define	LD_BATCH_MASK	(LD_BATCH - 1)
     66      1.2        ad #define	LD_MAX_LOCKS	1048576
     67      1.2        ad #define	LD_SLOP		16
     68      1.2        ad 
     69      1.2        ad #define	LD_LOCKED	0x01
     70      1.2        ad #define	LD_SLEEPER	0x02
     71      1.2        ad 
     72  1.8.6.9  jmcneill #define	LD_WRITE_LOCK	0x80000000
     73  1.8.6.9  jmcneill 
     74      1.2        ad typedef union lockdebuglk {
     75      1.2        ad 	struct {
     76  1.8.6.9  jmcneill 		u_int	lku_lock;
     77  1.8.6.9  jmcneill 		int	lku_oldspl;
     78      1.2        ad 	} ul;
     79  1.8.6.9  jmcneill 	uint8_t	lk_pad[CACHE_LINE_SIZE];
     80  1.8.6.9  jmcneill } volatile __aligned(CACHE_LINE_SIZE) lockdebuglk_t;
     81      1.2        ad 
     82      1.2        ad #define	lk_lock		ul.lku_lock
     83      1.2        ad #define	lk_oldspl	ul.lku_oldspl
     84      1.2        ad 
     85      1.2        ad typedef struct lockdebug {
     86  1.8.6.6     joerg 	struct rb_node	ld_rb_node;	/* must be the first member */
     87      1.2        ad 	_TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
     88      1.2        ad 	_TAILQ_ENTRY(struct lockdebug, volatile) ld_achain;
     89      1.2        ad 	volatile void	*ld_lock;
     90      1.2        ad 	lockops_t	*ld_lockops;
     91      1.2        ad 	struct lwp	*ld_lwp;
     92      1.2        ad 	uintptr_t	ld_locked;
     93      1.2        ad 	uintptr_t	ld_unlocked;
     94  1.8.6.2     joerg 	uintptr_t	ld_initaddr;
     95      1.2        ad 	uint16_t	ld_shares;
     96      1.2        ad 	uint16_t	ld_cpu;
     97      1.2        ad 	uint8_t		ld_flags;
     98      1.2        ad 	uint8_t		ld_shwant;	/* advisory */
     99      1.2        ad 	uint8_t		ld_exwant;	/* advisory */
    100      1.2        ad 	uint8_t		ld_unused;
    101      1.2        ad } volatile lockdebug_t;
    102      1.2        ad 
    103      1.2        ad typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
    104      1.2        ad 
    105  1.8.6.6     joerg lockdebuglk_t		ld_tree_lk;
    106      1.2        ad lockdebuglk_t		ld_sleeper_lk;
    107      1.2        ad lockdebuglk_t		ld_spinner_lk;
    108      1.2        ad lockdebuglk_t		ld_free_lk;
    109      1.2        ad 
    110  1.8.6.5     joerg lockdebuglist_t		ld_sleepers = TAILQ_HEAD_INITIALIZER(ld_sleepers);
    111  1.8.6.5     joerg lockdebuglist_t		ld_spinners = TAILQ_HEAD_INITIALIZER(ld_spinners);
    112  1.8.6.5     joerg lockdebuglist_t		ld_free = TAILQ_HEAD_INITIALIZER(ld_free);
    113  1.8.6.5     joerg lockdebuglist_t		ld_all = TAILQ_HEAD_INITIALIZER(ld_all);
    114      1.2        ad int			ld_nfree;
    115      1.2        ad int			ld_freeptr;
    116      1.2        ad int			ld_recurse;
    117      1.5        ad bool			ld_nomore;
    118      1.2        ad lockdebug_t		*ld_table[LD_MAX_LOCKS / LD_BATCH];
    119      1.2        ad 
    120      1.2        ad lockdebug_t		ld_prime[LD_BATCH];
    121      1.2        ad 
    122      1.5        ad static void	lockdebug_abort1(lockdebug_t *, lockdebuglk_t *lk,
    123  1.8.6.2     joerg 				 const char *, const char *, bool);
    124      1.5        ad static void	lockdebug_more(void);
    125      1.5        ad static void	lockdebug_init(void);
    126      1.2        ad 
    127  1.8.6.6     joerg static signed int
    128  1.8.6.6     joerg ld_rb_compare_nodes(const struct rb_node *n1, const struct rb_node *n2)
    129  1.8.6.6     joerg {
    130  1.8.6.6     joerg 	const lockdebug_t *ld1 = (const void *)n1;
    131  1.8.6.6     joerg 	const lockdebug_t *ld2 = (const void *)n2;
    132  1.8.6.7     joerg 	const uintptr_t a = (uintptr_t)ld1->ld_lock;
    133  1.8.6.7     joerg 	const uintptr_t b = (uintptr_t)ld2->ld_lock;
    134  1.8.6.7     joerg 
    135  1.8.6.7     joerg 	if (a < b)
    136  1.8.6.6     joerg 		return 1;
    137  1.8.6.7     joerg 	if (a > b)
    138  1.8.6.7     joerg 		return -1;
    139  1.8.6.6     joerg 	return 0;
    140  1.8.6.6     joerg }
    141  1.8.6.6     joerg 
    142  1.8.6.6     joerg static signed int
    143  1.8.6.6     joerg ld_rb_compare_key(const struct rb_node *n, const void *key)
    144  1.8.6.6     joerg {
    145  1.8.6.6     joerg 	const lockdebug_t *ld = (const void *)n;
    146  1.8.6.7     joerg 	const uintptr_t a = (uintptr_t)ld->ld_lock;
    147  1.8.6.7     joerg 	const uintptr_t b = (uintptr_t)key;
    148  1.8.6.7     joerg 
    149  1.8.6.7     joerg 	if (a < b)
    150  1.8.6.6     joerg 		return 1;
    151  1.8.6.7     joerg 	if (a > b)
    152  1.8.6.7     joerg 		return -1;
    153  1.8.6.6     joerg 	return 0;
    154  1.8.6.6     joerg }
    155  1.8.6.6     joerg 
    156  1.8.6.6     joerg static struct rb_tree ld_rb_tree;
    157  1.8.6.6     joerg 
    158  1.8.6.6     joerg static const struct rb_tree_ops ld_rb_tree_ops = {
    159  1.8.6.6     joerg 	.rb_compare_nodes = ld_rb_compare_nodes,
    160  1.8.6.6     joerg 	.rb_compare_key = ld_rb_compare_key,
    161  1.8.6.6     joerg };
    162  1.8.6.6     joerg 
    163  1.8.6.9  jmcneill static void
    164  1.8.6.9  jmcneill lockdebug_lock_init(lockdebuglk_t *lk)
    165  1.8.6.9  jmcneill {
    166  1.8.6.9  jmcneill 
    167  1.8.6.9  jmcneill 	lk->lk_lock = 0;
    168  1.8.6.9  jmcneill }
    169  1.8.6.9  jmcneill 
    170  1.8.6.9  jmcneill static void
    171      1.2        ad lockdebug_lock(lockdebuglk_t *lk)
    172      1.2        ad {
    173      1.2        ad 	int s;
    174      1.2        ad 
    175      1.8        ad 	s = splhigh();
    176  1.8.6.9  jmcneill 	do {
    177  1.8.6.9  jmcneill 		while (lk->lk_lock != 0) {
    178  1.8.6.9  jmcneill 			SPINLOCK_SPIN_HOOK;
    179  1.8.6.9  jmcneill 		}
    180  1.8.6.9  jmcneill 	} while (atomic_cas_uint(&lk->lk_lock, 0, LD_WRITE_LOCK) != 0);
    181      1.2        ad 	lk->lk_oldspl = s;
    182  1.8.6.9  jmcneill 	membar_enter();
    183      1.2        ad }
    184      1.2        ad 
    185  1.8.6.9  jmcneill static void
    186      1.2        ad lockdebug_unlock(lockdebuglk_t *lk)
    187      1.2        ad {
    188      1.2        ad 	int s;
    189      1.2        ad 
    190      1.2        ad 	s = lk->lk_oldspl;
    191  1.8.6.9  jmcneill 	membar_exit();
    192  1.8.6.9  jmcneill 	lk->lk_lock = 0;
    193  1.8.6.9  jmcneill 	splx(s);
    194  1.8.6.9  jmcneill }
    195  1.8.6.9  jmcneill 
    196  1.8.6.9  jmcneill static int
    197  1.8.6.9  jmcneill lockdebug_lock_rd(lockdebuglk_t *lk)
    198  1.8.6.9  jmcneill {
    199  1.8.6.9  jmcneill 	u_int val;
    200  1.8.6.9  jmcneill 	int s;
    201  1.8.6.9  jmcneill 
    202  1.8.6.9  jmcneill 	s = splhigh();
    203  1.8.6.9  jmcneill 	do {
    204  1.8.6.9  jmcneill 		while ((val = lk->lk_lock) == LD_WRITE_LOCK){
    205  1.8.6.9  jmcneill 			SPINLOCK_SPIN_HOOK;
    206  1.8.6.9  jmcneill 		}
    207  1.8.6.9  jmcneill 	} while (atomic_cas_uint(&lk->lk_lock, val, val + 1) != val);
    208  1.8.6.9  jmcneill 	membar_enter();
    209  1.8.6.9  jmcneill 	return s;
    210  1.8.6.9  jmcneill }
    211  1.8.6.9  jmcneill 
    212  1.8.6.9  jmcneill static void
    213  1.8.6.9  jmcneill lockdebug_unlock_rd(lockdebuglk_t *lk, int s)
    214  1.8.6.9  jmcneill {
    215  1.8.6.9  jmcneill 
    216  1.8.6.9  jmcneill 	membar_exit();
    217  1.8.6.9  jmcneill 	atomic_dec_uint(&lk->lk_lock);
    218      1.2        ad 	splx(s);
    219      1.2        ad }
    220      1.2        ad 
    221  1.8.6.6     joerg static inline lockdebug_t *
    222  1.8.6.6     joerg lockdebug_lookup1(volatile void *lock, lockdebuglk_t **lk)
    223  1.8.6.2     joerg {
    224  1.8.6.6     joerg 	lockdebug_t *ld;
    225  1.8.6.9  jmcneill 	int s;
    226  1.8.6.6     joerg 
    227  1.8.6.9  jmcneill 	s = lockdebug_lock_rd(&ld_tree_lk);
    228  1.8.6.6     joerg 	ld = (lockdebug_t *)rb_tree_find_node(&ld_rb_tree, __UNVOLATILE(lock));
    229  1.8.6.9  jmcneill 	lockdebug_unlock_rd(&ld_tree_lk, s);
    230  1.8.6.6     joerg 	if (ld == NULL)
    231  1.8.6.6     joerg 		return NULL;
    232  1.8.6.6     joerg 
    233  1.8.6.6     joerg 	if ((ld->ld_flags & LD_SLEEPER) != 0)
    234  1.8.6.6     joerg 		*lk = &ld_sleeper_lk;
    235  1.8.6.6     joerg 	else
    236  1.8.6.6     joerg 		*lk = &ld_spinner_lk;
    237  1.8.6.2     joerg 
    238  1.8.6.2     joerg 	lockdebug_lock(*lk);
    239  1.8.6.6     joerg 	return ld;
    240  1.8.6.2     joerg }
    241  1.8.6.2     joerg 
    242      1.2        ad /*
    243      1.2        ad  * lockdebug_lookup:
    244      1.2        ad  *
    245  1.8.6.6     joerg  *	Find a lockdebug structure by a pointer to a lock and return it locked.
    246      1.2        ad  */
    247      1.2        ad static inline lockdebug_t *
    248  1.8.6.6     joerg lockdebug_lookup(volatile void *lock, lockdebuglk_t **lk)
    249      1.2        ad {
    250  1.8.6.6     joerg 	lockdebug_t *ld;
    251      1.2        ad 
    252  1.8.6.6     joerg 	ld = lockdebug_lookup1(lock, lk);
    253  1.8.6.6     joerg 	if (ld == NULL)
    254  1.8.6.6     joerg 		panic("lockdebug_lookup: uninitialized lock (lock=%p)", lock);
    255      1.2        ad 	return ld;
    256      1.2        ad }
    257      1.2        ad 
    258      1.2        ad /*
    259      1.2        ad  * lockdebug_init:
    260      1.2        ad  *
    261      1.2        ad  *	Initialize the lockdebug system.  Allocate an initial pool of
    262      1.2        ad  *	lockdebug structures before the VM system is up and running.
    263      1.2        ad  */
    264      1.5        ad static void
    265      1.2        ad lockdebug_init(void)
    266      1.2        ad {
    267      1.2        ad 	lockdebug_t *ld;
    268      1.2        ad 	int i;
    269      1.2        ad 
    270  1.8.6.9  jmcneill 	lockdebug_lock_init(&ld_tree_lk);
    271  1.8.6.9  jmcneill 	lockdebug_lock_init(&ld_sleeper_lk);
    272  1.8.6.9  jmcneill 	lockdebug_lock_init(&ld_spinner_lk);
    273  1.8.6.9  jmcneill 	lockdebug_lock_init(&ld_free_lk);
    274      1.2        ad 
    275  1.8.6.6     joerg 	rb_tree_init(&ld_rb_tree, &ld_rb_tree_ops);
    276  1.8.6.6     joerg 
    277      1.2        ad 	ld = ld_prime;
    278      1.2        ad 	ld_table[0] = ld;
    279      1.2        ad 	for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
    280      1.2        ad 		TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
    281      1.2        ad 		TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
    282      1.2        ad 	}
    283      1.2        ad 	ld_freeptr = 1;
    284      1.2        ad 	ld_nfree = LD_BATCH - 1;
    285      1.2        ad }
    286      1.2        ad 
    287      1.2        ad /*
    288      1.2        ad  * lockdebug_alloc:
    289      1.2        ad  *
    290      1.2        ad  *	A lock is being initialized, so allocate an associated debug
    291      1.2        ad  *	structure.
    292      1.2        ad  */
    293  1.8.6.6     joerg bool
    294  1.8.6.2     joerg lockdebug_alloc(volatile void *lock, lockops_t *lo, uintptr_t initaddr)
    295      1.2        ad {
    296      1.2        ad 	struct cpu_info *ci;
    297      1.2        ad 	lockdebug_t *ld;
    298  1.8.6.6     joerg 	lockdebuglk_t *lk;
    299      1.2        ad 
    300      1.5        ad 	if (lo == NULL || panicstr != NULL)
    301  1.8.6.6     joerg 		return false;
    302      1.5        ad 	if (ld_freeptr == 0)
    303      1.5        ad 		lockdebug_init();
    304      1.2        ad 
    305  1.8.6.6     joerg 	if ((ld = lockdebug_lookup1(lock, &lk)) != NULL) {
    306  1.8.6.6     joerg 		lockdebug_abort1(ld, lk, __func__, "already initialized", true);
    307  1.8.6.6     joerg 		/* NOTREACHED */
    308  1.8.6.6     joerg 	}
    309  1.8.6.6     joerg 
    310      1.2        ad 	/*
    311      1.2        ad 	 * Pinch a new debug structure.  We may recurse because we call
    312      1.2        ad 	 * kmem_alloc(), which may need to initialize new locks somewhere
    313      1.7     skrll 	 * down the path.  If not recursing, we try to maintain at least
    314      1.2        ad 	 * LD_SLOP structures free, which should hopefully be enough to
    315      1.2        ad 	 * satisfy kmem_alloc().  If we can't provide a structure, not to
    316      1.2        ad 	 * worry: we'll just mark the lock as not having an ID.
    317      1.2        ad 	 */
    318      1.2        ad 	lockdebug_lock(&ld_free_lk);
    319  1.8.6.9  jmcneill 	ci = curcpu();
    320      1.2        ad 	ci->ci_lkdebug_recurse++;
    321      1.2        ad 
    322      1.2        ad 	if (TAILQ_EMPTY(&ld_free)) {
    323      1.5        ad 		if (ci->ci_lkdebug_recurse > 1 || ld_nomore) {
    324      1.2        ad 			ci->ci_lkdebug_recurse--;
    325      1.2        ad 			lockdebug_unlock(&ld_free_lk);
    326  1.8.6.6     joerg 			return false;
    327      1.2        ad 		}
    328      1.2        ad 		lockdebug_more();
    329      1.2        ad 	} else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP)
    330      1.2        ad 		lockdebug_more();
    331      1.2        ad 
    332      1.2        ad 	if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
    333      1.2        ad 		lockdebug_unlock(&ld_free_lk);
    334  1.8.6.6     joerg 		return false;
    335      1.2        ad 	}
    336      1.2        ad 
    337      1.2        ad 	TAILQ_REMOVE(&ld_free, ld, ld_chain);
    338      1.2        ad 	ld_nfree--;
    339      1.2        ad 
    340      1.2        ad 	ci->ci_lkdebug_recurse--;
    341      1.2        ad 	lockdebug_unlock(&ld_free_lk);
    342      1.2        ad 
    343      1.2        ad 	if (ld->ld_lock != NULL)
    344      1.2        ad 		panic("lockdebug_alloc: corrupt table");
    345      1.2        ad 
    346      1.2        ad 	if (lo->lo_sleeplock)
    347      1.2        ad 		lockdebug_lock(&ld_sleeper_lk);
    348      1.2        ad 	else
    349      1.2        ad 		lockdebug_lock(&ld_spinner_lk);
    350      1.2        ad 
    351      1.2        ad 	/* Initialise the structure. */
    352      1.2        ad 	ld->ld_lock = lock;
    353      1.2        ad 	ld->ld_lockops = lo;
    354      1.2        ad 	ld->ld_locked = 0;
    355      1.2        ad 	ld->ld_unlocked = 0;
    356      1.2        ad 	ld->ld_lwp = NULL;
    357  1.8.6.2     joerg 	ld->ld_initaddr = initaddr;
    358      1.2        ad 
    359  1.8.6.6     joerg 	lockdebug_lock(&ld_tree_lk);
    360  1.8.6.6     joerg 	rb_tree_insert_node(&ld_rb_tree, __UNVOLATILE(&ld->ld_rb_node));
    361  1.8.6.6     joerg 	lockdebug_unlock(&ld_tree_lk);
    362  1.8.6.6     joerg 
    363      1.2        ad 	if (lo->lo_sleeplock) {
    364      1.2        ad 		ld->ld_flags = LD_SLEEPER;
    365      1.2        ad 		lockdebug_unlock(&ld_sleeper_lk);
    366      1.2        ad 	} else {
    367      1.2        ad 		ld->ld_flags = 0;
    368      1.2        ad 		lockdebug_unlock(&ld_spinner_lk);
    369      1.2        ad 	}
    370      1.2        ad 
    371  1.8.6.6     joerg 	return true;
    372      1.2        ad }
    373      1.2        ad 
    374      1.2        ad /*
    375      1.2        ad  * lockdebug_free:
    376      1.2        ad  *
    377      1.2        ad  *	A lock is being destroyed, so release debugging resources.
    378      1.2        ad  */
    379      1.2        ad void
    380  1.8.6.6     joerg lockdebug_free(volatile void *lock)
    381      1.2        ad {
    382      1.2        ad 	lockdebug_t *ld;
    383      1.2        ad 	lockdebuglk_t *lk;
    384      1.2        ad 
    385      1.2        ad 	if (panicstr != NULL)
    386      1.2        ad 		return;
    387      1.2        ad 
    388  1.8.6.6     joerg 	ld = lockdebug_lookup(lock, &lk);
    389  1.8.6.6     joerg 	if (ld == NULL) {
    390      1.2        ad 		panic("lockdebug_free: destroying uninitialized lock %p"
    391  1.8.6.6     joerg 		    "(ld_lock=%p)", lock, ld->ld_lock);
    392  1.8.6.2     joerg 		lockdebug_abort1(ld, lk, __func__, "lock record follows",
    393  1.8.6.2     joerg 		    true);
    394      1.2        ad 	}
    395      1.2        ad 	if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0)
    396  1.8.6.2     joerg 		lockdebug_abort1(ld, lk, __func__, "is locked", true);
    397  1.8.6.6     joerg 	lockdebug_lock(&ld_tree_lk);
    398  1.8.6.6     joerg 	rb_tree_remove_node(&ld_rb_tree, __UNVOLATILE(&ld->ld_rb_node));
    399  1.8.6.6     joerg 	lockdebug_unlock(&ld_tree_lk);
    400      1.2        ad 	ld->ld_lock = NULL;
    401      1.2        ad 	lockdebug_unlock(lk);
    402      1.2        ad 
    403      1.2        ad 	lockdebug_lock(&ld_free_lk);
    404      1.2        ad 	TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
    405      1.2        ad 	ld_nfree++;
    406      1.2        ad 	lockdebug_unlock(&ld_free_lk);
    407      1.2        ad }
    408      1.2        ad 
    409      1.2        ad /*
    410      1.2        ad  * lockdebug_more:
    411      1.2        ad  *
    412      1.2        ad  *	Allocate a batch of debug structures and add to the free list.
    413      1.2        ad  *	Must be called with ld_free_lk held.
    414      1.2        ad  */
    415      1.5        ad static void
    416      1.2        ad lockdebug_more(void)
    417      1.2        ad {
    418      1.2        ad 	lockdebug_t *ld;
    419      1.2        ad 	void *block;
    420      1.5        ad 	int i, base, m;
    421      1.2        ad 
    422      1.2        ad 	while (ld_nfree < LD_SLOP) {
    423      1.2        ad 		lockdebug_unlock(&ld_free_lk);
    424      1.2        ad 		block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
    425      1.2        ad 		lockdebug_lock(&ld_free_lk);
    426      1.2        ad 
    427      1.2        ad 		if (block == NULL)
    428      1.2        ad 			return;
    429      1.2        ad 
    430      1.2        ad 		if (ld_nfree > LD_SLOP) {
    431      1.2        ad 			/* Somebody beat us to it. */
    432      1.2        ad 			lockdebug_unlock(&ld_free_lk);
    433      1.2        ad 			kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
    434      1.2        ad 			lockdebug_lock(&ld_free_lk);
    435      1.2        ad 			continue;
    436      1.2        ad 		}
    437      1.2        ad 
    438      1.2        ad 		base = ld_freeptr;
    439      1.2        ad 		ld_nfree += LD_BATCH;
    440      1.2        ad 		ld = block;
    441      1.2        ad 		base <<= LD_BATCH_SHIFT;
    442      1.5        ad 		m = min(LD_MAX_LOCKS, base + LD_BATCH);
    443      1.5        ad 
    444      1.5        ad 		if (m == LD_MAX_LOCKS)
    445      1.5        ad 			ld_nomore = true;
    446      1.2        ad 
    447      1.5        ad 		for (i = base; i < m; i++, ld++) {
    448      1.2        ad 			TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
    449      1.2        ad 			TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
    450      1.2        ad 		}
    451      1.2        ad 
    452  1.8.6.8     joerg 		membar_producer();
    453      1.2        ad 		ld_table[ld_freeptr++] = block;
    454      1.2        ad 	}
    455      1.2        ad }
    456      1.2        ad 
    457      1.2        ad /*
    458      1.2        ad  * lockdebug_wantlock:
    459      1.2        ad  *
    460      1.2        ad  *	Process the preamble to a lock acquire.
    461      1.2        ad  */
    462      1.2        ad void
    463  1.8.6.6     joerg lockdebug_wantlock(volatile void *lock, uintptr_t where, int shared)
    464      1.2        ad {
    465      1.2        ad 	struct lwp *l = curlwp;
    466      1.2        ad 	lockdebuglk_t *lk;
    467      1.2        ad 	lockdebug_t *ld;
    468      1.3   thorpej 	bool recurse;
    469      1.2        ad 
    470      1.2        ad 	(void)shared;
    471      1.4   thorpej 	recurse = false;
    472      1.2        ad 
    473      1.2        ad 	if (panicstr != NULL)
    474      1.2        ad 		return;
    475      1.2        ad 
    476  1.8.6.6     joerg 	if ((ld = lockdebug_lookup(lock, &lk)) == NULL)
    477      1.2        ad 		return;
    478      1.2        ad 
    479      1.2        ad 	if ((ld->ld_flags & LD_LOCKED) != 0) {
    480      1.2        ad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
    481      1.2        ad 			if (ld->ld_lwp == l)
    482      1.4   thorpej 				recurse = true;
    483      1.2        ad 		} else if (ld->ld_cpu == (uint16_t)cpu_number())
    484      1.4   thorpej 			recurse = true;
    485      1.2        ad 	}
    486      1.2        ad 
    487  1.8.6.2     joerg 	if (cpu_intr_p()) {
    488  1.8.6.2     joerg 		if ((ld->ld_flags & LD_SLEEPER) != 0)
    489  1.8.6.2     joerg 			lockdebug_abort1(ld, lk, __func__,
    490  1.8.6.2     joerg 			    "acquiring sleep lock from interrupt context",
    491  1.8.6.2     joerg 			    true);
    492  1.8.6.2     joerg 	}
    493  1.8.6.2     joerg 
    494      1.2        ad 	if (shared)
    495      1.2        ad 		ld->ld_shwant++;
    496      1.2        ad 	else
    497      1.2        ad 		ld->ld_exwant++;
    498      1.2        ad 
    499      1.2        ad 	if (recurse)
    500  1.8.6.2     joerg 		lockdebug_abort1(ld, lk, __func__, "locking against myself",
    501  1.8.6.2     joerg 		    true);
    502      1.2        ad 
    503      1.2        ad 	lockdebug_unlock(lk);
    504      1.2        ad }
    505      1.2        ad 
    506      1.2        ad /*
    507      1.2        ad  * lockdebug_locked:
    508      1.2        ad  *
    509      1.2        ad  *	Process a lock acquire operation.
    510      1.2        ad  */
    511      1.2        ad void
    512  1.8.6.6     joerg lockdebug_locked(volatile void *lock, uintptr_t where, int shared)
    513      1.2        ad {
    514      1.2        ad 	struct lwp *l = curlwp;
    515      1.2        ad 	lockdebuglk_t *lk;
    516      1.2        ad 	lockdebug_t *ld;
    517      1.2        ad 
    518      1.2        ad 	if (panicstr != NULL)
    519      1.2        ad 		return;
    520      1.2        ad 
    521  1.8.6.6     joerg 	if ((ld = lockdebug_lookup(lock, &lk)) == NULL)
    522      1.2        ad 		return;
    523      1.2        ad 
    524      1.2        ad 	if (shared) {
    525      1.2        ad 		l->l_shlocks++;
    526      1.2        ad 		ld->ld_shares++;
    527      1.2        ad 		ld->ld_shwant--;
    528      1.2        ad 	} else {
    529      1.2        ad 		if ((ld->ld_flags & LD_LOCKED) != 0)
    530      1.2        ad 			lockdebug_abort1(ld, lk, __func__,
    531  1.8.6.2     joerg 			    "already locked", true);
    532      1.2        ad 
    533      1.2        ad 		ld->ld_flags |= LD_LOCKED;
    534      1.2        ad 		ld->ld_locked = where;
    535      1.2        ad 		ld->ld_cpu = (uint16_t)cpu_number();
    536      1.2        ad 		ld->ld_lwp = l;
    537      1.2        ad 		ld->ld_exwant--;
    538      1.2        ad 
    539      1.2        ad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
    540      1.2        ad 			l->l_exlocks++;
    541      1.2        ad 			TAILQ_INSERT_TAIL(&ld_sleepers, ld, ld_chain);
    542      1.2        ad 		} else {
    543      1.2        ad 			curcpu()->ci_spin_locks2++;
    544      1.2        ad 			TAILQ_INSERT_TAIL(&ld_spinners, ld, ld_chain);
    545      1.2        ad 		}
    546      1.2        ad 	}
    547      1.2        ad 
    548      1.2        ad 	lockdebug_unlock(lk);
    549      1.2        ad }
    550      1.2        ad 
    551      1.2        ad /*
    552      1.2        ad  * lockdebug_unlocked:
    553      1.2        ad  *
    554      1.2        ad  *	Process a lock release operation.
    555      1.2        ad  */
    556      1.2        ad void
    557  1.8.6.6     joerg lockdebug_unlocked(volatile void *lock, uintptr_t where, int shared)
    558      1.2        ad {
    559      1.2        ad 	struct lwp *l = curlwp;
    560      1.2        ad 	lockdebuglk_t *lk;
    561      1.2        ad 	lockdebug_t *ld;
    562      1.2        ad 
    563      1.2        ad 	if (panicstr != NULL)
    564      1.2        ad 		return;
    565      1.2        ad 
    566  1.8.6.6     joerg 	if ((ld = lockdebug_lookup(lock, &lk)) == NULL)
    567      1.2        ad 		return;
    568      1.2        ad 
    569      1.2        ad 	if (shared) {
    570      1.2        ad 		if (l->l_shlocks == 0)
    571      1.2        ad 			lockdebug_abort1(ld, lk, __func__,
    572  1.8.6.2     joerg 			    "no shared locks held by LWP", true);
    573      1.2        ad 		if (ld->ld_shares == 0)
    574      1.2        ad 			lockdebug_abort1(ld, lk, __func__,
    575  1.8.6.2     joerg 			    "no shared holds on this lock", true);
    576      1.2        ad 		l->l_shlocks--;
    577      1.2        ad 		ld->ld_shares--;
    578      1.2        ad 	} else {
    579      1.2        ad 		if ((ld->ld_flags & LD_LOCKED) == 0)
    580  1.8.6.2     joerg 			lockdebug_abort1(ld, lk, __func__, "not locked",
    581  1.8.6.2     joerg 			    true);
    582      1.2        ad 
    583      1.2        ad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
    584      1.2        ad 			if (ld->ld_lwp != curlwp)
    585      1.2        ad 				lockdebug_abort1(ld, lk, __func__,
    586  1.8.6.2     joerg 				    "not held by current LWP", true);
    587      1.2        ad 			ld->ld_flags &= ~LD_LOCKED;
    588      1.2        ad 			ld->ld_unlocked = where;
    589      1.2        ad 			ld->ld_lwp = NULL;
    590      1.2        ad 			curlwp->l_exlocks--;
    591      1.2        ad 			TAILQ_REMOVE(&ld_sleepers, ld, ld_chain);
    592      1.2        ad 		} else {
    593      1.2        ad 			if (ld->ld_cpu != (uint16_t)cpu_number())
    594      1.2        ad 				lockdebug_abort1(ld, lk, __func__,
    595  1.8.6.2     joerg 				    "not held by current CPU", true);
    596      1.2        ad 			ld->ld_flags &= ~LD_LOCKED;
    597      1.2        ad 			ld->ld_unlocked = where;
    598      1.2        ad 			ld->ld_lwp = NULL;
    599      1.2        ad 			curcpu()->ci_spin_locks2--;
    600      1.2        ad 			TAILQ_REMOVE(&ld_spinners, ld, ld_chain);
    601      1.2        ad 		}
    602      1.2        ad 	}
    603      1.2        ad 
    604      1.2        ad 	lockdebug_unlock(lk);
    605      1.2        ad }
    606      1.2        ad 
    607      1.2        ad /*
    608      1.2        ad  * lockdebug_barrier:
    609      1.2        ad  *
    610      1.2        ad  *	Panic if we hold more than one specified spin lock, and optionally,
    611      1.2        ad  *	if we hold sleep locks.
    612      1.2        ad  */
    613      1.2        ad void
    614      1.2        ad lockdebug_barrier(volatile void *spinlock, int slplocks)
    615      1.2        ad {
    616      1.2        ad 	struct lwp *l = curlwp;
    617      1.2        ad 	lockdebug_t *ld;
    618      1.2        ad 	uint16_t cpuno;
    619  1.8.6.9  jmcneill 	int s;
    620      1.2        ad 
    621      1.2        ad 	if (panicstr != NULL)
    622      1.2        ad 		return;
    623      1.2        ad 
    624  1.8.6.9  jmcneill 	crit_enter();
    625  1.8.6.9  jmcneill 
    626      1.2        ad 	if (curcpu()->ci_spin_locks2 != 0) {
    627      1.2        ad 		cpuno = (uint16_t)cpu_number();
    628      1.2        ad 
    629  1.8.6.9  jmcneill 		s = lockdebug_lock_rd(&ld_spinner_lk);
    630      1.2        ad 		TAILQ_FOREACH(ld, &ld_spinners, ld_chain) {
    631      1.2        ad 			if (ld->ld_lock == spinlock) {
    632      1.2        ad 				if (ld->ld_cpu != cpuno)
    633      1.2        ad 					lockdebug_abort1(ld, &ld_spinner_lk,
    634      1.2        ad 					    __func__,
    635  1.8.6.2     joerg 					    "not held by current CPU", true);
    636      1.2        ad 				continue;
    637      1.2        ad 			}
    638  1.8.6.4     joerg 			if (ld->ld_cpu == cpuno && (l->l_pflag & LP_INTR) == 0)
    639      1.2        ad 				lockdebug_abort1(ld, &ld_spinner_lk,
    640  1.8.6.2     joerg 				    __func__, "spin lock held", true);
    641      1.2        ad 		}
    642  1.8.6.9  jmcneill 		lockdebug_unlock_rd(&ld_spinner_lk, s);
    643      1.2        ad 	}
    644      1.2        ad 
    645      1.2        ad 	if (!slplocks) {
    646      1.2        ad 		if (l->l_exlocks != 0) {
    647  1.8.6.9  jmcneill 			s = lockdebug_lock_rd(&ld_sleeper_lk);
    648      1.2        ad 			TAILQ_FOREACH(ld, &ld_sleepers, ld_chain) {
    649      1.2        ad 				if (ld->ld_lwp == l)
    650      1.2        ad 					lockdebug_abort1(ld, &ld_sleeper_lk,
    651  1.8.6.2     joerg 					    __func__, "sleep lock held", true);
    652      1.2        ad 			}
    653  1.8.6.9  jmcneill 			lockdebug_unlock_rd(&ld_sleeper_lk, s);
    654      1.2        ad 		}
    655      1.2        ad 		if (l->l_shlocks != 0)
    656      1.2        ad 			panic("lockdebug_barrier: holding %d shared locks",
    657      1.2        ad 			    l->l_shlocks);
    658      1.2        ad 	}
    659  1.8.6.9  jmcneill 
    660  1.8.6.9  jmcneill 	crit_exit();
    661      1.2        ad }
    662      1.2        ad 
    663      1.2        ad /*
    664  1.8.6.2     joerg  * lockdebug_mem_check:
    665  1.8.6.2     joerg  *
    666  1.8.6.2     joerg  *	Check for in-use locks within a memory region that is
    667  1.8.6.6     joerg  *	being freed.
    668  1.8.6.2     joerg  */
    669  1.8.6.2     joerg void
    670  1.8.6.2     joerg lockdebug_mem_check(const char *func, void *base, size_t sz)
    671  1.8.6.2     joerg {
    672  1.8.6.2     joerg 	lockdebug_t *ld;
    673  1.8.6.6     joerg 	lockdebuglk_t *lk;
    674  1.8.6.9  jmcneill 	int s;
    675  1.8.6.2     joerg 
    676  1.8.6.9  jmcneill 	s = lockdebug_lock_rd(&ld_tree_lk);
    677  1.8.6.6     joerg 	ld = (lockdebug_t *)rb_tree_find_node_geq(&ld_rb_tree, base);
    678  1.8.6.9  jmcneill 	if (ld != NULL) {
    679  1.8.6.9  jmcneill 		const uintptr_t lock = (uintptr_t)ld->ld_lock;
    680  1.8.6.9  jmcneill 
    681  1.8.6.9  jmcneill 		if ((uintptr_t)base > lock)
    682  1.8.6.9  jmcneill 			panic("%s: corrupt tree ld=%p, base=%p, sz=%zu",
    683  1.8.6.9  jmcneill 			    __func__, ld, base, sz);
    684  1.8.6.9  jmcneill 		if (lock >= (uintptr_t)base + sz)
    685  1.8.6.9  jmcneill 			ld = NULL;
    686  1.8.6.9  jmcneill 	}
    687  1.8.6.9  jmcneill 	lockdebug_unlock_rd(&ld_tree_lk, s);
    688  1.8.6.6     joerg 	if (ld == NULL)
    689  1.8.6.6     joerg 		return;
    690  1.8.6.9  jmcneill 
    691  1.8.6.6     joerg 	if ((ld->ld_flags & LD_SLEEPER) != 0)
    692  1.8.6.6     joerg 		lk = &ld_sleeper_lk;
    693  1.8.6.6     joerg 	else
    694  1.8.6.6     joerg 		lk = &ld_spinner_lk;
    695  1.8.6.2     joerg 
    696  1.8.6.6     joerg 	lockdebug_lock(lk);
    697  1.8.6.9  jmcneill 	lockdebug_abort1(ld, lk, func,
    698  1.8.6.9  jmcneill 	    "allocation contains active lock", !cold);
    699  1.8.6.9  jmcneill 	return;
    700  1.8.6.2     joerg }
    701  1.8.6.2     joerg 
    702  1.8.6.2     joerg /*
    703      1.2        ad  * lockdebug_dump:
    704      1.2        ad  *
    705      1.2        ad  *	Dump information about a lock on panic, or for DDB.
    706      1.2        ad  */
    707      1.2        ad static void
    708      1.2        ad lockdebug_dump(lockdebug_t *ld, void (*pr)(const char *, ...))
    709      1.2        ad {
    710      1.2        ad 	int sleeper = (ld->ld_flags & LD_SLEEPER);
    711      1.2        ad 
    712      1.2        ad 	(*pr)(
    713      1.2        ad 	    "lock address : %#018lx type     : %18s\n"
    714      1.2        ad 	    "shared holds : %18u exclusive: %18u\n"
    715      1.2        ad 	    "shares wanted: %18u exclusive: %18u\n"
    716      1.2        ad 	    "current cpu  : %18u last held: %18u\n"
    717      1.2        ad 	    "current lwp  : %#018lx last held: %#018lx\n"
    718  1.8.6.2     joerg 	    "last locked  : %#018lx unlocked : %#018lx\n"
    719  1.8.6.2     joerg 	    "initialized  : %#018lx\n",
    720      1.2        ad 	    (long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
    721      1.2        ad 	    (unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
    722      1.2        ad 	    (unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
    723      1.2        ad 	    (unsigned)cpu_number(), (unsigned)ld->ld_cpu,
    724      1.2        ad 	    (long)curlwp, (long)ld->ld_lwp,
    725  1.8.6.2     joerg 	    (long)ld->ld_locked, (long)ld->ld_unlocked,
    726  1.8.6.2     joerg 	    (long)ld->ld_initaddr);
    727      1.2        ad 
    728      1.2        ad 	if (ld->ld_lockops->lo_dump != NULL)
    729      1.2        ad 		(*ld->ld_lockops->lo_dump)(ld->ld_lock);
    730      1.2        ad 
    731      1.2        ad 	if (sleeper) {
    732      1.2        ad 		(*pr)("\n");
    733      1.2        ad 		turnstile_print(ld->ld_lock, pr);
    734      1.2        ad 	}
    735      1.2        ad }
    736      1.2        ad 
    737      1.2        ad /*
    738      1.2        ad  * lockdebug_dump:
    739      1.2        ad  *
    740      1.2        ad  *	Dump information about a known lock.
    741      1.2        ad  */
    742      1.5        ad static void
    743      1.2        ad lockdebug_abort1(lockdebug_t *ld, lockdebuglk_t *lk, const char *func,
    744  1.8.6.2     joerg 		 const char *msg, bool dopanic)
    745      1.2        ad {
    746      1.2        ad 
    747      1.2        ad 	printf_nolog("%s error: %s: %s\n\n", ld->ld_lockops->lo_name,
    748      1.2        ad 	    func, msg);
    749      1.2        ad 	lockdebug_dump(ld, printf_nolog);
    750      1.2        ad 	lockdebug_unlock(lk);
    751      1.2        ad 	printf_nolog("\n");
    752  1.8.6.2     joerg 	if (dopanic)
    753  1.8.6.2     joerg 		panic("LOCKDEBUG");
    754      1.2        ad }
    755      1.2        ad 
    756      1.2        ad #endif	/* LOCKDEBUG */
    757      1.2        ad 
    758      1.2        ad /*
    759      1.2        ad  * lockdebug_lock_print:
    760      1.2        ad  *
    761      1.2        ad  *	Handle the DDB 'show lock' command.
    762      1.2        ad  */
    763      1.2        ad #ifdef DDB
    764      1.2        ad void
    765      1.2        ad lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
    766      1.2        ad {
    767      1.2        ad #ifdef LOCKDEBUG
    768      1.2        ad 	lockdebug_t *ld;
    769      1.2        ad 
    770      1.2        ad 	TAILQ_FOREACH(ld, &ld_all, ld_achain) {
    771      1.2        ad 		if (ld->ld_lock == addr) {
    772      1.2        ad 			lockdebug_dump(ld, pr);
    773      1.2        ad 			return;
    774      1.2        ad 		}
    775      1.2        ad 	}
    776      1.2        ad 	(*pr)("Sorry, no record of a lock with address %p found.\n", addr);
    777      1.2        ad #else
    778      1.2        ad 	(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
    779      1.2        ad #endif	/* LOCKDEBUG */
    780      1.2        ad }
    781      1.2        ad #endif	/* DDB */
    782      1.2        ad 
    783      1.2        ad /*
    784      1.2        ad  * lockdebug_abort:
    785      1.2        ad  *
    786      1.2        ad  *	An error has been trapped - dump lock info and call panic().
    787      1.2        ad  */
    788      1.2        ad void
    789  1.8.6.6     joerg lockdebug_abort(volatile void *lock, lockops_t *ops, const char *func,
    790  1.8.6.6     joerg 		const char *msg)
    791      1.2        ad {
    792      1.2        ad #ifdef LOCKDEBUG
    793      1.2        ad 	lockdebug_t *ld;
    794      1.2        ad 	lockdebuglk_t *lk;
    795      1.2        ad 
    796  1.8.6.6     joerg 	if ((ld = lockdebug_lookup(lock, &lk)) != NULL) {
    797  1.8.6.2     joerg 		lockdebug_abort1(ld, lk, func, msg, true);
    798      1.2        ad 		/* NOTREACHED */
    799      1.2        ad 	}
    800      1.2        ad #endif	/* LOCKDEBUG */
    801      1.2        ad 
    802      1.2        ad 	printf_nolog("%s error: %s: %s\n\n"
    803      1.2        ad 	    "lock address : %#018lx\n"
    804      1.2        ad 	    "current cpu  : %18d\n"
    805      1.2        ad 	    "current lwp  : %#018lx\n",
    806      1.2        ad 	    ops->lo_name, func, msg, (long)lock, (int)cpu_number(),
    807      1.2        ad 	    (long)curlwp);
    808      1.2        ad 
    809      1.2        ad 	(*ops->lo_dump)(lock);
    810      1.2        ad 
    811      1.2        ad 	printf_nolog("\n");
    812      1.2        ad 	panic("lock error");
    813      1.2        ad }
    814