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