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