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