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