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