Home | History | Annotate | Line # | Download | only in kern
subr_lockdebug.c revision 1.1.2.11
      1  1.1.2.11  ad /*	$NetBSD: subr_lockdebug.c,v 1.1.2.11 2007/02/06 13:11:48 ad Exp $	*/
      2   1.1.2.1  ad 
      3   1.1.2.1  ad /*-
      4   1.1.2.6  ad  * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
      5   1.1.2.1  ad  * All rights reserved.
      6   1.1.2.1  ad  *
      7   1.1.2.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1.2.1  ad  * by Andrew Doran.
      9   1.1.2.1  ad  *
     10   1.1.2.1  ad  * Redistribution and use in source and binary forms, with or without
     11   1.1.2.1  ad  * modification, are permitted provided that the following conditions
     12   1.1.2.1  ad  * are met:
     13   1.1.2.1  ad  * 1. Redistributions of source code must retain the above copyright
     14   1.1.2.1  ad  *    notice, this list of conditions and the following disclaimer.
     15   1.1.2.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1.2.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1.2.1  ad  *    documentation and/or other materials provided with the distribution.
     18   1.1.2.1  ad  * 3. All advertising materials mentioning features or use of this software
     19   1.1.2.1  ad  *    must display the following acknowledgement:
     20   1.1.2.1  ad  *	This product includes software developed by the NetBSD
     21   1.1.2.1  ad  *	Foundation, Inc. and its contributors.
     22   1.1.2.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1.2.1  ad  *    contributors may be used to endorse or promote products derived
     24   1.1.2.1  ad  *    from this software without specific prior written permission.
     25   1.1.2.1  ad  *
     26   1.1.2.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1.2.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1.2.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1.2.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1.2.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1.2.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1.2.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1.2.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1.2.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1.2.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1.2.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1.2.1  ad  */
     38   1.1.2.1  ad 
     39   1.1.2.1  ad /*
     40   1.1.2.1  ad  * Basic lock debugging code shared among lock primatives.
     41   1.1.2.1  ad  */
     42   1.1.2.1  ad 
     43   1.1.2.1  ad #include "opt_multiprocessor.h"
     44  1.1.2.10  ad #include "opt_ddb.h"
     45   1.1.2.1  ad 
     46   1.1.2.1  ad #include <sys/cdefs.h>
     47  1.1.2.11  ad __KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.1.2.11 2007/02/06 13:11:48 ad Exp $");
     48   1.1.2.1  ad 
     49   1.1.2.1  ad #include <sys/param.h>
     50   1.1.2.1  ad #include <sys/proc.h>
     51   1.1.2.1  ad #include <sys/systm.h>
     52   1.1.2.5  ad #include <sys/kmem.h>
     53   1.1.2.1  ad #include <sys/lock.h>
     54   1.1.2.1  ad #include <sys/lockdebug.h>
     55  1.1.2.10  ad #include <sys/sleepq.h>
     56   1.1.2.1  ad 
     57   1.1.2.1  ad #include <machine/cpu.h>
     58   1.1.2.1  ad 
     59   1.1.2.1  ad #ifdef LOCKDEBUG
     60   1.1.2.1  ad 
     61   1.1.2.1  ad #define	LD_BATCH_SHIFT	9
     62   1.1.2.1  ad #define	LD_BATCH	(1 << LD_BATCH_SHIFT)
     63   1.1.2.1  ad #define	LD_BATCH_MASK	(LD_BATCH - 1)
     64   1.1.2.1  ad #define	LD_MAX_LOCKS	1048576
     65   1.1.2.1  ad #define	LD_SLOP		16
     66   1.1.2.1  ad 
     67   1.1.2.1  ad #define	LD_LOCKED	0x01
     68   1.1.2.1  ad #define	LD_SLEEPER	0x02
     69   1.1.2.1  ad 
     70   1.1.2.3  ad #define	LD_NOID		LD_MAX_LOCKS
     71   1.1.2.3  ad 
     72  1.1.2.10  ad typedef union lockdebuglk {
     73  1.1.2.10  ad 	struct {
     74  1.1.2.10  ad 		__cpu_simple_lock_t	lku_lock;
     75  1.1.2.10  ad 		int			lku_oldspl;
     76  1.1.2.10  ad 	} ul;
     77  1.1.2.11  ad 	uint8_t	lk_pad[PRESUMED_CACHELINE_SZ];
     78  1.1.2.11  ad } volatile __aligned(PRESUMED_CACHELINE_SZ) lockdebuglk_t;
     79  1.1.2.10  ad 
     80  1.1.2.10  ad #define	lk_lock		ul.lku_lock
     81  1.1.2.10  ad #define	lk_oldspl	ul.lku_oldspl
     82   1.1.2.1  ad 
     83   1.1.2.1  ad typedef struct lockdebug {
     84   1.1.2.1  ad 	_TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
     85  1.1.2.10  ad 	_TAILQ_ENTRY(struct lockdebug, volatile) ld_achain;
     86   1.1.2.3  ad 	volatile void	*ld_lock;
     87   1.1.2.1  ad 	lockops_t	*ld_lockops;
     88   1.1.2.1  ad 	struct lwp	*ld_lwp;
     89   1.1.2.1  ad 	uintptr_t	ld_locked;
     90   1.1.2.1  ad 	uintptr_t	ld_unlocked;
     91   1.1.2.1  ad 	u_int		ld_id;
     92  1.1.2.10  ad 	uint16_t	ld_shares;
     93  1.1.2.10  ad 	uint16_t	ld_cpu;
     94  1.1.2.10  ad 	uint8_t		ld_flags;
     95  1.1.2.10  ad 	uint8_t		ld_shwant;	/* advisory */
     96  1.1.2.10  ad 	uint8_t		ld_exwant;	/* advisory */
     97  1.1.2.10  ad 	uint8_t		ld_unused;
     98   1.1.2.1  ad } volatile lockdebug_t;
     99   1.1.2.1  ad 
    100   1.1.2.1  ad typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
    101   1.1.2.1  ad 
    102   1.1.2.1  ad lockdebuglk_t		ld_sleeper_lk;
    103   1.1.2.1  ad lockdebuglk_t		ld_spinner_lk;
    104   1.1.2.1  ad lockdebuglk_t		ld_free_lk;
    105   1.1.2.1  ad 
    106   1.1.2.1  ad lockdebuglist_t		ld_sleepers;
    107   1.1.2.1  ad lockdebuglist_t		ld_spinners;
    108   1.1.2.1  ad lockdebuglist_t		ld_free;
    109  1.1.2.10  ad lockdebuglist_t		ld_all;
    110   1.1.2.1  ad int			ld_nfree;
    111   1.1.2.1  ad int			ld_freeptr;
    112   1.1.2.1  ad int			ld_recurse;
    113   1.1.2.1  ad lockdebug_t		*ld_table[LD_MAX_LOCKS / LD_BATCH];
    114   1.1.2.1  ad 
    115   1.1.2.1  ad lockdebug_t		ld_prime[LD_BATCH];
    116   1.1.2.1  ad 
    117   1.1.2.1  ad void	lockdebug_abort1(lockdebug_t *, lockdebuglk_t *lk, const char *,
    118   1.1.2.1  ad 			 const char *);
    119   1.1.2.1  ad void	lockdebug_more(void);
    120   1.1.2.1  ad 
    121   1.1.2.1  ad static inline void
    122   1.1.2.1  ad lockdebug_lock(lockdebuglk_t *lk)
    123   1.1.2.1  ad {
    124   1.1.2.1  ad 	int s;
    125  1.1.2.10  ad 
    126   1.1.2.1  ad 	s = spllock();
    127   1.1.2.1  ad 	__cpu_simple_lock(&lk->lk_lock);
    128   1.1.2.1  ad 	lk->lk_oldspl = s;
    129   1.1.2.1  ad }
    130   1.1.2.1  ad 
    131   1.1.2.1  ad static inline void
    132   1.1.2.1  ad lockdebug_unlock(lockdebuglk_t *lk)
    133   1.1.2.1  ad {
    134   1.1.2.1  ad 	int s;
    135   1.1.2.1  ad 
    136   1.1.2.1  ad 	s = lk->lk_oldspl;
    137  1.1.2.10  ad 	__cpu_simple_unlock(&(lk->lk_lock));
    138   1.1.2.1  ad 	splx(s);
    139   1.1.2.1  ad }
    140   1.1.2.1  ad 
    141   1.1.2.1  ad /*
    142   1.1.2.1  ad  * lockdebug_lookup:
    143   1.1.2.1  ad  *
    144   1.1.2.1  ad  *	Find a lockdebug structure by ID and return it locked.
    145   1.1.2.1  ad  */
    146   1.1.2.1  ad static inline lockdebug_t *
    147   1.1.2.1  ad lockdebug_lookup(u_int id, lockdebuglk_t **lk)
    148   1.1.2.1  ad {
    149   1.1.2.5  ad 	lockdebug_t *base, *ld;
    150   1.1.2.1  ad 
    151   1.1.2.3  ad 	if (id == LD_NOID)
    152   1.1.2.3  ad 		return NULL;
    153   1.1.2.3  ad 
    154   1.1.2.5  ad 	if (id == 0 || id >= LD_MAX_LOCKS)
    155   1.1.2.8  ad 		panic("lockdebug_lookup: uninitialized lock (1, id=%d)", id);
    156   1.1.2.1  ad 
    157   1.1.2.5  ad 	base = ld_table[id >> LD_BATCH_SHIFT];
    158   1.1.2.5  ad 	ld = base + (id & LD_BATCH_MASK);
    159   1.1.2.5  ad 
    160   1.1.2.5  ad 	if (base == NULL || ld->ld_lock == NULL || ld->ld_id != id)
    161   1.1.2.8  ad 		panic("lockdebug_lookup: uninitialized lock (2, id=%d)", id);
    162   1.1.2.1  ad 
    163   1.1.2.1  ad 	if ((ld->ld_flags & LD_SLEEPER) != 0)
    164   1.1.2.1  ad 		*lk = &ld_sleeper_lk;
    165   1.1.2.1  ad 	else
    166   1.1.2.1  ad 		*lk = &ld_spinner_lk;
    167   1.1.2.1  ad 
    168   1.1.2.1  ad 	lockdebug_lock(*lk);
    169   1.1.2.1  ad 	return ld;
    170   1.1.2.1  ad }
    171   1.1.2.1  ad 
    172   1.1.2.1  ad /*
    173   1.1.2.1  ad  * lockdebug_init:
    174   1.1.2.1  ad  *
    175   1.1.2.1  ad  *	Initialize the lockdebug system.  Allocate an initial pool of
    176   1.1.2.1  ad  *	lockdebug structures before the VM system is up and running.
    177   1.1.2.1  ad  */
    178   1.1.2.1  ad void
    179   1.1.2.1  ad lockdebug_init(void)
    180   1.1.2.1  ad {
    181   1.1.2.1  ad 	lockdebug_t *ld;
    182   1.1.2.1  ad 	int i;
    183   1.1.2.1  ad 
    184   1.1.2.1  ad 	__cpu_simple_lock_init(&ld_sleeper_lk.lk_lock);
    185   1.1.2.1  ad 	__cpu_simple_lock_init(&ld_spinner_lk.lk_lock);
    186   1.1.2.1  ad 	__cpu_simple_lock_init(&ld_free_lk.lk_lock);
    187   1.1.2.1  ad 
    188   1.1.2.1  ad 	TAILQ_INIT(&ld_free);
    189  1.1.2.10  ad 	TAILQ_INIT(&ld_all);
    190   1.1.2.1  ad 	TAILQ_INIT(&ld_sleepers);
    191   1.1.2.1  ad 	TAILQ_INIT(&ld_spinners);
    192   1.1.2.1  ad 
    193   1.1.2.1  ad 	ld = ld_prime;
    194   1.1.2.1  ad 	ld_table[0] = ld;
    195   1.1.2.1  ad 	for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
    196   1.1.2.1  ad 		ld->ld_id = i;
    197   1.1.2.1  ad 		TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
    198  1.1.2.10  ad 		TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
    199   1.1.2.1  ad 	}
    200   1.1.2.1  ad 	ld_freeptr = 1;
    201   1.1.2.5  ad 	ld_nfree = LD_BATCH - 1;
    202   1.1.2.1  ad }
    203   1.1.2.1  ad 
    204   1.1.2.1  ad /*
    205   1.1.2.1  ad  * lockdebug_alloc:
    206   1.1.2.1  ad  *
    207   1.1.2.1  ad  *	A lock is being initialized, so allocate an associated debug
    208   1.1.2.1  ad  *	structure.
    209   1.1.2.1  ad  */
    210   1.1.2.1  ad u_int
    211   1.1.2.3  ad lockdebug_alloc(volatile void *lock, lockops_t *lo)
    212   1.1.2.1  ad {
    213   1.1.2.3  ad 	struct cpu_info *ci;
    214   1.1.2.1  ad 	lockdebug_t *ld;
    215   1.1.2.1  ad 
    216   1.1.2.8  ad 	if (ld_freeptr == 0) {
    217   1.1.2.8  ad 		printf("lockdebug_alloc: not initialized yet, lock=%p\n",
    218   1.1.2.8  ad 		    __UNVOLATILE(lock));
    219   1.1.2.8  ad 		return LD_NOID;
    220   1.1.2.8  ad 	}
    221   1.1.2.1  ad 
    222   1.1.2.8  ad 	if (panicstr != NULL)
    223   1.1.2.8  ad 		return LD_NOID;
    224   1.1.2.5  ad 
    225   1.1.2.3  ad 	ci = curcpu();
    226   1.1.2.3  ad 
    227   1.1.2.3  ad 	/*
    228   1.1.2.3  ad 	 * Pinch a new debug structure.  We may recurse because we call
    229   1.1.2.5  ad 	 * kmem_alloc(), which may need to initialize new locks somewhere
    230   1.1.2.3  ad 	 * down the path.  If not recursing, we try to maintain at keep
    231   1.1.2.3  ad 	 * LD_SLOP structures free, which should hopefully be enough to
    232   1.1.2.5  ad 	 * satisfy kmem_alloc().  If we can't provide a structure, not to
    233   1.1.2.3  ad 	 * worry: we'll just mark the lock as not having an ID.
    234   1.1.2.3  ad 	 */
    235   1.1.2.1  ad 	lockdebug_lock(&ld_free_lk);
    236   1.1.2.3  ad 	ci->ci_lkdebug_recurse++;
    237   1.1.2.3  ad 
    238   1.1.2.3  ad 	if (TAILQ_EMPTY(&ld_free)) {
    239   1.1.2.3  ad 		if (ci->ci_lkdebug_recurse > 1) {
    240   1.1.2.3  ad 			ci->ci_lkdebug_recurse--;
    241   1.1.2.3  ad 			lockdebug_unlock(&ld_free_lk);
    242   1.1.2.5  ad 			return LD_NOID;
    243   1.1.2.3  ad 		}
    244   1.1.2.1  ad 		lockdebug_more();
    245   1.1.2.3  ad 	} else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP)
    246   1.1.2.3  ad 		lockdebug_more();
    247   1.1.2.3  ad 
    248   1.1.2.5  ad 	if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
    249   1.1.2.5  ad 		lockdebug_unlock(&ld_free_lk);
    250   1.1.2.5  ad 		return LD_NOID;
    251   1.1.2.5  ad 	}
    252   1.1.2.5  ad 
    253   1.1.2.1  ad 	TAILQ_REMOVE(&ld_free, ld, ld_chain);
    254   1.1.2.1  ad 	ld_nfree--;
    255   1.1.2.3  ad 
    256   1.1.2.3  ad 	ci->ci_lkdebug_recurse--;
    257   1.1.2.1  ad 	lockdebug_unlock(&ld_free_lk);
    258   1.1.2.1  ad 
    259   1.1.2.1  ad 	if (ld->ld_lock != NULL)
    260   1.1.2.1  ad 		panic("lockdebug_alloc: corrupt table");
    261   1.1.2.1  ad 
    262   1.1.2.3  ad 	if (lo->lo_sleeplock)
    263   1.1.2.1  ad 		lockdebug_lock(&ld_sleeper_lk);
    264   1.1.2.1  ad 	else
    265   1.1.2.1  ad 		lockdebug_lock(&ld_spinner_lk);
    266   1.1.2.1  ad 
    267   1.1.2.1  ad 	/* Initialise the structure. */
    268   1.1.2.1  ad 	ld->ld_lock = lock;
    269   1.1.2.1  ad 	ld->ld_lockops = lo;
    270   1.1.2.1  ad 	ld->ld_locked = 0;
    271   1.1.2.1  ad 	ld->ld_unlocked = 0;
    272   1.1.2.1  ad 	ld->ld_lwp = NULL;
    273   1.1.2.1  ad 
    274   1.1.2.3  ad 	if (lo->lo_sleeplock) {
    275   1.1.2.1  ad 		ld->ld_flags = LD_SLEEPER;
    276   1.1.2.1  ad 		lockdebug_unlock(&ld_sleeper_lk);
    277   1.1.2.1  ad 	} else {
    278   1.1.2.1  ad 		ld->ld_flags = 0;
    279   1.1.2.1  ad 		lockdebug_unlock(&ld_spinner_lk);
    280   1.1.2.1  ad 	}
    281   1.1.2.1  ad 
    282   1.1.2.1  ad 	return ld->ld_id;
    283   1.1.2.1  ad }
    284   1.1.2.1  ad 
    285   1.1.2.1  ad /*
    286   1.1.2.1  ad  * lockdebug_free:
    287   1.1.2.1  ad  *
    288   1.1.2.1  ad  *	A lock is being destroyed, so release debugging resources.
    289   1.1.2.1  ad  */
    290   1.1.2.1  ad void
    291   1.1.2.3  ad lockdebug_free(volatile void *lock, u_int id)
    292   1.1.2.1  ad {
    293   1.1.2.1  ad 	lockdebug_t *ld;
    294   1.1.2.1  ad 	lockdebuglk_t *lk;
    295   1.1.2.1  ad 
    296   1.1.2.1  ad 	if (panicstr != NULL)
    297   1.1.2.1  ad 		return;
    298   1.1.2.1  ad 
    299   1.1.2.3  ad 	if ((ld = lockdebug_lookup(id, &lk)) == NULL)
    300   1.1.2.3  ad 		return;
    301   1.1.2.1  ad 
    302   1.1.2.1  ad 	if (ld->ld_lock != lock) {
    303   1.1.2.1  ad 		panic("lockdebug_free: destroying uninitialized lock %p"
    304   1.1.2.1  ad 		    "(ld_id=%d ld_lock=%p)", lock, id, ld->ld_lock);
    305  1.1.2.10  ad 		lockdebug_abort1(ld, lk, __func__, "lock record follows");
    306   1.1.2.1  ad 	}
    307  1.1.2.10  ad 	if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0)
    308  1.1.2.10  ad 		lockdebug_abort1(ld, lk, __func__, "is locked");
    309   1.1.2.1  ad 
    310   1.1.2.1  ad 	ld->ld_lock = NULL;
    311   1.1.2.1  ad 
    312   1.1.2.1  ad 	lockdebug_unlock(lk);
    313   1.1.2.1  ad 
    314   1.1.2.1  ad 	lockdebug_lock(&ld_free_lk);
    315   1.1.2.1  ad 	TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
    316   1.1.2.1  ad 	ld_nfree++;
    317   1.1.2.1  ad 	lockdebug_unlock(&ld_free_lk);
    318   1.1.2.1  ad }
    319   1.1.2.1  ad 
    320   1.1.2.1  ad /*
    321   1.1.2.1  ad  * lockdebug_more:
    322   1.1.2.1  ad  *
    323   1.1.2.6  ad  *	Allocate a batch of debug structures and add to the free list.
    324   1.1.2.6  ad  *	Must be called with ld_free_lk held.
    325   1.1.2.1  ad  */
    326   1.1.2.1  ad void
    327   1.1.2.1  ad lockdebug_more(void)
    328   1.1.2.1  ad {
    329   1.1.2.1  ad 	lockdebug_t *ld;
    330   1.1.2.1  ad 	void *block;
    331   1.1.2.1  ad 	int i, base;
    332   1.1.2.1  ad 
    333   1.1.2.3  ad 	while (ld_nfree < LD_SLOP) {
    334   1.1.2.1  ad 		lockdebug_unlock(&ld_free_lk);
    335   1.1.2.5  ad 		block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
    336   1.1.2.1  ad 		lockdebug_lock(&ld_free_lk);
    337   1.1.2.1  ad 
    338   1.1.2.5  ad 		if (block == NULL)
    339   1.1.2.5  ad 			return;
    340   1.1.2.5  ad 
    341   1.1.2.5  ad 		if (ld_nfree > LD_SLOP) {
    342   1.1.2.1  ad 			/* Somebody beat us to it. */
    343   1.1.2.1  ad 			lockdebug_unlock(&ld_free_lk);
    344   1.1.2.5  ad 			kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
    345   1.1.2.1  ad 			lockdebug_lock(&ld_free_lk);
    346   1.1.2.1  ad 			continue;
    347   1.1.2.1  ad 		}
    348   1.1.2.5  ad 
    349   1.1.2.5  ad 		base = ld_freeptr;
    350   1.1.2.3  ad 		ld_nfree += LD_BATCH;
    351   1.1.2.1  ad 		ld = block;
    352   1.1.2.1  ad 		base <<= LD_BATCH_SHIFT;
    353   1.1.2.1  ad 
    354   1.1.2.1  ad 		for (i = 0; i < LD_BATCH; i++, ld++) {
    355   1.1.2.1  ad 			ld->ld_id = i + base;
    356   1.1.2.1  ad 			TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
    357  1.1.2.10  ad 			TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
    358   1.1.2.1  ad 		}
    359   1.1.2.1  ad 
    360   1.1.2.1  ad 		mb_write();
    361   1.1.2.5  ad 		ld_table[ld_freeptr++] = block;
    362   1.1.2.1  ad 	}
    363   1.1.2.1  ad }
    364   1.1.2.1  ad 
    365   1.1.2.1  ad /*
    366   1.1.2.7  ad  * lockdebug_wantlock:
    367   1.1.2.7  ad  *
    368   1.1.2.7  ad  *	Process the preamble to a lock acquire.
    369   1.1.2.7  ad  */
    370   1.1.2.7  ad void
    371   1.1.2.7  ad lockdebug_wantlock(u_int id, uintptr_t where, int shared)
    372   1.1.2.7  ad {
    373   1.1.2.7  ad 	struct lwp *l = curlwp;
    374   1.1.2.7  ad 	lockdebuglk_t *lk;
    375   1.1.2.7  ad 	lockdebug_t *ld;
    376  1.1.2.10  ad 	boolean_t recurse;
    377   1.1.2.7  ad 
    378   1.1.2.9  ad 	(void)shared;
    379  1.1.2.10  ad 	recurse = FALSE;
    380   1.1.2.9  ad 
    381   1.1.2.9  ad 	if (panicstr != NULL)
    382   1.1.2.7  ad 		return;
    383   1.1.2.7  ad 
    384   1.1.2.7  ad 	if ((ld = lockdebug_lookup(id, &lk)) == NULL)
    385   1.1.2.7  ad 		return;
    386   1.1.2.7  ad 
    387   1.1.2.7  ad 	if ((ld->ld_flags & LD_LOCKED) != 0) {
    388   1.1.2.7  ad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
    389   1.1.2.7  ad 			if (ld->ld_lwp == l)
    390  1.1.2.10  ad 				recurse = TRUE;
    391  1.1.2.10  ad 		} else if (ld->ld_cpu == (uint16_t)cpu_number())
    392  1.1.2.10  ad 			recurse = TRUE;
    393   1.1.2.7  ad 	}
    394   1.1.2.7  ad 
    395  1.1.2.10  ad 	if (shared)
    396  1.1.2.10  ad 		ld->ld_shwant++;
    397  1.1.2.10  ad 	else
    398  1.1.2.10  ad 		ld->ld_exwant++;
    399  1.1.2.10  ad 
    400  1.1.2.10  ad 	if (recurse)
    401  1.1.2.10  ad 		lockdebug_abort1(ld, lk, __func__, "locking against myself");
    402  1.1.2.10  ad 
    403   1.1.2.7  ad 	lockdebug_unlock(lk);
    404   1.1.2.7  ad }
    405   1.1.2.7  ad 
    406   1.1.2.7  ad /*
    407   1.1.2.1  ad  * lockdebug_locked:
    408   1.1.2.1  ad  *
    409   1.1.2.1  ad  *	Process a lock acquire operation.
    410   1.1.2.1  ad  */
    411   1.1.2.1  ad void
    412   1.1.2.1  ad lockdebug_locked(u_int id, uintptr_t where, int shared)
    413   1.1.2.1  ad {
    414   1.1.2.1  ad 	struct lwp *l = curlwp;
    415   1.1.2.1  ad 	lockdebuglk_t *lk;
    416   1.1.2.1  ad 	lockdebug_t *ld;
    417   1.1.2.1  ad 
    418   1.1.2.1  ad 	if (panicstr != NULL)
    419   1.1.2.1  ad 		return;
    420   1.1.2.1  ad 
    421   1.1.2.3  ad 	if ((ld = lockdebug_lookup(id, &lk)) == NULL)
    422   1.1.2.3  ad 		return;
    423   1.1.2.1  ad 
    424   1.1.2.1  ad 	if (shared) {
    425   1.1.2.1  ad 		l->l_shlocks++;
    426   1.1.2.1  ad 		ld->ld_shares++;
    427  1.1.2.10  ad 		ld->ld_shwant--;
    428   1.1.2.1  ad 	} else {
    429   1.1.2.7  ad 		if ((ld->ld_flags & LD_LOCKED) != 0)
    430  1.1.2.10  ad 			lockdebug_abort1(ld, lk, __func__,
    431   1.1.2.7  ad 			    "already locked");
    432   1.1.2.7  ad 
    433   1.1.2.1  ad 		ld->ld_flags |= LD_LOCKED;
    434   1.1.2.1  ad 		ld->ld_locked = where;
    435  1.1.2.10  ad 		ld->ld_cpu = (uint16_t)cpu_number();
    436   1.1.2.1  ad 		ld->ld_lwp = l;
    437  1.1.2.10  ad 		ld->ld_exwant--;
    438   1.1.2.1  ad 
    439   1.1.2.1  ad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
    440   1.1.2.1  ad 			l->l_exlocks++;
    441   1.1.2.1  ad 			TAILQ_INSERT_TAIL(&ld_sleepers, ld, ld_chain);
    442   1.1.2.1  ad 		} else {
    443   1.1.2.1  ad 			curcpu()->ci_spin_locks2++;
    444   1.1.2.1  ad 			TAILQ_INSERT_TAIL(&ld_spinners, ld, ld_chain);
    445   1.1.2.1  ad 		}
    446   1.1.2.1  ad 	}
    447   1.1.2.1  ad 
    448   1.1.2.1  ad 	lockdebug_unlock(lk);
    449   1.1.2.1  ad }
    450   1.1.2.1  ad 
    451   1.1.2.1  ad /*
    452   1.1.2.1  ad  * lockdebug_unlocked:
    453   1.1.2.1  ad  *
    454   1.1.2.1  ad  *	Process a lock release operation.
    455   1.1.2.1  ad  */
    456   1.1.2.1  ad void
    457   1.1.2.1  ad lockdebug_unlocked(u_int id, uintptr_t where, int shared)
    458   1.1.2.1  ad {
    459   1.1.2.1  ad 	struct lwp *l = curlwp;
    460   1.1.2.1  ad 	lockdebuglk_t *lk;
    461   1.1.2.1  ad 	lockdebug_t *ld;
    462   1.1.2.1  ad 
    463   1.1.2.1  ad 	if (panicstr != NULL)
    464   1.1.2.1  ad 		return;
    465   1.1.2.1  ad 
    466   1.1.2.3  ad 	if ((ld = lockdebug_lookup(id, &lk)) == NULL)
    467   1.1.2.3  ad 		return;
    468   1.1.2.1  ad 
    469   1.1.2.1  ad 	if (shared) {
    470   1.1.2.1  ad 		if (l->l_shlocks == 0)
    471  1.1.2.10  ad 			lockdebug_abort1(ld, lk, __func__,
    472   1.1.2.6  ad 			    "no shared locks held by LWP");
    473   1.1.2.1  ad 		if (ld->ld_shares == 0)
    474  1.1.2.10  ad 			lockdebug_abort1(ld, lk, __func__,
    475   1.1.2.6  ad 			    "no shared holds on this lock");
    476   1.1.2.1  ad 		l->l_shlocks--;
    477   1.1.2.1  ad 		ld->ld_shares--;
    478   1.1.2.1  ad 	} else {
    479   1.1.2.1  ad 		if ((ld->ld_flags & LD_LOCKED) == 0)
    480  1.1.2.10  ad 			lockdebug_abort1(ld, lk, __func__, "not locked");
    481   1.1.2.1  ad 
    482   1.1.2.1  ad 		if ((ld->ld_flags & LD_SLEEPER) != 0) {
    483   1.1.2.1  ad 			if (ld->ld_lwp != curlwp)
    484  1.1.2.10  ad 				lockdebug_abort1(ld, lk, __func__,
    485   1.1.2.1  ad 				    "not held by current LWP");
    486   1.1.2.1  ad 			ld->ld_flags &= ~LD_LOCKED;
    487   1.1.2.1  ad 			ld->ld_unlocked = where;
    488   1.1.2.1  ad 			ld->ld_lwp = NULL;
    489   1.1.2.1  ad 			curlwp->l_exlocks--;
    490   1.1.2.1  ad 			TAILQ_REMOVE(&ld_sleepers, ld, ld_chain);
    491   1.1.2.1  ad 		} else {
    492  1.1.2.10  ad 			if (ld->ld_cpu != (uint16_t)cpu_number())
    493  1.1.2.10  ad 				lockdebug_abort1(ld, lk, __func__,
    494   1.1.2.1  ad 				    "not held by current CPU");
    495   1.1.2.1  ad 			ld->ld_flags &= ~LD_LOCKED;
    496   1.1.2.1  ad 			ld->ld_unlocked = where;
    497   1.1.2.1  ad 			ld->ld_lwp = NULL;
    498   1.1.2.1  ad 			curcpu()->ci_spin_locks2--;
    499   1.1.2.1  ad 			TAILQ_REMOVE(&ld_spinners, ld, ld_chain);
    500   1.1.2.1  ad 		}
    501   1.1.2.1  ad 	}
    502   1.1.2.1  ad 
    503   1.1.2.1  ad 	lockdebug_unlock(lk);
    504   1.1.2.1  ad }
    505   1.1.2.1  ad 
    506   1.1.2.1  ad /*
    507   1.1.2.1  ad  * lockdebug_barrier:
    508   1.1.2.1  ad  *
    509   1.1.2.1  ad  *	Panic if we hold more than one specified spin lock, and optionally,
    510   1.1.2.1  ad  *	if we hold sleep locks.
    511   1.1.2.1  ad  */
    512   1.1.2.1  ad void
    513   1.1.2.3  ad lockdebug_barrier(volatile void *spinlock, int slplocks)
    514   1.1.2.1  ad {
    515   1.1.2.1  ad 	struct lwp *l = curlwp;
    516   1.1.2.1  ad 	lockdebug_t *ld;
    517  1.1.2.10  ad 	uint16_t cpuno;
    518   1.1.2.1  ad 
    519   1.1.2.1  ad 	if (panicstr != NULL)
    520   1.1.2.1  ad 		return;
    521   1.1.2.1  ad 
    522   1.1.2.1  ad 	if (curcpu()->ci_spin_locks2 != 0) {
    523  1.1.2.10  ad 		cpuno = (uint16_t)cpu_number();
    524   1.1.2.1  ad 
    525   1.1.2.1  ad 		lockdebug_lock(&ld_spinner_lk);
    526   1.1.2.1  ad 		TAILQ_FOREACH(ld, &ld_spinners, ld_chain) {
    527   1.1.2.1  ad 			if (ld->ld_lock == spinlock) {
    528   1.1.2.1  ad 				if (ld->ld_cpu != cpuno)
    529   1.1.2.1  ad 					lockdebug_abort1(ld, &ld_spinner_lk,
    530  1.1.2.10  ad 					    __func__,
    531   1.1.2.1  ad 					    "not held by current CPU");
    532   1.1.2.1  ad 				continue;
    533   1.1.2.1  ad 			}
    534   1.1.2.1  ad 			if (ld->ld_cpu == cpuno)
    535   1.1.2.1  ad 				lockdebug_abort1(ld, &ld_spinner_lk,
    536  1.1.2.10  ad 				    __func__, "spin lock held");
    537   1.1.2.1  ad 		}
    538   1.1.2.1  ad 		lockdebug_unlock(&ld_spinner_lk);
    539   1.1.2.1  ad 	}
    540   1.1.2.1  ad 
    541   1.1.2.1  ad 	if (!slplocks) {
    542   1.1.2.1  ad 		if (l->l_exlocks != 0) {
    543   1.1.2.1  ad 			lockdebug_lock(&ld_sleeper_lk);
    544   1.1.2.1  ad 			TAILQ_FOREACH(ld, &ld_sleepers, ld_chain) {
    545   1.1.2.1  ad 				if (ld->ld_lwp == l)
    546   1.1.2.1  ad 					lockdebug_abort1(ld, &ld_sleeper_lk,
    547  1.1.2.10  ad 					    __func__, "sleep lock held");
    548   1.1.2.1  ad 			}
    549   1.1.2.1  ad 			lockdebug_unlock(&ld_sleeper_lk);
    550   1.1.2.1  ad 		}
    551   1.1.2.1  ad 		if (l->l_shlocks != 0)
    552   1.1.2.1  ad 			panic("lockdebug_barrier: holding %d shared locks",
    553   1.1.2.1  ad 			    l->l_shlocks);
    554   1.1.2.1  ad 	}
    555   1.1.2.1  ad }
    556   1.1.2.1  ad 
    557  1.1.2.10  ad /*
    558  1.1.2.10  ad  * lockdebug_dump:
    559  1.1.2.10  ad  *
    560  1.1.2.10  ad  *	Dump information about a lock on panic, or for DDB.
    561  1.1.2.10  ad  */
    562  1.1.2.10  ad static void
    563  1.1.2.10  ad lockdebug_dump(lockdebug_t *ld, void (*pr)(const char *, ...))
    564   1.1.2.1  ad {
    565  1.1.2.10  ad 	int sleeper = (ld->ld_flags & LD_SLEEPER);
    566   1.1.2.1  ad 
    567  1.1.2.10  ad 	(*pr)(
    568   1.1.2.1  ad 	    "lock address : %#018lx type     : %18s\n"
    569  1.1.2.10  ad 	    "shared holds : %18u exclusive: %18u\n"
    570  1.1.2.10  ad 	    "shares wanted: %18u exclusive: %18u\n"
    571  1.1.2.10  ad 	    "current cpu  : %18u last held: %18u\n"
    572  1.1.2.10  ad 	    "current lwp  : %#018lx last held: %#018lx\n"
    573  1.1.2.10  ad 	    "last locked  : %#018lx unlocked : %#018lx\n",
    574  1.1.2.10  ad 	    (long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
    575  1.1.2.10  ad 	    (unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
    576  1.1.2.10  ad 	    (unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
    577  1.1.2.10  ad 	    (unsigned)cpu_number(), (unsigned)ld->ld_cpu,
    578  1.1.2.10  ad 	    (long)curlwp, (long)ld->ld_lwp,
    579  1.1.2.10  ad 	    (long)ld->ld_locked, (long)ld->ld_unlocked);
    580   1.1.2.1  ad 
    581   1.1.2.3  ad 	if (ld->ld_lockops->lo_dump != NULL)
    582   1.1.2.5  ad 		(*ld->ld_lockops->lo_dump)(ld->ld_lock);
    583   1.1.2.1  ad 
    584  1.1.2.10  ad 	if (sleeper) {
    585  1.1.2.10  ad 		(*pr)("\n");
    586  1.1.2.10  ad 		turnstile_print(ld->ld_lock, pr);
    587  1.1.2.10  ad 	}
    588  1.1.2.10  ad }
    589  1.1.2.10  ad 
    590  1.1.2.10  ad /*
    591  1.1.2.10  ad  * lockdebug_dump:
    592  1.1.2.10  ad  *
    593  1.1.2.10  ad  *	Dump information about a known lock.
    594  1.1.2.10  ad  */
    595  1.1.2.10  ad void
    596  1.1.2.10  ad lockdebug_abort1(lockdebug_t *ld, lockdebuglk_t *lk, const char *func,
    597  1.1.2.10  ad 		 const char *msg)
    598  1.1.2.10  ad {
    599  1.1.2.10  ad 
    600  1.1.2.10  ad 	printf_nolog("%s error: %s: %s\n\n", ld->ld_lockops->lo_name,
    601  1.1.2.10  ad 	    func, msg);
    602  1.1.2.10  ad 	lockdebug_dump(ld, printf_nolog);
    603  1.1.2.10  ad 	lockdebug_unlock(lk);
    604   1.1.2.5  ad 	printf_nolog("\n");
    605   1.1.2.5  ad 	panic("LOCKDEBUG");
    606   1.1.2.1  ad }
    607   1.1.2.1  ad 
    608   1.1.2.3  ad #endif	/* LOCKDEBUG */
    609   1.1.2.1  ad 
    610   1.1.2.1  ad /*
    611  1.1.2.10  ad  * lockdebug_lock_print:
    612  1.1.2.10  ad  *
    613  1.1.2.10  ad  *	Handle the DDB 'show lock' command.
    614  1.1.2.10  ad  */
    615  1.1.2.10  ad #ifdef DDB
    616  1.1.2.10  ad void
    617  1.1.2.10  ad lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
    618  1.1.2.10  ad {
    619  1.1.2.10  ad #ifdef LOCKDEBUG
    620  1.1.2.10  ad 	lockdebug_t *ld;
    621  1.1.2.10  ad 
    622  1.1.2.10  ad 	TAILQ_FOREACH(ld, &ld_all, ld_achain) {
    623  1.1.2.10  ad 		if (ld->ld_lock == addr) {
    624  1.1.2.10  ad 			lockdebug_dump(ld, pr);
    625  1.1.2.10  ad 			return;
    626  1.1.2.10  ad 		}
    627  1.1.2.10  ad 	}
    628  1.1.2.10  ad 	(*pr)("Sorry, no record of a lock with address %p found.\n", addr);
    629  1.1.2.10  ad #else
    630  1.1.2.10  ad 	(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
    631  1.1.2.10  ad #endif	/* LOCKDEBUG */
    632  1.1.2.10  ad }
    633  1.1.2.10  ad #endif	/* DDB */
    634  1.1.2.10  ad 
    635  1.1.2.10  ad /*
    636   1.1.2.1  ad  * lockdebug_abort:
    637   1.1.2.1  ad  *
    638   1.1.2.1  ad  *	An error has been trapped - dump lock info and call panic().
    639   1.1.2.1  ad  */
    640   1.1.2.1  ad void
    641   1.1.2.3  ad lockdebug_abort(int id, volatile void *lock, lockops_t *ops,
    642   1.1.2.3  ad 		const char *func, const char *msg)
    643   1.1.2.1  ad {
    644   1.1.2.3  ad #ifdef LOCKDEBUG
    645   1.1.2.3  ad 	lockdebug_t *ld;
    646   1.1.2.3  ad 	lockdebuglk_t *lk;
    647   1.1.2.3  ad 
    648   1.1.2.3  ad 	if ((ld = lockdebug_lookup(id, &lk)) != NULL) {
    649   1.1.2.3  ad 		lockdebug_abort1(ld, lk, func, msg);
    650   1.1.2.3  ad 		/* NOTREACHED */
    651   1.1.2.3  ad 	}
    652   1.1.2.3  ad #endif	/* LOCKDEBUG */
    653   1.1.2.1  ad 
    654   1.1.2.5  ad 	printf_nolog("%s error: %s: %s\n\n"
    655   1.1.2.1  ad 	    "lock address : %#018lx\n"
    656   1.1.2.1  ad 	    "current cpu  : %18d\n"
    657   1.1.2.1  ad 	    "current lwp  : %#018lx\n",
    658   1.1.2.3  ad 	    ops->lo_name, func, msg, (long)lock, (int)cpu_number(),
    659   1.1.2.3  ad 	    (long)curlwp);
    660   1.1.2.1  ad 
    661   1.1.2.5  ad 	(*ops->lo_dump)(lock);
    662   1.1.2.1  ad 
    663   1.1.2.5  ad 	printf_nolog("\n");
    664   1.1.2.5  ad 	panic("lock error");
    665   1.1.2.1  ad }
    666