NetBSD/sys/kern/kern_lock.c

317 lines
8.5 KiB
C
Raw Normal View History

/* $NetBSD: kern_lock.c,v 1.138 2008/04/27 14:13:05 ad Exp $ */
/*-
2008-01-30 17:54:25 +03:00
* Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
2007-02-10 00:55:00 +03:00
* NASA Ames Research Center, and 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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.
*/
2001-11-12 18:25:01 +03:00
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.138 2008/04/27 14:13:05 ad Exp $");
2001-11-12 18:25:01 +03:00
#include "opt_multiprocessor.h"
2007-02-10 00:55:00 +03:00
1997-07-06 16:19:53 +04:00
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/systm.h>
#include <sys/kernel.h>
2007-02-10 00:55:00 +03:00
#include <sys/lockdebug.h>
#include <sys/cpu.h>
#include <sys/syslog.h>
2007-12-01 02:05:43 +03:00
#include <sys/atomic.h>
2007-02-10 00:55:00 +03:00
#include <machine/stdarg.h>
#include <machine/lock.h>
1997-07-06 16:19:53 +04:00
#include <dev/lockstat.h>
2008-01-30 17:54:25 +03:00
#define RETURN_ADDRESS (uintptr_t)__builtin_return_address(0)
bool kernel_lock_dodebug;
__cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
__aligned(CACHE_LINE_SIZE);
1997-07-06 16:19:53 +04:00
#if defined(DEBUG) || defined(LKM)
void
assert_sleepable(void)
{
#if !defined(_RUMPKERNEL)
const char *reason;
if (panicstr != NULL) {
2007-07-29 15:45:21 +04:00
return;
}
LOCKDEBUG_BARRIER(kernel_lock, 1);
reason = NULL;
if (CURCPU_IDLE_P() && !cold) {
reason = "idle";
}
if (cpu_intr_p()) {
reason = "interrupt";
}
if ((curlwp->l_pflag & LP_INTR) != 0) {
reason = "softint";
}
if (reason) {
panic("%s: %s caller=%p", __func__, reason,
(void *)RETURN_ADDRESS);
}
#endif /* !defined(_RUMPKERNEL) */
}
#endif /* defined(DEBUG) || defined(LKM) */
2007-02-10 00:55:00 +03:00
/*
* rump doesn't need the kernel lock so force it out. We cannot
* currently easily include it for compilation because of
2007-12-01 02:05:43 +03:00
* a) SPINLOCK_* b) membar_producer(). They are defined in different
* places / way for each arch, so just simply do not bother to
* fight a lot for no gain (i.e. pain but still no gain).
*/
#ifndef _RUMPKERNEL
/*
* Functions for manipulating the kernel_lock. We put them here
* so that they show up in profiles.
*/
2007-02-10 00:55:00 +03:00
#define _KERNEL_LOCK_ABORT(msg) \
LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
2007-02-10 00:55:00 +03:00
#ifdef LOCKDEBUG
#define _KERNEL_LOCK_ASSERT(cond) \
do { \
if (!(cond)) \
_KERNEL_LOCK_ABORT("assertion failed: " #cond); \
} while (/* CONSTCOND */ 0)
#else
#define _KERNEL_LOCK_ASSERT(cond) /* nothing */
#endif
2007-02-10 00:55:00 +03:00
void _kernel_lock_dump(volatile void *);
lockops_t _kernel_lock_ops = {
"Kernel lock",
0,
_kernel_lock_dump
};
/*
* Initialize the kernel lock.
*/
void
kernel_lock_init(void)
{
KASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
__cpu_simple_lock_init(kernel_lock);
kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
RETURN_ADDRESS);
}
/*
2007-02-10 00:55:00 +03:00
* Print debugging information about the kernel lock.
*/
void
2007-02-10 00:55:00 +03:00
_kernel_lock_dump(volatile void *junk)
{
struct cpu_info *ci = curcpu();
2007-02-10 00:55:00 +03:00
(void)junk;
2007-02-10 00:55:00 +03:00
printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
}
2007-02-10 00:55:00 +03:00
/*
* Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
* acquisition is from process context.
*/
void
_kernel_lock(int nlocks)
{
struct cpu_info *ci;
2007-02-10 00:55:00 +03:00
LOCKSTAT_TIMER(spintime);
LOCKSTAT_FLAG(lsflag);
struct lwp *owant;
u_int spins;
int s;
struct lwp *l = curlwp;
2007-02-10 00:55:00 +03:00
_KERNEL_LOCK_ASSERT(nlocks > 0);
s = splvm();
ci = curcpu();
2007-02-10 00:55:00 +03:00
if (ci->ci_biglock_count != 0) {
_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
2007-02-10 00:55:00 +03:00
ci->ci_biglock_count += nlocks;
l->l_blcnt += nlocks;
splx(s);
2007-02-10 00:55:00 +03:00
return;
}
_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
0);
if (__cpu_simple_lock_try(kernel_lock)) {
2007-02-10 00:55:00 +03:00
ci->ci_biglock_count = nlocks;
l->l_blcnt = nlocks;
LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock,
RETURN_ADDRESS, 0);
2007-02-10 00:55:00 +03:00
splx(s);
return;
}
/*
* To remove the ordering constraint between adaptive mutexes
* and kernel_lock we must make it appear as if this thread is
* blocking. For non-interlocked mutex release, a store fence
* is required to ensure that the result of any mutex_exit()
* by the current LWP becomes visible on the bus before the set
* of ci->ci_biglock_wanted becomes visible.
2007-02-10 00:55:00 +03:00
*/
membar_producer();
2007-02-10 00:55:00 +03:00
owant = ci->ci_biglock_wanted;
ci->ci_biglock_wanted = l;
2007-02-10 00:55:00 +03:00
/*
* Spin until we acquire the lock. Once we have it, record the
* time spent with lockstat.
*/
LOCKSTAT_ENTER(lsflag);
LOCKSTAT_START_TIMER(lsflag, spintime);
2007-02-10 00:55:00 +03:00
spins = 0;
2007-02-10 00:55:00 +03:00
do {
splx(s);
while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
if (SPINLOCK_SPINOUT(spins)) {
extern volatile int start_init_exec;
if (!start_init_exec)
_KERNEL_LOCK_ABORT("spinout");
}
SPINLOCK_BACKOFF_HOOK;
2007-02-10 00:55:00 +03:00
SPINLOCK_SPIN_HOOK;
}
s = splvm();
} while (!__cpu_simple_lock_try(kernel_lock));
2007-02-10 00:55:00 +03:00
ci->ci_biglock_count = nlocks;
l->l_blcnt = nlocks;
LOCKSTAT_STOP_TIMER(lsflag, spintime);
LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS, 0);
if (owant == NULL) {
LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
}
LOCKSTAT_EXIT(lsflag);
splx(s);
2007-02-10 00:55:00 +03:00
/*
* Now that we have kernel_lock, reset ci_biglock_wanted. This
* store must be unbuffered (immediately visible on the bus) in
* order for non-interlocked mutex release to work correctly.
* It must be visible before a mutex_exit() can execute on this
* processor.
*
* Note: only where CAS is available in hardware will this be
* an unbuffered write, but non-interlocked release cannot be
* done on CPUs without CAS in hardware.
2007-02-10 00:55:00 +03:00
*/
(void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
/*
* Issue a memory barrier as we have acquired a lock. This also
* prevents stores from a following mutex_exit() being reordered
* to occur before our store to ci_biglock_wanted above.
*/
membar_enter();
}
/*
2007-02-10 00:55:00 +03:00
* Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
* all holds. If 'l' is non-null, the release is from process context.
*/
void
_kernel_unlock(int nlocks, int *countp)
{
struct cpu_info *ci;
2007-02-10 00:55:00 +03:00
u_int olocks;
int s;
struct lwp *l = curlwp;
2007-02-10 00:55:00 +03:00
_KERNEL_LOCK_ASSERT(nlocks < 2);
olocks = l->l_blcnt;
2007-02-10 00:55:00 +03:00
if (olocks == 0) {
_KERNEL_LOCK_ASSERT(nlocks <= 0);
if (countp != NULL)
*countp = 0;
return;
}
_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
2007-02-10 00:55:00 +03:00
if (nlocks == 0)
nlocks = olocks;
else if (nlocks == -1) {
nlocks = 1;
_KERNEL_LOCK_ASSERT(olocks == 1);
}
s = splvm();
ci = curcpu();
_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
if (ci->ci_biglock_count == nlocks) {
LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
RETURN_ADDRESS, 0);
ci->ci_biglock_count = 0;
__cpu_simple_unlock(kernel_lock);
l->l_blcnt -= nlocks;
splx(s);
} else {
ci->ci_biglock_count -= nlocks;
l->l_blcnt -= nlocks;
splx(s);
}
2007-02-10 00:55:00 +03:00
if (countp != NULL)
*countp = olocks;
}
#endif /* !_RUMPKERNEL */