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