• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright © International Business Machines  Corp., 2009
4  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
5  *
6  * DESCRIPTION
7  *      Glibc independent futex library for testing kernel functionality.
8  *
9  * AUTHOR
10  *      Darren Hart <dvhltc@us.ibm.com>
11  */
12 
13 #ifndef FUTEXTEST_H
14 #define FUTEXTEST_H
15 
16 #include <unistd.h>
17 #include <sys/syscall.h>
18 #include <sys/types.h>
19 #include "lapi/futex.h"
20 #include "tst_timer.h"
21 
22 #define FUTEX_INITIALIZER 0
23 
24 enum futex_fn_type {
25 	FUTEX_FN_FUTEX,
26 	FUTEX_FN_FUTEX64,
27 };
28 
29 struct futex_test_variants {
30 	enum futex_fn_type fntype;
31 	enum tst_ts_type tstype;
32 	int (*gettime)(clockid_t clk_id, void *ts);
33 	char *desc;
34 };
35 
futex_supported_by_kernel(enum futex_fn_type fntype)36 static inline void futex_supported_by_kernel(enum futex_fn_type fntype)
37 {
38 	if (fntype != FUTEX_FN_FUTEX64)
39 		return;
40 
41 	/* Check if the syscall is implemented on the platform */
42 	TEST(sys_futex_time64(NULL, 0, 0, NULL, NULL, 0));
43 	if (TST_RET == -1 && TST_ERR == ENOSYS)
44 		tst_brk(TCONF, "Test not supported on kernel/platform");
45 }
46 
47 /**
48  * futex_syscall() - futex syscall wrapper
49  * @fntype:	Futex function type
50  * @uaddr:	address of first futex
51  * @op:		futex op code
52  * @val:	typically expected value of uaddr, but varies by op
53  * @timeout:	typically an absolute struct tst_ts (except where noted
54  *		otherwise). Overloaded by some ops
55  * @uaddr2:	address of second futex for some ops\
56  * @val3:	varies by op
57  * @opflags:	flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG
58  *
59  * futex_syscall() is used by all the following futex op wrappers. It can also be
60  * used for misuse and abuse testing. Generally, the specific op wrappers
61  * should be used instead. It is a macro instead of an static inline function as
62  * some of the types over overloaded (timeout is used for nr_requeue for
63  * example).
64  *
65  * These argument descriptions are the defaults for all
66  * like-named arguments in the following wrappers except where noted below.
67  */
futex_syscall(enum futex_fn_type fntype,futex_t * uaddr,int futex_op,futex_t val,void * timeout,futex_t * uaddr2,int val3,int opflags)68 static inline int futex_syscall(enum futex_fn_type fntype, futex_t *uaddr,
69 				int futex_op, futex_t val, void *timeout,
70 				futex_t *uaddr2, int val3, int opflags)
71 {
72 	int (*func)(int *uaddr, int futex_op, int val, void *to, int *uaddr2, int val3);
73 
74 	if (fntype == FUTEX_FN_FUTEX)
75 		func = sys_futex;
76 	else
77 		func = sys_futex_time64;
78 
79 	return func((int *)uaddr, futex_op | opflags, val, timeout, (int *)uaddr2, val3);
80 }
81 
82 /**
83  * futex_wait() - block on uaddr with optional timeout
84  * @timeout:	relative timeout
85  */
86 static inline int
futex_wait(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,struct tst_ts * timeout,int opflags)87 futex_wait(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
88 	   struct tst_ts *timeout, int opflags)
89 {
90 	return futex_syscall(fntype, uaddr, FUTEX_WAIT, val,
91 			     tst_ts_get(timeout), NULL, 0, opflags);
92 }
93 
94 /**
95  * futex_wake() - wake one or more tasks blocked on uaddr
96  * @nr_wake:	wake up to this many tasks
97  */
98 static inline int
futex_wake(enum futex_fn_type fntype,futex_t * uaddr,int nr_wake,int opflags)99 futex_wake(enum futex_fn_type fntype, futex_t *uaddr, int nr_wake, int opflags)
100 {
101 	return futex_syscall(fntype, uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0,
102 			     opflags);
103 }
104 
105 /**
106  * futex_wait_bitset() - block on uaddr with bitset
107  * @bitset:	bitset to be used with futex_wake_bitset
108  */
109 static inline int
futex_wait_bitset(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,struct tst_ts * timeout,u_int32_t bitset,int opflags)110 futex_wait_bitset(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
111 		  struct tst_ts *timeout, u_int32_t bitset, int opflags)
112 {
113 	return futex_syscall(fntype, uaddr, FUTEX_WAIT_BITSET, val,
114 			     tst_ts_get(timeout), NULL, bitset, opflags);
115 }
116 
117 /**
118  * futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset
119  * @bitset:	bitset to compare with that used in futex_wait_bitset
120  */
121 static inline int
futex_wake_bitset(enum futex_fn_type fntype,futex_t * uaddr,int nr_wake,u_int32_t bitset,int opflags)122 futex_wake_bitset(enum futex_fn_type fntype, futex_t *uaddr, int nr_wake,
123 		  u_int32_t bitset, int opflags)
124 {
125 	return futex_syscall(fntype, uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL,
126 			     NULL, bitset, opflags);
127 }
128 
129 /**
130  * futex_lock_pi() - block on uaddr as a PI mutex
131  * @detect:	whether (1) or not (0) to perform deadlock detection
132  */
133 static inline int
futex_lock_pi(enum futex_fn_type fntype,futex_t * uaddr,struct tst_ts * timeout,int detect,int opflags)134 futex_lock_pi(enum futex_fn_type fntype, futex_t *uaddr, struct tst_ts *timeout,
135 	      int detect, int opflags)
136 {
137 	return futex_syscall(fntype, uaddr, FUTEX_LOCK_PI, detect,
138 			     tst_ts_get(timeout), NULL, 0, opflags);
139 }
140 
141 /**
142  * futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter
143  */
144 static inline int
futex_unlock_pi(enum futex_fn_type fntype,futex_t * uaddr,int opflags)145 futex_unlock_pi(enum futex_fn_type fntype, futex_t *uaddr, int opflags)
146 {
147 	return futex_syscall(fntype, uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0,
148 			     opflags); }
149 
150 /**
151  * futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION
152  */
153 static inline int
futex_wake_op(enum futex_fn_type fntype,futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_wake2,int wake_op,int opflags)154 futex_wake_op(enum futex_fn_type fntype, futex_t *uaddr, futex_t *uaddr2,
155 	      int nr_wake, int nr_wake2, int wake_op, int opflags)
156 {
157 	return futex_syscall(fntype, uaddr, FUTEX_WAKE_OP, nr_wake,
158 			     (void *)((unsigned long)nr_wake2), uaddr2, wake_op,
159 			     opflags);
160 }
161 
162 /**
163  * futex_requeue() - requeue without expected value comparison, deprecated
164  * @nr_wake:	wake up to this many tasks
165  * @nr_requeue:	requeue up to this many tasks
166  *
167  * Due to its inherently racy implementation, futex_requeue() is deprecated in
168  * favor of futex_cmp_requeue().
169  */
170 static inline int
futex_requeue(enum futex_fn_type fntype,futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)171 futex_requeue(enum futex_fn_type fntype, futex_t *uaddr, futex_t *uaddr2,
172 	      int nr_wake, int nr_requeue, int opflags)
173 {
174 	return futex_syscall(fntype, uaddr, FUTEX_REQUEUE, nr_wake,
175 			     (void *)((unsigned long)nr_requeue), uaddr2, 0,
176 			     opflags);
177 }
178 
179 /**
180  * futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
181  * @nr_wake:	wake up to this many tasks
182  * @nr_requeue:	requeue up to this many tasks
183  */
184 static inline int
futex_cmp_requeue(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)185 futex_cmp_requeue(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
186 		  futex_t *uaddr2, int nr_wake, int nr_requeue, int opflags)
187 {
188 	return futex_syscall(fntype, uaddr, FUTEX_CMP_REQUEUE, nr_wake,
189 			     (void *)((unsigned long)nr_requeue), uaddr2, val,
190 			     opflags);
191 }
192 
193 /**
194  * futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2
195  * @uaddr:	non-PI futex source
196  * @uaddr2:	PI futex target
197  *
198  * This is the first half of the requeue_pi mechanism. It shall always be
199  * paired with futex_cmp_requeue_pi().
200  */
201 static inline int
futex_wait_requeue_pi(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,struct tst_ts * timeout,int opflags)202 futex_wait_requeue_pi(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
203 		      futex_t *uaddr2, struct tst_ts *timeout, int opflags)
204 {
205 	return futex_syscall(fntype, uaddr, FUTEX_WAIT_REQUEUE_PI, val,
206 			     tst_ts_get(timeout), uaddr2, 0, opflags);
207 }
208 
209 /**
210  * futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware)
211  * @uaddr:	non-PI futex source
212  * @uaddr2:	PI futex target
213  * @nr_wake:	wake up to this many tasks
214  * @nr_requeue:	requeue up to this many tasks
215  */
216 static inline int
futex_cmp_requeue_pi(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)217 futex_cmp_requeue_pi(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
218 		     futex_t *uaddr2, int nr_wake, int nr_requeue, int opflags)
219 {
220 	return futex_syscall(fntype, uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake,
221 			     (void *)((unsigned long)nr_requeue), uaddr2, val,
222 			     opflags);
223 }
224 
225 /**
226  * futex_cmpxchg() - atomic compare and exchange
227  * @uaddr:	The address of the futex to be modified
228  * @oldval:	The expected value of the futex
229  * @newval:	The new value to try and assign the futex
230  *
231  * Implement cmpxchg using gcc atomic builtins.
232  * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
233  *
234  * Return the old futex value.
235  */
236 static inline u_int32_t
futex_cmpxchg(futex_t * uaddr,u_int32_t oldval,u_int32_t newval)237 futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)
238 {
239 	return __sync_val_compare_and_swap(uaddr, oldval, newval);
240 }
241 
242 /**
243  * futex_dec() - atomic decrement of the futex value
244  * @uaddr:	The address of the futex to be modified
245  *
246  * Return the new futex value.
247  */
248 static inline u_int32_t
futex_dec(futex_t * uaddr)249 futex_dec(futex_t *uaddr)
250 {
251 	return __sync_sub_and_fetch(uaddr, 1);
252 }
253 
254 /**
255  * futex_inc() - atomic increment of the futex value
256  * @uaddr:	the address of the futex to be modified
257  *
258  * Return the new futex value.
259  */
260 static inline u_int32_t
futex_inc(futex_t * uaddr)261 futex_inc(futex_t *uaddr)
262 {
263 	return __sync_add_and_fetch(uaddr, 1);
264 }
265 
266 /**
267  * futex_set() - atomic decrement of the futex value
268  * @uaddr:	the address of the futex to be modified
269  * @newval:	New value for the atomic_t
270  *
271  * Return the new futex value.
272  */
273 static inline u_int32_t
futex_set(futex_t * uaddr,u_int32_t newval)274 futex_set(futex_t *uaddr, u_int32_t newval)
275 {
276 	*uaddr = newval;
277 	return newval;
278 }
279 
280 /**
281  * futex_waked_someone() - ECHCK func for TST_RETRY_FUNC
282  * @ret:	return value of futex_wake
283  *
284  * Return value drives TST_RETRY_FUNC macro.
285  */
286 static inline int
futex_waked_someone(int ret)287 futex_waked_someone(int ret)
288 {
289 	if (ret < 0)
290 		tst_brk(TBROK | TERRNO, "futex_wake returned: %d", ret);
291 
292 	return ret;
293 }
294 
295 #endif /* _FUTEXTEST_H */
296