xref: /aosp_15_r20/external/linux-kselftest/tools/testing/selftests/futex/include/futextest.h (revision 053f45be4e351dfd5e965df293cd45b779f579ee)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /******************************************************************************
3  *
4  *   Copyright © International Business Machines  Corp., 2009
5  *
6  * DESCRIPTION
7  *      Glibc independent futex library for testing kernel functionality.
8  *
9  * AUTHOR
10  *      Darren Hart <[email protected]>
11  *
12  * HISTORY
13  *      2009-Nov-6: Initial version by Darren Hart <[email protected]>
14  *
15  *****************************************************************************/
16 
17 #ifndef _FUTEXTEST_H
18 #define _FUTEXTEST_H
19 
20 #include <unistd.h>
21 #include <sys/syscall.h>
22 #include <sys/types.h>
23 #include <linux/futex.h>
24 
25 typedef volatile u_int32_t futex_t;
26 #define FUTEX_INITIALIZER 0
27 
28 /* Define the newer op codes if the system header file is not up to date. */
29 #ifndef FUTEX_WAIT_BITSET
30 #define FUTEX_WAIT_BITSET		9
31 #endif
32 #ifndef FUTEX_WAKE_BITSET
33 #define FUTEX_WAKE_BITSET		10
34 #endif
35 #ifndef FUTEX_WAIT_REQUEUE_PI
36 #define FUTEX_WAIT_REQUEUE_PI		11
37 #endif
38 #ifndef FUTEX_CMP_REQUEUE_PI
39 #define FUTEX_CMP_REQUEUE_PI		12
40 #endif
41 #ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE
42 #define FUTEX_WAIT_REQUEUE_PI_PRIVATE	(FUTEX_WAIT_REQUEUE_PI | \
43 					 FUTEX_PRIVATE_FLAG)
44 #endif
45 #ifndef FUTEX_REQUEUE_PI_PRIVATE
46 #define FUTEX_CMP_REQUEUE_PI_PRIVATE	(FUTEX_CMP_REQUEUE_PI | \
47 					 FUTEX_PRIVATE_FLAG)
48 #endif
49 
50 #define timespec64 __kernel_timespec
51 
52 /**
53  * futex() - SYS_futex syscall wrapper
54  * @uaddr:	address of first futex
55  * @op:		futex op code
56  * @val:	typically expected value of uaddr, but varies by op
57  * @timeout:	typically an absolute struct timespec64 (except where noted
58  *              otherwise). Overloaded by some ops
59  * @uaddr2:	address of second futex for some ops\
60  * @val3:	varies by op
61  * @opflags:	flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG
62  *
63  * futex() is used by all the following futex op wrappers. It can also be
64  * used for misuse and abuse testing. Generally, the specific op wrappers
65  * should be used instead. It is a macro instead of an static inline function as
66  * some of the types over overloaded (timeout is used for nr_requeue for
67  * example).
68  *
69  * These argument descriptions are the defaults for all
70  * like-named arguments in the following wrappers except where noted below.
71  */
72 /**
73  * We only support 64-bit time_t for the timeout.
74  * On 64-bit architectures we can use __NR_futex
75  * On 32-bit architectures we use __NR_futex_time64. This only works on kernel
76  * versions 5.1+.
77  */
78 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
79 # define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \
80 	syscall(__NR_futex, uaddr, op | opflags, val, timeout, uaddr2, val3)
81 #else
82 # define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \
83 	syscall(__NR_futex_time64, uaddr, op | opflags, val, timeout, uaddr2, val3)
84 #endif
85 
86 static inline int
gettime64(clock_t clockid,struct timespec64 * tv)87 gettime64(clock_t clockid, struct timespec64 *tv)
88 {
89 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
90 	return syscall(__NR_clock_gettime, clockid, tv);
91 #else
92 	return syscall(__NR_clock_gettime64, clockid, tv);
93 #endif
94 }
95 
96 /**
97  * futex_wait() - block on uaddr with optional timeout
98  * @timeout:	relative timeout
99  */
100 static inline int
futex_wait(futex_t * uaddr,futex_t val,struct timespec64 * timeout,int opflags)101 futex_wait(futex_t *uaddr, futex_t val, struct timespec64 *timeout, int opflags)
102 {
103 	return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);
104 }
105 
106 /**
107  * futex_wake() - wake one or more tasks blocked on uaddr
108  * @nr_wake:	wake up to this many tasks
109  */
110 static inline int
futex_wake(futex_t * uaddr,int nr_wake,int opflags)111 futex_wake(futex_t *uaddr, int nr_wake, int opflags)
112 {
113 	return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);
114 }
115 
116 /**
117  * futex_wait_bitset() - block on uaddr with bitset
118  * @bitset:	bitset to be used with futex_wake_bitset
119  */
120 static inline int
futex_wait_bitset(futex_t * uaddr,futex_t val,struct timespec64 * timeout,u_int32_t bitset,int opflags)121 futex_wait_bitset(futex_t *uaddr, futex_t val, struct timespec64 *timeout,
122 		  u_int32_t bitset, int opflags)
123 {
124 	return futex(uaddr, FUTEX_WAIT_BITSET, val, timeout, NULL, bitset,
125 		     opflags);
126 }
127 
128 /**
129  * futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset
130  * @bitset:	bitset to compare with that used in futex_wait_bitset
131  */
132 static inline int
futex_wake_bitset(futex_t * uaddr,int nr_wake,u_int32_t bitset,int opflags)133 futex_wake_bitset(futex_t *uaddr, int nr_wake, u_int32_t bitset, int opflags)
134 {
135 	return futex(uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL, NULL, bitset,
136 		     opflags);
137 }
138 
139 /**
140  * futex_lock_pi() - block on uaddr as a PI mutex
141  * @detect:	whether (1) or not (0) to perform deadlock detection
142  */
143 static inline int
futex_lock_pi(futex_t * uaddr,struct timespec64 * timeout,int detect,int opflags)144 futex_lock_pi(futex_t *uaddr, struct timespec64 *timeout, int detect,
145 	      int opflags)
146 {
147 	return futex(uaddr, FUTEX_LOCK_PI, detect, timeout, NULL, 0, opflags);
148 }
149 
150 /**
151  * futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter
152  */
153 static inline int
futex_unlock_pi(futex_t * uaddr,int opflags)154 futex_unlock_pi(futex_t *uaddr, int opflags)
155 {
156 	return futex(uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0, opflags);
157 }
158 
159 /**
160  * futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION
161  */
162 static inline int
futex_wake_op(futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_wake2,int wake_op,int opflags)163 futex_wake_op(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_wake2,
164 	      int wake_op, int opflags)
165 {
166 	return futex(uaddr, FUTEX_WAKE_OP, nr_wake, nr_wake2, uaddr2, wake_op,
167 		     opflags);
168 }
169 
170 /**
171  * futex_requeue() - requeue without expected value comparison, deprecated
172  * @nr_wake:	wake up to this many tasks
173  * @nr_requeue:	requeue up to this many tasks
174  *
175  * Due to its inherently racy implementation, futex_requeue() is deprecated in
176  * favor of futex_cmp_requeue().
177  */
178 static inline int
futex_requeue(futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)179 futex_requeue(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_requeue,
180 	      int opflags)
181 {
182 	return futex(uaddr, FUTEX_REQUEUE, nr_wake, nr_requeue, uaddr2, 0,
183 		     opflags);
184 }
185 
186 /**
187  * futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
188  * @nr_wake:	wake up to this many tasks
189  * @nr_requeue:	requeue up to this many tasks
190  */
191 static inline int
futex_cmp_requeue(futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)192 futex_cmp_requeue(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
193 		  int nr_requeue, int opflags)
194 {
195 	return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,
196 		     val, opflags);
197 }
198 
199 /**
200  * futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2
201  * @uaddr:	non-PI futex source
202  * @uaddr2:	PI futex target
203  *
204  * This is the first half of the requeue_pi mechanism. It shall always be
205  * paired with futex_cmp_requeue_pi().
206  */
207 static inline int
futex_wait_requeue_pi(futex_t * uaddr,futex_t val,futex_t * uaddr2,struct timespec64 * timeout,int opflags)208 futex_wait_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2,
209 		      struct timespec64 *timeout, int opflags)
210 {
211 	return futex(uaddr, FUTEX_WAIT_REQUEUE_PI, val, timeout, uaddr2, 0,
212 		     opflags);
213 }
214 
215 /**
216  * futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware)
217  * @uaddr:	non-PI futex source
218  * @uaddr2:	PI futex target
219  * @nr_wake:	wake up to this many tasks
220  * @nr_requeue:	requeue up to this many tasks
221  */
222 static inline int
futex_cmp_requeue_pi(futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)223 futex_cmp_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
224 		     int nr_requeue, int opflags)
225 {
226 	return futex(uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake, nr_requeue, uaddr2,
227 		     val, opflags);
228 }
229 
230 /**
231  * futex_cmpxchg() - atomic compare and exchange
232  * @uaddr:	The address of the futex to be modified
233  * @oldval:	The expected value of the futex
234  * @newval:	The new value to try and assign the futex
235  *
236  * Implement cmpxchg using gcc atomic builtins.
237  * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
238  *
239  * Return the old futex value.
240  */
241 static inline u_int32_t
futex_cmpxchg(futex_t * uaddr,u_int32_t oldval,u_int32_t newval)242 futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)
243 {
244 	return __sync_val_compare_and_swap(uaddr, oldval, newval);
245 }
246 
247 /**
248  * futex_dec() - atomic decrement of the futex value
249  * @uaddr:	The address of the futex to be modified
250  *
251  * Return the new futex value.
252  */
253 static inline u_int32_t
futex_dec(futex_t * uaddr)254 futex_dec(futex_t *uaddr)
255 {
256 	return __sync_sub_and_fetch(uaddr, 1);
257 }
258 
259 /**
260  * futex_inc() - atomic increment of the futex value
261  * @uaddr:	the address of the futex to be modified
262  *
263  * Return the new futex value.
264  */
265 static inline u_int32_t
futex_inc(futex_t * uaddr)266 futex_inc(futex_t *uaddr)
267 {
268 	return __sync_add_and_fetch(uaddr, 1);
269 }
270 
271 /**
272  * futex_set() - atomic decrement of the futex value
273  * @uaddr:	the address of the futex to be modified
274  * @newval:	New value for the atomic_t
275  *
276  * Return the new futex value.
277  */
278 static inline u_int32_t
futex_set(futex_t * uaddr,u_int32_t newval)279 futex_set(futex_t *uaddr, u_int32_t newval)
280 {
281 	*uaddr = newval;
282 	return newval;
283 }
284 
285 #endif
286