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