Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.165
      1 /*	$NetBSD: kern_lock.c,v 1.165 2020/01/17 20:26:22 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.165 2020/01/17 20:26:22 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 void
    161 _kernel_lock(int nlocks)
    162 {
    163 	struct cpu_info *ci;
    164 	LOCKSTAT_TIMER(spintime);
    165 	LOCKSTAT_FLAG(lsflag);
    166 	struct lwp *owant;
    167 	u_int count;
    168 #ifdef LOCKDEBUG
    169 	u_int spins = 0;
    170 #endif
    171 	int s;
    172 	struct lwp *l = curlwp;
    173 
    174 	_KERNEL_LOCK_ASSERT(nlocks > 0);
    175 
    176 	s = splvm();
    177 	ci = curcpu();
    178 	if (ci->ci_biglock_count != 0) {
    179 		_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    180 		ci->ci_biglock_count += nlocks;
    181 		l->l_blcnt += nlocks;
    182 		splx(s);
    183 		return;
    184 	}
    185 
    186 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
    187 	LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, kernel_lock, RETURN_ADDRESS,
    188 	    0);
    189 
    190 	if (__predict_true(__cpu_simple_lock_try(kernel_lock))) {
    191 		ci->ci_biglock_count = nlocks;
    192 		l->l_blcnt = nlocks;
    193 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
    194 		    RETURN_ADDRESS, 0);
    195 		splx(s);
    196 		return;
    197 	}
    198 
    199 	/*
    200 	 * To remove the ordering constraint between adaptive mutexes
    201 	 * and kernel_lock we must make it appear as if this thread is
    202 	 * blocking.  For non-interlocked mutex release, a store fence
    203 	 * is required to ensure that the result of any mutex_exit()
    204 	 * by the current LWP becomes visible on the bus before the set
    205 	 * of ci->ci_biglock_wanted becomes visible.
    206 	 *
    207 	 * However, we won't set ci_biglock_wanted until we've spun for
    208 	 * a bit, as we don't want to make any lock waiters in rw_oncpu()
    209 	 * or mutex_oncpu() block prematurely.
    210 	 */
    211 	membar_producer();
    212 	owant = ci->ci_biglock_wanted;
    213 
    214 	/*
    215 	 * Spin until we acquire the lock.  Once we have it, record the
    216 	 * time spent with lockstat.
    217 	 */
    218 	LOCKSTAT_ENTER(lsflag);
    219 	LOCKSTAT_START_TIMER(lsflag, spintime);
    220 
    221 	count = SPINLOCK_BACKOFF_MIN;
    222 	do {
    223 		splx(s);
    224 		while (__SIMPLELOCK_LOCKED_P(kernel_lock)) {
    225 #ifdef LOCKDEBUG
    226 			if (SPINLOCK_SPINOUT(spins)) {
    227 				extern int start_init_exec;
    228 				if (!start_init_exec)
    229 					_KERNEL_LOCK_ABORT("spinout");
    230 			}
    231 #endif
    232 			SPINLOCK_BACKOFF(count);
    233 			if (count == SPINLOCK_BACKOFF_MAX) {
    234 				/* Ok, waiting for real. */
    235 				ci->ci_biglock_wanted = l;
    236 			}
    237 		}
    238 		s = splvm();
    239 	} while (!__cpu_simple_lock_try(kernel_lock));
    240 
    241 	ci->ci_biglock_count = nlocks;
    242 	l->l_blcnt = nlocks;
    243 	splx(s);
    244 
    245 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    246 	LOCKDEBUG_LOCKED(kernel_lock_dodebug, kernel_lock, NULL,
    247 	    RETURN_ADDRESS, 0);
    248 	if (owant == NULL) {
    249 		LOCKSTAT_EVENT_RA(lsflag, kernel_lock,
    250 		    LB_KERNEL_LOCK | LB_SPIN, 1, spintime, RETURN_ADDRESS);
    251 	}
    252 	LOCKSTAT_EXIT(lsflag);
    253 
    254 	/*
    255 	 * Now that we have kernel_lock, reset ci_biglock_wanted.  This
    256 	 * store must be unbuffered (immediately visible on the bus) in
    257 	 * order for non-interlocked mutex release to work correctly.
    258 	 * It must be visible before a mutex_exit() can execute on this
    259 	 * processor.
    260 	 *
    261 	 * Note: only where CAS is available in hardware will this be
    262 	 * an unbuffered write, but non-interlocked release cannot be
    263 	 * done on CPUs without CAS in hardware.
    264 	 */
    265 	(void)atomic_swap_ptr(&ci->ci_biglock_wanted, owant);
    266 
    267 	/*
    268 	 * Issue a memory barrier as we have acquired a lock.  This also
    269 	 * prevents stores from a following mutex_exit() being reordered
    270 	 * to occur before our store to ci_biglock_wanted above.
    271 	 */
    272 #ifndef __HAVE_ATOMIC_AS_MEMBAR
    273 	membar_enter();
    274 #endif
    275 }
    276 
    277 /*
    278  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
    279  * all holds.
    280  */
    281 void
    282 _kernel_unlock(int nlocks, int *countp)
    283 {
    284 	struct cpu_info *ci;
    285 	u_int olocks;
    286 	int s;
    287 	struct lwp *l = curlwp;
    288 
    289 	_KERNEL_LOCK_ASSERT(nlocks < 2);
    290 
    291 	olocks = l->l_blcnt;
    292 
    293 	if (olocks == 0) {
    294 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
    295 		if (countp != NULL)
    296 			*countp = 0;
    297 		return;
    298 	}
    299 
    300 	_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(kernel_lock));
    301 
    302 	if (nlocks == 0)
    303 		nlocks = olocks;
    304 	else if (nlocks == -1) {
    305 		nlocks = 1;
    306 		_KERNEL_LOCK_ASSERT(olocks == 1);
    307 	}
    308 	s = splvm();
    309 	ci = curcpu();
    310 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
    311 	if (ci->ci_biglock_count == nlocks) {
    312 		LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, kernel_lock,
    313 		    RETURN_ADDRESS, 0);
    314 		ci->ci_biglock_count = 0;
    315 		__cpu_simple_unlock(kernel_lock);
    316 		l->l_blcnt -= nlocks;
    317 		splx(s);
    318 		if (l->l_dopreempt)
    319 			kpreempt(0);
    320 	} else {
    321 		ci->ci_biglock_count -= nlocks;
    322 		l->l_blcnt -= nlocks;
    323 		splx(s);
    324 	}
    325 
    326 	if (countp != NULL)
    327 		*countp = olocks;
    328 }
    329 
    330 bool
    331 _kernel_locked_p(void)
    332 {
    333 	return __SIMPLELOCK_LOCKED_P(kernel_lock);
    334 }
    335