Home | History | Annotate | Line # | Download | only in kern
kern_mutex.c revision 1.85
      1 /*	$NetBSD: kern_mutex.c,v 1.85 2019/12/11 20:34:06 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2006, 2007, 2008, 2019 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 and Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Kernel mutex implementation, modeled after those found in Solaris,
     34  * a description of which can be found in:
     35  *
     36  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     37  *	    Richard McDougall.
     38  */
     39 
     40 #define	__MUTEX_PRIVATE
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.85 2019/12/11 20:34:06 ad Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/atomic.h>
     47 #include <sys/proc.h>
     48 #include <sys/mutex.h>
     49 #include <sys/sched.h>
     50 #include <sys/sleepq.h>
     51 #include <sys/systm.h>
     52 #include <sys/lockdebug.h>
     53 #include <sys/kernel.h>
     54 #include <sys/intr.h>
     55 #include <sys/lock.h>
     56 #include <sys/types.h>
     57 #include <sys/cpu.h>
     58 #include <sys/pserialize.h>
     59 
     60 #include <dev/lockstat.h>
     61 
     62 #include <machine/lock.h>
     63 
     64 /*
     65  * When not running a debug kernel, spin mutexes are not much
     66  * more than an splraiseipl() and splx() pair.
     67  */
     68 
     69 #if defined(DIAGNOSTIC) || defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
     70 #define	FULL
     71 #endif
     72 
     73 /*
     74  * Debugging support.
     75  */
     76 
     77 #define	MUTEX_WANTLOCK(mtx)					\
     78     LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),		\
     79         (uintptr_t)__builtin_return_address(0), 0)
     80 #define	MUTEX_TESTLOCK(mtx)					\
     81     LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),		\
     82         (uintptr_t)__builtin_return_address(0), -1)
     83 #define	MUTEX_LOCKED(mtx)					\
     84     LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), NULL,		\
     85         (uintptr_t)__builtin_return_address(0), 0)
     86 #define	MUTEX_UNLOCKED(mtx)					\
     87     LOCKDEBUG_UNLOCKED(MUTEX_DEBUG_P(mtx), (mtx),		\
     88         (uintptr_t)__builtin_return_address(0), 0)
     89 #define	MUTEX_ABORT(mtx, msg)					\
     90     mutex_abort(__func__, __LINE__, mtx, msg)
     91 
     92 #if defined(LOCKDEBUG)
     93 
     94 #define	MUTEX_DASSERT(mtx, cond)				\
     95 do {								\
     96 	if (__predict_false(!(cond)))				\
     97 		MUTEX_ABORT(mtx, "assertion failed: " #cond);	\
     98 } while (/* CONSTCOND */ 0)
     99 
    100 #else	/* LOCKDEBUG */
    101 
    102 #define	MUTEX_DASSERT(mtx, cond)	/* nothing */
    103 
    104 #endif /* LOCKDEBUG */
    105 
    106 #if defined(DIAGNOSTIC)
    107 
    108 #define	MUTEX_ASSERT(mtx, cond)					\
    109 do {								\
    110 	if (__predict_false(!(cond)))				\
    111 		MUTEX_ABORT(mtx, "assertion failed: " #cond);	\
    112 } while (/* CONSTCOND */ 0)
    113 
    114 #else	/* DIAGNOSTIC */
    115 
    116 #define	MUTEX_ASSERT(mtx, cond)	/* nothing */
    117 
    118 #endif	/* DIAGNOSTIC */
    119 
    120 /*
    121  * Some architectures can't use __cpu_simple_lock as is so allow a way
    122  * for them to use an alternate definition.
    123  */
    124 #ifndef MUTEX_SPINBIT_LOCK_INIT
    125 #define MUTEX_SPINBIT_LOCK_INIT(mtx)	__cpu_simple_lock_init(&(mtx)->mtx_lock)
    126 #endif
    127 #ifndef MUTEX_SPINBIT_LOCKED_P
    128 #define MUTEX_SPINBIT_LOCKED_P(mtx)	__SIMPLELOCK_LOCKED_P(&(mtx)->mtx_lock)
    129 #endif
    130 #ifndef MUTEX_SPINBIT_LOCK_TRY
    131 #define MUTEX_SPINBIT_LOCK_TRY(mtx)	__cpu_simple_lock_try(&(mtx)->mtx_lock)
    132 #endif
    133 #ifndef MUTEX_SPINBIT_LOCK_UNLOCK
    134 #define MUTEX_SPINBIT_LOCK_UNLOCK(mtx)	__cpu_simple_unlock(&(mtx)->mtx_lock)
    135 #endif
    136 
    137 #ifndef MUTEX_INITIALIZE_SPIN_IPL
    138 #define MUTEX_INITIALIZE_SPIN_IPL(mtx, ipl) \
    139 					((mtx)->mtx_ipl = makeiplcookie((ipl)))
    140 #endif
    141 
    142 /*
    143  * Spin mutex SPL save / restore.
    144  */
    145 
    146 #define	MUTEX_SPIN_SPLRAISE(mtx)					\
    147 do {									\
    148 	struct cpu_info *x__ci;						\
    149 	int x__cnt, s;							\
    150 	s = splraiseipl(MUTEX_SPIN_IPL(mtx));				\
    151 	x__ci = curcpu();						\
    152 	x__cnt = x__ci->ci_mtx_count--;					\
    153 	__insn_barrier();						\
    154 	if (x__cnt == 0)						\
    155 		x__ci->ci_mtx_oldspl = (s);				\
    156 } while (/* CONSTCOND */ 0)
    157 
    158 #define	MUTEX_SPIN_SPLRESTORE(mtx)					\
    159 do {									\
    160 	struct cpu_info *x__ci = curcpu();				\
    161 	int s = x__ci->ci_mtx_oldspl;					\
    162 	__insn_barrier();						\
    163 	if (++(x__ci->ci_mtx_count) == 0)			\
    164 		splx(s);						\
    165 } while (/* CONSTCOND */ 0)
    166 
    167 /*
    168  * Memory barriers.
    169  */
    170 #ifdef __HAVE_ATOMIC_AS_MEMBAR
    171 #define	MUTEX_MEMBAR_ENTER()
    172 #define	MUTEX_MEMBAR_EXIT()
    173 #else
    174 #define	MUTEX_MEMBAR_ENTER()		membar_enter()
    175 #define	MUTEX_MEMBAR_EXIT()		membar_exit()
    176 #endif
    177 
    178 /*
    179  * For architectures that provide 'simple' mutexes: they provide a
    180  * CAS function that is either MP-safe, or does not need to be MP
    181  * safe.  Adaptive mutexes on these architectures do not require an
    182  * additional interlock.
    183  */
    184 
    185 #ifdef __HAVE_SIMPLE_MUTEXES
    186 
    187 #define	MUTEX_OWNER(owner)						\
    188 	(owner & MUTEX_THREAD)
    189 #define	MUTEX_HAS_WAITERS(mtx)						\
    190 	(((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0)
    191 
    192 #define	MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug)				\
    193 do {									\
    194 	if (!dodebug)							\
    195 		(mtx)->mtx_owner |= MUTEX_BIT_NODEBUG;			\
    196 } while (/* CONSTCOND */ 0)
    197 
    198 #define	MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl)			\
    199 do {									\
    200 	(mtx)->mtx_owner = MUTEX_BIT_SPIN;				\
    201 	if (!dodebug)							\
    202 		(mtx)->mtx_owner |= MUTEX_BIT_NODEBUG;			\
    203 	MUTEX_INITIALIZE_SPIN_IPL((mtx), (ipl));			\
    204 	MUTEX_SPINBIT_LOCK_INIT((mtx));					\
    205 } while (/* CONSTCOND */ 0)
    206 
    207 #define	MUTEX_DESTROY(mtx)						\
    208 do {									\
    209 	(mtx)->mtx_owner = MUTEX_THREAD;				\
    210 } while (/* CONSTCOND */ 0)
    211 
    212 #define	MUTEX_SPIN_P(mtx)		\
    213     (((mtx)->mtx_owner & MUTEX_BIT_SPIN) != 0)
    214 #define	MUTEX_ADAPTIVE_P(mtx)		\
    215     (((mtx)->mtx_owner & MUTEX_BIT_SPIN) == 0)
    216 
    217 #define	MUTEX_DEBUG_P(mtx)	(((mtx)->mtx_owner & MUTEX_BIT_NODEBUG) == 0)
    218 #if defined(LOCKDEBUG)
    219 #define	MUTEX_OWNED(owner)		(((owner) & ~MUTEX_BIT_NODEBUG) != 0)
    220 #define	MUTEX_INHERITDEBUG(n, o)	(n) |= (o) & MUTEX_BIT_NODEBUG
    221 #else /* defined(LOCKDEBUG) */
    222 #define	MUTEX_OWNED(owner)		((owner) != 0)
    223 #define	MUTEX_INHERITDEBUG(n, o)	/* nothing */
    224 #endif /* defined(LOCKDEBUG) */
    225 
    226 static inline int
    227 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
    228 {
    229 	int rv;
    230 	uintptr_t oldown = 0;
    231 	uintptr_t newown = curthread;
    232 
    233 	MUTEX_INHERITDEBUG(oldown, mtx->mtx_owner);
    234 	MUTEX_INHERITDEBUG(newown, oldown);
    235 	rv = MUTEX_CAS(&mtx->mtx_owner, oldown, newown);
    236 	MUTEX_MEMBAR_ENTER();
    237 	return rv;
    238 }
    239 
    240 static inline int
    241 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
    242 {
    243 	int rv;
    244 	rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS);
    245 	MUTEX_MEMBAR_ENTER();
    246 	return rv;
    247 }
    248 
    249 static inline void
    250 MUTEX_RELEASE(kmutex_t *mtx)
    251 {
    252 	uintptr_t newown;
    253 
    254 	MUTEX_MEMBAR_EXIT();
    255 	newown = 0;
    256 	MUTEX_INHERITDEBUG(newown, mtx->mtx_owner);
    257 	mtx->mtx_owner = newown;
    258 }
    259 #endif	/* __HAVE_SIMPLE_MUTEXES */
    260 
    261 /*
    262  * Patch in stubs via strong alias where they are not available.
    263  */
    264 
    265 #if defined(LOCKDEBUG)
    266 #undef	__HAVE_MUTEX_STUBS
    267 #undef	__HAVE_SPIN_MUTEX_STUBS
    268 #endif
    269 
    270 #ifndef __HAVE_MUTEX_STUBS
    271 __strong_alias(mutex_enter,mutex_vector_enter);
    272 __strong_alias(mutex_exit,mutex_vector_exit);
    273 #endif
    274 
    275 #ifndef __HAVE_SPIN_MUTEX_STUBS
    276 __strong_alias(mutex_spin_enter,mutex_vector_enter);
    277 __strong_alias(mutex_spin_exit,mutex_vector_exit);
    278 #endif
    279 
    280 static void	mutex_abort(const char *, size_t, const kmutex_t *,
    281     const char *);
    282 static void	mutex_dump(const volatile void *, lockop_printer_t);
    283 
    284 lockops_t mutex_spin_lockops = {
    285 	.lo_name = "Mutex",
    286 	.lo_type = LOCKOPS_SPIN,
    287 	.lo_dump = mutex_dump,
    288 };
    289 
    290 lockops_t mutex_adaptive_lockops = {
    291 	.lo_name = "Mutex",
    292 	.lo_type = LOCKOPS_SLEEP,
    293 	.lo_dump = mutex_dump,
    294 };
    295 
    296 syncobj_t mutex_syncobj = {
    297 	.sobj_flag	= SOBJ_SLEEPQ_SORTED,
    298 	.sobj_unsleep	= turnstile_unsleep,
    299 	.sobj_changepri	= turnstile_changepri,
    300 	.sobj_lendpri	= sleepq_lendpri,
    301 	.sobj_owner	= (void *)mutex_owner,
    302 };
    303 
    304 /*
    305  * mutex_dump:
    306  *
    307  *	Dump the contents of a mutex structure.
    308  */
    309 static void
    310 mutex_dump(const volatile void *cookie, lockop_printer_t pr)
    311 {
    312 	const volatile kmutex_t *mtx = cookie;
    313 
    314 	pr("owner field  : %#018lx wait/spin: %16d/%d\n",
    315 	    (long)MUTEX_OWNER(mtx->mtx_owner), MUTEX_HAS_WAITERS(mtx),
    316 	    MUTEX_SPIN_P(mtx));
    317 }
    318 
    319 /*
    320  * mutex_abort:
    321  *
    322  *	Dump information about an error and panic the system.  This
    323  *	generates a lot of machine code in the DIAGNOSTIC case, so
    324  *	we ask the compiler to not inline it.
    325  */
    326 static void __noinline
    327 mutex_abort(const char *func, size_t line, const kmutex_t *mtx, const char *msg)
    328 {
    329 
    330 	LOCKDEBUG_ABORT(func, line, mtx, (MUTEX_SPIN_P(mtx) ?
    331 	    &mutex_spin_lockops : &mutex_adaptive_lockops), msg);
    332 }
    333 
    334 /*
    335  * mutex_init:
    336  *
    337  *	Initialize a mutex for use.  Note that adaptive mutexes are in
    338  *	essence spin mutexes that can sleep to avoid deadlock and wasting
    339  *	CPU time.  We can't easily provide a type of mutex that always
    340  *	sleeps - see comments in mutex_vector_enter() about releasing
    341  *	mutexes unlocked.
    342  */
    343 void _mutex_init(kmutex_t *, kmutex_type_t, int, uintptr_t);
    344 void
    345 _mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl,
    346     uintptr_t return_address)
    347 {
    348 	lockops_t *lockops __unused;
    349 	bool dodebug;
    350 
    351 	memset(mtx, 0, sizeof(*mtx));
    352 
    353 	if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
    354 	    ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
    355 	    ipl == IPL_SOFTSERIAL) {
    356 		lockops = (type == MUTEX_NODEBUG ?
    357 		    NULL : &mutex_adaptive_lockops);
    358 		dodebug = LOCKDEBUG_ALLOC(mtx, lockops, return_address);
    359 		MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug);
    360 	} else {
    361 		lockops = (type == MUTEX_NODEBUG ?
    362 		    NULL : &mutex_spin_lockops);
    363 		dodebug = LOCKDEBUG_ALLOC(mtx, lockops, return_address);
    364 		MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
    365 	}
    366 }
    367 
    368 void
    369 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
    370 {
    371 
    372 	_mutex_init(mtx, type, ipl, (uintptr_t)__builtin_return_address(0));
    373 }
    374 
    375 /*
    376  * mutex_destroy:
    377  *
    378  *	Tear down a mutex.
    379  */
    380 void
    381 mutex_destroy(kmutex_t *mtx)
    382 {
    383 
    384 	if (MUTEX_ADAPTIVE_P(mtx)) {
    385 		MUTEX_ASSERT(mtx, !MUTEX_OWNED(mtx->mtx_owner) &&
    386 		    !MUTEX_HAS_WAITERS(mtx));
    387 	} else {
    388 		MUTEX_ASSERT(mtx, !MUTEX_SPINBIT_LOCKED_P(mtx));
    389 	}
    390 
    391 	LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx), mtx);
    392 	MUTEX_DESTROY(mtx);
    393 }
    394 
    395 #ifdef MULTIPROCESSOR
    396 /*
    397  * mutex_oncpu:
    398  *
    399  *	Return true if an adaptive mutex owner is running on a CPU in the
    400  *	system.  If the target is waiting on the kernel big lock, then we
    401  *	must release it.  This is necessary to avoid deadlock.
    402  */
    403 static bool
    404 mutex_oncpu(uintptr_t owner)
    405 {
    406 	struct cpu_info *ci;
    407 	lwp_t *l;
    408 
    409 	KASSERT(kpreempt_disabled());
    410 
    411 	if (!MUTEX_OWNED(owner)) {
    412 		return false;
    413 	}
    414 
    415 	/*
    416 	 * See lwp_dtor() why dereference of the LWP pointer is safe.
    417 	 * We must have kernel preemption disabled for that.
    418 	 */
    419 	l = (lwp_t *)MUTEX_OWNER(owner);
    420 	ci = l->l_cpu;
    421 
    422 	if (ci && ci->ci_curlwp == l) {
    423 		/* Target is running; do we need to block? */
    424 		return (ci->ci_biglock_wanted != l);
    425 	}
    426 
    427 	/* Not running.  It may be safe to block now. */
    428 	return false;
    429 }
    430 #endif	/* MULTIPROCESSOR */
    431 
    432 /*
    433  * mutex_vector_enter:
    434  *
    435  *	Support routine for mutex_enter() that must handle all cases.  In
    436  *	the LOCKDEBUG case, mutex_enter() is always aliased here, even if
    437  *	fast-path stubs are available.  If a mutex_spin_enter() stub is
    438  *	not available, then it is also aliased directly here.
    439  */
    440 void
    441 mutex_vector_enter(kmutex_t *mtx)
    442 {
    443 	uintptr_t owner, curthread;
    444 	turnstile_t *ts;
    445 #ifdef MULTIPROCESSOR
    446 	u_int count;
    447 #endif
    448 	LOCKSTAT_COUNTER(spincnt);
    449 	LOCKSTAT_COUNTER(slpcnt);
    450 	LOCKSTAT_TIMER(spintime);
    451 	LOCKSTAT_TIMER(slptime);
    452 	LOCKSTAT_FLAG(lsflag);
    453 
    454 	/*
    455 	 * Handle spin mutexes.
    456 	 */
    457 	if (MUTEX_SPIN_P(mtx)) {
    458 #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR)
    459 		u_int spins = 0;
    460 #endif
    461 		MUTEX_SPIN_SPLRAISE(mtx);
    462 		MUTEX_WANTLOCK(mtx);
    463 #ifdef FULL
    464 		if (MUTEX_SPINBIT_LOCK_TRY(mtx)) {
    465 			MUTEX_LOCKED(mtx);
    466 			return;
    467 		}
    468 #if !defined(MULTIPROCESSOR)
    469 		MUTEX_ABORT(mtx, "locking against myself");
    470 #else /* !MULTIPROCESSOR */
    471 
    472 		LOCKSTAT_ENTER(lsflag);
    473 		LOCKSTAT_START_TIMER(lsflag, spintime);
    474 		count = SPINLOCK_BACKOFF_MIN;
    475 
    476 		/*
    477 		 * Spin testing the lock word and do exponential backoff
    478 		 * to reduce cache line ping-ponging between CPUs.
    479 		 */
    480 		do {
    481 			while (MUTEX_SPINBIT_LOCKED_P(mtx)) {
    482 				SPINLOCK_BACKOFF(count);
    483 #ifdef LOCKDEBUG
    484 				if (SPINLOCK_SPINOUT(spins))
    485 					MUTEX_ABORT(mtx, "spinout");
    486 #endif	/* LOCKDEBUG */
    487 			}
    488 		} while (!MUTEX_SPINBIT_LOCK_TRY(mtx));
    489 
    490 		if (count != SPINLOCK_BACKOFF_MIN) {
    491 			LOCKSTAT_STOP_TIMER(lsflag, spintime);
    492 			LOCKSTAT_EVENT(lsflag, mtx,
    493 			    LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
    494 		}
    495 		LOCKSTAT_EXIT(lsflag);
    496 #endif	/* !MULTIPROCESSOR */
    497 #endif	/* FULL */
    498 		MUTEX_LOCKED(mtx);
    499 		return;
    500 	}
    501 
    502 	curthread = (uintptr_t)curlwp;
    503 
    504 	MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
    505 	MUTEX_ASSERT(mtx, curthread != 0);
    506 	MUTEX_ASSERT(mtx, !cpu_intr_p());
    507 	MUTEX_WANTLOCK(mtx);
    508 
    509 	if (panicstr == NULL) {
    510 		KDASSERT(pserialize_not_in_read_section());
    511 		LOCKDEBUG_BARRIER(&kernel_lock, 1);
    512 	}
    513 
    514 	LOCKSTAT_ENTER(lsflag);
    515 
    516 	/*
    517 	 * Adaptive mutex; spin trying to acquire the mutex.  If we
    518 	 * determine that the owner is not running on a processor,
    519 	 * then we stop spinning, and sleep instead.
    520 	 */
    521 	KPREEMPT_DISABLE(curlwp);
    522 	for (owner = mtx->mtx_owner;;) {
    523 		if (!MUTEX_OWNED(owner)) {
    524 			/*
    525 			 * Mutex owner clear could mean two things:
    526 			 *
    527 			 *	* The mutex has been released.
    528 			 *	* The owner field hasn't been set yet.
    529 			 *
    530 			 * Try to acquire it again.  If that fails,
    531 			 * we'll just loop again.
    532 			 */
    533 			if (MUTEX_ACQUIRE(mtx, curthread))
    534 				break;
    535 			owner = mtx->mtx_owner;
    536 			continue;
    537 		}
    538 		if (__predict_false(MUTEX_OWNER(owner) == curthread)) {
    539 			MUTEX_ABORT(mtx, "locking against myself");
    540 		}
    541 #ifdef MULTIPROCESSOR
    542 		/*
    543 		 * Check to see if the owner is running on a processor.
    544 		 * If so, then we should just spin, as the owner will
    545 		 * likely release the lock very soon.
    546 		 */
    547 		if (mutex_oncpu(owner)) {
    548 			LOCKSTAT_START_TIMER(lsflag, spintime);
    549 			count = SPINLOCK_BACKOFF_MIN;
    550 			do {
    551 				KPREEMPT_ENABLE(curlwp);
    552 				SPINLOCK_BACKOFF(count);
    553 				KPREEMPT_DISABLE(curlwp);
    554 				owner = mtx->mtx_owner;
    555 			} while (mutex_oncpu(owner));
    556 			LOCKSTAT_STOP_TIMER(lsflag, spintime);
    557 			LOCKSTAT_COUNT(spincnt, 1);
    558 			if (!MUTEX_OWNED(owner))
    559 				continue;
    560 		}
    561 #endif
    562 
    563 		ts = turnstile_lookup(mtx);
    564 
    565 		/*
    566 		 * Once we have the turnstile chain interlock, mark the
    567 		 * mutex as having waiters.  If that fails, spin again:
    568 		 * chances are that the mutex has been released.
    569 		 */
    570 		if (!MUTEX_SET_WAITERS(mtx, owner)) {
    571 			turnstile_exit(mtx);
    572 			owner = mtx->mtx_owner;
    573 			continue;
    574 		}
    575 
    576 #ifdef MULTIPROCESSOR
    577 		/*
    578 		 * mutex_exit() is permitted to release the mutex without
    579 		 * any interlocking instructions, and the following can
    580 		 * occur as a result:
    581 		 *
    582 		 *  CPU 1: MUTEX_SET_WAITERS()      CPU2: mutex_exit()
    583 		 * ---------------------------- ----------------------------
    584 		 *		..		    acquire cache line
    585 		 *		..                   test for waiters
    586 		 *	acquire cache line    <-      lose cache line
    587 		 *	 lock cache line	           ..
    588 		 *     verify mutex is held                ..
    589 		 *	    set waiters  	           ..
    590 		 *	 unlock cache line		   ..
    591 		 *	  lose cache line     ->    acquire cache line
    592 		 *		..	          clear lock word, waiters
    593 		 *	  return success
    594 		 *
    595 		 * There is another race that can occur: a third CPU could
    596 		 * acquire the mutex as soon as it is released.  Since
    597 		 * adaptive mutexes are primarily spin mutexes, this is not
    598 		 * something that we need to worry about too much.  What we
    599 		 * do need to ensure is that the waiters bit gets set.
    600 		 *
    601 		 * To allow the unlocked release, we need to make some
    602 		 * assumptions here:
    603 		 *
    604 		 * o Release is the only non-atomic/unlocked operation
    605 		 *   that can be performed on the mutex.  (It must still
    606 		 *   be atomic on the local CPU, e.g. in case interrupted
    607 		 *   or preempted).
    608 		 *
    609 		 * o At any given time, MUTEX_SET_WAITERS() can only ever
    610 		 *   be in progress on one CPU in the system - guaranteed
    611 		 *   by the turnstile chain lock.
    612 		 *
    613 		 * o No other operations other than MUTEX_SET_WAITERS()
    614 		 *   and release can modify a mutex with a non-zero
    615 		 *   owner field.
    616 		 *
    617 		 * o The result of a successful MUTEX_SET_WAITERS() call
    618 		 *   is an unbuffered write that is immediately visible
    619 		 *   to all other processors in the system.
    620 		 *
    621 		 * o If the holding LWP switches away, it posts a store
    622 		 *   fence before changing curlwp, ensuring that any
    623 		 *   overwrite of the mutex waiters flag by mutex_exit()
    624 		 *   completes before the modification of curlwp becomes
    625 		 *   visible to this CPU.
    626 		 *
    627 		 * o mi_switch() posts a store fence before setting curlwp
    628 		 *   and before resuming execution of an LWP.
    629 		 *
    630 		 * o _kernel_lock() posts a store fence before setting
    631 		 *   curcpu()->ci_biglock_wanted, and after clearing it.
    632 		 *   This ensures that any overwrite of the mutex waiters
    633 		 *   flag by mutex_exit() completes before the modification
    634 		 *   of ci_biglock_wanted becomes visible.
    635 		 *
    636 		 * We now post a read memory barrier (after setting the
    637 		 * waiters field) and check the lock holder's status again.
    638 		 * Some of the possible outcomes (not an exhaustive list):
    639 		 *
    640 		 * 1. The on-CPU check returns true: the holding LWP is
    641 		 *    running again.  The lock may be released soon and
    642 		 *    we should spin.  Importantly, we can't trust the
    643 		 *    value of the waiters flag.
    644 		 *
    645 		 * 2. The on-CPU check returns false: the holding LWP is
    646 		 *    not running.  We now have the opportunity to check
    647 		 *    if mutex_exit() has blatted the modifications made
    648 		 *    by MUTEX_SET_WAITERS().
    649 		 *
    650 		 * 3. The on-CPU check returns false: the holding LWP may
    651 		 *    or may not be running.  It has context switched at
    652 		 *    some point during our check.  Again, we have the
    653 		 *    chance to see if the waiters bit is still set or
    654 		 *    has been overwritten.
    655 		 *
    656 		 * 4. The on-CPU check returns false: the holding LWP is
    657 		 *    running on a CPU, but wants the big lock.  It's OK
    658 		 *    to check the waiters field in this case.
    659 		 *
    660 		 * 5. The has-waiters check fails: the mutex has been
    661 		 *    released, the waiters flag cleared and another LWP
    662 		 *    now owns the mutex.
    663 		 *
    664 		 * 6. The has-waiters check fails: the mutex has been
    665 		 *    released.
    666 		 *
    667 		 * If the waiters bit is not set it's unsafe to go asleep,
    668 		 * as we might never be awoken.
    669 		 */
    670 		if ((membar_consumer(), mutex_oncpu(owner)) ||
    671 		    (membar_consumer(), !MUTEX_HAS_WAITERS(mtx))) {
    672 			turnstile_exit(mtx);
    673 			owner = mtx->mtx_owner;
    674 			continue;
    675 		}
    676 #endif	/* MULTIPROCESSOR */
    677 
    678 		LOCKSTAT_START_TIMER(lsflag, slptime);
    679 
    680 		turnstile_block(ts, TS_WRITER_Q, mtx, &mutex_syncobj);
    681 
    682 		LOCKSTAT_STOP_TIMER(lsflag, slptime);
    683 		LOCKSTAT_COUNT(slpcnt, 1);
    684 
    685 		owner = mtx->mtx_owner;
    686 	}
    687 	KPREEMPT_ENABLE(curlwp);
    688 
    689 	LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1,
    690 	    slpcnt, slptime);
    691 	LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN,
    692 	    spincnt, spintime);
    693 	LOCKSTAT_EXIT(lsflag);
    694 
    695 	MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
    696 	MUTEX_LOCKED(mtx);
    697 }
    698 
    699 /*
    700  * mutex_vector_exit:
    701  *
    702  *	Support routine for mutex_exit() that handles all cases.
    703  */
    704 void
    705 mutex_vector_exit(kmutex_t *mtx)
    706 {
    707 	turnstile_t *ts;
    708 	uintptr_t curthread;
    709 
    710 	if (MUTEX_SPIN_P(mtx)) {
    711 #ifdef FULL
    712 		if (__predict_false(!MUTEX_SPINBIT_LOCKED_P(mtx))) {
    713 			MUTEX_ABORT(mtx, "exiting unheld spin mutex");
    714 		}
    715 		MUTEX_UNLOCKED(mtx);
    716 		MUTEX_SPINBIT_LOCK_UNLOCK(mtx);
    717 #endif
    718 		MUTEX_SPIN_SPLRESTORE(mtx);
    719 		return;
    720 	}
    721 
    722 #ifndef __HAVE_MUTEX_STUBS
    723 	if (__predict_false(cold)) {
    724 		MUTEX_UNLOCKED(mtx);
    725 		MUTEX_RELEASE(mtx);
    726 		return;
    727 	}
    728 #endif
    729 
    730 	curthread = (uintptr_t)curlwp;
    731 	MUTEX_DASSERT(mtx, curthread != 0);
    732 	MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
    733 	MUTEX_UNLOCKED(mtx);
    734 #if !defined(LOCKDEBUG)
    735 	__USE(curthread);
    736 #endif
    737 
    738 #ifdef LOCKDEBUG
    739 	/*
    740 	 * Avoid having to take the turnstile chain lock every time
    741 	 * around.  Raise the priority level to splhigh() in order
    742 	 * to disable preemption and so make the following atomic.
    743 	 */
    744 	{
    745 		int s = splhigh();
    746 		if (!MUTEX_HAS_WAITERS(mtx)) {
    747 			MUTEX_RELEASE(mtx);
    748 			splx(s);
    749 			return;
    750 		}
    751 		splx(s);
    752 	}
    753 #endif
    754 
    755 	/*
    756 	 * Get this lock's turnstile.  This gets the interlock on
    757 	 * the sleep queue.  Once we have that, we can clear the
    758 	 * lock.  If there was no turnstile for the lock, there
    759 	 * were no waiters remaining.
    760 	 */
    761 	ts = turnstile_lookup(mtx);
    762 
    763 	if (ts == NULL) {
    764 		MUTEX_RELEASE(mtx);
    765 		turnstile_exit(mtx);
    766 	} else {
    767 		MUTEX_RELEASE(mtx);
    768 		turnstile_wakeup(ts, TS_WRITER_Q,
    769 		    TS_WAITERS(ts, TS_WRITER_Q), NULL);
    770 	}
    771 }
    772 
    773 #ifndef __HAVE_SIMPLE_MUTEXES
    774 /*
    775  * mutex_wakeup:
    776  *
    777  *	Support routine for mutex_exit() that wakes up all waiters.
    778  *	We assume that the mutex has been released, but it need not
    779  *	be.
    780  */
    781 void
    782 mutex_wakeup(kmutex_t *mtx)
    783 {
    784 	turnstile_t *ts;
    785 
    786 	ts = turnstile_lookup(mtx);
    787 	if (ts == NULL) {
    788 		turnstile_exit(mtx);
    789 		return;
    790 	}
    791 	MUTEX_CLEAR_WAITERS(mtx);
    792 	turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL);
    793 }
    794 #endif	/* !__HAVE_SIMPLE_MUTEXES */
    795 
    796 /*
    797  * mutex_owned:
    798  *
    799  *	Return true if the current LWP (adaptive) or CPU (spin)
    800  *	holds the mutex.
    801  */
    802 int
    803 mutex_owned(const kmutex_t *mtx)
    804 {
    805 
    806 	if (mtx == NULL)
    807 		return 0;
    808 	if (MUTEX_ADAPTIVE_P(mtx))
    809 		return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp;
    810 #ifdef FULL
    811 	return MUTEX_SPINBIT_LOCKED_P(mtx);
    812 #else
    813 	return 1;
    814 #endif
    815 }
    816 
    817 /*
    818  * mutex_owner:
    819  *
    820  *	Return the current owner of an adaptive mutex.  Used for
    821  *	priority inheritance.
    822  */
    823 lwp_t *
    824 mutex_owner(const kmutex_t *mtx)
    825 {
    826 
    827 	MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
    828 	return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner);
    829 }
    830 
    831 /*
    832  * mutex_owner_running:
    833  *
    834  *	Return true if an adaptive mutex is unheld, or held and the owner is
    835  *	running on a CPU.  For the pagedaemon.
    836  */
    837 bool
    838 mutex_owner_running(const kmutex_t *mtx)
    839 {
    840 #ifdef MULTIPROCESSOR
    841 	uintptr_t owner;
    842 	bool rv;
    843 
    844 	MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
    845 	kpreempt_disable();
    846 	owner = mtx->mtx_owner;
    847 	rv = !MUTEX_OWNED(owner) || mutex_oncpu(MUTEX_OWNER(owner));
    848 	kpreempt_enable();
    849 	return rv;
    850 #else
    851 	return mutex_owner(mtx) == curlwp;
    852 #endif
    853 }
    854 
    855 /*
    856  * mutex_ownable:
    857  *
    858  *	When compiled with DEBUG and LOCKDEBUG defined, ensure that
    859  *	the mutex is available.  We cannot use !mutex_owned() since
    860  *	that won't work correctly for spin mutexes.
    861  */
    862 int
    863 mutex_ownable(const kmutex_t *mtx)
    864 {
    865 
    866 #ifdef LOCKDEBUG
    867 	MUTEX_TESTLOCK(mtx);
    868 #endif
    869 	return 1;
    870 }
    871 
    872 /*
    873  * mutex_tryenter:
    874  *
    875  *	Try to acquire the mutex; return non-zero if we did.
    876  */
    877 int
    878 mutex_tryenter(kmutex_t *mtx)
    879 {
    880 	uintptr_t curthread;
    881 
    882 	/*
    883 	 * Handle spin mutexes.
    884 	 */
    885 	if (MUTEX_SPIN_P(mtx)) {
    886 		MUTEX_SPIN_SPLRAISE(mtx);
    887 #ifdef FULL
    888 		if (MUTEX_SPINBIT_LOCK_TRY(mtx)) {
    889 			MUTEX_WANTLOCK(mtx);
    890 			MUTEX_LOCKED(mtx);
    891 			return 1;
    892 		}
    893 		MUTEX_SPIN_SPLRESTORE(mtx);
    894 #else
    895 		MUTEX_WANTLOCK(mtx);
    896 		MUTEX_LOCKED(mtx);
    897 		return 1;
    898 #endif
    899 	} else {
    900 		curthread = (uintptr_t)curlwp;
    901 		MUTEX_ASSERT(mtx, curthread != 0);
    902 		if (MUTEX_ACQUIRE(mtx, curthread)) {
    903 			MUTEX_WANTLOCK(mtx);
    904 			MUTEX_LOCKED(mtx);
    905 			MUTEX_DASSERT(mtx,
    906 			    MUTEX_OWNER(mtx->mtx_owner) == curthread);
    907 			return 1;
    908 		}
    909 	}
    910 
    911 	return 0;
    912 }
    913 
    914 #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
    915 /*
    916  * mutex_spin_retry:
    917  *
    918  *	Support routine for mutex_spin_enter().  Assumes that the caller
    919  *	has already raised the SPL, and adjusted counters.
    920  */
    921 void
    922 mutex_spin_retry(kmutex_t *mtx)
    923 {
    924 #ifdef MULTIPROCESSOR
    925 	u_int count;
    926 	LOCKSTAT_TIMER(spintime);
    927 	LOCKSTAT_FLAG(lsflag);
    928 #ifdef LOCKDEBUG
    929 	u_int spins = 0;
    930 #endif	/* LOCKDEBUG */
    931 
    932 	MUTEX_WANTLOCK(mtx);
    933 
    934 	LOCKSTAT_ENTER(lsflag);
    935 	LOCKSTAT_START_TIMER(lsflag, spintime);
    936 	count = SPINLOCK_BACKOFF_MIN;
    937 
    938 	/*
    939 	 * Spin testing the lock word and do exponential backoff
    940 	 * to reduce cache line ping-ponging between CPUs.
    941 	 */
    942 	do {
    943 		while (MUTEX_SPINBIT_LOCKED_P(mtx)) {
    944 			SPINLOCK_BACKOFF(count);
    945 #ifdef LOCKDEBUG
    946 			if (SPINLOCK_SPINOUT(spins))
    947 				MUTEX_ABORT(mtx, "spinout");
    948 #endif	/* LOCKDEBUG */
    949 		}
    950 	} while (!MUTEX_SPINBIT_LOCK_TRY(mtx));
    951 
    952 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    953 	LOCKSTAT_EVENT(lsflag, mtx, LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
    954 	LOCKSTAT_EXIT(lsflag);
    955 
    956 	MUTEX_LOCKED(mtx);
    957 #else	/* MULTIPROCESSOR */
    958 	MUTEX_ABORT(mtx, "locking against myself");
    959 #endif	/* MULTIPROCESSOR */
    960 }
    961 #endif	/* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */
    962