1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
3 *
4 * Copyright © International Business Machines Corp., 2009
5 *
6 * DESCRIPTION
7 * Block on a futex and wait for timeout.
8 *
9 * AUTHOR
10 * Darren Hart <[email protected]>
11 *
12 * HISTORY
13 * 2009-Nov-6: Initial version by Darren Hart <[email protected]>
14 * 2021-Apr-26: More test cases by André Almeida <[email protected]>
15 *
16 *****************************************************************************/
17
18 #include <pthread.h>
19 #include "futextest.h"
20 #include "futex2test.h"
21 #include "logging.h"
22
23 #define TEST_NAME "futex-wait-timeout"
24
25 static long timeout_ns = 100000; /* 100us default timeout */
26 static futex_t futex_pi;
27 static pthread_barrier_t barrier;
28
usage(char * prog)29 void usage(char *prog)
30 {
31 printf("Usage: %s\n", prog);
32 printf(" -c Use color\n");
33 printf(" -h Display this help message\n");
34 printf(" -t N Timeout in nanoseconds (default: 100,000)\n");
35 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
36 VQUIET, VCRITICAL, VINFO);
37 }
38
39 /*
40 * Get a PI lock and hold it forever, so the main thread lock_pi will block
41 * and we can test the timeout
42 */
get_pi_lock(void * arg)43 void *get_pi_lock(void *arg)
44 {
45 int ret;
46 volatile futex_t lock = 0;
47
48 ret = futex_lock_pi(&futex_pi, NULL, 0, 0);
49 if (ret != 0)
50 error("futex_lock_pi failed\n", ret);
51
52 pthread_barrier_wait(&barrier);
53
54 /* Blocks forever */
55 ret = futex_wait(&lock, 0, NULL, 0);
56 error("futex_wait failed\n", ret);
57
58 return NULL;
59 }
60
61 /*
62 * Check if the function returned the expected error
63 */
test_timeout(int res,int * ret,char * test_name,int err)64 static void test_timeout(int res, int *ret, char *test_name, int err)
65 {
66 if (!res || errno != err) {
67 if (errno == ENOSYS) {
68 ksft_test_result_skip("%s returned %d\n", test_name,
69 errno);
70 } else {
71 ksft_test_result_fail("%s returned %d\n", test_name,
72 res < 0 ? errno : res);
73 *ret = RET_FAIL;
74 }
75 } else {
76 ksft_test_result_pass("%s succeeds\n", test_name);
77 }
78 }
79
80 /*
81 * Calculate absolute timeout and correct overflow
82 */
futex_get_abs_timeout(clockid_t clockid,struct timespec64 * to,long timeout_ns)83 static int futex_get_abs_timeout(clockid_t clockid, struct timespec64 *to,
84 long timeout_ns)
85 {
86 if (gettime64(clockid, to)) {
87 error("gettime64 failed\n", errno);
88 return errno;
89 }
90
91 to->tv_nsec += timeout_ns;
92
93 if (to->tv_nsec >= 1000000000) {
94 to->tv_sec++;
95 to->tv_nsec -= 1000000000;
96 }
97
98 return 0;
99 }
100
main(int argc,char * argv[])101 int main(int argc, char *argv[])
102 {
103 futex_t f1 = FUTEX_INITIALIZER;
104 int res, ret = RET_PASS;
105 struct timespec64 to;
106 pthread_t thread;
107 int c;
108 struct futex_waitv waitv = {
109 .uaddr = (uintptr_t)&f1,
110 .val = f1,
111 .flags = FUTEX_32,
112 .__reserved = 0
113 };
114
115 while ((c = getopt(argc, argv, "cht:v:")) != -1) {
116 switch (c) {
117 case 'c':
118 log_color(1);
119 break;
120 case 'h':
121 usage(basename(argv[0]));
122 exit(0);
123 case 't':
124 timeout_ns = atoi(optarg);
125 break;
126 case 'v':
127 log_verbosity(atoi(optarg));
128 break;
129 default:
130 usage(basename(argv[0]));
131 exit(1);
132 }
133 }
134
135 ksft_print_header();
136 ksft_set_plan(9);
137 ksft_print_msg("%s: Block on a futex and wait for timeout\n",
138 basename(argv[0]));
139 ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
140
141 pthread_barrier_init(&barrier, NULL, 2);
142 pthread_create(&thread, NULL, get_pi_lock, NULL);
143
144 /* initialize relative timeout */
145 to.tv_sec = 0;
146 to.tv_nsec = timeout_ns;
147
148 res = futex_wait(&f1, f1, &to, 0);
149 test_timeout(res, &ret, "futex_wait relative", ETIMEDOUT);
150
151 /* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
152 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
153 return RET_FAIL;
154 res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
155 test_timeout(res, &ret, "futex_wait_bitset realtime", ETIMEDOUT);
156
157 /* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
158 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
159 return RET_FAIL;
160 res = futex_wait_bitset(&f1, f1, &to, 1, 0);
161 test_timeout(res, &ret, "futex_wait_bitset monotonic", ETIMEDOUT);
162
163 /* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
164 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
165 return RET_FAIL;
166 res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
167 test_timeout(res, &ret, "futex_wait_requeue_pi realtime", ETIMEDOUT);
168
169 /* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
170 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
171 return RET_FAIL;
172 res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
173 test_timeout(res, &ret, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
174
175 /* Wait until the other thread calls futex_lock_pi() */
176 pthread_barrier_wait(&barrier);
177 pthread_barrier_destroy(&barrier);
178 /*
179 * FUTEX_LOCK_PI with CLOCK_REALTIME
180 * Due to historical reasons, FUTEX_LOCK_PI supports only realtime
181 * clock, but requires the caller to not set CLOCK_REALTIME flag.
182 *
183 * If you call FUTEX_LOCK_PI with a monotonic clock, it'll be
184 * interpreted as a realtime clock, and (unless you mess your machine's
185 * time or your time machine) the monotonic clock value is always
186 * smaller than realtime and the syscall will timeout immediately.
187 */
188 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
189 return RET_FAIL;
190 res = futex_lock_pi(&futex_pi, &to, 0, 0);
191 test_timeout(res, &ret, "futex_lock_pi realtime", ETIMEDOUT);
192
193 /* Test operations that don't support FUTEX_CLOCK_REALTIME */
194 res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
195 test_timeout(res, &ret, "futex_lock_pi invalid timeout flag", ENOSYS);
196
197 /* futex_waitv with CLOCK_MONOTONIC */
198 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
199 return RET_FAIL;
200 res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
201 test_timeout(res, &ret, "futex_waitv monotonic", ETIMEDOUT);
202
203 /* futex_waitv with CLOCK_REALTIME */
204 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
205 return RET_FAIL;
206 res = futex_waitv(&waitv, 1, 0, &to, CLOCK_REALTIME);
207 test_timeout(res, &ret, "futex_waitv realtime", ETIMEDOUT);
208
209 ksft_print_cnts();
210 return ret;
211 }
212