/*-
* Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
* 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.
*/
/*
* lockdebug_init:
*
* Initialize the lockdebug system. Allocate an initial pool of
* lockdebug structures before the VM system is up and running.
*/
static void
lockdebug_init(void)
{
lockdebug_t *ld;
int i;
/*
* Pinch a new debug structure. We may recurse because we call
* kmem_alloc(), which may need to initialize new locks somewhere
* down the path. If not recursing, we try to maintain at least
* 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.
*/
ci = curcpu();
ci->ci_lkdebug_recurse++;
if (TAILQ_EMPTY(&ld_free)) {
if (ci->ci_lkdebug_recurse > 1 || ld_nomore) {
ci->ci_lkdebug_recurse--;
__cpu_simple_unlock(&ld_mod_lk);
splx(s);
return false;
}
s = lockdebug_more(s);
} else if (ci->ci_lkdebug_recurse == 1 && ld_nfree < LD_SLOP) {
s = lockdebug_more(s);
}
if (__predict_false((ld = TAILQ_FIRST(&ld_free)) == NULL)) {
__cpu_simple_unlock(&ld_mod_lk);
splx(s);
return false;
}
TAILQ_REMOVE(&ld_free, ld, ld_chain);
ld_nfree--;
ci->ci_lkdebug_recurse--;
/*
* lockdebug_more:
*
* Allocate a batch of debug structures and add to the free list.
* Must be called with ld_mod_lk held.
*/
static int
lockdebug_more(int s)
{
lockdebug_t *ld;
void *block;
int i, base, m;
/*
* 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() || cpu_softintr_p()) {
return s;
}
while (ld_nfree < LD_SLOP) {
__cpu_simple_unlock(&ld_mod_lk);
splx(s);
block = kmem_zalloc(LD_BATCH * sizeof(lockdebug_t), KM_SLEEP);
s = splhigh();
__cpu_simple_lock(&ld_mod_lk);
if (ld_nfree > LD_SLOP) {
/* Somebody beat us to it. */
__cpu_simple_unlock(&ld_mod_lk);
splx(s);
kmem_free(block, LD_BATCH * sizeof(lockdebug_t));
s = splhigh();
__cpu_simple_lock(&ld_mod_lk);
continue;
}
base = ld_freeptr;
ld_nfree += LD_BATCH;
ld = block;
base <<= LD_BATCH_SHIFT;
m = uimin(LD_MAX_LOCKS, base + LD_BATCH);
if (m == LD_MAX_LOCKS)
ld_nomore = true;
for (i = base; i < m; i++, ld++) {
__cpu_simple_lock_init(&ld->ld_spinlock);
TAILQ_INSERT_TAIL(&ld_free, ld, ld_chain);
TAILQ_INSERT_TAIL(&ld_all, ld, ld_achain);
}
membar_producer();
}
return s;
}
/*
* lockdebug_wantlock:
*
* Process the preamble to a lock acquire. The "shared"
* parameter controls which ld_{ex,sh}want counter is
* updated; a negative value of shared updates neither.
*/
void
lockdebug_wantlock(const char *func, size_t line,
const volatile void *lock, uintptr_t where, int shared)
{
struct lwp *l = curlwp;
lockdebug_t *ld;
bool recurse;
int s;
(void)shared;
recurse = false;
if (__predict_false(panicstr != NULL || ld_panic))
return;
s = splhigh();
if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
splx(s);
return;
}
if ((ld->ld_flags & LD_LOCKED) != 0 || ld->ld_shares != 0) {
if ((ld->ld_flags & LD_SLEEPER) != 0) {
if (ld->ld_lwp == l)
recurse = true;
} else if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
recurse = true;
}
if (cpu_intr_p()) {
if (__predict_false((ld->ld_flags & LD_SLEEPER) != 0)) {
lockdebug_abort1(func, line, ld, s,
"acquiring sleep lock from interrupt context",
true);
return;
}
}
if (shared > 0)
ld->ld_shwant++;
else if (shared == 0)
ld->ld_exwant++;
if (__predict_false(recurse)) {
lockdebug_abort1(func, line, ld, s, "locking against myself",
true);
return;
}
if (l->l_ld_wanted == NULL) {
l->l_ld_wanted = ld;
}
__cpu_simple_unlock(&ld->ld_spinlock);
splx(s);
}
/*
* lockdebug_locked:
*
* Process a lock acquire operation.
*/
void
lockdebug_locked(const char *func, size_t line,
volatile void *lock, void *cvlock, uintptr_t where, int shared)
{
struct lwp *l = curlwp;
lockdebug_t *ld;
int s;
if (__predict_false(panicstr != NULL || ld_panic))
return;
/*
* lockdebug_unlocked:
*
* Process a lock release operation.
*/
void
lockdebug_unlocked(const char *func, size_t line,
volatile void *lock, uintptr_t where, int shared)
{
struct lwp *l = curlwp;
lockdebug_t *ld;
int s;
if (__predict_false(panicstr != NULL || ld_panic))
return;
s = splhigh();
if ((ld = lockdebug_lookup(func, line, lock, where)) == NULL) {
splx(s);
return;
}
if (shared) {
if (__predict_false(l->l_shlocks == 0)) {
lockdebug_abort1(func, line, ld, s,
"no shared locks held by LWP", true);
return;
}
if (__predict_false(ld->ld_shares == 0)) {
lockdebug_abort1(func, line, ld, s,
"no shared holds on this lock", true);
return;
}
l->l_shlocks--;
ld->ld_shares--;
if (ld->ld_lwp == l) {
ld->ld_unlocked = where;
ld->ld_lwp = NULL;
}
if (ld->ld_cpu == (uint16_t)cpu_index(curcpu()))
ld->ld_cpu = (uint16_t)-1;
} else {
if (__predict_false((ld->ld_flags & LD_LOCKED) == 0)) {
lockdebug_abort1(func, line, ld, s, "not locked", true);
return;
}
if ((ld->ld_flags & LD_SLEEPER) != 0) {
if (__predict_false(ld->ld_lwp != curlwp)) {
lockdebug_abort1(func, line, ld, s,
"not held by current LWP", true);
return;
}
TAILQ_REMOVE(&l->l_ld_locks, ld, ld_chain);
} else {
uint16_t idx = (uint16_t)cpu_index(curcpu());
if (__predict_false(ld->ld_cpu != idx)) {
lockdebug_abort1(func, line, ld, s,
"not held by current CPU", true);
return;
}
TAILQ_REMOVE(&curcpu()->ci_data.cpu_ld_locks, ld,
ld_chain);
}
ld->ld_flags &= ~LD_LOCKED;
ld->ld_unlocked = where;
ld->ld_lwp = NULL;
}
__cpu_simple_unlock(&ld->ld_spinlock);
splx(s);
}
/*
* lockdebug_barrier:
*
* Panic if we hold more than one specified lock, and optionally, if we
* hold any sleep locks.
*/
void
lockdebug_barrier(const char *func, size_t line, volatile void *onelock,
int slplocks)
{
struct lwp *l = curlwp;
lockdebug_t *ld;
int s;
if (__predict_false(panicstr != NULL || ld_panic))
return;
/*
* lockdebug_mem_check:
*
* Check for in-use locks within a memory region that is
* being freed.
*/
void
lockdebug_mem_check(const char *func, size_t line, void *base, size_t sz)
{
lockdebug_t *ld;
struct cpu_info *ci;
int s;
if (__predict_false(panicstr != NULL || ld_panic))
return;
kcov_silence_enter();
s = splhigh();
ci = curcpu();
__cpu_simple_lock(&ci->ci_data.cpu_ld_lock);
ld = (lockdebug_t *)rb_tree_find_node_geq(&ld_rb_tree, base);
if (ld != NULL) {
const uintptr_t lock = (uintptr_t)ld->ld_lock;
if (__predict_false((uintptr_t)base > lock))
panic("%s,%zu: corrupt tree ld=%p, base=%p, sz=%zu",
func, line, ld, base, sz);
if (lock >= (uintptr_t)base + sz)
ld = NULL;
}
__cpu_simple_unlock(&ci->ci_data.cpu_ld_lock);
if (__predict_false(ld != NULL)) {
__cpu_simple_lock(&ld->ld_spinlock);
lockdebug_abort1(func, line, ld, s,
"allocation contains active lock", !cold);
kcov_silence_leave();
return;
}
splx(s);
kcov_silence_leave();
}
#endif /* _KERNEL */
/*
* lockdebug_dump:
*
* Dump information about a lock on panic, or for DDB.
*/
static void
lockdebug_dump(lwp_t *l, lockdebug_t *ld, void (*pr)(const char *, ...)
__printflike(1, 2))
{
int sleeper = (ld->ld_flags & LD_SLEEPER);
lockops_t *lo = ld->ld_lockops;
char locksym[128], initsym[128], lockedsym[128], unlockedsym[128];
#ifdef _KERNEL
if (lo->lo_dump != NULL)
(*lo->lo_dump)(ld->ld_lock, pr);
if (sleeper) {
turnstile_print(ld->ld_lock, pr);
}
#endif
}
#ifdef _KERNEL
/*
* lockdebug_abort1:
*
* An error has been trapped - dump lock info and panic.
*/
static void
lockdebug_abort1(const char *func, size_t line, lockdebug_t *ld, int s,
const char *msg, bool dopanic)
{
/*
* Don't make the situation worse 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) {
__cpu_simple_unlock(&ld->ld_spinlock);
splx(s);
return;
}
(*pr)("[Locks tracked through LWPs]\n");
lockdebug_show_all_locks_lwp(pr, show_trace);
(*pr)("\n");
(*pr)("[Locks tracked through CPUs]\n");
lockdebug_show_all_locks_cpu(pr, show_trace);
(*pr)("\n");
#else
(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
#endif /* LOCKDEBUG */
}
RB_TREE_FOREACH(_ld, &ld_rb_tree) {
ld = _ld;
if (ld->ld_lock == NULL) {
n_null++;
continue;
}
if (ld->ld_lockops->lo_name[0] == 'M') {
if (ld->ld_lockops->lo_type == LOCKOPS_SLEEP)
n_adaptive_mutex++;
else
n_spin_mutex++;
continue;
}
if (ld->ld_lockops->lo_name[0] == 'R') {
n_rwlock++;
continue;
}
n_others++;
}
(*pr)(
"spin mutex: %u\n"
"adaptive mutex: %u\n"
"rwlock: %u\n"
"null locks: %u\n"
"others: %u\n",
n_spin_mutex, n_adaptive_mutex, n_rwlock,
n_null, n_others);
#else
(*pr)("Sorry, kernel not built with the LOCKDEBUG option.\n");
#endif /* LOCKDEBUG */
}
#endif /* _KERNEL */
#endif /* DDB */
#ifdef _KERNEL
/*
* lockdebug_dismiss:
*
* The system is rebooting, and potentially from an unsafe
* place so avoid any future aborts.
*/
void
lockdebug_dismiss(void)
{
atomic_inc_uint_nv(&ld_panic);
}
/*
* lockdebug_abort:
*
* An error has been trapped - dump lock info and call panic().
*/
void
lockdebug_abort(const char *func, size_t line, const volatile void *lock,
lockops_t *ops, const char *msg)
{
#ifdef LOCKDEBUG
lockdebug_t *ld;
int s;
/*
* Don't make the situation worse 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)
return;