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