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