1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
5 * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice(s), this list of conditions and the following disclaimer as
13 *    the first lines of this file unmodified other than the possible
14 *    addition of one or more copyright notices.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice(s), this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 * DAMAGE.
30 *
31 * $FreeBSD$
32 */
33
34#ifndef	_SYS_SX_H_
35#define	_SYS_SX_H_
36
37#include <sys/_lock.h>
38#include <sys/_sx.h>
39
40#ifdef	_KERNEL
41#include <sys/pcpu.h>
42#include <sys/lock_profile.h>
43#include <sys/lockstat.h>
44#include <machine/atomic.h>
45#endif
46
47/*
48 * In general, the sx locks and rwlocks use very similar algorithms.
49 * The main difference in the implementations is how threads are
50 * blocked when a lock is unavailable.  For this, sx locks use sleep
51 * queues which do not support priority propagation, and rwlocks use
52 * turnstiles which do.
53 *
54 * The sx_lock field consists of several fields.  The low bit
55 * indicates if the lock is locked with a shared or exclusive lock.  A
56 * value of 0 indicates an exclusive lock, and a value of 1 indicates
57 * a shared lock.  Bit 1 is a boolean indicating if there are any
58 * threads waiting for a shared lock.  Bit 2 is a boolean indicating
59 * if there are any threads waiting for an exclusive lock.  Bit 3 is a
60 * boolean indicating if an exclusive lock is recursively held.  The
61 * rest of the variable's definition is dependent on the value of the
62 * first bit.  For an exclusive lock, it is a pointer to the thread
63 * holding the lock, similar to the mtx_lock field of mutexes.  For
64 * shared locks, it is a count of read locks that are held.
65 *
66 * When the lock is not locked by any thread, it is encoded as a
67 * shared lock with zero waiters.
68 */
69
70#define	SX_LOCK_SHARED			0x01
71#define	SX_LOCK_SHARED_WAITERS		0x02
72#define	SX_LOCK_EXCLUSIVE_WAITERS	0x04
73#define	SX_LOCK_WRITE_SPINNER		0x08
74#define	SX_LOCK_RECURSED		0x10
75#define	SX_LOCK_FLAGMASK						\
76	(SX_LOCK_SHARED | SX_LOCK_SHARED_WAITERS |			\
77	SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_RECURSED | SX_LOCK_WRITE_SPINNER)
78#define	SX_LOCK_WAITERS			(SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)
79
80#define	SX_OWNER(x)			((x) & ~SX_LOCK_FLAGMASK)
81#define	SX_SHARERS_SHIFT		5
82#define	SX_SHARERS(x)			(SX_OWNER(x) >> SX_SHARERS_SHIFT)
83#define	SX_SHARERS_LOCK(x)						\
84	((x) << SX_SHARERS_SHIFT | SX_LOCK_SHARED)
85#define	SX_ONE_SHARER			(1 << SX_SHARERS_SHIFT)
86
87#define	SX_LOCK_UNLOCKED		SX_SHARERS_LOCK(0)
88#define	SX_LOCK_DESTROYED						\
89	(SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)
90
91#ifdef _KERNEL
92
93#define	sx_recurse	lock_object.lo_data
94
95#define	SX_READ_VALUE(sx)	((sx)->sx_lock)
96
97#define	lv_sx_owner(v) \
98	((v & SX_LOCK_SHARED) ? NULL : (struct thread *)SX_OWNER(v))
99
100/*
101 * Function prototipes.  Routines that start with an underscore are not part
102 * of the public interface and are wrappered with a macro.
103 */
104void	sx_sysinit(void *arg);
105#define	sx_init(sx, desc)	sx_init_flags((sx), (desc), 0)
106void	sx_init_flags(struct sx *sx, const char *description, int opts);
107void	sx_destroy(struct sx *sx);
108int	sx_try_slock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF);
109int	sx_try_slock_(struct sx *sx, const char *file, int line);
110int	sx_try_xlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF);
111int	sx_try_xlock_(struct sx *sx, const char *file, int line);
112int	sx_try_upgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF);
113int	sx_try_upgrade_(struct sx *sx, const char *file, int line);
114void	sx_downgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF);
115void	sx_downgrade_(struct sx *sx, const char *file, int line);
116int	_sx_slock_int(struct sx *sx, int opts LOCK_FILE_LINE_ARG_DEF);
117int	_sx_slock(struct sx *sx, int opts, const char *file, int line);
118int	_sx_xlock(struct sx *sx, int opts, const char *file, int line);
119void	_sx_sunlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF);
120void	_sx_sunlock(struct sx *sx, const char *file, int line);
121void	_sx_xunlock(struct sx *sx, const char *file, int line);
122int	_sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF);
123void	_sx_xunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF);
124#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
125void	_sx_assert(const struct sx *sx, int what, const char *file, int line);
126#endif
127#ifdef DDB
128int	sx_chain(struct thread *td, struct thread **ownerp);
129#endif
130
131struct sx_args {
132	struct sx 	*sa_sx;
133	const char	*sa_desc;
134	int		sa_flags;
135};
136
137#define	SX_SYSINIT_FLAGS(name, sxa, desc, flags)			\
138	static struct sx_args name##_args = {				\
139		(sxa),							\
140		(desc),							\
141		(flags)							\
142	};								\
143	SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
144	    sx_sysinit, &name##_args);					\
145	SYSUNINIT(name##_sx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
146	    sx_destroy, (sxa))
147
148#define	SX_SYSINIT(name, sxa, desc)	SX_SYSINIT_FLAGS(name, sxa, desc, 0)
149
150/*
151 * Full lock operations that are suitable to be inlined in non-debug kernels.
152 * If the lock can't be acquired or released trivially then the work is
153 * deferred to 'tougher' functions.
154 */
155
156#if	(LOCK_DEBUG == 0)
157/* Acquire an exclusive lock. */
158static __inline int
159__sx_xlock(struct sx *sx, struct thread *td, int opts, const char *file,
160    int line)
161{
162	uintptr_t tid = (uintptr_t)td;
163	uintptr_t v = SX_LOCK_UNLOCKED;
164	int error = 0;
165
166	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__acquire) ||
167	    !atomic_fcmpset_acq_ptr(&sx->sx_lock, &v, tid)))
168		error = _sx_xlock_hard(sx, v, opts);
169
170	return (error);
171}
172
173/* Release an exclusive lock. */
174static __inline void
175__sx_xunlock(struct sx *sx, struct thread *td, const char *file, int line)
176{
177	uintptr_t x = (uintptr_t)td;
178
179	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__release) ||
180	    !atomic_fcmpset_rel_ptr(&sx->sx_lock, &x, SX_LOCK_UNLOCKED)))
181		_sx_xunlock_hard(sx, x);
182}
183#endif
184
185/*
186 * Public interface for lock operations.
187 */
188#ifndef LOCK_DEBUG
189#error	"LOCK_DEBUG not defined, include <sys/lock.h> before <sys/sx.h>"
190#endif
191#if	(LOCK_DEBUG > 0) || defined(SX_NOINLINE)
192#define	sx_xlock_(sx, file, line)					\
193	(void)_sx_xlock((sx), 0, (file), (line))
194#define	sx_xlock_sig_(sx, file, line)					\
195	_sx_xlock((sx), SX_INTERRUPTIBLE, (file), (line))
196#define	sx_xunlock_(sx, file, line)					\
197	_sx_xunlock((sx), (file), (line))
198#else
199#define	sx_xlock_(sx, file, line)					\
200	(void)__sx_xlock((sx), curthread, 0, (file), (line))
201#define	sx_xlock_sig_(sx, file, line)					\
202	__sx_xlock((sx), curthread, SX_INTERRUPTIBLE, (file), (line))
203#define	sx_xunlock_(sx, file, line)					\
204	__sx_xunlock((sx), curthread, (file), (line))
205#endif	/* LOCK_DEBUG > 0 || SX_NOINLINE */
206#if	(LOCK_DEBUG > 0)
207#define	sx_slock_(sx, file, line)					\
208	(void)_sx_slock((sx), 0, (file), (line))
209#define	sx_slock_sig_(sx, file, line)					\
210	_sx_slock((sx), SX_INTERRUPTIBLE, (file) , (line))
211#define	sx_sunlock_(sx, file, line)					\
212	_sx_sunlock((sx), (file), (line))
213#define	sx_try_slock(sx)	sx_try_slock_((sx), LOCK_FILE, LOCK_LINE)
214#define	sx_try_xlock(sx)	sx_try_xlock_((sx), LOCK_FILE, LOCK_LINE)
215#define	sx_try_upgrade(sx)	sx_try_upgrade_((sx), LOCK_FILE, LOCK_LINE)
216#define	sx_downgrade(sx)	sx_downgrade_((sx), LOCK_FILE, LOCK_LINE)
217#else
218#define	sx_slock_(sx, file, line)					\
219	(void)_sx_slock_int((sx), 0)
220#define	sx_slock_sig_(sx, file, line)					\
221	_sx_slock_int((sx), SX_INTERRUPTIBLE)
222#define	sx_sunlock_(sx, file, line)					\
223	_sx_sunlock_int((sx))
224#define	sx_try_slock(sx)	sx_try_slock_int((sx))
225#define	sx_try_xlock(sx)	sx_try_xlock_int((sx))
226#define	sx_try_upgrade(sx)	sx_try_upgrade_int((sx))
227#define	sx_downgrade(sx)	sx_downgrade_int((sx))
228#endif
229#ifdef INVARIANTS
230#define	sx_assert_(sx, what, file, line)				\
231	_sx_assert((sx), (what), (file), (line))
232#else
233#define	sx_assert_(sx, what, file, line)	(void)0
234#endif
235
236#define	sx_xlock(sx)		sx_xlock_((sx), LOCK_FILE, LOCK_LINE)
237#define	sx_xlock_sig(sx)	sx_xlock_sig_((sx), LOCK_FILE, LOCK_LINE)
238#define	sx_xunlock(sx)		sx_xunlock_((sx), LOCK_FILE, LOCK_LINE)
239#define	sx_slock(sx)		sx_slock_((sx), LOCK_FILE, LOCK_LINE)
240#define	sx_slock_sig(sx)	sx_slock_sig_((sx), LOCK_FILE, LOCK_LINE)
241#define	sx_sunlock(sx)		sx_sunlock_((sx), LOCK_FILE, LOCK_LINE)
242#define	sx_assert(sx, what)	sx_assert_((sx), (what), __FILE__, __LINE__)
243
244/*
245 * Return a pointer to the owning thread if the lock is exclusively
246 * locked.
247 */
248#define	sx_xholder(sx)							\
249	((sx)->sx_lock & SX_LOCK_SHARED ? NULL :			\
250	(struct thread *)SX_OWNER((sx)->sx_lock))
251
252#define	sx_xlocked(sx)							\
253	(((sx)->sx_lock & ~(SX_LOCK_FLAGMASK & ~SX_LOCK_SHARED)) ==	\
254	    (uintptr_t)curthread)
255
256#define	sx_unlock_(sx, file, line) do {					\
257	if (sx_xlocked(sx))						\
258		sx_xunlock_(sx, file, line);				\
259	else								\
260		sx_sunlock_(sx, file, line);				\
261} while (0)
262
263#define	sx_unlock(sx)	sx_unlock_((sx), LOCK_FILE, LOCK_LINE)
264
265#define	sx_sleep(chan, sx, pri, wmesg, timo)				\
266	_sleep((chan), &(sx)->lock_object, (pri), (wmesg),		\
267	    tick_sbt * (timo), 0,  C_HARDCLOCK)
268
269/*
270 * Options passed to sx_init_flags().
271 */
272#define	SX_DUPOK		0x01
273#define	SX_NOPROFILE		0x02
274#define	SX_NOWITNESS		0x04
275#define	SX_QUIET		0x08
276#define	SX_RECURSE		0x20
277#define	SX_NEW			0x40
278
279/*
280 * Options passed to sx_*lock_hard().
281 */
282#define	SX_INTERRUPTIBLE	0x40
283
284#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
285#define	SA_LOCKED		LA_LOCKED
286#define	SA_SLOCKED		LA_SLOCKED
287#define	SA_XLOCKED		LA_XLOCKED
288#define	SA_UNLOCKED		LA_UNLOCKED
289#define	SA_RECURSED		LA_RECURSED
290#define	SA_NOTRECURSED		LA_NOTRECURSED
291
292/* Backwards compatibility. */
293#define	SX_LOCKED		LA_LOCKED
294#define	SX_SLOCKED		LA_SLOCKED
295#define	SX_XLOCKED		LA_XLOCKED
296#define	SX_UNLOCKED		LA_UNLOCKED
297#define	SX_RECURSED		LA_RECURSED
298#define	SX_NOTRECURSED		LA_NOTRECURSED
299#endif
300
301#endif /* _KERNEL */
302
303#ifdef _STANDALONE
304/* since we have no threads in the boot loader, trivially implement no-op version */
305#define sx_xlock(s) (1)
306#define sx_try_xlock(s) (1)
307#define sx_xunlock(s) (1)
308#define SX_DUPOK 0
309#define SX_NEW 0
310#define SX_NOWITNESS 0
311
312static __inline void
313sx_init_flags(struct sx *sx, const char *description, int opts)
314{
315
316}
317
318static __inline void
319sx_destroy(struct sx *sx)
320{
321
322}
323#endif /* _STANDALONE */
324
325#endif /* !_SYS_SX_H_ */
326