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