Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.137.4.1
      1  1.137.4.1      yamt /*	$NetBSD: kern_lock.c,v 1.137.4.1 2008/05/16 02:25:25 yamt Exp $	*/
      2       1.19   thorpej 
      3       1.19   thorpej /*-
      4      1.134        ad  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5       1.19   thorpej  * All rights reserved.
      6       1.19   thorpej  *
      7       1.19   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.19   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.105        ad  * NASA Ames Research Center, and by Andrew Doran.
     10       1.19   thorpej  *
     11       1.19   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.19   thorpej  * modification, are permitted provided that the following conditions
     13       1.19   thorpej  * are met:
     14       1.19   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.19   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.19   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.19   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.19   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.19   thorpej  *
     20       1.19   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.19   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.19   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.19   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.19   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.19   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.19   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.19   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.19   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.19   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.19   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31       1.19   thorpej  */
     32        1.2      fvdl 
     33       1.60     lukem #include <sys/cdefs.h>
     34  1.137.4.1      yamt __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.137.4.1 2008/05/16 02:25:25 yamt Exp $");
     35        1.7   thorpej 
     36       1.21   thorpej #include "opt_multiprocessor.h"
     37      1.105        ad 
     38        1.1      fvdl #include <sys/param.h>
     39        1.1      fvdl #include <sys/proc.h>
     40        1.1      fvdl #include <sys/lock.h>
     41        1.2      fvdl #include <sys/systm.h>
     42      1.125        ad #include <sys/kernel.h>
     43      1.105        ad #include <sys/lockdebug.h>
     44      1.122        ad #include <sys/cpu.h>
     45      1.122        ad #include <sys/syslog.h>
     46      1.128        ad #include <sys/atomic.h>
     47      1.105        ad 
     48      1.110  christos #include <machine/stdarg.h>
     49      1.131        ad #include <machine/lock.h>
     50        1.1      fvdl 
     51       1.98        ad #include <dev/lockstat.h>
     52       1.98        ad 
     53      1.134        ad #define	RETURN_ADDRESS	(uintptr_t)__builtin_return_address(0)
     54       1.25   thorpej 
     55      1.127      yamt bool	kernel_lock_dodebug;
     56      1.132        ad 
     57      1.132        ad __cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
     58      1.132        ad     __aligned(CACHE_LINE_SIZE);
     59        1.1      fvdl 
     60      1.135      yamt #if defined(DEBUG) || defined(LKM)
     61       1.96      yamt void
     62      1.135      yamt assert_sleepable(void)
     63       1.96      yamt {
     64      1.135      yamt #if !defined(_RUMPKERNEL)
     65      1.135      yamt 	const char *reason;
     66       1.96      yamt 
     67      1.135      yamt 	if (panicstr != NULL) {
     68      1.117        ad 		return;
     69      1.135      yamt 	}
     70      1.135      yamt 
     71      1.132        ad 	LOCKDEBUG_BARRIER(kernel_lock, 1);
     72      1.135      yamt 
     73      1.135      yamt 	reason = NULL;
     74      1.125        ad 	if (CURCPU_IDLE_P() && !cold) {
     75      1.135      yamt 		reason = "idle";
     76      1.135      yamt 	}
     77      1.135      yamt 	if (cpu_intr_p()) {
     78      1.135      yamt 		reason = "interrupt";
     79       1.97      yamt 	}
     80      1.135      yamt 	if ((curlwp->l_pflag & LP_INTR) != 0) {
     81      1.135      yamt 		reason = "softint";
     82      1.135      yamt 	}
     83      1.135      yamt 
     84      1.135      yamt 	if (reason) {
     85      1.135      yamt 		panic("%s: %s caller=%p", __func__, reason,
     86      1.135      yamt 		    (void *)RETURN_ADDRESS);
     87      1.135      yamt 	}
     88      1.135      yamt #endif /* !defined(_RUMPKERNEL) */
     89       1.96      yamt }
     90      1.135      yamt #endif /* defined(DEBUG) || defined(LKM) */
     91      1.105        ad 
     92       1.62   thorpej /*
     93      1.124     pooka  * rump doesn't need the kernel lock so force it out.  We cannot
     94      1.124     pooka  * currently easily include it for compilation because of
     95      1.128        ad  * a) SPINLOCK_* b) membar_producer().  They are defined in different
     96      1.124     pooka  * places / way for each arch, so just simply do not bother to
     97      1.124     pooka  * fight a lot for no gain (i.e. pain but still no gain).
     98      1.124     pooka  */
     99      1.124     pooka #ifndef _RUMPKERNEL
    100      1.124     pooka /*
    101       1.62   thorpej  * Functions for manipulating the kernel_lock.  We put them here
    102       1.62   thorpej  * so that they show up in profiles.
    103       1.62   thorpej  */
    104       1.62   thorpej 
    105      1.105        ad #define	_KERNEL_LOCK_ABORT(msg)						\
    106      1.132        ad     LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
    107      1.105        ad 
    108      1.105        ad #ifdef LOCKDEBUG
    109      1.105        ad #define	_KERNEL_LOCK_ASSERT(cond)					\
    110      1.105        ad do {									\
    111      1.105        ad 	if (!(cond))							\
    112      1.105        ad 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
    113      1.105        ad } while (/* CONSTCOND */ 0)
    114      1.105        ad #else
    115      1.105        ad #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
    116      1.105        ad #endif
    117      1.105        ad 
    118      1.105        ad void	_kernel_lock_dump(volatile void *);
    119      1.105        ad 
    120      1.105        ad lockops_t _kernel_lock_ops = {
    121      1.105        ad 	"Kernel lock",
    122      1.105        ad 	0,
    123      1.105        ad 	_kernel_lock_dump
    124      1.105        ad };
    125      1.105        ad 
    126       1.85      yamt /*
    127      1.105        ad  * Initialize the kernel lock.
    128       1.85      yamt  */
    129       1.62   thorpej void
    130      1.122        ad kernel_lock_init(void)
    131       1.62   thorpej {
    132       1.62   thorpej 
    133      1.132        ad 	KASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
    134      1.132        ad 	__cpu_simple_lock_init(kernel_lock);
    135      1.132        ad 	kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
    136      1.122        ad 	    RETURN_ADDRESS);
    137       1.62   thorpej }
    138       1.62   thorpej 
    139       1.62   thorpej /*
    140      1.105        ad  * Print debugging information about the kernel lock.
    141       1.62   thorpej  */
    142       1.62   thorpej void
    143      1.105        ad _kernel_lock_dump(volatile void *junk)
    144       1.62   thorpej {
    145       1.85      yamt 	struct cpu_info *ci = curcpu();
    146       1.62   thorpej 
    147      1.105        ad 	(void)junk;
    148       1.85      yamt 
    149      1.105        ad 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
    150      1.105        ad 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
    151       1.62   thorpej }
    152       1.62   thorpej 
    153      1.105        ad /*
    154      1.105        ad  * Acquire 'nlocks' holds on the kernel lock.  If 'l' is non-null, the
    155      1.105        ad  * acquisition is from process context.
    156      1.105        ad  */
    157       1.62   thorpej void
    158      1.137  drochner _kernel_lock(int nlocks)
    159       1.62   thorpej {
    160  1.137.4.1      yamt 	struct cpu_info *ci;
    161      1.105        ad 	LOCKSTAT_TIMER(spintime);
    162      1.105        ad 	LOCKSTAT_FLAG(lsflag);
    163      1.105        ad 	struct lwp *owant;
    164      1.105        ad 	u_int spins;
    165       1.85      yamt 	int s;
    166      1.137  drochner 	struct lwp *l = curlwp;
    167       1.85      yamt 
    168      1.105        ad 	_KERNEL_LOCK_ASSERT(nlocks > 0);
    169       1.62   thorpej 
    170  1.137.4.1      yamt 	s = splvm();
    171  1.137.4.1      yamt 	ci = curcpu();
    172      1.105        ad 	if (ci->ci_biglock_count != 0) {
    173      1.132        ad 		_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    174      1.105        ad 		ci->ci_biglock_count += nlocks;
    175      1.122        ad 		l->l_blcnt += nlocks;
    176  1.137.4.1      yamt 		splx(s);
    177      1.105        ad 		return;
    178      1.105        ad 	}
    179      1.105        ad 
    180      1.122        ad 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
    181      1.132        ad 	LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
    182  1.137.4.1      yamt 	    false, false);
    183      1.107        ad 
    184      1.132        ad 	if (__cpu_simple_lock_try(kernel_lock)) {
    185      1.105        ad 		ci->ci_biglock_count = nlocks;
    186      1.122        ad 		l->l_blcnt = nlocks;
    187      1.132        ad 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock,
    188      1.127      yamt 		    RETURN_ADDRESS, 0);
    189      1.105        ad 		splx(s);
    190      1.105        ad 		return;
    191      1.105        ad 	}
    192      1.105        ad 
    193      1.132        ad 	/*
    194      1.132        ad 	 * To remove the ordering constraint between adaptive mutexes
    195      1.132        ad 	 * and kernel_lock we must make it appear as if this thread is
    196      1.132        ad 	 * blocking.  For non-interlocked mutex release, a store fence
    197      1.132        ad 	 * is required to ensure that the result of any mutex_exit()
    198      1.132        ad 	 * by the current LWP becomes visible on the bus before the set
    199      1.132        ad 	 * of ci->ci_biglock_wanted becomes visible.
    200      1.132        ad 	 */
    201      1.132        ad 	membar_producer();
    202      1.132        ad 	owant = ci->ci_biglock_wanted;
    203      1.132        ad 	ci->ci_biglock_wanted = l;
    204      1.105        ad 
    205      1.105        ad 	/*
    206      1.132        ad 	 * Spin until we acquire the lock.  Once we have it, record the
    207      1.132        ad 	 * time spent with lockstat.
    208      1.105        ad 	 */
    209      1.132        ad 	LOCKSTAT_ENTER(lsflag);
    210      1.132        ad 	LOCKSTAT_START_TIMER(lsflag, spintime);
    211      1.105        ad 
    212      1.105        ad 	spins = 0;
    213      1.105        ad 	do {
    214      1.122        ad 		splx(s);
    215      1.132        ad 		while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
    216      1.132        ad 			if (SPINLOCK_SPINOUT(spins)) {
    217      1.136        ad 				extern volatile int start_init_exec;
    218      1.136        ad 				if (!start_init_exec)
    219      1.136        ad 					_KERNEL_LOCK_ABORT("spinout");
    220      1.132        ad 			}
    221      1.122        ad 			SPINLOCK_BACKOFF_HOOK;
    222      1.105        ad 			SPINLOCK_SPIN_HOOK;
    223      1.105        ad 		}
    224      1.132        ad 		s = splvm();
    225      1.132        ad 	} while (!__cpu_simple_lock_try(kernel_lock));
    226      1.105        ad 
    227      1.122        ad 	ci->ci_biglock_count = nlocks;
    228      1.122        ad 	l->l_blcnt = nlocks;
    229      1.107        ad 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    230      1.132        ad 	LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS, 0);
    231      1.132        ad 	if (owant == NULL) {
    232      1.132        ad 		LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
    233      1.132        ad 		    LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
    234      1.132        ad 	}
    235      1.132        ad 	LOCKSTAT_EXIT(lsflag);
    236       1.85      yamt 	splx(s);
    237      1.105        ad 
    238      1.105        ad 	/*
    239      1.132        ad 	 * Now that we have kernel_lock, reset ci_biglock_wanted.  This
    240      1.132        ad 	 * store must be unbuffered (immediately visible on the bus) in
    241      1.132        ad 	 * order for non-interlocked mutex release to work correctly.
    242      1.132        ad 	 * It must be visible before a mutex_exit() can execute on this
    243      1.132        ad 	 * processor.
    244      1.132        ad 	 *
    245      1.132        ad 	 * Note: only where CAS is available in hardware will this be
    246      1.132        ad 	 * an unbuffered write, but non-interlocked release cannot be
    247      1.132        ad 	 * done on CPUs without CAS in hardware.
    248      1.105        ad 	 */
    249      1.132        ad 	(void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
    250      1.132        ad 
    251      1.132        ad 	/*
    252      1.132        ad 	 * Issue a memory barrier as we have acquired a lock.  This also
    253      1.132        ad 	 * prevents stores from a following mutex_exit() being reordered
    254      1.132        ad 	 * to occur before our store to ci_biglock_wanted above.
    255      1.132        ad 	 */
    256      1.132        ad 	membar_enter();
    257       1.62   thorpej }
    258       1.62   thorpej 
    259       1.62   thorpej /*
    260      1.105        ad  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
    261      1.105        ad  * all holds.  If 'l' is non-null, the release is from process context.
    262       1.62   thorpej  */
    263       1.62   thorpej void
    264      1.137  drochner _kernel_unlock(int nlocks, int *countp)
    265       1.62   thorpej {
    266  1.137.4.1      yamt 	struct cpu_info *ci;
    267      1.105        ad 	u_int olocks;
    268      1.105        ad 	int s;
    269      1.137  drochner 	struct lwp *l = curlwp;
    270       1.62   thorpej 
    271      1.105        ad 	_KERNEL_LOCK_ASSERT(nlocks < 2);
    272       1.62   thorpej 
    273      1.122        ad 	olocks = l->l_blcnt;
    274       1.77      yamt 
    275      1.105        ad 	if (olocks == 0) {
    276      1.105        ad 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
    277      1.105        ad 		if (countp != NULL)
    278      1.105        ad 			*countp = 0;
    279      1.105        ad 		return;
    280      1.105        ad 	}
    281       1.77      yamt 
    282      1.132        ad 	_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    283       1.85      yamt 
    284      1.105        ad 	if (nlocks == 0)
    285      1.105        ad 		nlocks = olocks;
    286      1.105        ad 	else if (nlocks == -1) {
    287      1.105        ad 		nlocks = 1;
    288      1.105        ad 		_KERNEL_LOCK_ASSERT(olocks == 1);
    289      1.105        ad 	}
    290  1.137.4.1      yamt 	s = splvm();
    291  1.137.4.1      yamt 	ci = curcpu();
    292      1.122        ad 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
    293      1.122        ad 	if (ci->ci_biglock_count == nlocks) {
    294      1.132        ad 		LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
    295      1.127      yamt 		    RETURN_ADDRESS, 0);
    296      1.122        ad 		ci->ci_biglock_count = 0;
    297      1.132        ad 		__cpu_simple_unlock(kernel_lock);
    298  1.137.4.1      yamt 		l->l_blcnt -= nlocks;
    299      1.122        ad 		splx(s);
    300  1.137.4.1      yamt 		if (l->l_dopreempt)
    301  1.137.4.1      yamt 			kpreempt(0);
    302  1.137.4.1      yamt 	} else {
    303      1.122        ad 		ci->ci_biglock_count -= nlocks;
    304  1.137.4.1      yamt 		l->l_blcnt -= nlocks;
    305  1.137.4.1      yamt 		splx(s);
    306  1.137.4.1      yamt 	}
    307       1.77      yamt 
    308      1.105        ad 	if (countp != NULL)
    309      1.105        ad 		*countp = olocks;
    310       1.77      yamt }
    311      1.124     pooka #endif /* !_RUMPKERNEL */
    312