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