2008-05-27 21:50:03 +04:00
|
|
|
/* $NetBSD: kern_lock.c,v 1.143 2008/05/27 17:50:03 ad Exp $ */
|
1999-07-25 10:24:22 +04:00
|
|
|
|
|
|
|
/*-
|
2008-01-30 17:54:25 +03:00
|
|
|
* Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
|
1999-07-25 10:24:22 +04:00
|
|
|
* 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.
|
1999-07-25 10:24:22 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
1997-07-06 16:35:33 +04:00
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2008-05-27 21:50:03 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.143 2008/05/27 17:50:03 ad Exp $");
|
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>
|
1997-07-06 16:35:33 +04:00
|
|
|
#include <sys/systm.h>
|
2007-11-06 03:42:39 +03:00
|
|
|
#include <sys/kernel.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/lockdebug.h>
|
2007-10-11 23:45:24 +04:00
|
|
|
#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
|
|
|
|
2007-03-04 09:20:25 +03:00
|
|
|
#include <machine/stdarg.h>
|
2008-01-05 00:17:40 +03:00
|
|
|
#include <machine/lock.h>
|
1997-07-06 16:19:53 +04:00
|
|
|
|
2006-09-07 05:08:45 +04:00
|
|
|
#include <dev/lockstat.h>
|
|
|
|
|
2008-01-30 17:54:25 +03:00
|
|
|
#define RETURN_ADDRESS (uintptr_t)__builtin_return_address(0)
|
1999-08-27 05:14:38 +04:00
|
|
|
|
2007-11-21 13:19:06 +03:00
|
|
|
bool kernel_lock_dodebug;
|
2008-01-10 23:14:10 +03:00
|
|
|
|
|
|
|
__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
|
|
|
|
2008-03-17 11:27:50 +03:00
|
|
|
#if defined(DEBUG) || defined(LKM)
|
2006-07-21 14:07:29 +04:00
|
|
|
void
|
2008-03-17 11:27:50 +03:00
|
|
|
assert_sleepable(void)
|
2006-07-21 14:07:29 +04:00
|
|
|
{
|
2008-03-17 11:27:50 +03:00
|
|
|
#if !defined(_RUMPKERNEL)
|
|
|
|
const char *reason;
|
2006-07-21 14:07:29 +04:00
|
|
|
|
2008-03-17 11:27:50 +03:00
|
|
|
if (panicstr != NULL) {
|
2007-07-29 15:45:21 +04:00
|
|
|
return;
|
2008-03-17 11:27:50 +03:00
|
|
|
}
|
|
|
|
|
2008-01-10 23:14:10 +03:00
|
|
|
LOCKDEBUG_BARRIER(kernel_lock, 1);
|
2008-03-17 11:27:50 +03:00
|
|
|
|
|
|
|
reason = NULL;
|
2007-11-06 03:42:39 +03:00
|
|
|
if (CURCPU_IDLE_P() && !cold) {
|
2008-03-17 11:27:50 +03:00
|
|
|
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);
|
2006-07-21 14:22:51 +04:00
|
|
|
}
|
2008-03-17 11:27:50 +03:00
|
|
|
#endif /* !defined(_RUMPKERNEL) */
|
2006-07-21 14:07:29 +04:00
|
|
|
}
|
2008-03-17 11:27:50 +03:00
|
|
|
#endif /* defined(DEBUG) || defined(LKM) */
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-10-31 18:36:07 +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
|
2007-10-31 18:36:07 +03:00
|
|
|
* 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
|
2002-05-21 05:38:26 +04:00
|
|
|
/*
|
|
|
|
* 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) \
|
2008-01-10 23:14:10 +03:00
|
|
|
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 */
|
2004-10-26 04:14:46 +04:00
|
|
|
#endif
|
2002-05-21 05:38:26 +04:00
|
|
|
|
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.
|
|
|
|
*/
|
2002-05-21 05:38:26 +04:00
|
|
|
void
|
2007-10-11 23:45:24 +04:00
|
|
|
kernel_lock_init(void)
|
2002-05-21 05:38:26 +04:00
|
|
|
{
|
|
|
|
|
2008-01-10 23:14:10 +03:00
|
|
|
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,
|
2007-10-11 23:45:24 +04:00
|
|
|
RETURN_ADDRESS);
|
2002-05-21 05:38:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Print debugging information about the kernel lock.
|
2002-05-21 05:38:26 +04:00
|
|
|
*/
|
|
|
|
void
|
2007-02-10 00:55:00 +03:00
|
|
|
_kernel_lock_dump(volatile void *junk)
|
2002-05-21 05:38:26 +04:00
|
|
|
{
|
2004-10-26 04:14:46 +04:00
|
|
|
struct cpu_info *ci = curcpu();
|
2002-05-21 05:38:26 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
(void)junk;
|
2004-10-26 04:14:46 +04:00
|
|
|
|
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);
|
2002-05-21 05:38:26 +04:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2002-05-21 05:38:26 +04:00
|
|
|
void
|
2008-04-01 23:49:31 +04:00
|
|
|
_kernel_lock(int nlocks)
|
2002-05-21 05:38:26 +04:00
|
|
|
{
|
2008-04-27 18:13:05 +04:00
|
|
|
struct cpu_info *ci;
|
2007-02-10 00:55:00 +03:00
|
|
|
LOCKSTAT_TIMER(spintime);
|
|
|
|
LOCKSTAT_FLAG(lsflag);
|
|
|
|
struct lwp *owant;
|
|
|
|
u_int spins;
|
2004-10-26 04:14:46 +04:00
|
|
|
int s;
|
2008-04-01 23:49:31 +04:00
|
|
|
struct lwp *l = curlwp;
|
2004-10-26 04:14:46 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
_KERNEL_LOCK_ASSERT(nlocks > 0);
|
2002-05-21 05:38:26 +04:00
|
|
|
|
2008-04-27 18:13:05 +04:00
|
|
|
s = splvm();
|
|
|
|
ci = curcpu();
|
2007-02-10 00:55:00 +03:00
|
|
|
if (ci->ci_biglock_count != 0) {
|
2008-01-10 23:14:10 +03:00
|
|
|
_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
|
2007-02-10 00:55:00 +03:00
|
|
|
ci->ci_biglock_count += nlocks;
|
2007-10-11 23:45:24 +04:00
|
|
|
l->l_blcnt += nlocks;
|
2008-04-27 18:13:05 +04:00
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-11 23:45:24 +04:00
|
|
|
_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
|
2008-01-10 23:14:10 +03:00
|
|
|
LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
|
2008-05-06 21:11:45 +04:00
|
|
|
false, false);
|
2007-02-20 19:10:10 +03:00
|
|
|
|
2008-01-10 23:14:10 +03:00
|
|
|
if (__cpu_simple_lock_try(kernel_lock)) {
|
2007-02-10 00:55:00 +03:00
|
|
|
ci->ci_biglock_count = nlocks;
|
2007-10-11 23:45:24 +04:00
|
|
|
l->l_blcnt = nlocks;
|
2008-01-10 23:14:10 +03:00
|
|
|
LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock,
|
2007-11-21 13:19:06 +03:00
|
|
|
RETURN_ADDRESS, 0);
|
2007-02-10 00:55:00 +03:00
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-01-10 23:14:10 +03:00
|
|
|
* 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
|
|
|
*/
|
2008-01-10 23:14:10 +03:00
|
|
|
membar_producer();
|
2007-02-10 00:55:00 +03:00
|
|
|
owant = ci->ci_biglock_wanted;
|
2008-01-10 23:14:10 +03:00
|
|
|
ci->ci_biglock_wanted = l;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-01-10 23:14:10 +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
|
|
|
|
2008-01-10 23:14:10 +03:00
|
|
|
spins = 0;
|
2007-02-10 00:55:00 +03:00
|
|
|
do {
|
2007-10-11 23:45:24 +04:00
|
|
|
splx(s);
|
2008-01-10 23:14:10 +03:00
|
|
|
while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
|
|
|
|
if (SPINLOCK_SPINOUT(spins)) {
|
2008-05-27 21:50:03 +04:00
|
|
|
extern int start_init_exec;
|
2008-03-30 19:39:46 +04:00
|
|
|
if (!start_init_exec)
|
|
|
|
_KERNEL_LOCK_ABORT("spinout");
|
2008-01-10 23:14:10 +03:00
|
|
|
}
|
2007-10-11 23:45:24 +04:00
|
|
|
SPINLOCK_BACKOFF_HOOK;
|
2007-02-10 00:55:00 +03:00
|
|
|
SPINLOCK_SPIN_HOOK;
|
|
|
|
}
|
2008-01-10 23:14:10 +03:00
|
|
|
s = splvm();
|
|
|
|
} while (!__cpu_simple_lock_try(kernel_lock));
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-10-11 23:45:24 +04:00
|
|
|
ci->ci_biglock_count = nlocks;
|
|
|
|
l->l_blcnt = nlocks;
|
2007-02-20 19:10:10 +03:00
|
|
|
LOCKSTAT_STOP_TIMER(lsflag, spintime);
|
2008-01-10 23:14:10 +03:00
|
|
|
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);
|
2004-10-26 04:14:46 +04:00
|
|
|
splx(s);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/*
|
2008-01-10 23:14:10 +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
|
|
|
*/
|
2008-01-10 23:14:10 +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();
|
2002-05-21 05:38:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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.
|
2002-05-21 05:38:26 +04:00
|
|
|
*/
|
|
|
|
void
|
2008-04-01 23:49:31 +04:00
|
|
|
_kernel_unlock(int nlocks, int *countp)
|
2002-05-21 05:38:26 +04:00
|
|
|
{
|
2008-04-27 18:13:05 +04:00
|
|
|
struct cpu_info *ci;
|
2007-02-10 00:55:00 +03:00
|
|
|
u_int olocks;
|
|
|
|
int s;
|
2008-04-01 23:49:31 +04:00
|
|
|
struct lwp *l = curlwp;
|
2002-05-21 05:38:26 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
_KERNEL_LOCK_ASSERT(nlocks < 2);
|
2002-05-21 05:38:26 +04:00
|
|
|
|
2007-10-11 23:45:24 +04:00
|
|
|
olocks = l->l_blcnt;
|
2004-05-18 15:59:11 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (olocks == 0) {
|
|
|
|
_KERNEL_LOCK_ASSERT(nlocks <= 0);
|
|
|
|
if (countp != NULL)
|
|
|
|
*countp = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-18 15:59:11 +04:00
|
|
|
|
2008-01-10 23:14:10 +03:00
|
|
|
_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
|
2004-10-26 04:14:46 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (nlocks == 0)
|
|
|
|
nlocks = olocks;
|
|
|
|
else if (nlocks == -1) {
|
|
|
|
nlocks = 1;
|
|
|
|
_KERNEL_LOCK_ASSERT(olocks == 1);
|
|
|
|
}
|
2008-04-27 18:13:05 +04:00
|
|
|
s = splvm();
|
|
|
|
ci = curcpu();
|
2007-10-11 23:45:24 +04:00
|
|
|
_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
|
|
|
|
if (ci->ci_biglock_count == nlocks) {
|
2008-01-10 23:14:10 +03:00
|
|
|
LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
|
2007-11-21 13:19:06 +03:00
|
|
|
RETURN_ADDRESS, 0);
|
2007-10-11 23:45:24 +04:00
|
|
|
ci->ci_biglock_count = 0;
|
2008-01-10 23:14:10 +03:00
|
|
|
__cpu_simple_unlock(kernel_lock);
|
2008-04-27 18:13:05 +04:00
|
|
|
l->l_blcnt -= nlocks;
|
2007-10-11 23:45:24 +04:00
|
|
|
splx(s);
|
2008-04-28 19:36:01 +04:00
|
|
|
if (l->l_dopreempt)
|
|
|
|
kpreempt(0);
|
2008-04-27 18:13:05 +04:00
|
|
|
} else {
|
2007-10-11 23:45:24 +04:00
|
|
|
ci->ci_biglock_count -= nlocks;
|
2008-04-27 18:13:05 +04:00
|
|
|
l->l_blcnt -= nlocks;
|
|
|
|
splx(s);
|
|
|
|
}
|
2004-05-18 15:59:11 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (countp != NULL)
|
|
|
|
*countp = olocks;
|
2004-05-18 15:59:11 +04:00
|
|
|
}
|
2007-10-31 18:36:07 +03:00
|
|
|
#endif /* !_RUMPKERNEL */
|