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