xref: /netbsd-src/sys/kern/kern_mutex.c (revision 6779c0232f05d52f6057b4bc5f46e515eb8f8989)
1*6779c023Sriastradh /*	$NetBSD: kern_mutex.c,v 1.112 2023/10/15 10:28:23 riastradh Exp $	*/
2b07ec3fcSad 
3b07ec3fcSad /*-
46ed72b5fSad  * Copyright (c) 2002, 2006, 2007, 2008, 2019, 2023
56ed72b5fSad  *     The NetBSD Foundation, Inc.
6b07ec3fcSad  * All rights reserved.
7b07ec3fcSad  *
8b07ec3fcSad  * This code is derived from software contributed to The NetBSD Foundation
9b07ec3fcSad  * by Jason R. Thorpe and Andrew Doran.
10b07ec3fcSad  *
11b07ec3fcSad  * Redistribution and use in source and binary forms, with or without
12b07ec3fcSad  * modification, are permitted provided that the following conditions
13b07ec3fcSad  * are met:
14b07ec3fcSad  * 1. Redistributions of source code must retain the above copyright
15b07ec3fcSad  *    notice, this list of conditions and the following disclaimer.
16b07ec3fcSad  * 2. Redistributions in binary form must reproduce the above copyright
17b07ec3fcSad  *    notice, this list of conditions and the following disclaimer in the
18b07ec3fcSad  *    documentation and/or other materials provided with the distribution.
19b07ec3fcSad  *
20b07ec3fcSad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21b07ec3fcSad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22b07ec3fcSad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23b07ec3fcSad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24b07ec3fcSad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25b07ec3fcSad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26b07ec3fcSad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27b07ec3fcSad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28b07ec3fcSad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b07ec3fcSad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30b07ec3fcSad  * POSSIBILITY OF SUCH DAMAGE.
31b07ec3fcSad  */
32b07ec3fcSad 
33b07ec3fcSad /*
34b07ec3fcSad  * Kernel mutex implementation, modeled after those found in Solaris,
35b07ec3fcSad  * a description of which can be found in:
36b07ec3fcSad  *
37b07ec3fcSad  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
38b07ec3fcSad  *	    Richard McDougall.
39b07ec3fcSad  */
40b07ec3fcSad 
41b07ec3fcSad #define	__MUTEX_PRIVATE
42b07ec3fcSad 
43b07ec3fcSad #include <sys/cdefs.h>
44*6779c023Sriastradh __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.112 2023/10/15 10:28:23 riastradh Exp $");
45b07ec3fcSad 
46b07ec3fcSad #include <sys/param.h>
47*6779c023Sriastradh 
4883685e65Spooka #include <sys/atomic.h>
49*6779c023Sriastradh #include <sys/cpu.h>
50*6779c023Sriastradh #include <sys/intr.h>
51*6779c023Sriastradh #include <sys/kernel.h>
52*6779c023Sriastradh #include <sys/lock.h>
53*6779c023Sriastradh #include <sys/lockdebug.h>
54b07ec3fcSad #include <sys/mutex.h>
55*6779c023Sriastradh #include <sys/proc.h>
56*6779c023Sriastradh #include <sys/pserialize.h>
57b07ec3fcSad #include <sys/sched.h>
58b07ec3fcSad #include <sys/sleepq.h>
59fac91bbeSriastradh #include <sys/syncobj.h>
60*6779c023Sriastradh #include <sys/systm.h>
61*6779c023Sriastradh #include <sys/types.h>
62b07ec3fcSad 
63b07ec3fcSad #include <dev/lockstat.h>
64b07ec3fcSad 
650664a045Sad #include <machine/lock.h>
660664a045Sad 
67b07ec3fcSad /*
68b07ec3fcSad  * When not running a debug kernel, spin mutexes are not much
69b07ec3fcSad  * more than an splraiseipl() and splx() pair.
70b07ec3fcSad  */
71b07ec3fcSad 
72b07ec3fcSad #if defined(DIAGNOSTIC) || defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
73b07ec3fcSad #define	FULL
74b07ec3fcSad #endif
75b07ec3fcSad 
76b07ec3fcSad /*
77b07ec3fcSad  * Debugging support.
78b07ec3fcSad  */
79b07ec3fcSad 
80b07ec3fcSad #define	MUTEX_WANTLOCK(mtx)					\
8138d5e341Syamt     LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),		\
82060c06beSmlelstv         (uintptr_t)__builtin_return_address(0), 0)
8348e395b1Spgoyette #define	MUTEX_TESTLOCK(mtx)					\
8448e395b1Spgoyette     LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),		\
8548e395b1Spgoyette         (uintptr_t)__builtin_return_address(0), -1)
86b07ec3fcSad #define	MUTEX_LOCKED(mtx)					\
877b8f5124Sad     LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), NULL,		\
88b07ec3fcSad         (uintptr_t)__builtin_return_address(0), 0)
89b07ec3fcSad #define	MUTEX_UNLOCKED(mtx)					\
9038d5e341Syamt     LOCKDEBUG_UNLOCKED(MUTEX_DEBUG_P(mtx), (mtx),		\
91b07ec3fcSad         (uintptr_t)__builtin_return_address(0), 0)
92b07ec3fcSad #define	MUTEX_ABORT(mtx, msg)					\
939be065fbSchristos     mutex_abort(__func__, __LINE__, mtx, msg)
94b07ec3fcSad 
95b07ec3fcSad #if defined(LOCKDEBUG)
96b07ec3fcSad 
97b07ec3fcSad #define	MUTEX_DASSERT(mtx, cond)				\
98b07ec3fcSad do {								\
9901c290b0Sozaki-r 	if (__predict_false(!(cond)))				\
100b07ec3fcSad 		MUTEX_ABORT(mtx, "assertion failed: " #cond);	\
1013d895437Sskrll } while (/* CONSTCOND */ 0)
102b07ec3fcSad 
103b07ec3fcSad #else	/* LOCKDEBUG */
104b07ec3fcSad 
105b07ec3fcSad #define	MUTEX_DASSERT(mtx, cond)	/* nothing */
106b07ec3fcSad 
107b07ec3fcSad #endif /* LOCKDEBUG */
108b07ec3fcSad 
109b07ec3fcSad #if defined(DIAGNOSTIC)
110b07ec3fcSad 
111b07ec3fcSad #define	MUTEX_ASSERT(mtx, cond)					\
112b07ec3fcSad do {								\
11301c290b0Sozaki-r 	if (__predict_false(!(cond)))				\
114b07ec3fcSad 		MUTEX_ABORT(mtx, "assertion failed: " #cond);	\
115b07ec3fcSad } while (/* CONSTCOND */ 0)
116b07ec3fcSad 
117b07ec3fcSad #else	/* DIAGNOSTIC */
118b07ec3fcSad 
119b07ec3fcSad #define	MUTEX_ASSERT(mtx, cond)	/* nothing */
120b07ec3fcSad 
121b07ec3fcSad #endif	/* DIAGNOSTIC */
122b07ec3fcSad 
123b07ec3fcSad /*
124c3dcd587Smatt  * Some architectures can't use __cpu_simple_lock as is so allow a way
125c3dcd587Smatt  * for them to use an alternate definition.
126c3dcd587Smatt  */
127c3dcd587Smatt #ifndef MUTEX_SPINBIT_LOCK_INIT
128c3dcd587Smatt #define MUTEX_SPINBIT_LOCK_INIT(mtx)	__cpu_simple_lock_init(&(mtx)->mtx_lock)
129c3dcd587Smatt #endif
130c3dcd587Smatt #ifndef MUTEX_SPINBIT_LOCKED_P
131c3dcd587Smatt #define MUTEX_SPINBIT_LOCKED_P(mtx)	__SIMPLELOCK_LOCKED_P(&(mtx)->mtx_lock)
132c3dcd587Smatt #endif
133c3dcd587Smatt #ifndef MUTEX_SPINBIT_LOCK_TRY
134c3dcd587Smatt #define MUTEX_SPINBIT_LOCK_TRY(mtx)	__cpu_simple_lock_try(&(mtx)->mtx_lock)
135c3dcd587Smatt #endif
136c3dcd587Smatt #ifndef MUTEX_SPINBIT_LOCK_UNLOCK
137c3dcd587Smatt #define MUTEX_SPINBIT_LOCK_UNLOCK(mtx)	__cpu_simple_unlock(&(mtx)->mtx_lock)
138c3dcd587Smatt #endif
139c3dcd587Smatt 
140c3dcd587Smatt #ifndef MUTEX_INITIALIZE_SPIN_IPL
141c3dcd587Smatt #define MUTEX_INITIALIZE_SPIN_IPL(mtx, ipl) \
142c3dcd587Smatt 					((mtx)->mtx_ipl = makeiplcookie((ipl)))
143c3dcd587Smatt #endif
144c3dcd587Smatt 
145c3dcd587Smatt /*
146b07ec3fcSad  * Spin mutex SPL save / restore.
147b07ec3fcSad  */
148b07ec3fcSad 
149b07ec3fcSad #define	MUTEX_SPIN_SPLRAISE(mtx)					\
150b07ec3fcSad do {									\
15128e2c7f0Sskrll 	const int s = splraiseipl(MUTEX_SPIN_IPL(mtx));			\
15228e2c7f0Sskrll 	struct cpu_info * const x__ci = curcpu();			\
15328e2c7f0Sskrll 	const int x__cnt = x__ci->ci_mtx_count--;			\
15456688361Sad 	__insn_barrier();						\
1557742d02dSrmind 	if (x__cnt == 0)						\
15628e2c7f0Sskrll 		x__ci->ci_mtx_oldspl = s;				\
157b07ec3fcSad } while (/* CONSTCOND */ 0)
158b07ec3fcSad 
159b07ec3fcSad #define	MUTEX_SPIN_SPLRESTORE(mtx)					\
160b07ec3fcSad do {									\
16128e2c7f0Sskrll 	struct cpu_info * const x__ci = curcpu();			\
16228e2c7f0Sskrll 	const int s = x__ci->ci_mtx_oldspl;				\
163b07ec3fcSad 	__insn_barrier();						\
1647742d02dSrmind 	if (++(x__ci->ci_mtx_count) == 0)				\
165b07ec3fcSad 		splx(s);						\
166b07ec3fcSad } while (/* CONSTCOND */ 0)
167b07ec3fcSad 
168b07ec3fcSad /*
169874c8f50Sad  * Memory barriers.
170874c8f50Sad  */
171874c8f50Sad #ifdef __HAVE_ATOMIC_AS_MEMBAR
172874c8f50Sad #define	MUTEX_MEMBAR_ENTER()
173874c8f50Sad #else
174874c8f50Sad #define	MUTEX_MEMBAR_ENTER()		membar_enter()
175874c8f50Sad #endif
176874c8f50Sad 
177874c8f50Sad /*
178b07ec3fcSad  * For architectures that provide 'simple' mutexes: they provide a
179b07ec3fcSad  * CAS function that is either MP-safe, or does not need to be MP
180b07ec3fcSad  * safe.  Adaptive mutexes on these architectures do not require an
181b07ec3fcSad  * additional interlock.
182b07ec3fcSad  */
183b07ec3fcSad 
184b07ec3fcSad #ifdef __HAVE_SIMPLE_MUTEXES
185b07ec3fcSad 
186b07ec3fcSad #define	MUTEX_OWNER(owner)						\
187b07ec3fcSad 	(owner & MUTEX_THREAD)
1887c0780d9Sad #define	MUTEX_HAS_WAITERS(mtx)						\
1897c0780d9Sad 	(((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0)
190b07ec3fcSad 
19138d5e341Syamt #define	MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug)				\
1923d895437Sskrll do {									\
193c53ddcffSskrll 	if (!dodebug)							\
194c53ddcffSskrll 		(mtx)->mtx_owner |= MUTEX_BIT_NODEBUG;			\
1953d895437Sskrll } while (/* CONSTCOND */ 0)
196b07ec3fcSad 
19738d5e341Syamt #define	MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl)			\
198b07ec3fcSad do {									\
199b07ec3fcSad 	(mtx)->mtx_owner = MUTEX_BIT_SPIN;				\
200c53ddcffSskrll 	if (!dodebug)							\
201c53ddcffSskrll 		(mtx)->mtx_owner |= MUTEX_BIT_NODEBUG;			\
202c3dcd587Smatt 	MUTEX_INITIALIZE_SPIN_IPL((mtx), (ipl));			\
203c3dcd587Smatt 	MUTEX_SPINBIT_LOCK_INIT((mtx));					\
204b07ec3fcSad } while (/* CONSTCOND */ 0)
205b07ec3fcSad 
206b07ec3fcSad #define	MUTEX_DESTROY(mtx)						\
207b07ec3fcSad do {									\
208b07ec3fcSad 	(mtx)->mtx_owner = MUTEX_THREAD;				\
2093d895437Sskrll } while (/* CONSTCOND */ 0)
210b07ec3fcSad 
21188d36e0eSad #define	MUTEX_SPIN_P(owner)		\
21288d36e0eSad     (((owner) & MUTEX_BIT_SPIN) != 0)
21388d36e0eSad #define	MUTEX_ADAPTIVE_P(owner)		\
21488d36e0eSad     (((owner) & MUTEX_BIT_SPIN) == 0)
215b07ec3fcSad 
21626788c94Sthorpej #ifndef MUTEX_CAS
21726788c94Sthorpej #define	MUTEX_CAS(p, o, n)		\
21826788c94Sthorpej 	(atomic_cas_ulong((volatile unsigned long *)(p), (o), (n)) == (o))
21926788c94Sthorpej #endif /* MUTEX_CAS */
22026788c94Sthorpej 
221c53ddcffSskrll #define	MUTEX_DEBUG_P(mtx)	(((mtx)->mtx_owner & MUTEX_BIT_NODEBUG) == 0)
22238d5e341Syamt #if defined(LOCKDEBUG)
223c53ddcffSskrll #define	MUTEX_OWNED(owner)		(((owner) & ~MUTEX_BIT_NODEBUG) != 0)
224a35d1a8cSmatt #define	MUTEX_INHERITDEBUG(n, o)	(n) |= (o) & MUTEX_BIT_NODEBUG
22538d5e341Syamt #else /* defined(LOCKDEBUG) */
22638d5e341Syamt #define	MUTEX_OWNED(owner)		((owner) != 0)
227a35d1a8cSmatt #define	MUTEX_INHERITDEBUG(n, o)	/* nothing */
22838d5e341Syamt #endif /* defined(LOCKDEBUG) */
229b07ec3fcSad 
230b07ec3fcSad static inline int
MUTEX_ACQUIRE(kmutex_t * mtx,uintptr_t curthread)231b07ec3fcSad MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
232b07ec3fcSad {
233b07ec3fcSad 	int rv;
234a35d1a8cSmatt 	uintptr_t oldown = 0;
235a35d1a8cSmatt 	uintptr_t newown = curthread;
23638d5e341Syamt 
237a35d1a8cSmatt 	MUTEX_INHERITDEBUG(oldown, mtx->mtx_owner);
238a35d1a8cSmatt 	MUTEX_INHERITDEBUG(newown, oldown);
239a35d1a8cSmatt 	rv = MUTEX_CAS(&mtx->mtx_owner, oldown, newown);
240322364beSriastradh 	membar_acquire();
241b07ec3fcSad 	return rv;
242b07ec3fcSad }
243b07ec3fcSad 
244b07ec3fcSad static inline int
MUTEX_SET_WAITERS(kmutex_t * mtx,uintptr_t owner)245b07ec3fcSad MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
246b07ec3fcSad {
247b07ec3fcSad 	int rv;
24890059faeSriastradh 
249b07ec3fcSad 	rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS);
250874c8f50Sad 	MUTEX_MEMBAR_ENTER();
251b07ec3fcSad 	return rv;
252b07ec3fcSad }
253b07ec3fcSad 
254b07ec3fcSad static inline void
MUTEX_RELEASE(kmutex_t * mtx)255b07ec3fcSad MUTEX_RELEASE(kmutex_t *mtx)
256b07ec3fcSad {
257a35d1a8cSmatt 	uintptr_t newown;
25838d5e341Syamt 
259a35d1a8cSmatt 	newown = 0;
260a35d1a8cSmatt 	MUTEX_INHERITDEBUG(newown, mtx->mtx_owner);
261322364beSriastradh 	atomic_store_release(&mtx->mtx_owner, newown);
262b07ec3fcSad }
263b07ec3fcSad #endif	/* __HAVE_SIMPLE_MUTEXES */
264b07ec3fcSad 
265b07ec3fcSad /*
266b07ec3fcSad  * Patch in stubs via strong alias where they are not available.
267b07ec3fcSad  */
268b07ec3fcSad 
269b07ec3fcSad #if defined(LOCKDEBUG)
270b07ec3fcSad #undef	__HAVE_MUTEX_STUBS
271b07ec3fcSad #undef	__HAVE_SPIN_MUTEX_STUBS
272b07ec3fcSad #endif
273b07ec3fcSad 
274b07ec3fcSad #ifndef __HAVE_MUTEX_STUBS
275b07ec3fcSad __strong_alias(mutex_enter,mutex_vector_enter);
276b07ec3fcSad __strong_alias(mutex_exit,mutex_vector_exit);
277b07ec3fcSad #endif
278b07ec3fcSad 
279b07ec3fcSad #ifndef __HAVE_SPIN_MUTEX_STUBS
280b07ec3fcSad __strong_alias(mutex_spin_enter,mutex_vector_enter);
281b07ec3fcSad __strong_alias(mutex_spin_exit,mutex_vector_exit);
282b07ec3fcSad #endif
283b07ec3fcSad 
284617315ebSriastradh static void	mutex_abort(const char *, size_t, volatile const kmutex_t *,
285e7f0067cSchristos 		    const char *);
28600cd510aSozaki-r static void	mutex_dump(const volatile void *, lockop_printer_t);
287617315ebSriastradh static lwp_t	*mutex_owner(wchan_t);
288b07ec3fcSad 
289b07ec3fcSad lockops_t mutex_spin_lockops = {
290a45a6f17Sozaki-r 	.lo_name = "Mutex",
291a45a6f17Sozaki-r 	.lo_type = LOCKOPS_SPIN,
292a45a6f17Sozaki-r 	.lo_dump = mutex_dump,
293b07ec3fcSad };
294b07ec3fcSad 
295b07ec3fcSad lockops_t mutex_adaptive_lockops = {
296a45a6f17Sozaki-r 	.lo_name = "Mutex",
297a45a6f17Sozaki-r 	.lo_type = LOCKOPS_SLEEP,
298a45a6f17Sozaki-r 	.lo_dump = mutex_dump,
299b07ec3fcSad };
300b07ec3fcSad 
301e781af39Syamt syncobj_t mutex_syncobj = {
302f4853583Sriastradh 	.sobj_name	= "mutex",
3038812081aSozaki-r 	.sobj_flag	= SOBJ_SLEEPQ_SORTED,
3046ed72b5fSad 	.sobj_boostpri  = PRI_KERNEL,
3058812081aSozaki-r 	.sobj_unsleep	= turnstile_unsleep,
3068812081aSozaki-r 	.sobj_changepri	= turnstile_changepri,
3078812081aSozaki-r 	.sobj_lendpri	= sleepq_lendpri,
308617315ebSriastradh 	.sobj_owner	= mutex_owner,
309e781af39Syamt };
310e781af39Syamt 
311b07ec3fcSad /*
312b07ec3fcSad  * mutex_dump:
313b07ec3fcSad  *
314b07ec3fcSad  *	Dump the contents of a mutex structure.
315b07ec3fcSad  */
316e67774d0Sozaki-r static void
mutex_dump(const volatile void * cookie,lockop_printer_t pr)31700cd510aSozaki-r mutex_dump(const volatile void *cookie, lockop_printer_t pr)
318b07ec3fcSad {
319e7f0067cSchristos 	const volatile kmutex_t *mtx = cookie;
32088d36e0eSad 	uintptr_t owner = mtx->mtx_owner;
321b07ec3fcSad 
32200cd510aSozaki-r 	pr("owner field  : %#018lx wait/spin: %16d/%d\n",
3237c0780d9Sad 	    (long)MUTEX_OWNER(owner), MUTEX_HAS_WAITERS(mtx),
32488d36e0eSad 	    MUTEX_SPIN_P(owner));
325b07ec3fcSad }
326b07ec3fcSad 
327b07ec3fcSad /*
328b07ec3fcSad  * mutex_abort:
329b07ec3fcSad  *
3306bcf70b5Sad  *	Dump information about an error and panic the system.  This
3316bcf70b5Sad  *	generates a lot of machine code in the DIAGNOSTIC case, so
3326bcf70b5Sad  *	we ask the compiler to not inline it.
333b07ec3fcSad  */
334e67774d0Sozaki-r static void __noinline
mutex_abort(const char * func,size_t line,volatile const kmutex_t * mtx,const char * msg)335617315ebSriastradh mutex_abort(const char *func, size_t line, volatile const kmutex_t *mtx,
336617315ebSriastradh     const char *msg)
337b07ec3fcSad {
338b07ec3fcSad 
33988d36e0eSad 	LOCKDEBUG_ABORT(func, line, mtx, (MUTEX_SPIN_P(mtx->mtx_owner) ?
3409be065fbSchristos 	    &mutex_spin_lockops : &mutex_adaptive_lockops), msg);
341b07ec3fcSad }
342b07ec3fcSad 
343b07ec3fcSad /*
344b07ec3fcSad  * mutex_init:
345b07ec3fcSad  *
346b07ec3fcSad  *	Initialize a mutex for use.  Note that adaptive mutexes are in
347b07ec3fcSad  *	essence spin mutexes that can sleep to avoid deadlock and wasting
348b07ec3fcSad  *	CPU time.  We can't easily provide a type of mutex that always
349b07ec3fcSad  *	sleeps - see comments in mutex_vector_enter() about releasing
350b07ec3fcSad  *	mutexes unlocked.
351b07ec3fcSad  */
352b07ec3fcSad void
_mutex_init(kmutex_t * mtx,kmutex_type_t type,int ipl,uintptr_t return_address)3535e1cf642Sozaki-r _mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl,
3545e1cf642Sozaki-r     uintptr_t return_address)
355b07ec3fcSad {
35676e423e5Sad 	lockops_t *lockops __unused;
35738d5e341Syamt 	bool dodebug;
358b07ec3fcSad 
359b07ec3fcSad 	memset(mtx, 0, sizeof(*mtx));
360b07ec3fcSad 
361f2fb0b38Sad 	if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
362f2fb0b38Sad 	    ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
363f2fb0b38Sad 	    ipl == IPL_SOFTSERIAL) {
36476e423e5Sad 		lockops = (type == MUTEX_NODEBUG ?
36576e423e5Sad 		    NULL : &mutex_adaptive_lockops);
36676e423e5Sad 		dodebug = LOCKDEBUG_ALLOC(mtx, lockops, return_address);
36738d5e341Syamt 		MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug);
36876e423e5Sad 	} else {
36976e423e5Sad 		lockops = (type == MUTEX_NODEBUG ?
37076e423e5Sad 		    NULL : &mutex_spin_lockops);
37176e423e5Sad 		dodebug = LOCKDEBUG_ALLOC(mtx, lockops, return_address);
37238d5e341Syamt 		MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
373b07ec3fcSad 	}
374b07ec3fcSad }
375b07ec3fcSad 
3765e1cf642Sozaki-r void
mutex_init(kmutex_t * mtx,kmutex_type_t type,int ipl)3775e1cf642Sozaki-r mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
3785e1cf642Sozaki-r {
3795e1cf642Sozaki-r 
3805e1cf642Sozaki-r 	_mutex_init(mtx, type, ipl, (uintptr_t)__builtin_return_address(0));
3815e1cf642Sozaki-r }
3825e1cf642Sozaki-r 
383b07ec3fcSad /*
384b07ec3fcSad  * mutex_destroy:
385b07ec3fcSad  *
386b07ec3fcSad  *	Tear down a mutex.
387b07ec3fcSad  */
388b07ec3fcSad void
mutex_destroy(kmutex_t * mtx)389b07ec3fcSad mutex_destroy(kmutex_t *mtx)
390b07ec3fcSad {
39188d36e0eSad 	uintptr_t owner = mtx->mtx_owner;
392b07ec3fcSad 
39388d36e0eSad 	if (MUTEX_ADAPTIVE_P(owner)) {
394faf73726Schs 		MUTEX_ASSERT(mtx, !MUTEX_OWNED(owner));
395faf73726Schs 		MUTEX_ASSERT(mtx, !MUTEX_HAS_WAITERS(mtx));
396b07ec3fcSad 	} else {
397c3dcd587Smatt 		MUTEX_ASSERT(mtx, !MUTEX_SPINBIT_LOCKED_P(mtx));
398b07ec3fcSad 	}
399b07ec3fcSad 
40038d5e341Syamt 	LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx), mtx);
401b07ec3fcSad 	MUTEX_DESTROY(mtx);
402b07ec3fcSad }
403b07ec3fcSad 
404ccfaf6e4Srmind #ifdef MULTIPROCESSOR
405b07ec3fcSad /*
406ccfaf6e4Srmind  * mutex_oncpu:
407b07ec3fcSad  *
408b07ec3fcSad  *	Return true if an adaptive mutex owner is running on a CPU in the
409b07ec3fcSad  *	system.  If the target is waiting on the kernel big lock, then we
41088ab7da9Sad  *	must release it.  This is necessary to avoid deadlock.
411b07ec3fcSad  */
412ccfaf6e4Srmind static bool
mutex_oncpu(uintptr_t owner)413ccfaf6e4Srmind mutex_oncpu(uintptr_t owner)
414b07ec3fcSad {
415b07ec3fcSad 	struct cpu_info *ci;
416ccfaf6e4Srmind 	lwp_t *l;
417b07ec3fcSad 
418ccfaf6e4Srmind 	KASSERT(kpreempt_disabled());
419b07ec3fcSad 
420ccfaf6e4Srmind 	if (!MUTEX_OWNED(owner)) {
421ccfaf6e4Srmind 		return false;
422ccfaf6e4Srmind 	}
423b07ec3fcSad 
424ccfaf6e4Srmind 	/*
425ccfaf6e4Srmind 	 * See lwp_dtor() why dereference of the LWP pointer is safe.
426ccfaf6e4Srmind 	 * We must have kernel preemption disabled for that.
427ccfaf6e4Srmind 	 */
428ccfaf6e4Srmind 	l = (lwp_t *)MUTEX_OWNER(owner);
429ccfaf6e4Srmind 	ci = l->l_cpu;
43088ab7da9Sad 
431ccfaf6e4Srmind 	if (ci && ci->ci_curlwp == l) {
43288ab7da9Sad 		/* Target is running; do we need to block? */
43357b6c53cSriastradh 		return (atomic_load_relaxed(&ci->ci_biglock_wanted) != l);
434ccfaf6e4Srmind 	}
435ccfaf6e4Srmind 
436ccfaf6e4Srmind 	/* Not running.  It may be safe to block now. */
437ccfaf6e4Srmind 	return false;
438b07ec3fcSad }
43988ab7da9Sad #endif	/* MULTIPROCESSOR */
440b07ec3fcSad 
441b07ec3fcSad /*
442b07ec3fcSad  * mutex_vector_enter:
443b07ec3fcSad  *
4445a84a46bSrmind  *	Support routine for mutex_enter() that must handle all cases.  In
445b07ec3fcSad  *	the LOCKDEBUG case, mutex_enter() is always aliased here, even if
4464b3129d5Sprlw1  *	fast-path stubs are available.  If a mutex_spin_enter() stub is
447b07ec3fcSad  *	not available, then it is also aliased directly here.
448b07ec3fcSad  */
449b07ec3fcSad void
mutex_vector_enter(kmutex_t * mtx)450b07ec3fcSad mutex_vector_enter(kmutex_t *mtx)
451b07ec3fcSad {
452b07ec3fcSad 	uintptr_t owner, curthread;
453b07ec3fcSad 	turnstile_t *ts;
454b07ec3fcSad #ifdef MULTIPROCESSOR
455b07ec3fcSad 	u_int count;
456b07ec3fcSad #endif
457b07ec3fcSad 	LOCKSTAT_COUNTER(spincnt);
458b07ec3fcSad 	LOCKSTAT_COUNTER(slpcnt);
459b07ec3fcSad 	LOCKSTAT_TIMER(spintime);
460b07ec3fcSad 	LOCKSTAT_TIMER(slptime);
461b07ec3fcSad 	LOCKSTAT_FLAG(lsflag);
462b07ec3fcSad 
463b07ec3fcSad 	/*
464b07ec3fcSad 	 * Handle spin mutexes.
465b07ec3fcSad 	 */
466cd30436cSad 	KPREEMPT_DISABLE(curlwp);
46788d36e0eSad 	owner = mtx->mtx_owner;
46888d36e0eSad 	if (MUTEX_SPIN_P(owner)) {
469b07ec3fcSad #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR)
470b07ec3fcSad 		u_int spins = 0;
471b07ec3fcSad #endif
472cd30436cSad 		KPREEMPT_ENABLE(curlwp);
473b07ec3fcSad 		MUTEX_SPIN_SPLRAISE(mtx);
474b07ec3fcSad 		MUTEX_WANTLOCK(mtx);
475b07ec3fcSad #ifdef FULL
476c3dcd587Smatt 		if (MUTEX_SPINBIT_LOCK_TRY(mtx)) {
477b07ec3fcSad 			MUTEX_LOCKED(mtx);
478b07ec3fcSad 			return;
479b07ec3fcSad 		}
480b07ec3fcSad #if !defined(MULTIPROCESSOR)
481b07ec3fcSad 		MUTEX_ABORT(mtx, "locking against myself");
482b07ec3fcSad #else /* !MULTIPROCESSOR */
483b07ec3fcSad 
484b07ec3fcSad 		LOCKSTAT_ENTER(lsflag);
485b07ec3fcSad 		LOCKSTAT_START_TIMER(lsflag, spintime);
486b07ec3fcSad 		count = SPINLOCK_BACKOFF_MIN;
487b07ec3fcSad 
488b07ec3fcSad 		/*
489b07ec3fcSad 		 * Spin testing the lock word and do exponential backoff
490b07ec3fcSad 		 * to reduce cache line ping-ponging between CPUs.
491b07ec3fcSad 		 */
492b07ec3fcSad 		do {
493c3dcd587Smatt 			while (MUTEX_SPINBIT_LOCKED_P(mtx)) {
494ab671aa0Sthorpej 				SPINLOCK_SPIN_HOOK;
495b07ec3fcSad 				SPINLOCK_BACKOFF(count);
496b07ec3fcSad #ifdef LOCKDEBUG
497b07ec3fcSad 				if (SPINLOCK_SPINOUT(spins))
498b07ec3fcSad 					MUTEX_ABORT(mtx, "spinout");
499b07ec3fcSad #endif	/* LOCKDEBUG */
500b07ec3fcSad 			}
501c3dcd587Smatt 		} while (!MUTEX_SPINBIT_LOCK_TRY(mtx));
502b07ec3fcSad 
503b07ec3fcSad 		if (count != SPINLOCK_BACKOFF_MIN) {
504b07ec3fcSad 			LOCKSTAT_STOP_TIMER(lsflag, spintime);
505b07ec3fcSad 			LOCKSTAT_EVENT(lsflag, mtx,
506b07ec3fcSad 			    LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
507b07ec3fcSad 		}
508b07ec3fcSad 		LOCKSTAT_EXIT(lsflag);
509b07ec3fcSad #endif	/* !MULTIPROCESSOR */
510b07ec3fcSad #endif	/* FULL */
511b07ec3fcSad 		MUTEX_LOCKED(mtx);
512b07ec3fcSad 		return;
513b07ec3fcSad 	}
514b07ec3fcSad 
515b07ec3fcSad 	curthread = (uintptr_t)curlwp;
516b07ec3fcSad 
51788d36e0eSad 	MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(owner));
518b07ec3fcSad 	MUTEX_ASSERT(mtx, curthread != 0);
5199a52f68cSozaki-r 	MUTEX_ASSERT(mtx, !cpu_intr_p());
520b07ec3fcSad 	MUTEX_WANTLOCK(mtx);
521b07ec3fcSad 
522c92291afSozaki-r 	if (__predict_true(panicstr == NULL)) {
5238aed7a99Sozaki-r 		KDASSERT(pserialize_not_in_read_section());
524b07ec3fcSad 		LOCKDEBUG_BARRIER(&kernel_lock, 1);
525b07ec3fcSad 	}
526b07ec3fcSad 
527b07ec3fcSad 	LOCKSTAT_ENTER(lsflag);
528b07ec3fcSad 
529b07ec3fcSad 	/*
530b07ec3fcSad 	 * Adaptive mutex; spin trying to acquire the mutex.  If we
531b07ec3fcSad 	 * determine that the owner is not running on a processor,
532b07ec3fcSad 	 * then we stop spinning, and sleep instead.
533b07ec3fcSad 	 */
53488d36e0eSad 	for (;;) {
535b07ec3fcSad 		if (!MUTEX_OWNED(owner)) {
536b07ec3fcSad 			/*
537b07ec3fcSad 			 * Mutex owner clear could mean two things:
538b07ec3fcSad 			 *
539b07ec3fcSad 			 *	* The mutex has been released.
540b07ec3fcSad 			 *	* The owner field hasn't been set yet.
541b07ec3fcSad 			 *
542b07ec3fcSad 			 * Try to acquire it again.  If that fails,
543b07ec3fcSad 			 * we'll just loop again.
544b07ec3fcSad 			 */
545b07ec3fcSad 			if (MUTEX_ACQUIRE(mtx, curthread))
546b07ec3fcSad 				break;
547baba2744Sad 			owner = mtx->mtx_owner;
548b07ec3fcSad 			continue;
549b07ec3fcSad 		}
550ccfaf6e4Srmind 		if (__predict_false(MUTEX_OWNER(owner) == curthread)) {
551b07ec3fcSad 			MUTEX_ABORT(mtx, "locking against myself");
552ccfaf6e4Srmind 		}
553b07ec3fcSad #ifdef MULTIPROCESSOR
554b07ec3fcSad 		/*
555b07ec3fcSad 		 * Check to see if the owner is running on a processor.
556b07ec3fcSad 		 * If so, then we should just spin, as the owner will
557cd30436cSad 		 * likely release the lock very soon.
558b07ec3fcSad 		 */
559ccfaf6e4Srmind 		if (mutex_oncpu(owner)) {
560b07ec3fcSad 			LOCKSTAT_START_TIMER(lsflag, spintime);
561b07ec3fcSad 			count = SPINLOCK_BACKOFF_MIN;
562ccfaf6e4Srmind 			do {
563251f7169Srmind 				KPREEMPT_ENABLE(curlwp);
564baba2744Sad 				SPINLOCK_BACKOFF(count);
565251f7169Srmind 				KPREEMPT_DISABLE(curlwp);
566b07ec3fcSad 				owner = mtx->mtx_owner;
567ccfaf6e4Srmind 			} while (mutex_oncpu(owner));
568b07ec3fcSad 			LOCKSTAT_STOP_TIMER(lsflag, spintime);
569b07ec3fcSad 			LOCKSTAT_COUNT(spincnt, 1);
570b07ec3fcSad 			if (!MUTEX_OWNED(owner))
571b07ec3fcSad 				continue;
572b07ec3fcSad 		}
573b07ec3fcSad #endif
574b07ec3fcSad 
575b07ec3fcSad 		ts = turnstile_lookup(mtx);
576b07ec3fcSad 
577b07ec3fcSad 		/*
578b07ec3fcSad 		 * Once we have the turnstile chain interlock, mark the
5792d94c39cSskrll 		 * mutex as having waiters.  If that fails, spin again:
580b07ec3fcSad 		 * chances are that the mutex has been released.
581b07ec3fcSad 		 */
582b07ec3fcSad 		if (!MUTEX_SET_WAITERS(mtx, owner)) {
583b07ec3fcSad 			turnstile_exit(mtx);
584baba2744Sad 			owner = mtx->mtx_owner;
585b07ec3fcSad 			continue;
586b07ec3fcSad 		}
587b07ec3fcSad 
588b07ec3fcSad #ifdef MULTIPROCESSOR
589b07ec3fcSad 		/*
590b07ec3fcSad 		 * mutex_exit() is permitted to release the mutex without
591b07ec3fcSad 		 * any interlocking instructions, and the following can
592b07ec3fcSad 		 * occur as a result:
593b07ec3fcSad 		 *
594b07ec3fcSad 		 *  CPU 1: MUTEX_SET_WAITERS()      CPU2: mutex_exit()
595b07ec3fcSad 		 * ---------------------------- ----------------------------
59690059faeSriastradh 		 *		..		load mtx->mtx_owner
59790059faeSriastradh 		 *		..		see has-waiters bit clear
59890059faeSriastradh 		 *	set has-waiters bit  	           ..
59990059faeSriastradh 		 *		..		store mtx->mtx_owner := 0
600b07ec3fcSad 		 *	  return success
601b07ec3fcSad 		 *
602ccfaf6e4Srmind 		 * There is another race that can occur: a third CPU could
603b07ec3fcSad 		 * acquire the mutex as soon as it is released.  Since
604b07ec3fcSad 		 * adaptive mutexes are primarily spin mutexes, this is not
605b07ec3fcSad 		 * something that we need to worry about too much.  What we
606b07ec3fcSad 		 * do need to ensure is that the waiters bit gets set.
607b07ec3fcSad 		 *
608b07ec3fcSad 		 * To allow the unlocked release, we need to make some
609b07ec3fcSad 		 * assumptions here:
610b07ec3fcSad 		 *
611b07ec3fcSad 		 * o Release is the only non-atomic/unlocked operation
612b07ec3fcSad 		 *   that can be performed on the mutex.  (It must still
613b07ec3fcSad 		 *   be atomic on the local CPU, e.g. in case interrupted
614b07ec3fcSad 		 *   or preempted).
615b07ec3fcSad 		 *
61690059faeSriastradh 		 * o At any given time on each mutex, MUTEX_SET_WAITERS()
61790059faeSriastradh 		 *   can only ever be in progress on one CPU in the
61890059faeSriastradh 		 *   system - guaranteed by the turnstile chain lock.
619b07ec3fcSad 		 *
620b07ec3fcSad 		 * o No other operations other than MUTEX_SET_WAITERS()
621b07ec3fcSad 		 *   and release can modify a mutex with a non-zero
622b07ec3fcSad 		 *   owner field.
623b07ec3fcSad 		 *
624b07ec3fcSad 		 * o If the holding LWP switches away, it posts a store
625b07ec3fcSad 		 *   fence before changing curlwp, ensuring that any
626b07ec3fcSad 		 *   overwrite of the mutex waiters flag by mutex_exit()
627b07ec3fcSad 		 *   completes before the modification of curlwp becomes
628b07ec3fcSad 		 *   visible to this CPU.
629b07ec3fcSad 		 *
630b18dbbf9Sskrll 		 * o cpu_switchto() posts a store fence after setting curlwp
631b07ec3fcSad 		 *   and before resuming execution of an LWP.
632b07ec3fcSad 		 *
633b07ec3fcSad 		 * o _kernel_lock() posts a store fence before setting
634b07ec3fcSad 		 *   curcpu()->ci_biglock_wanted, and after clearing it.
635b07ec3fcSad 		 *   This ensures that any overwrite of the mutex waiters
636b07ec3fcSad 		 *   flag by mutex_exit() completes before the modification
637b07ec3fcSad 		 *   of ci_biglock_wanted becomes visible.
638b07ec3fcSad 		 *
63990059faeSriastradh 		 * After MUTEX_SET_WAITERS() succeeds, simultaneously
64090059faeSriastradh 		 * confirming that the same LWP still holds the mutex
64190059faeSriastradh 		 * since we took the turnstile lock and notifying it that
64290059faeSriastradh 		 * we're waiting, we check the lock holder's status again.
64390059faeSriastradh 		 * Some of the possible outcomes (not an exhaustive list;
64490059faeSriastradh 		 * XXX this should be made exhaustive):
645b07ec3fcSad 		 *
646ccfaf6e4Srmind 		 * 1. The on-CPU check returns true: the holding LWP is
647b07ec3fcSad 		 *    running again.  The lock may be released soon and
648b07ec3fcSad 		 *    we should spin.  Importantly, we can't trust the
649b07ec3fcSad 		 *    value of the waiters flag.
650b07ec3fcSad 		 *
651ccfaf6e4Srmind 		 * 2. The on-CPU check returns false: the holding LWP is
652d1de8f5eSyamt 		 *    not running.  We now have the opportunity to check
653b07ec3fcSad 		 *    if mutex_exit() has blatted the modifications made
654b07ec3fcSad 		 *    by MUTEX_SET_WAITERS().
655b07ec3fcSad 		 *
656ccfaf6e4Srmind 		 * 3. The on-CPU check returns false: the holding LWP may
657b07ec3fcSad 		 *    or may not be running.  It has context switched at
658b07ec3fcSad 		 *    some point during our check.  Again, we have the
659b07ec3fcSad 		 *    chance to see if the waiters bit is still set or
660b07ec3fcSad 		 *    has been overwritten.
661b07ec3fcSad 		 *
662ccfaf6e4Srmind 		 * 4. The on-CPU check returns false: the holding LWP is
663b07ec3fcSad 		 *    running on a CPU, but wants the big lock.  It's OK
664b07ec3fcSad 		 *    to check the waiters field in this case.
665b07ec3fcSad 		 *
666b07ec3fcSad 		 * 5. The has-waiters check fails: the mutex has been
667b07ec3fcSad 		 *    released, the waiters flag cleared and another LWP
668b07ec3fcSad 		 *    now owns the mutex.
669b07ec3fcSad 		 *
670b07ec3fcSad 		 * 6. The has-waiters check fails: the mutex has been
671b07ec3fcSad 		 *    released.
672b07ec3fcSad 		 *
673b07ec3fcSad 		 * If the waiters bit is not set it's unsafe to go asleep,
674b07ec3fcSad 		 * as we might never be awoken.
675b07ec3fcSad 		 */
67688d36e0eSad 		if (mutex_oncpu(owner)) {
677b07ec3fcSad 			turnstile_exit(mtx);
678baba2744Sad 			owner = mtx->mtx_owner;
679b07ec3fcSad 			continue;
680b07ec3fcSad 		}
68188d36e0eSad 		membar_consumer();
6827c0780d9Sad 		if (!MUTEX_HAS_WAITERS(mtx)) {
68388d36e0eSad 			turnstile_exit(mtx);
6847c0780d9Sad 			owner = mtx->mtx_owner;
68588d36e0eSad 			continue;
68688d36e0eSad 		}
687b07ec3fcSad #endif	/* MULTIPROCESSOR */
688b07ec3fcSad 
689b07ec3fcSad 		LOCKSTAT_START_TIMER(lsflag, slptime);
690b07ec3fcSad 
691e781af39Syamt 		turnstile_block(ts, TS_WRITER_Q, mtx, &mutex_syncobj);
692b07ec3fcSad 
693b07ec3fcSad 		LOCKSTAT_STOP_TIMER(lsflag, slptime);
694b07ec3fcSad 		LOCKSTAT_COUNT(slpcnt, 1);
695baba2744Sad 
696baba2744Sad 		owner = mtx->mtx_owner;
697b07ec3fcSad 	}
698ccfaf6e4Srmind 	KPREEMPT_ENABLE(curlwp);
699b07ec3fcSad 
700b07ec3fcSad 	LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1,
701b07ec3fcSad 	    slpcnt, slptime);
702b07ec3fcSad 	LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN,
703b07ec3fcSad 	    spincnt, spintime);
704b07ec3fcSad 	LOCKSTAT_EXIT(lsflag);
705b07ec3fcSad 
706b07ec3fcSad 	MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
707b07ec3fcSad 	MUTEX_LOCKED(mtx);
708b07ec3fcSad }
709b07ec3fcSad 
710b07ec3fcSad /*
711b07ec3fcSad  * mutex_vector_exit:
712b07ec3fcSad  *
713b07ec3fcSad  *	Support routine for mutex_exit() that handles all cases.
714b07ec3fcSad  */
715b07ec3fcSad void
mutex_vector_exit(kmutex_t * mtx)716b07ec3fcSad mutex_vector_exit(kmutex_t *mtx)
717b07ec3fcSad {
718b07ec3fcSad 	turnstile_t *ts;
719b07ec3fcSad 	uintptr_t curthread;
720b07ec3fcSad 
72188d36e0eSad 	if (MUTEX_SPIN_P(mtx->mtx_owner)) {
722b07ec3fcSad #ifdef FULL
723c3dcd587Smatt 		if (__predict_false(!MUTEX_SPINBIT_LOCKED_P(mtx))) {
724b07ec3fcSad 			MUTEX_ABORT(mtx, "exiting unheld spin mutex");
72558420c12Sad 		}
726b07ec3fcSad 		MUTEX_UNLOCKED(mtx);
727c3dcd587Smatt 		MUTEX_SPINBIT_LOCK_UNLOCK(mtx);
728b07ec3fcSad #endif
729b07ec3fcSad 		MUTEX_SPIN_SPLRESTORE(mtx);
730b07ec3fcSad 		return;
731b07ec3fcSad 	}
732b07ec3fcSad 
733ab436b50Sad #ifndef __HAVE_MUTEX_STUBS
734697d97f8Sad 	/*
735697d97f8Sad 	 * On some architectures without mutex stubs, we can enter here to
736697d97f8Sad 	 * release mutexes before interrupts and whatnot are up and running.
737697d97f8Sad 	 * We need this hack to keep them sweet.
738697d97f8Sad 	 */
739ab436b50Sad 	if (__predict_false(cold)) {
740ab436b50Sad 		MUTEX_UNLOCKED(mtx);
741ab436b50Sad 		MUTEX_RELEASE(mtx);
742ab436b50Sad 		return;
743ab436b50Sad 	}
744ab436b50Sad #endif
745ab436b50Sad 
746b07ec3fcSad 	curthread = (uintptr_t)curlwp;
747b07ec3fcSad 	MUTEX_DASSERT(mtx, curthread != 0);
748b07ec3fcSad 	MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
749b07ec3fcSad 	MUTEX_UNLOCKED(mtx);
750923e17fdSmrg #if !defined(LOCKDEBUG)
751923e17fdSmrg 	__USE(curthread);
752923e17fdSmrg #endif
753b07ec3fcSad 
75488ab7da9Sad #ifdef LOCKDEBUG
75588ab7da9Sad 	/*
75688ab7da9Sad 	 * Avoid having to take the turnstile chain lock every time
75788ab7da9Sad 	 * around.  Raise the priority level to splhigh() in order
75888ab7da9Sad 	 * to disable preemption and so make the following atomic.
7598479531bSad 	 * This also blocks out soft interrupts that could set the
7608479531bSad 	 * waiters bit.
76188ab7da9Sad 	 */
76288ab7da9Sad 	{
76388ab7da9Sad 		int s = splhigh();
7647c0780d9Sad 		if (!MUTEX_HAS_WAITERS(mtx)) {
76588ab7da9Sad 			MUTEX_RELEASE(mtx);
76688ab7da9Sad 			splx(s);
76788ab7da9Sad 			return;
76888ab7da9Sad 		}
76988ab7da9Sad 		splx(s);
77088ab7da9Sad 	}
77188ab7da9Sad #endif
77288ab7da9Sad 
773b07ec3fcSad 	/*
774b07ec3fcSad 	 * Get this lock's turnstile.  This gets the interlock on
775b07ec3fcSad 	 * the sleep queue.  Once we have that, we can clear the
776b07ec3fcSad 	 * lock.  If there was no turnstile for the lock, there
777b07ec3fcSad 	 * were no waiters remaining.
778b07ec3fcSad 	 */
779b07ec3fcSad 	ts = turnstile_lookup(mtx);
780b07ec3fcSad 
781b07ec3fcSad 	if (ts == NULL) {
782b07ec3fcSad 		MUTEX_RELEASE(mtx);
783b07ec3fcSad 		turnstile_exit(mtx);
784b07ec3fcSad 	} else {
785b07ec3fcSad 		MUTEX_RELEASE(mtx);
786b07ec3fcSad 		turnstile_wakeup(ts, TS_WRITER_Q,
787b07ec3fcSad 		    TS_WAITERS(ts, TS_WRITER_Q), NULL);
788b07ec3fcSad 	}
789b07ec3fcSad }
790b07ec3fcSad 
79110a11a26Sad #ifndef __HAVE_SIMPLE_MUTEXES
79210a11a26Sad /*
79310a11a26Sad  * mutex_wakeup:
79410a11a26Sad  *
79510a11a26Sad  *	Support routine for mutex_exit() that wakes up all waiters.
79610a11a26Sad  *	We assume that the mutex has been released, but it need not
79710a11a26Sad  *	be.
79810a11a26Sad  */
79910a11a26Sad void
mutex_wakeup(kmutex_t * mtx)80010a11a26Sad mutex_wakeup(kmutex_t *mtx)
80110a11a26Sad {
80210a11a26Sad 	turnstile_t *ts;
80310a11a26Sad 
80410a11a26Sad 	ts = turnstile_lookup(mtx);
80510a11a26Sad 	if (ts == NULL) {
80610a11a26Sad 		turnstile_exit(mtx);
80710a11a26Sad 		return;
80810a11a26Sad 	}
80910a11a26Sad 	MUTEX_CLEAR_WAITERS(mtx);
81010a11a26Sad 	turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL);
81110a11a26Sad }
81210a11a26Sad #endif	/* !__HAVE_SIMPLE_MUTEXES */
81310a11a26Sad 
814b07ec3fcSad /*
815b07ec3fcSad  * mutex_owned:
816b07ec3fcSad  *
8176bcf70b5Sad  *	Return true if the current LWP (adaptive) or CPU (spin)
8186bcf70b5Sad  *	holds the mutex.
819b07ec3fcSad  */
820b07ec3fcSad int
mutex_owned(const kmutex_t * mtx)8219d349e2aSchristos mutex_owned(const kmutex_t *mtx)
822b07ec3fcSad {
823b07ec3fcSad 
824ebca8ee8Sad 	if (mtx == NULL)
825ebca8ee8Sad 		return 0;
82688d36e0eSad 	if (MUTEX_ADAPTIVE_P(mtx->mtx_owner))
827b07ec3fcSad 		return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp;
828b07ec3fcSad #ifdef FULL
829c3dcd587Smatt 	return MUTEX_SPINBIT_LOCKED_P(mtx);
830b07ec3fcSad #else
831b07ec3fcSad 	return 1;
832b07ec3fcSad #endif
833b07ec3fcSad }
834b07ec3fcSad 
835b07ec3fcSad /*
836b07ec3fcSad  * mutex_owner:
837b07ec3fcSad  *
838cbe547e6Sad  *	Return the current owner of an adaptive mutex.  Used for
839cbe547e6Sad  *	priority inheritance.
840b07ec3fcSad  */
841617315ebSriastradh static lwp_t *
mutex_owner(wchan_t wchan)842617315ebSriastradh mutex_owner(wchan_t wchan)
843e781af39Syamt {
844617315ebSriastradh 	volatile const kmutex_t *mtx = wchan;
845e781af39Syamt 
84688d36e0eSad 	MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx->mtx_owner));
847cbe547e6Sad 	return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner);
848e781af39Syamt }
849e781af39Syamt 
850b07ec3fcSad /*
85148e395b1Spgoyette  * mutex_ownable:
85248e395b1Spgoyette  *
85348e395b1Spgoyette  *	When compiled with DEBUG and LOCKDEBUG defined, ensure that
85448e395b1Spgoyette  *	the mutex is available.  We cannot use !mutex_owned() since
85548e395b1Spgoyette  *	that won't work correctly for spin mutexes.
85648e395b1Spgoyette  */
85748e395b1Spgoyette int
mutex_ownable(const kmutex_t * mtx)8589d349e2aSchristos mutex_ownable(const kmutex_t *mtx)
85948e395b1Spgoyette {
86048e395b1Spgoyette 
86148e395b1Spgoyette #ifdef LOCKDEBUG
86248e395b1Spgoyette 	MUTEX_TESTLOCK(mtx);
86348e395b1Spgoyette #endif
86448e395b1Spgoyette 	return 1;
86548e395b1Spgoyette }
86648e395b1Spgoyette 
86748e395b1Spgoyette /*
868b07ec3fcSad  * mutex_tryenter:
869b07ec3fcSad  *
870b07ec3fcSad  *	Try to acquire the mutex; return non-zero if we did.
871b07ec3fcSad  */
872b07ec3fcSad int
mutex_tryenter(kmutex_t * mtx)873b07ec3fcSad mutex_tryenter(kmutex_t *mtx)
874b07ec3fcSad {
875b07ec3fcSad 	uintptr_t curthread;
876b07ec3fcSad 
877b07ec3fcSad 	/*
878b07ec3fcSad 	 * Handle spin mutexes.
879b07ec3fcSad 	 */
88088d36e0eSad 	if (MUTEX_SPIN_P(mtx->mtx_owner)) {
881b07ec3fcSad 		MUTEX_SPIN_SPLRAISE(mtx);
882b07ec3fcSad #ifdef FULL
883c3dcd587Smatt 		if (MUTEX_SPINBIT_LOCK_TRY(mtx)) {
88410a11a26Sad 			MUTEX_WANTLOCK(mtx);
885b07ec3fcSad 			MUTEX_LOCKED(mtx);
886b07ec3fcSad 			return 1;
887b07ec3fcSad 		}
888b07ec3fcSad 		MUTEX_SPIN_SPLRESTORE(mtx);
889b07ec3fcSad #else
89010a11a26Sad 		MUTEX_WANTLOCK(mtx);
891b07ec3fcSad 		MUTEX_LOCKED(mtx);
892b07ec3fcSad 		return 1;
893b07ec3fcSad #endif
894b07ec3fcSad 	} else {
895b07ec3fcSad 		curthread = (uintptr_t)curlwp;
896b07ec3fcSad 		MUTEX_ASSERT(mtx, curthread != 0);
897b07ec3fcSad 		if (MUTEX_ACQUIRE(mtx, curthread)) {
89810a11a26Sad 			MUTEX_WANTLOCK(mtx);
899b07ec3fcSad 			MUTEX_LOCKED(mtx);
900b07ec3fcSad 			MUTEX_DASSERT(mtx,
901b07ec3fcSad 			    MUTEX_OWNER(mtx->mtx_owner) == curthread);
902b07ec3fcSad 			return 1;
903b07ec3fcSad 		}
904b07ec3fcSad 	}
905b07ec3fcSad 
906b07ec3fcSad 	return 0;
907b07ec3fcSad }
908b07ec3fcSad 
909b07ec3fcSad #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
910b07ec3fcSad /*
911b07ec3fcSad  * mutex_spin_retry:
912b07ec3fcSad  *
913b07ec3fcSad  *	Support routine for mutex_spin_enter().  Assumes that the caller
914b07ec3fcSad  *	has already raised the SPL, and adjusted counters.
915b07ec3fcSad  */
916b07ec3fcSad void
mutex_spin_retry(kmutex_t * mtx)917b07ec3fcSad mutex_spin_retry(kmutex_t *mtx)
918b07ec3fcSad {
919b07ec3fcSad #ifdef MULTIPROCESSOR
920b07ec3fcSad 	u_int count;
921b07ec3fcSad 	LOCKSTAT_TIMER(spintime);
922b07ec3fcSad 	LOCKSTAT_FLAG(lsflag);
923b07ec3fcSad #ifdef LOCKDEBUG
924b07ec3fcSad 	u_int spins = 0;
925b07ec3fcSad #endif	/* LOCKDEBUG */
926b07ec3fcSad 
927b07ec3fcSad 	MUTEX_WANTLOCK(mtx);
928b07ec3fcSad 
929b07ec3fcSad 	LOCKSTAT_ENTER(lsflag);
930b07ec3fcSad 	LOCKSTAT_START_TIMER(lsflag, spintime);
931b07ec3fcSad 	count = SPINLOCK_BACKOFF_MIN;
932b07ec3fcSad 
933b07ec3fcSad 	/*
934b07ec3fcSad 	 * Spin testing the lock word and do exponential backoff
935b07ec3fcSad 	 * to reduce cache line ping-ponging between CPUs.
936b07ec3fcSad 	 */
937b07ec3fcSad 	do {
938c3dcd587Smatt 		while (MUTEX_SPINBIT_LOCKED_P(mtx)) {
939b07ec3fcSad 			SPINLOCK_BACKOFF(count);
940b07ec3fcSad #ifdef LOCKDEBUG
941b07ec3fcSad 			if (SPINLOCK_SPINOUT(spins))
942b07ec3fcSad 				MUTEX_ABORT(mtx, "spinout");
943b07ec3fcSad #endif	/* LOCKDEBUG */
944b07ec3fcSad 		}
945c3dcd587Smatt 	} while (!MUTEX_SPINBIT_LOCK_TRY(mtx));
946b07ec3fcSad 
947b07ec3fcSad 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
948b07ec3fcSad 	LOCKSTAT_EVENT(lsflag, mtx, LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
949b07ec3fcSad 	LOCKSTAT_EXIT(lsflag);
950b07ec3fcSad 
951b07ec3fcSad 	MUTEX_LOCKED(mtx);
952b07ec3fcSad #else	/* MULTIPROCESSOR */
953b07ec3fcSad 	MUTEX_ABORT(mtx, "locking against myself");
954b07ec3fcSad #endif	/* MULTIPROCESSOR */
955b07ec3fcSad }
956b07ec3fcSad #endif	/* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */
957