Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.157.4.1
      1  1.157.4.1    bouyer /*	$NetBSD: kern_lock.c,v 1.157.4.1 2017/04/21 16:54:02 bouyer Exp $	*/
      2       1.19   thorpej 
      3       1.19   thorpej /*-
      4      1.148        ad  * Copyright (c) 2002, 2006, 2007, 2008, 2009 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.157.4.1    bouyer __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.157.4.1 2017/04/21 16:54:02 bouyer Exp $");
     35      1.105        ad 
     36        1.1      fvdl #include <sys/param.h>
     37        1.1      fvdl #include <sys/proc.h>
     38        1.1      fvdl #include <sys/lock.h>
     39        1.2      fvdl #include <sys/systm.h>
     40      1.125        ad #include <sys/kernel.h>
     41      1.105        ad #include <sys/lockdebug.h>
     42      1.122        ad #include <sys/cpu.h>
     43      1.122        ad #include <sys/syslog.h>
     44      1.128        ad #include <sys/atomic.h>
     45      1.148        ad #include <sys/lwp.h>
     46      1.105        ad 
     47      1.131        ad #include <machine/lock.h>
     48        1.1      fvdl 
     49       1.98        ad #include <dev/lockstat.h>
     50       1.98        ad 
     51      1.134        ad #define	RETURN_ADDRESS	(uintptr_t)__builtin_return_address(0)
     52       1.25   thorpej 
     53      1.127      yamt bool	kernel_lock_dodebug;
     54      1.132        ad 
     55      1.132        ad __cpu_simple_lock_t kernel_lock[CACHE_LINE_SIZE / sizeof(__cpu_simple_lock_t)]
     56      1.153      matt     __cacheline_aligned;
     57        1.1      fvdl 
     58       1.96      yamt void
     59      1.135      yamt assert_sleepable(void)
     60       1.96      yamt {
     61      1.135      yamt 	const char *reason;
     62      1.148        ad 	uint64_t pctr;
     63      1.148        ad 	bool idle;
     64       1.96      yamt 
     65      1.135      yamt 	if (panicstr != NULL) {
     66      1.117        ad 		return;
     67      1.135      yamt 	}
     68      1.135      yamt 
     69      1.132        ad 	LOCKDEBUG_BARRIER(kernel_lock, 1);
     70      1.135      yamt 
     71      1.148        ad 	/*
     72      1.148        ad 	 * Avoid disabling/re-enabling preemption here since this
     73      1.149    dyoung 	 * routine may be called in delicate situations.
     74      1.148        ad 	 */
     75      1.148        ad 	do {
     76      1.148        ad 		pctr = lwp_pctr();
     77      1.148        ad 		idle = CURCPU_IDLE_P();
     78      1.148        ad 	} while (pctr != lwp_pctr());
     79      1.148        ad 
     80      1.135      yamt 	reason = NULL;
     81      1.156     skrll 	if (idle && !cold &&
     82      1.156     skrll 	    kcpuset_isset(kcpuset_running, cpu_index(curcpu()))) {
     83      1.135      yamt 		reason = "idle";
     84      1.135      yamt 	}
     85      1.135      yamt 	if (cpu_intr_p()) {
     86      1.135      yamt 		reason = "interrupt";
     87       1.97      yamt 	}
     88      1.148        ad 	if (cpu_softintr_p()) {
     89      1.135      yamt 		reason = "softint";
     90      1.135      yamt 	}
     91      1.135      yamt 
     92      1.135      yamt 	if (reason) {
     93      1.135      yamt 		panic("%s: %s caller=%p", __func__, reason,
     94      1.135      yamt 		    (void *)RETURN_ADDRESS);
     95      1.135      yamt 	}
     96       1.96      yamt }
     97      1.105        ad 
     98       1.62   thorpej /*
     99       1.62   thorpej  * Functions for manipulating the kernel_lock.  We put them here
    100       1.62   thorpej  * so that they show up in profiles.
    101       1.62   thorpej  */
    102       1.62   thorpej 
    103      1.105        ad #define	_KERNEL_LOCK_ABORT(msg)						\
    104  1.157.4.1    bouyer     LOCKDEBUG_ABORT(__func__, __LINE__, kernel_lock, &_kernel_lock_ops, msg)
    105      1.105        ad 
    106      1.105        ad #ifdef LOCKDEBUG
    107      1.105        ad #define	_KERNEL_LOCK_ASSERT(cond)					\
    108      1.105        ad do {									\
    109      1.105        ad 	if (!(cond))							\
    110      1.105        ad 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
    111      1.105        ad } while (/* CONSTCOND */ 0)
    112      1.105        ad #else
    113      1.105        ad #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
    114      1.105        ad #endif
    115      1.105        ad 
    116      1.105        ad void	_kernel_lock_dump(volatile void *);
    117      1.105        ad 
    118      1.105        ad lockops_t _kernel_lock_ops = {
    119      1.105        ad 	"Kernel lock",
    120      1.144        ad 	LOCKOPS_SPIN,
    121      1.105        ad 	_kernel_lock_dump
    122      1.105        ad };
    123      1.105        ad 
    124       1.85      yamt /*
    125      1.105        ad  * Initialize the kernel lock.
    126       1.85      yamt  */
    127       1.62   thorpej void
    128      1.122        ad kernel_lock_init(void)
    129       1.62   thorpej {
    130       1.62   thorpej 
    131      1.132        ad 	__cpu_simple_lock_init(kernel_lock);
    132      1.132        ad 	kernel_lock_dodebug = LOCKDEBUG_ALLOC(kernel_lock, &_kernel_lock_ops,
    133      1.122        ad 	    RETURN_ADDRESS);
    134       1.62   thorpej }
    135      1.155    martin CTASSERT(CACHE_LINE_SIZE >= sizeof(__cpu_simple_lock_t));
    136       1.62   thorpej 
    137       1.62   thorpej /*
    138      1.105        ad  * Print debugging information about the kernel lock.
    139       1.62   thorpej  */
    140       1.62   thorpej void
    141      1.105        ad _kernel_lock_dump(volatile void *junk)
    142       1.62   thorpej {
    143       1.85      yamt 	struct cpu_info *ci = curcpu();
    144       1.62   thorpej 
    145      1.105        ad 	(void)junk;
    146       1.85      yamt 
    147      1.105        ad 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
    148      1.105        ad 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
    149       1.62   thorpej }
    150       1.62   thorpej 
    151      1.105        ad /*
    152      1.150       mrg  * Acquire 'nlocks' holds on the kernel lock.
    153      1.105        ad  */
    154       1.62   thorpej void
    155      1.137  drochner _kernel_lock(int nlocks)
    156       1.62   thorpej {
    157      1.138        ad 	struct cpu_info *ci;
    158      1.105        ad 	LOCKSTAT_TIMER(spintime);
    159      1.105        ad 	LOCKSTAT_FLAG(lsflag);
    160      1.105        ad 	struct lwp *owant;
    161      1.105        ad 	u_int spins;
    162       1.85      yamt 	int s;
    163      1.137  drochner 	struct lwp *l = curlwp;
    164       1.85      yamt 
    165      1.105        ad 	_KERNEL_LOCK_ASSERT(nlocks > 0);
    166       1.62   thorpej 
    167      1.138        ad 	s = splvm();
    168      1.138        ad 	ci = curcpu();
    169      1.105        ad 	if (ci->ci_biglock_count != 0) {
    170      1.132        ad 		_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    171      1.105        ad 		ci->ci_biglock_count += nlocks;
    172      1.122        ad 		l->l_blcnt += nlocks;
    173      1.138        ad 		splx(s);
    174      1.105        ad 		return;
    175      1.105        ad 	}
    176      1.105        ad 
    177      1.122        ad 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
    178      1.132        ad 	LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
    179      1.154   mlelstv 	    0);
    180      1.107        ad 
    181      1.132        ad 	if (__cpu_simple_lock_try(kernel_lock)) {
    182      1.105        ad 		ci->ci_biglock_count = nlocks;
    183      1.122        ad 		l->l_blcnt = nlocks;
    184      1.144        ad 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
    185      1.127      yamt 		    RETURN_ADDRESS, 0);
    186      1.105        ad 		splx(s);
    187      1.105        ad 		return;
    188      1.105        ad 	}
    189      1.105        ad 
    190      1.132        ad 	/*
    191      1.132        ad 	 * To remove the ordering constraint between adaptive mutexes
    192      1.132        ad 	 * and kernel_lock we must make it appear as if this thread is
    193      1.132        ad 	 * blocking.  For non-interlocked mutex release, a store fence
    194      1.132        ad 	 * is required to ensure that the result of any mutex_exit()
    195      1.132        ad 	 * by the current LWP becomes visible on the bus before the set
    196      1.132        ad 	 * of ci->ci_biglock_wanted becomes visible.
    197      1.132        ad 	 */
    198      1.132        ad 	membar_producer();
    199      1.132        ad 	owant = ci->ci_biglock_wanted;
    200      1.132        ad 	ci->ci_biglock_wanted = l;
    201      1.105        ad 
    202      1.105        ad 	/*
    203      1.132        ad 	 * Spin until we acquire the lock.  Once we have it, record the
    204      1.132        ad 	 * time spent with lockstat.
    205      1.105        ad 	 */
    206      1.132        ad 	LOCKSTAT_ENTER(lsflag);
    207      1.132        ad 	LOCKSTAT_START_TIMER(lsflag, spintime);
    208      1.105        ad 
    209      1.105        ad 	spins = 0;
    210      1.105        ad 	do {
    211      1.122        ad 		splx(s);
    212      1.132        ad 		while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
    213      1.132        ad 			if (SPINLOCK_SPINOUT(spins)) {
    214      1.143        ad 				extern int start_init_exec;
    215      1.136        ad 				if (!start_init_exec)
    216      1.136        ad 					_KERNEL_LOCK_ABORT("spinout");
    217      1.132        ad 			}
    218      1.122        ad 			SPINLOCK_BACKOFF_HOOK;
    219      1.105        ad 			SPINLOCK_SPIN_HOOK;
    220      1.105        ad 		}
    221      1.132        ad 		s = splvm();
    222      1.132        ad 	} while (!__cpu_simple_lock_try(kernel_lock));
    223      1.105        ad 
    224      1.122        ad 	ci->ci_biglock_count = nlocks;
    225      1.122        ad 	l->l_blcnt = nlocks;
    226      1.107        ad 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    227      1.144        ad 	LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
    228      1.144        ad 	    RETURN_ADDRESS, 0);
    229      1.132        ad 	if (owant == NULL) {
    230      1.132        ad 		LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
    231      1.132        ad 		    LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
    232      1.132        ad 	}
    233      1.132        ad 	LOCKSTAT_EXIT(lsflag);
    234       1.85      yamt 	splx(s);
    235      1.105        ad 
    236      1.105        ad 	/*
    237      1.132        ad 	 * Now that we have kernel_lock, reset ci_biglock_wanted.  This
    238      1.132        ad 	 * store must be unbuffered (immediately visible on the bus) in
    239      1.157     skrll 	 * order for non-interlocked mutex release to work correctly.
    240      1.132        ad 	 * It must be visible before a mutex_exit() can execute on this
    241      1.132        ad 	 * processor.
    242      1.132        ad 	 *
    243      1.132        ad 	 * Note: only where CAS is available in hardware will this be
    244      1.132        ad 	 * an unbuffered write, but non-interlocked release cannot be
    245      1.132        ad 	 * done on CPUs without CAS in hardware.
    246      1.105        ad 	 */
    247      1.132        ad 	(void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
    248      1.132        ad 
    249      1.132        ad 	/*
    250      1.132        ad 	 * Issue a memory barrier as we have acquired a lock.  This also
    251      1.132        ad 	 * prevents stores from a following mutex_exit() being reordered
    252      1.132        ad 	 * to occur before our store to ci_biglock_wanted above.
    253      1.132        ad 	 */
    254      1.132        ad 	membar_enter();
    255       1.62   thorpej }
    256       1.62   thorpej 
    257       1.62   thorpej /*
    258      1.105        ad  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
    259      1.150       mrg  * all holds.
    260       1.62   thorpej  */
    261       1.62   thorpej void
    262      1.137  drochner _kernel_unlock(int nlocks, int *countp)
    263       1.62   thorpej {
    264      1.138        ad 	struct cpu_info *ci;
    265      1.105        ad 	u_int olocks;
    266      1.105        ad 	int s;
    267      1.137  drochner 	struct lwp *l = curlwp;
    268       1.62   thorpej 
    269      1.105        ad 	_KERNEL_LOCK_ASSERT(nlocks < 2);
    270       1.62   thorpej 
    271      1.122        ad 	olocks = l->l_blcnt;
    272       1.77      yamt 
    273      1.105        ad 	if (olocks == 0) {
    274      1.105        ad 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
    275      1.105        ad 		if (countp != NULL)
    276      1.105        ad 			*countp = 0;
    277      1.105        ad 		return;
    278      1.105        ad 	}
    279       1.77      yamt 
    280      1.132        ad 	_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    281       1.85      yamt 
    282      1.105        ad 	if (nlocks == 0)
    283      1.105        ad 		nlocks = olocks;
    284      1.105        ad 	else if (nlocks == -1) {
    285      1.105        ad 		nlocks = 1;
    286      1.105        ad 		_KERNEL_LOCK_ASSERT(olocks == 1);
    287      1.105        ad 	}
    288      1.138        ad 	s = splvm();
    289      1.138        ad 	ci = curcpu();
    290      1.122        ad 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
    291      1.122        ad 	if (ci->ci_biglock_count == nlocks) {
    292      1.132        ad 		LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
    293      1.127      yamt 		    RETURN_ADDRESS, 0);
    294      1.122        ad 		ci->ci_biglock_count = 0;
    295      1.132        ad 		__cpu_simple_unlock(kernel_lock);
    296      1.138        ad 		l->l_blcnt -= nlocks;
    297      1.122        ad 		splx(s);
    298      1.139        ad 		if (l->l_dopreempt)
    299      1.139        ad 			kpreempt(0);
    300      1.138        ad 	} else {
    301      1.122        ad 		ci->ci_biglock_count -= nlocks;
    302      1.138        ad 		l->l_blcnt -= nlocks;
    303      1.138        ad 		splx(s);
    304      1.138        ad 	}
    305       1.77      yamt 
    306      1.105        ad 	if (countp != NULL)
    307      1.105        ad 		*countp = olocks;
    308       1.77      yamt }
    309      1.152  jmcneill 
    310      1.152  jmcneill bool
    311      1.152  jmcneill _kernel_locked_p(void)
    312      1.152  jmcneill {
    313      1.152  jmcneill 	return __SIMPLELOCK_LOCKED_P(kernel_lock);
    314      1.152  jmcneill }
    315