2008-11-07 22:50:00 +03:00
|
|
|
/* $NetBSD: subr_lockdebug.c,v 1.39 2008/11/07 19:50:00 cegger Exp $ */
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/*-
|
2008-02-18 21:31:10 +03:00
|
|
|
* Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
|
2007-02-10 00:55:00 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Andrew Doran.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2007-10-27 05:23:25 +04:00
|
|
|
* Basic lock debugging code shared among lock primitives.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
|
|
|
|
2007-09-21 23:14:12 +04:00
|
|
|
#include <sys/cdefs.h>
|
2008-11-07 22:50:00 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.39 2008/11/07 19:50:00 cegger Exp $");
|
2007-09-21 23:14:12 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
#include "opt_ddb.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/systm.h>
|
2007-10-11 23:45:24 +04:00
|
|
|
#include <sys/kernel.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/lockdebug.h>
|
|
|
|
#include <sys/sleepq.h>
|
2007-10-11 23:45:24 +04:00
|
|
|
#include <sys/cpu.h>
|
2007-12-01 02:05:43 +03:00
|
|
|
#include <sys/atomic.h>
|
2008-01-05 15:39:27 +03:00
|
|
|
#include <sys/lock.h>
|
2008-06-04 18:31:15 +04:00
|
|
|
#include <sys/rb.h>
|
2007-11-21 13:19:06 +03:00
|
|
|
|
2008-01-05 00:17:40 +03:00
|
|
|
#include <machine/lock.h>
|
|
|
|
|
2008-02-18 21:31:10 +03:00
|
|
|
unsigned int ld_panic;
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
|
|
|
|
#define LD_BATCH_SHIFT 9
|
|
|
|
#define LD_BATCH (1 << LD_BATCH_SHIFT)
|
|
|
|
#define LD_BATCH_MASK (LD_BATCH - 1)
|
|
|
|
#define LD_MAX_LOCKS 1048576
|
|
|
|
#define LD_SLOP 16
|
|
|
|
|
|
|
|
#define LD_LOCKED 0x01
|
|
|
|
#define LD_SLEEPER 0x02
|
|
|
|
|
2007-12-08 18:00:13 +03:00
|
|
|
#define LD_WRITE_LOCK 0x80000000
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
typedef struct lockdebug {
|
2007-11-21 13:19:06 +03:00
|
|
|
struct rb_node ld_rb_node; /* must be the first member */
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_lock_t ld_spinlock;
|
2007-02-10 00:55:00 +03:00
|
|
|
_TAILQ_ENTRY(struct lockdebug, volatile) ld_chain;
|
|
|
|
_TAILQ_ENTRY(struct lockdebug, volatile) ld_achain;
|
|
|
|
volatile void *ld_lock;
|
|
|
|
lockops_t *ld_lockops;
|
|
|
|
struct lwp *ld_lwp;
|
|
|
|
uintptr_t ld_locked;
|
|
|
|
uintptr_t ld_unlocked;
|
2007-10-11 23:45:24 +04:00
|
|
|
uintptr_t ld_initaddr;
|
2007-02-10 00:55:00 +03:00
|
|
|
uint16_t ld_shares;
|
|
|
|
uint16_t ld_cpu;
|
|
|
|
uint8_t ld_flags;
|
|
|
|
uint8_t ld_shwant; /* advisory */
|
|
|
|
uint8_t ld_exwant; /* advisory */
|
|
|
|
uint8_t ld_unused;
|
|
|
|
} volatile lockdebug_t;
|
|
|
|
|
|
|
|
typedef _TAILQ_HEAD(lockdebuglist, struct lockdebug, volatile) lockdebuglist_t;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_lock_t ld_mod_lk;
|
2007-11-12 02:22:23 +03:00
|
|
|
lockdebuglist_t ld_free = TAILQ_HEAD_INITIALIZER(ld_free);
|
|
|
|
lockdebuglist_t ld_all = TAILQ_HEAD_INITIALIZER(ld_all);
|
2007-02-10 00:55:00 +03:00
|
|
|
int ld_nfree;
|
|
|
|
int ld_freeptr;
|
|
|
|
int ld_recurse;
|
2007-03-10 18:56:21 +03:00
|
|
|
bool ld_nomore;
|
2007-02-10 00:55:00 +03:00
|
|
|
lockdebug_t ld_prime[LD_BATCH];
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
static void lockdebug_abort1(lockdebug_t *, int, const char *,
|
|
|
|
const char *, bool);
|
|
|
|
static int lockdebug_more(int);
|
2007-03-10 18:56:21 +03:00
|
|
|
static void lockdebug_init(void);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-11-21 13:19:06 +03:00
|
|
|
static signed int
|
2008-07-01 00:14:09 +04:00
|
|
|
ld_rbto_compare_nodes(const struct rb_node *n1, const struct rb_node *n2)
|
2007-11-21 13:19:06 +03:00
|
|
|
{
|
|
|
|
const lockdebug_t *ld1 = (const void *)n1;
|
|
|
|
const lockdebug_t *ld2 = (const void *)n2;
|
2007-11-22 13:47:37 +03:00
|
|
|
const uintptr_t a = (uintptr_t)ld1->ld_lock;
|
|
|
|
const uintptr_t b = (uintptr_t)ld2->ld_lock;
|
|
|
|
|
|
|
|
if (a < b)
|
2007-11-21 13:19:06 +03:00
|
|
|
return 1;
|
2007-11-22 13:47:37 +03:00
|
|
|
if (a > b)
|
|
|
|
return -1;
|
2007-11-21 13:19:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static signed int
|
2008-07-01 00:14:09 +04:00
|
|
|
ld_rbto_compare_key(const struct rb_node *n, const void *key)
|
2007-11-21 13:19:06 +03:00
|
|
|
{
|
|
|
|
const lockdebug_t *ld = (const void *)n;
|
2007-11-22 13:47:37 +03:00
|
|
|
const uintptr_t a = (uintptr_t)ld->ld_lock;
|
|
|
|
const uintptr_t b = (uintptr_t)key;
|
|
|
|
|
|
|
|
if (a < b)
|
2007-11-21 13:19:06 +03:00
|
|
|
return 1;
|
2007-11-22 13:47:37 +03:00
|
|
|
if (a > b)
|
|
|
|
return -1;
|
2007-11-21 13:19:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rb_tree ld_rb_tree;
|
|
|
|
|
|
|
|
static const struct rb_tree_ops ld_rb_tree_ops = {
|
2008-07-01 00:14:09 +04:00
|
|
|
.rbto_compare_nodes = ld_rbto_compare_nodes,
|
|
|
|
.rbto_compare_key = ld_rbto_compare_key,
|
2007-11-21 13:19:06 +03:00
|
|
|
};
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
static inline lockdebug_t *
|
|
|
|
lockdebug_lookup1(volatile void *lock)
|
2007-12-08 18:00:13 +03:00
|
|
|
{
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_t *ld;
|
|
|
|
struct cpu_info *ci;
|
2007-12-08 18:00:13 +03:00
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
ci = curcpu();
|
|
|
|
__cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
|
|
|
|
ld = (lockdebug_t *)rb_tree_find_node(&ld_rb_tree, __UNVOLATILE(lock));
|
|
|
|
__cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
|
|
|
|
if (ld == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
__cpu_simple_lock(&ld->ld_spinlock);
|
2007-12-08 18:00:13 +03:00
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
return ld;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2007-12-08 18:00:13 +03:00
|
|
|
static void
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_lock_cpus(void)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
2008-05-06 22:40:57 +04:00
|
|
|
CPU_INFO_ITERATOR cii;
|
|
|
|
struct cpu_info *ci;
|
2007-12-08 18:00:13 +03:00
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
for (CPU_INFO_FOREACH(cii, ci)) {
|
|
|
|
__cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
|
|
|
|
}
|
2007-12-08 18:00:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_unlock_cpus(void)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
2008-05-06 22:40:57 +04:00
|
|
|
CPU_INFO_ITERATOR cii;
|
|
|
|
struct cpu_info *ci;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
for (CPU_INFO_FOREACH(cii, ci)) {
|
|
|
|
__cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2007-11-21 14:33:11 +03:00
|
|
|
/*
|
|
|
|
* lockdebug_lookup:
|
|
|
|
*
|
|
|
|
* Find a lockdebug structure by a pointer to a lock and return it locked.
|
|
|
|
*/
|
|
|
|
static inline lockdebug_t *
|
2008-11-07 05:40:35 +03:00
|
|
|
lockdebug_lookup(volatile void *lock, uintptr_t where)
|
2007-11-21 14:33:11 +03:00
|
|
|
{
|
|
|
|
lockdebug_t *ld;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
ld = lockdebug_lookup1(lock);
|
2007-11-21 14:33:11 +03:00
|
|
|
if (ld == NULL)
|
2008-11-07 22:50:00 +03:00
|
|
|
panic("lockdebug_lookup: uninitialized lock (lock=%p, from=%08"PRIxPTR")", lock, where);
|
2007-11-21 14:33:11 +03:00
|
|
|
return ld;
|
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* lockdebug_init:
|
|
|
|
*
|
|
|
|
* Initialize the lockdebug system. Allocate an initial pool of
|
|
|
|
* lockdebug structures before the VM system is up and running.
|
|
|
|
*/
|
2007-03-10 18:56:21 +03:00
|
|
|
static void
|
2007-02-10 00:55:00 +03:00
|
|
|
lockdebug_init(void)
|
|
|
|
{
|
|
|
|
lockdebug_t *ld;
|
|
|
|
int i;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
TAILQ_INIT(&curcpu()->ci_data.cpu_ld_locks);
|
|
|
|
TAILQ_INIT(&curlwp->l_ld_locks);
|
|
|
|
__cpu_simple_lock_init(&curcpu()->ci_data.cpu_ld_lock);
|
|
|
|
__cpu_simple_lock_init(&ld_mod_lk);
|
2007-11-12 09:14:57 +03:00
|
|
|
|
2007-11-21 13:19:06 +03:00
|
|
|
rb_tree_init(&ld_rb_tree, &ld_rb_tree_ops);
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
ld = ld_prime;
|
|
|
|
for (i = 1, ld++; i < LD_BATCH; i++, ld++) {
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_lock_init(&ld->ld_spinlock);
|
2007-02-10 00:55:00 +03:00
|
|
|
TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
|
|
|
|
TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
|
|
|
|
}
|
|
|
|
ld_freeptr = 1;
|
|
|
|
ld_nfree = LD_BATCH - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_alloc:
|
|
|
|
*
|
|
|
|
* A lock is being initialized, so allocate an associated debug
|
|
|
|
* structure.
|
|
|
|
*/
|
2007-11-21 13:19:06 +03:00
|
|
|
bool
|
2007-10-11 23:45:24 +04:00
|
|
|
lockdebug_alloc(volatile void *lock, lockops_t *lo, uintptr_t initaddr)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
struct cpu_info *ci;
|
|
|
|
lockdebug_t *ld;
|
2008-05-06 22:40:57 +04:00
|
|
|
int s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
if (lo == NULL || panicstr != NULL || ld_panic)
|
2007-11-21 13:19:06 +03:00
|
|
|
return false;
|
2007-03-10 18:56:21 +03:00
|
|
|
if (ld_freeptr == 0)
|
|
|
|
lockdebug_init();
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
|
|
|
__cpu_simple_lock(&ld_mod_lk);
|
|
|
|
if ((ld = lockdebug_lookup1(lock)) != NULL) {
|
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
|
|
|
lockdebug_abort1(ld, s, __func__, "already initialized", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return false;
|
2007-11-21 14:33:11 +03:00
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* Pinch a new debug structure. We may recurse because we call
|
|
|
|
* kmem_alloc(), which may need to initialize new locks somewhere
|
2007-05-21 15:56:35 +04:00
|
|
|
* down the path. If not recursing, we try to maintain at least
|
2007-02-10 00:55:00 +03:00
|
|
|
* LD_SLOP structures free, which should hopefully be enough to
|
|
|
|
* satisfy kmem_alloc(). If we can't provide a structure, not to
|
|
|
|
* worry: we'll just mark the lock as not having an ID.
|
|
|
|
*/
|
2007-12-08 18:00:13 +03:00
|
|
|
ci = curcpu();
|
2007-02-10 00:55:00 +03:00
|
|
|
ci->ci_lkdebug_recurse++;
|
|
|
|
if (TAILQ_EMPTY(&ld_free)) {
|
2007-03-10 18:56:21 +03:00
|
|
|
if (ci->ci_lkdebug_recurse > 1 || ld_nomore) {
|
2007-02-10 00:55:00 +03:00
|
|
|
ci->ci_lkdebug_recurse--;
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
|
|
|
splx(s);
|
2007-11-21 13:19:06 +03:00
|
|
|
return false;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
s = lockdebug_more(s);
|
|
|
|
} else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP) {
|
|
|
|
s = lockdebug_more(s);
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
if ((ld = TAILQ_FIRST(&ld_free)) == NULL) {
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
|
|
|
splx(s);
|
2007-11-21 13:19:06 +03:00
|
|
|
return false;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&ld_free, ld, ld_chain);
|
|
|
|
ld_nfree--;
|
|
|
|
ci->ci_lkdebug_recurse--;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
if (ld->ld_lock != NULL) {
|
2007-02-10 00:55:00 +03:00
|
|
|
panic("lockdebug_alloc: corrupt table");
|
2008-05-06 22:40:57 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/* Initialise the structure. */
|
|
|
|
ld->ld_lock = lock;
|
|
|
|
ld->ld_lockops = lo;
|
|
|
|
ld->ld_locked = 0;
|
|
|
|
ld->ld_unlocked = 0;
|
|
|
|
ld->ld_lwp = NULL;
|
2007-10-11 23:45:24 +04:00
|
|
|
ld->ld_initaddr = initaddr;
|
2008-05-31 17:15:21 +04:00
|
|
|
ld->ld_flags = (lo->lo_type == LOCKOPS_SLEEP ? LD_SLEEPER : 0);
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_lock_cpus();
|
2007-11-21 13:19:06 +03:00
|
|
|
rb_tree_insert_node(&ld_rb_tree, __UNVOLATILE(&ld->ld_rb_node));
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_unlock_cpus();
|
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
splx(s);
|
2007-11-21 13:19:06 +03:00
|
|
|
return true;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_free:
|
|
|
|
*
|
|
|
|
* A lock is being destroyed, so release debugging resources.
|
|
|
|
*/
|
|
|
|
void
|
2007-11-21 13:19:06 +03:00
|
|
|
lockdebug_free(volatile void *lock)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
lockdebug_t *ld;
|
2008-05-06 22:40:57 +04:00
|
|
|
int s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
if (panicstr != NULL || ld_panic)
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
|
|
|
__cpu_simple_lock(&ld_mod_lk);
|
2008-11-07 05:40:35 +03:00
|
|
|
ld = lockdebug_lookup(lock, (uintptr_t) __builtin_return_address(0));
|
2007-11-21 13:19:06 +03:00
|
|
|
if (ld == NULL) {
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
2008-05-31 17:15:21 +04:00
|
|
|
panic("lockdebug_free: destroying uninitialized object %p"
|
2007-11-21 13:19:06 +03:00
|
|
|
"(ld_lock=%p)", lock, ld->ld_lock);
|
2008-05-31 17:15:21 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__, "record follows", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2008-02-18 19:02:41 +03:00
|
|
|
if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
2008-05-31 17:15:21 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__, "is locked or in use", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_lock_cpus();
|
2007-11-21 13:19:06 +03:00
|
|
|
rb_tree_remove_node(&ld_rb_tree, __UNVOLATILE(&ld->ld_rb_node));
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_unlock_cpus();
|
2007-02-10 00:55:00 +03:00
|
|
|
ld->ld_lock = NULL;
|
|
|
|
TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
|
|
|
|
ld_nfree++;
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld->ld_spinlock);
|
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_more:
|
|
|
|
*
|
|
|
|
* Allocate a batch of debug structures and add to the free list.
|
2008-05-06 22:40:57 +04:00
|
|
|
* Must be called with ld_mod_lk held.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
2008-05-06 22:40:57 +04:00
|
|
|
static int
|
|
|
|
lockdebug_more(int s)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
lockdebug_t *ld;
|
|
|
|
void *block;
|
2007-03-10 18:56:21 +03:00
|
|
|
int i, base, m;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-31 17:15:21 +04:00
|
|
|
/*
|
|
|
|
* Can't call kmem_alloc() if in interrupt context. XXX We could
|
|
|
|
* deadlock, because we don't know which locks the caller holds.
|
|
|
|
*/
|
|
|
|
if (cpu_intr_p() || (curlwp->l_pflag & LP_INTR) != 0) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
while (ld_nfree < LD_SLOP) {
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
|
|
|
__cpu_simple_lock(&ld_mod_lk);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
if (block == NULL)
|
2008-05-06 22:40:57 +04:00
|
|
|
return s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
if (ld_nfree > LD_SLOP) {
|
|
|
|
/* Somebody beat us to it. */
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld_mod_lk);
|
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
|
|
|
__cpu_simple_lock(&ld_mod_lk);
|
2007-02-10 00:55:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
base = ld_freeptr;
|
|
|
|
ld_nfree += LD_BATCH;
|
|
|
|
ld = block;
|
|
|
|
base <<= LD_BATCH_SHIFT;
|
2007-03-10 18:56:21 +03:00
|
|
|
m = min(LD_MAX_LOCKS, base + LD_BATCH);
|
|
|
|
|
|
|
|
if (m == LD_MAX_LOCKS)
|
|
|
|
ld_nomore = true;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-03-10 18:56:21 +03:00
|
|
|
for (i = base; i < m; i++, ld++) {
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_lock_init(&ld->ld_spinlock);
|
2007-02-10 00:55:00 +03:00
|
|
|
TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
|
|
|
|
TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
|
|
|
|
}
|
|
|
|
|
2007-12-01 02:05:43 +03:00
|
|
|
membar_producer();
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
|
|
|
|
return s;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_wantlock:
|
|
|
|
*
|
|
|
|
* Process the preamble to a lock acquire.
|
|
|
|
*/
|
|
|
|
void
|
2008-05-06 21:11:45 +04:00
|
|
|
lockdebug_wantlock(volatile void *lock, uintptr_t where, bool shared,
|
|
|
|
bool trylock)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
struct lwp *l = curlwp;
|
|
|
|
lockdebug_t *ld;
|
2007-02-22 02:48:10 +03:00
|
|
|
bool recurse;
|
2008-05-06 22:40:57 +04:00
|
|
|
int s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
(void)shared;
|
2007-02-22 09:34:42 +03:00
|
|
|
recurse = false;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
if (panicstr != NULL || ld_panic)
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
2008-11-07 05:40:35 +03:00
|
|
|
if ((ld = lockdebug_lookup(lock, where)) == NULL) {
|
2008-05-06 22:40:57 +04:00
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
2008-05-06 22:40:57 +04:00
|
|
|
}
|
2008-05-03 10:24:55 +04:00
|
|
|
if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
if ((ld->ld_flags & LD_SLEEPER) != 0) {
|
2008-05-06 21:11:45 +04:00
|
|
|
if (ld->ld_lwp == l && !(shared && trylock))
|
2007-02-22 09:34:42 +03:00
|
|
|
recurse = true;
|
2007-02-10 00:55:00 +03:00
|
|
|
} else if (ld->ld_cpu == (uint16_t)cpu_number())
|
2007-02-22 09:34:42 +03:00
|
|
|
recurse = true;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2007-10-11 23:45:24 +04:00
|
|
|
if (cpu_intr_p()) {
|
2008-02-18 19:02:41 +03:00
|
|
|
if ((ld->ld_flags & LD_SLEEPER) != 0) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__,
|
2007-10-11 23:45:24 +04:00
|
|
|
"acquiring sleep lock from interrupt context",
|
|
|
|
true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-10-11 23:45:24 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
if (shared)
|
|
|
|
ld->ld_shwant++;
|
|
|
|
else
|
|
|
|
ld->ld_exwant++;
|
2008-02-18 19:02:41 +03:00
|
|
|
if (recurse) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__, "locking against myself",
|
2007-10-11 23:45:24 +04:00
|
|
|
true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld->ld_spinlock);
|
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_locked:
|
|
|
|
*
|
|
|
|
* Process a lock acquire operation.
|
|
|
|
*/
|
|
|
|
void
|
2008-05-31 17:15:21 +04:00
|
|
|
lockdebug_locked(volatile void *lock, void *cvlock, uintptr_t where,
|
|
|
|
int shared)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
struct lwp *l = curlwp;
|
|
|
|
lockdebug_t *ld;
|
2008-05-06 22:40:57 +04:00
|
|
|
int s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
if (panicstr != NULL || ld_panic)
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
2008-11-07 05:40:35 +03:00
|
|
|
if ((ld = lockdebug_lookup(lock, where)) == NULL) {
|
2008-05-06 22:40:57 +04:00
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
2008-05-06 22:40:57 +04:00
|
|
|
}
|
2008-05-31 17:15:21 +04:00
|
|
|
if (cvlock) {
|
|
|
|
KASSERT(ld->ld_lockops->lo_type == LOCKOPS_CV);
|
|
|
|
if (lock == (void *)&lbolt) {
|
|
|
|
/* nothing */
|
|
|
|
} else if (ld->ld_shares++ == 0) {
|
|
|
|
ld->ld_locked = (uintptr_t)cvlock;
|
|
|
|
} else if (cvlock != (void *)ld->ld_locked) {
|
|
|
|
lockdebug_abort1(ld, s, __func__, "multiple locks used"
|
|
|
|
" with condition variable", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (shared) {
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_shlocks++;
|
|
|
|
ld->ld_shares++;
|
|
|
|
ld->ld_shwant--;
|
|
|
|
} else {
|
2008-02-18 19:02:41 +03:00
|
|
|
if ((ld->ld_flags & LD_LOCKED) != 0) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__, "already locked",
|
|
|
|
true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
ld->ld_flags |= LD_LOCKED;
|
|
|
|
ld->ld_locked = where;
|
|
|
|
ld->ld_exwant--;
|
|
|
|
if ((ld->ld_flags & LD_SLEEPER) != 0) {
|
2008-05-06 22:40:57 +04:00
|
|
|
TAILQ_INSERT_TAIL(&l->l_ld_locks, ld, ld_chain);
|
2007-02-10 00:55:00 +03:00
|
|
|
} else {
|
2008-05-06 22:40:57 +04:00
|
|
|
TAILQ_INSERT_TAIL(&curcpu()->ci_data.cpu_ld_locks,
|
|
|
|
ld, ld_chain);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
2008-05-03 10:24:55 +04:00
|
|
|
ld->ld_cpu = (uint16_t)cpu_number();
|
|
|
|
ld->ld_lwp = l;
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld->ld_spinlock);
|
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_unlocked:
|
|
|
|
*
|
|
|
|
* Process a lock release operation.
|
|
|
|
*/
|
|
|
|
void
|
2007-11-21 13:19:06 +03:00
|
|
|
lockdebug_unlocked(volatile void *lock, uintptr_t where, int shared)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
struct lwp *l = curlwp;
|
|
|
|
lockdebug_t *ld;
|
2008-05-06 22:40:57 +04:00
|
|
|
int s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
if (panicstr != NULL || ld_panic)
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
2008-11-07 05:40:35 +03:00
|
|
|
if ((ld = lockdebug_lookup(lock, where)) == NULL) {
|
2008-05-06 22:40:57 +04:00
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
2008-05-06 22:40:57 +04:00
|
|
|
}
|
2008-05-31 17:15:21 +04:00
|
|
|
if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
|
|
|
|
if (lock == (void *)&lbolt) {
|
|
|
|
/* nothing */
|
|
|
|
} else {
|
|
|
|
ld->ld_shares--;
|
|
|
|
}
|
|
|
|
} else if (shared) {
|
2008-02-18 19:02:41 +03:00
|
|
|
if (l->l_shlocks == 0) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__,
|
2007-10-11 23:45:24 +04:00
|
|
|
"no shared locks held by LWP", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ld->ld_shares == 0) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__,
|
2007-10-11 23:45:24 +04:00
|
|
|
"no shared holds on this lock", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_shlocks--;
|
|
|
|
ld->ld_shares--;
|
2008-05-03 10:24:55 +04:00
|
|
|
if (ld->ld_lwp == l)
|
|
|
|
ld->ld_lwp = NULL;
|
|
|
|
if (ld->ld_cpu == (uint16_t)cpu_number())
|
|
|
|
ld->ld_cpu = (uint16_t)-1;
|
2007-02-10 00:55:00 +03:00
|
|
|
} else {
|
2008-02-18 19:02:41 +03:00
|
|
|
if ((ld->ld_flags & LD_LOCKED) == 0) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__, "not locked", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
if ((ld->ld_flags & LD_SLEEPER) != 0) {
|
2008-02-18 19:02:41 +03:00
|
|
|
if (ld->ld_lwp != curlwp) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__,
|
2007-10-11 23:45:24 +04:00
|
|
|
"not held by current LWP", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
ld->ld_flags &= ~LD_LOCKED;
|
|
|
|
ld->ld_unlocked = where;
|
|
|
|
ld->ld_lwp = NULL;
|
2008-05-06 22:40:57 +04:00
|
|
|
TAILQ_REMOVE(&l->l_ld_locks, ld, ld_chain);
|
2007-02-10 00:55:00 +03:00
|
|
|
} else {
|
2008-02-18 19:02:41 +03:00
|
|
|
if (ld->ld_cpu != (uint16_t)cpu_number()) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, __func__,
|
2007-10-11 23:45:24 +04:00
|
|
|
"not held by current CPU", true);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
ld->ld_flags &= ~LD_LOCKED;
|
|
|
|
ld->ld_unlocked = where;
|
|
|
|
ld->ld_lwp = NULL;
|
2008-05-06 22:40:57 +04:00
|
|
|
TAILQ_REMOVE(&curcpu()->ci_data.cpu_ld_locks, ld,
|
|
|
|
ld_chain);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld->ld_spinlock);
|
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2008-05-31 17:15:21 +04:00
|
|
|
/*
|
|
|
|
* lockdebug_wakeup:
|
|
|
|
*
|
|
|
|
* Process a wakeup on a condition variable.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lockdebug_wakeup(volatile void *lock, uintptr_t where)
|
|
|
|
{
|
|
|
|
lockdebug_t *ld;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (panicstr != NULL || ld_panic || lock == (void *)&lbolt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
/* Find the CV... */
|
2008-11-07 05:40:35 +03:00
|
|
|
if ((ld = lockdebug_lookup(lock, where)) == NULL) {
|
2008-05-31 17:15:21 +04:00
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If it has any waiters, ensure that they are using the
|
|
|
|
* same interlock.
|
|
|
|
*/
|
|
|
|
if (ld->ld_shares != 0 && !mutex_owned((kmutex_t *)ld->ld_locked)) {
|
|
|
|
lockdebug_abort1(ld, s, __func__, "interlocking mutex not "
|
|
|
|
"held during wakeup", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
__cpu_simple_unlock(&ld->ld_spinlock);
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* lockdebug_barrier:
|
|
|
|
*
|
|
|
|
* Panic if we hold more than one specified spin lock, and optionally,
|
|
|
|
* if we hold sleep locks.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lockdebug_barrier(volatile void *spinlock, int slplocks)
|
|
|
|
{
|
|
|
|
struct lwp *l = curlwp;
|
|
|
|
lockdebug_t *ld;
|
2008-05-06 22:40:57 +04:00
|
|
|
int s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
if (panicstr != NULL || ld_panic)
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
|
|
|
if ((l->l_pflag & LP_INTR) == 0) {
|
|
|
|
TAILQ_FOREACH(ld, &curcpu()->ci_data.cpu_ld_locks, ld_chain) {
|
2007-02-10 00:55:00 +03:00
|
|
|
if (ld->ld_lock == spinlock) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_lock(&ld->ld_spinlock);
|
|
|
|
lockdebug_abort1(ld, s, __func__,
|
|
|
|
"spin lock held", true);
|
|
|
|
return;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
if (slplocks) {
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((ld = TAILQ_FIRST(&l->l_ld_locks)) != NULL) {
|
|
|
|
__cpu_simple_lock(&ld->ld_spinlock);
|
|
|
|
lockdebug_abort1(ld, s, __func__, "sleep lock held", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
if (l->l_shlocks != 0) {
|
|
|
|
panic("lockdebug_barrier: holding %d shared locks",
|
|
|
|
l->l_shlocks);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 23:45:24 +04:00
|
|
|
/*
|
|
|
|
* lockdebug_mem_check:
|
|
|
|
*
|
|
|
|
* Check for in-use locks within a memory region that is
|
2007-11-21 13:19:06 +03:00
|
|
|
* being freed.
|
2007-10-11 23:45:24 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
lockdebug_mem_check(const char *func, void *base, size_t sz)
|
|
|
|
{
|
|
|
|
lockdebug_t *ld;
|
2008-05-06 22:40:57 +04:00
|
|
|
struct cpu_info *ci;
|
2007-12-08 18:00:13 +03:00
|
|
|
int s;
|
2007-10-11 23:45:24 +04:00
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
if (panicstr != NULL || ld_panic)
|
2007-12-26 19:01:34 +03:00
|
|
|
return;
|
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
|
|
|
ci = curcpu();
|
|
|
|
__cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
|
2007-11-21 13:19:06 +03:00
|
|
|
ld = (lockdebug_t *)rb_tree_find_node_geq(&ld_rb_tree, base);
|
2007-12-08 18:00:13 +03:00
|
|
|
if (ld != NULL) {
|
|
|
|
const uintptr_t lock = (uintptr_t)ld->ld_lock;
|
|
|
|
|
|
|
|
if ((uintptr_t)base > lock)
|
|
|
|
panic("%s: corrupt tree ld=%p, base=%p, sz=%zu",
|
|
|
|
__func__, ld, base, sz);
|
|
|
|
if (lock >= (uintptr_t)base + sz)
|
|
|
|
ld = NULL;
|
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
|
|
|
|
if (ld != NULL) {
|
|
|
|
__cpu_simple_lock(&ld->ld_spinlock);
|
|
|
|
lockdebug_abort1(ld, s, func,
|
|
|
|
"allocation contains active lock", !cold);
|
2007-11-21 13:19:06 +03:00
|
|
|
return;
|
2008-05-06 22:40:57 +04:00
|
|
|
}
|
|
|
|
splx(s);
|
2007-10-11 23:45:24 +04:00
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* lockdebug_dump:
|
|
|
|
*
|
|
|
|
* Dump information about a lock on panic, or for DDB.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lockdebug_dump(lockdebug_t *ld, void (*pr)(const char *, ...))
|
|
|
|
{
|
|
|
|
int sleeper = (ld->ld_flags & LD_SLEEPER);
|
|
|
|
|
|
|
|
(*pr)(
|
|
|
|
"lock address : %#018lx type : %18s\n"
|
2008-05-31 17:15:21 +04:00
|
|
|
"initialized : %#018lx",
|
2007-02-10 00:55:00 +03:00
|
|
|
(long)ld->ld_lock, (sleeper ? "sleep/adaptive" : "spin"),
|
2007-10-11 23:45:24 +04:00
|
|
|
(long)ld->ld_initaddr);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-31 17:15:21 +04:00
|
|
|
if (ld->ld_lockops->lo_type == LOCKOPS_CV) {
|
|
|
|
(*pr)(" interlock: %#018lx\n", ld->ld_locked);
|
|
|
|
} else {
|
|
|
|
(*pr)("\n"
|
|
|
|
"shared holds : %18u exclusive: %18u\n"
|
|
|
|
"shares wanted: %18u exclusive: %18u\n"
|
|
|
|
"current cpu : %18u last held: %18u\n"
|
|
|
|
"current lwp : %#018lx last held: %#018lx\n"
|
|
|
|
"last locked : %#018lx unlocked : %#018lx\n",
|
|
|
|
(unsigned)ld->ld_shares, ((ld->ld_flags & LD_LOCKED) != 0),
|
|
|
|
(unsigned)ld->ld_shwant, (unsigned)ld->ld_exwant,
|
|
|
|
(unsigned)cpu_number(), (unsigned)ld->ld_cpu,
|
|
|
|
(long)curlwp, (long)ld->ld_lwp,
|
|
|
|
(long)ld->ld_locked, (long)ld->ld_unlocked);
|
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (ld->ld_lockops->lo_dump != NULL)
|
|
|
|
(*ld->ld_lockops->lo_dump)(ld->ld_lock);
|
|
|
|
|
|
|
|
if (sleeper) {
|
|
|
|
(*pr)("\n");
|
|
|
|
turnstile_print(ld->ld_lock, pr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-02-18 19:02:41 +03:00
|
|
|
* lockdebug_abort1:
|
2007-02-10 00:55:00 +03:00
|
|
|
*
|
2008-02-18 19:02:41 +03:00
|
|
|
* An error has been trapped - dump lock info and panic.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
2007-03-10 18:56:21 +03:00
|
|
|
static void
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(lockdebug_t *ld, int s, const char *func,
|
2007-10-11 23:45:24 +04:00
|
|
|
const char *msg, bool dopanic)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
/*
|
|
|
|
* Don't make the situation wose if the system is already going
|
|
|
|
* down in flames. Once a panic is triggered, lockdebug state
|
|
|
|
* becomes stale and cannot be trusted.
|
|
|
|
*/
|
|
|
|
if (atomic_inc_uint_nv(&ld_panic) != 1) {
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld->ld_spinlock);
|
|
|
|
splx(s);
|
2008-02-18 19:02:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
printf_nolog("%s error: %s: %s\n\n", ld->ld_lockops->lo_name,
|
|
|
|
func, msg);
|
|
|
|
lockdebug_dump(ld, printf_nolog);
|
2008-05-06 22:40:57 +04:00
|
|
|
__cpu_simple_unlock(&ld->ld_spinlock);
|
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
printf_nolog("\n");
|
2007-10-11 23:45:24 +04:00
|
|
|
if (dopanic)
|
|
|
|
panic("LOCKDEBUG");
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LOCKDEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_lock_print:
|
|
|
|
*
|
|
|
|
* Handle the DDB 'show lock' command.
|
|
|
|
*/
|
|
|
|
#ifdef DDB
|
|
|
|
void
|
|
|
|
lockdebug_lock_print(void *addr, void (*pr)(const char *, ...))
|
|
|
|
{
|
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
lockdebug_t *ld;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(ld, &ld_all, ld_achain) {
|
|
|
|
if (ld->ld_lock == addr) {
|
|
|
|
lockdebug_dump(ld, pr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*pr)("Sorry, no record of a lock with address %p found.\n", addr);
|
|
|
|
#else
|
|
|
|
(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
|
|
|
|
#endif /* LOCKDEBUG */
|
|
|
|
}
|
|
|
|
#endif /* DDB */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lockdebug_abort:
|
|
|
|
*
|
|
|
|
* An error has been trapped - dump lock info and call panic().
|
|
|
|
*/
|
|
|
|
void
|
2007-11-21 13:19:06 +03:00
|
|
|
lockdebug_abort(volatile void *lock, lockops_t *ops, const char *func,
|
|
|
|
const char *msg)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
lockdebug_t *ld;
|
2008-05-06 22:40:57 +04:00
|
|
|
int s;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-06 22:40:57 +04:00
|
|
|
s = splhigh();
|
2008-11-07 05:40:35 +03:00
|
|
|
if ((ld = lockdebug_lookup(lock,
|
|
|
|
(uintptr_t) __builtin_return_address(0))) != NULL) {
|
2008-05-06 22:40:57 +04:00
|
|
|
lockdebug_abort1(ld, s, func, msg, true);
|
|
|
|
return;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2008-05-06 22:40:57 +04:00
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
#endif /* LOCKDEBUG */
|
|
|
|
|
2008-02-18 19:02:41 +03:00
|
|
|
/*
|
|
|
|
* Complain first on the occurrance only. Otherwise proceeed to
|
|
|
|
* panic where we will `rendezvous' with other CPUs if the machine
|
|
|
|
* is going down in flames.
|
|
|
|
*/
|
|
|
|
if (atomic_inc_uint_nv(&ld_panic) == 1) {
|
|
|
|
printf_nolog("%s error: %s: %s\n\n"
|
|
|
|
"lock address : %#018lx\n"
|
|
|
|
"current cpu : %18d\n"
|
|
|
|
"current lwp : %#018lx\n",
|
|
|
|
ops->lo_name, func, msg, (long)lock, (int)cpu_number(),
|
|
|
|
(long)curlwp);
|
|
|
|
(*ops->lo_dump)(lock);
|
|
|
|
printf_nolog("\n");
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
panic("lock error");
|
|
|
|
}
|