Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.88.2.9
      1  1.88.2.9     yamt /*	$NetBSD: kern_lock.c,v 1.88.2.9 2008/02/04 09:24:12 yamt Exp $	*/
      2      1.19  thorpej 
      3      1.19  thorpej /*-
      4  1.88.2.9     yamt  * 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.88.2.3     yamt  * 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.88.2.9     yamt __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.88.2.9 2008/02/04 09:24:12 yamt Exp $");
     42       1.7  thorpej 
     43      1.21  thorpej #include "opt_multiprocessor.h"
     44  1.88.2.3     yamt 
     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.88.2.6     yamt #include <sys/kernel.h>
     50  1.88.2.3     yamt #include <sys/lockdebug.h>
     51  1.88.2.5     yamt #include <sys/cpu.h>
     52  1.88.2.5     yamt #include <sys/syslog.h>
     53  1.88.2.7     yamt #include <sys/atomic.h>
     54  1.88.2.3     yamt 
     55  1.88.2.4     yamt #include <machine/stdarg.h>
     56  1.88.2.8     yamt #include <machine/lock.h>
     57       1.1     fvdl 
     58  1.88.2.2     yamt #include <dev/lockstat.h>
     59  1.88.2.2     yamt 
     60  1.88.2.9     yamt #define	RETURN_ADDRESS	(uintptr_t)__builtin_return_address(0)
     61      1.73     yamt 
     62  1.88.2.7     yamt bool	kernel_lock_dodebug;
     63  1.88.2.8     yamt 
     64  1.88.2.8     yamt __cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
     65  1.88.2.8     yamt     __aligned(CACHE_LINE_SIZE);
     66  1.88.2.8     yamt 
     67  1.88.2.5     yamt #if defined(LOCKDEBUG)
     68  1.88.2.2     yamt void
     69  1.88.2.2     yamt assert_sleepable(struct simplelock *interlock, const char *msg)
     70  1.88.2.2     yamt {
     71  1.88.2.2     yamt 
     72  1.88.2.4     yamt 	if (panicstr != NULL)
     73  1.88.2.4     yamt 		return;
     74  1.88.2.8     yamt 	LOCKDEBUG_BARRIER(kernel_lock, 1);
     75  1.88.2.6     yamt 	if (CURCPU_IDLE_P() && !cold) {
     76  1.88.2.4     yamt 		panic("assert_sleepable: idle");
     77  1.88.2.2     yamt 	}
     78  1.88.2.2     yamt }
     79  1.88.2.5     yamt #endif
     80  1.88.2.3     yamt 
     81      1.62  thorpej /*
     82  1.88.2.6     yamt  * rump doesn't need the kernel lock so force it out.  We cannot
     83  1.88.2.6     yamt  * currently easily include it for compilation because of
     84  1.88.2.7     yamt  * a) SPINLOCK_* b) membar_producer().  They are defined in different
     85  1.88.2.6     yamt  * places / way for each arch, so just simply do not bother to
     86  1.88.2.6     yamt  * fight a lot for no gain (i.e. pain but still no gain).
     87  1.88.2.6     yamt  */
     88  1.88.2.6     yamt #ifndef _RUMPKERNEL
     89  1.88.2.6     yamt /*
     90      1.62  thorpej  * Functions for manipulating the kernel_lock.  We put them here
     91      1.62  thorpej  * so that they show up in profiles.
     92      1.62  thorpej  */
     93      1.62  thorpej 
     94  1.88.2.3     yamt #define	_KERNEL_LOCK_ABORT(msg)						\
     95  1.88.2.8     yamt     LOCKDEBUG_ABORT(kernel_lock, &_kernel_lock_ops, __func__, msg)
     96  1.88.2.3     yamt 
     97  1.88.2.3     yamt #ifdef LOCKDEBUG
     98  1.88.2.3     yamt #define	_KERNEL_LOCK_ASSERT(cond)					\
     99  1.88.2.3     yamt do {									\
    100  1.88.2.3     yamt 	if (!(cond))							\
    101  1.88.2.3     yamt 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
    102  1.88.2.3     yamt } while (/* CONSTCOND */ 0)
    103  1.88.2.3     yamt #else
    104  1.88.2.3     yamt #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
    105      1.85     yamt #endif
    106      1.62  thorpej 
    107  1.88.2.3     yamt void	_kernel_lock_dump(volatile void *);
    108  1.88.2.3     yamt 
    109  1.88.2.3     yamt lockops_t _kernel_lock_ops = {
    110  1.88.2.3     yamt 	"Kernel lock",
    111  1.88.2.3     yamt 	0,
    112  1.88.2.3     yamt 	_kernel_lock_dump
    113  1.88.2.3     yamt };
    114  1.88.2.3     yamt 
    115  1.88.2.3     yamt /*
    116  1.88.2.3     yamt  * Initialize the kernel lock.
    117  1.88.2.3     yamt  */
    118      1.62  thorpej void
    119  1.88.2.5     yamt kernel_lock_init(void)
    120      1.62  thorpej {
    121      1.62  thorpej 
    122  1.88.2.8     yamt 	KASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
    123  1.88.2.8     yamt 	__cpu_simple_lock_init(kernel_lock);
    124  1.88.2.8     yamt 	kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
    125  1.88.2.5     yamt 	    RETURN_ADDRESS);
    126      1.62  thorpej }
    127      1.62  thorpej 
    128      1.62  thorpej /*
    129  1.88.2.3     yamt  * Print debugging information about the kernel lock.
    130      1.62  thorpej  */
    131      1.62  thorpej void
    132  1.88.2.3     yamt _kernel_lock_dump(volatile void *junk)
    133      1.62  thorpej {
    134      1.85     yamt 	struct cpu_info *ci = curcpu();
    135      1.62  thorpej 
    136  1.88.2.3     yamt 	(void)junk;
    137      1.85     yamt 
    138  1.88.2.3     yamt 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
    139  1.88.2.3     yamt 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
    140      1.62  thorpej }
    141      1.62  thorpej 
    142  1.88.2.3     yamt /*
    143  1.88.2.3     yamt  * Acquire 'nlocks' holds on the kernel lock.  If 'l' is non-null, the
    144  1.88.2.3     yamt  * acquisition is from process context.
    145  1.88.2.3     yamt  */
    146      1.62  thorpej void
    147  1.88.2.3     yamt _kernel_lock(int nlocks, struct lwp *l)
    148      1.62  thorpej {
    149      1.85     yamt 	struct cpu_info *ci = curcpu();
    150  1.88.2.3     yamt 	LOCKSTAT_TIMER(spintime);
    151  1.88.2.3     yamt 	LOCKSTAT_FLAG(lsflag);
    152  1.88.2.3     yamt 	struct lwp *owant;
    153  1.88.2.3     yamt 	u_int spins;
    154      1.85     yamt 	int s;
    155      1.85     yamt 
    156  1.88.2.3     yamt 	if (nlocks == 0)
    157  1.88.2.3     yamt 		return;
    158  1.88.2.3     yamt 	_KERNEL_LOCK_ASSERT(nlocks > 0);
    159      1.62  thorpej 
    160  1.88.2.5     yamt 	l = curlwp;
    161      1.62  thorpej 
    162  1.88.2.3     yamt 	if (ci->ci_biglock_count != 0) {
    163  1.88.2.8     yamt 		_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    164  1.88.2.3     yamt 		ci->ci_biglock_count += nlocks;
    165  1.88.2.5     yamt 		l->l_blcnt += nlocks;
    166  1.88.2.3     yamt 		return;
    167  1.88.2.3     yamt 	}
    168      1.62  thorpej 
    169  1.88.2.5     yamt 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
    170  1.88.2.8     yamt 	LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
    171  1.88.2.7     yamt 	    0);
    172      1.62  thorpej 
    173  1.88.2.5     yamt 	s = splvm();
    174  1.88.2.8     yamt 	if (__cpu_simple_lock_try(kernel_lock)) {
    175  1.88.2.3     yamt 		ci->ci_biglock_count = nlocks;
    176  1.88.2.5     yamt 		l->l_blcnt = nlocks;
    177  1.88.2.8     yamt 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock,
    178  1.88.2.7     yamt 		    RETURN_ADDRESS, 0);
    179  1.88.2.3     yamt 		splx(s);
    180  1.88.2.3     yamt 		return;
    181  1.88.2.3     yamt 	}
    182      1.62  thorpej 
    183  1.88.2.3     yamt 	/*
    184  1.88.2.8     yamt 	 * To remove the ordering constraint between adaptive mutexes
    185  1.88.2.8     yamt 	 * and kernel_lock we must make it appear as if this thread is
    186  1.88.2.8     yamt 	 * blocking.  For non-interlocked mutex release, a store fence
    187  1.88.2.8     yamt 	 * is required to ensure that the result of any mutex_exit()
    188  1.88.2.8     yamt 	 * by the current LWP becomes visible on the bus before the set
    189  1.88.2.8     yamt 	 * of ci->ci_biglock_wanted becomes visible.
    190  1.88.2.3     yamt 	 */
    191  1.88.2.8     yamt 	membar_producer();
    192  1.88.2.3     yamt 	owant = ci->ci_biglock_wanted;
    193  1.88.2.8     yamt 	ci->ci_biglock_wanted = l;
    194      1.77     yamt 
    195  1.88.2.8     yamt 	/*
    196  1.88.2.8     yamt 	 * Spin until we acquire the lock.  Once we have it, record the
    197  1.88.2.8     yamt 	 * time spent with lockstat.
    198  1.88.2.8     yamt 	 */
    199  1.88.2.8     yamt 	LOCKSTAT_ENTER(lsflag);
    200  1.88.2.8     yamt 	LOCKSTAT_START_TIMER(lsflag, spintime);
    201      1.85     yamt 
    202  1.88.2.8     yamt 	spins = 0;
    203  1.88.2.3     yamt 	do {
    204  1.88.2.5     yamt 		splx(s);
    205  1.88.2.8     yamt 		while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
    206  1.88.2.8     yamt 			if (SPINLOCK_SPINOUT(spins)) {
    207  1.88.2.3     yamt 				_KERNEL_LOCK_ABORT("spinout");
    208  1.88.2.8     yamt 			}
    209  1.88.2.5     yamt 			SPINLOCK_BACKOFF_HOOK;
    210  1.88.2.3     yamt 			SPINLOCK_SPIN_HOOK;
    211  1.88.2.3     yamt 		}
    212  1.88.2.8     yamt 		s = splvm();
    213  1.88.2.8     yamt 	} while (!__cpu_simple_lock_try(kernel_lock));
    214      1.85     yamt 
    215  1.88.2.5     yamt 	ci->ci_biglock_count = nlocks;
    216  1.88.2.5     yamt 	l->l_blcnt = nlocks;
    217  1.88.2.3     yamt 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    218  1.88.2.8     yamt 	LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS, 0);
    219  1.88.2.8     yamt 	if (owant == NULL) {
    220  1.88.2.8     yamt 		LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
    221  1.88.2.8     yamt 		    LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
    222  1.88.2.8     yamt 	}
    223  1.88.2.8     yamt 	LOCKSTAT_EXIT(lsflag);
    224  1.88.2.3     yamt 	splx(s);
    225      1.77     yamt 
    226  1.88.2.3     yamt 	/*
    227  1.88.2.8     yamt 	 * Now that we have kernel_lock, reset ci_biglock_wanted.  This
    228  1.88.2.8     yamt 	 * store must be unbuffered (immediately visible on the bus) in
    229  1.88.2.8     yamt 	 * order for non-interlocked mutex release to work correctly.
    230  1.88.2.8     yamt 	 * It must be visible before a mutex_exit() can execute on this
    231  1.88.2.8     yamt 	 * processor.
    232  1.88.2.8     yamt 	 *
    233  1.88.2.8     yamt 	 * Note: only where CAS is available in hardware will this be
    234  1.88.2.8     yamt 	 * an unbuffered write, but non-interlocked release cannot be
    235  1.88.2.8     yamt 	 * done on CPUs without CAS in hardware.
    236  1.88.2.3     yamt 	 */
    237  1.88.2.8     yamt 	(void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
    238  1.88.2.8     yamt 
    239  1.88.2.8     yamt 	/*
    240  1.88.2.8     yamt 	 * Issue a memory barrier as we have acquired a lock.  This also
    241  1.88.2.8     yamt 	 * prevents stores from a following mutex_exit() being reordered
    242  1.88.2.8     yamt 	 * to occur before our store to ci_biglock_wanted above.
    243  1.88.2.8     yamt 	 */
    244  1.88.2.8     yamt 	membar_enter();
    245      1.77     yamt }
    246      1.77     yamt 
    247  1.88.2.3     yamt /*
    248  1.88.2.3     yamt  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
    249  1.88.2.3     yamt  * all holds.  If 'l' is non-null, the release is from process context.
    250  1.88.2.3     yamt  */
    251      1.77     yamt void
    252  1.88.2.3     yamt _kernel_unlock(int nlocks, struct lwp *l, int *countp)
    253      1.77     yamt {
    254  1.88.2.3     yamt 	struct cpu_info *ci = curcpu();
    255  1.88.2.3     yamt 	u_int olocks;
    256  1.88.2.3     yamt 	int s;
    257      1.77     yamt 
    258  1.88.2.5     yamt 	l = curlwp;
    259      1.85     yamt 
    260  1.88.2.3     yamt 	_KERNEL_LOCK_ASSERT(nlocks < 2);
    261      1.85     yamt 
    262  1.88.2.5     yamt 	olocks = l->l_blcnt;
    263  1.88.2.3     yamt 
    264  1.88.2.3     yamt 	if (olocks == 0) {
    265  1.88.2.3     yamt 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
    266  1.88.2.3     yamt 		if (countp != NULL)
    267  1.88.2.3     yamt 			*countp = 0;
    268  1.88.2.3     yamt 		return;
    269  1.88.2.3     yamt 	}
    270  1.88.2.3     yamt 
    271  1.88.2.8     yamt 	_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    272  1.88.2.3     yamt 
    273  1.88.2.3     yamt 	if (nlocks == 0)
    274  1.88.2.3     yamt 		nlocks = olocks;
    275  1.88.2.3     yamt 	else if (nlocks == -1) {
    276  1.88.2.3     yamt 		nlocks = 1;
    277  1.88.2.3     yamt 		_KERNEL_LOCK_ASSERT(olocks == 1);
    278  1.88.2.3     yamt 	}
    279  1.88.2.3     yamt 
    280  1.88.2.5     yamt 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
    281  1.88.2.5     yamt 
    282  1.88.2.5     yamt 	l->l_blcnt -= nlocks;
    283  1.88.2.5     yamt 	if (ci->ci_biglock_count == nlocks) {
    284  1.88.2.5     yamt 		s = splvm();
    285  1.88.2.8     yamt 		LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
    286  1.88.2.7     yamt 		    RETURN_ADDRESS, 0);
    287  1.88.2.5     yamt 		ci->ci_biglock_count = 0;
    288  1.88.2.8     yamt 		__cpu_simple_unlock(kernel_lock);
    289  1.88.2.5     yamt 		splx(s);
    290  1.88.2.5     yamt 	} else
    291  1.88.2.5     yamt 		ci->ci_biglock_count -= nlocks;
    292  1.88.2.3     yamt 
    293  1.88.2.3     yamt 	if (countp != NULL)
    294  1.88.2.3     yamt 		*countp = olocks;
    295      1.77     yamt }
    296  1.88.2.6     yamt #endif /* !_RUMPKERNEL */
    297