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 
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  */
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  */
61 static void test_timeout(int res, int *ret, char *test_name, int err)
62 {
63 	if (!res || errno != err) {
64 		ksft_test_result_fail("%s returned %d\n", test_name,
65 				      res < 0 ? errno : res);
66 		*ret = RET_FAIL;
67 	} else {
68 		ksft_test_result_pass("%s succeeds\n", test_name);
69 	}
70 }
71 
72 /*
73  * Calculate absolute timeout and correct overflow
74  */
75 static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
76 				 long timeout_ns)
77 {
78 	if (clock_gettime(clockid, to)) {
79 		error("clock_gettime failed\n", errno);
80 		return errno;
81 	}
82 
83 	to->tv_nsec += timeout_ns;
84 
85 	if (to->tv_nsec >= 1000000000) {
86 		to->tv_sec++;
87 		to->tv_nsec -= 1000000000;
88 	}
89 
90 	return 0;
91 }
92 
93 int main(int argc, char *argv[])
94 {
95 	futex_t f1 = FUTEX_INITIALIZER;
96 	int res, ret = RET_PASS;
97 	struct timespec to;
98 	pthread_t thread;
99 	int c;
100 	struct futex_waitv waitv = {
101 			.uaddr = (uintptr_t)&f1,
102 			.val = f1,
103 			.flags = FUTEX_32,
104 			.__reserved = 0
105 		};
106 
107 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
108 		switch (c) {
109 		case 'c':
110 			log_color(1);
111 			break;
112 		case 'h':
113 			usage(basename(argv[0]));
114 			exit(0);
115 		case 't':
116 			timeout_ns = atoi(optarg);
117 			break;
118 		case 'v':
119 			log_verbosity(atoi(optarg));
120 			break;
121 		default:
122 			usage(basename(argv[0]));
123 			exit(1);
124 		}
125 	}
126 
127 	ksft_print_header();
128 	ksft_set_plan(9);
129 	ksft_print_msg("%s: Block on a futex and wait for timeout\n",
130 	       basename(argv[0]));
131 	ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
132 
133 	pthread_create(&thread, NULL, get_pi_lock, NULL);
134 
135 	/* initialize relative timeout */
136 	to.tv_sec = 0;
137 	to.tv_nsec = timeout_ns;
138 
139 	res = futex_wait(&f1, f1, &to, 0);
140 	test_timeout(res, &ret, "futex_wait relative", ETIMEDOUT);
141 
142 	/* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
143 	if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
144 		return RET_FAIL;
145 	res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
146 	test_timeout(res, &ret, "futex_wait_bitset realtime", ETIMEDOUT);
147 
148 	/* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
149 	if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
150 		return RET_FAIL;
151 	res = futex_wait_bitset(&f1, f1, &to, 1, 0);
152 	test_timeout(res, &ret, "futex_wait_bitset monotonic", ETIMEDOUT);
153 
154 	/* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
155 	if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
156 		return RET_FAIL;
157 	res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
158 	test_timeout(res, &ret, "futex_wait_requeue_pi realtime", ETIMEDOUT);
159 
160 	/* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
161 	if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
162 		return RET_FAIL;
163 	res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
164 	test_timeout(res, &ret, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
165 
166 	/*
167 	 * FUTEX_LOCK_PI with CLOCK_REALTIME
168 	 * Due to historical reasons, FUTEX_LOCK_PI supports only realtime
169 	 * clock, but requires the caller to not set CLOCK_REALTIME flag.
170 	 *
171 	 * If you call FUTEX_LOCK_PI with a monotonic clock, it'll be
172 	 * interpreted as a realtime clock, and (unless you mess your machine's
173 	 * time or your time machine) the monotonic clock value is always
174 	 * smaller than realtime and the syscall will timeout immediately.
175 	 */
176 	if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
177 		return RET_FAIL;
178 	res = futex_lock_pi(&futex_pi, &to, 0, 0);
179 	test_timeout(res, &ret, "futex_lock_pi realtime", ETIMEDOUT);
180 
181 	/* Test operations that don't support FUTEX_CLOCK_REALTIME */
182 	res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
183 	test_timeout(res, &ret, "futex_lock_pi invalid timeout flag", ENOSYS);
184 
185 	/* futex_waitv with CLOCK_MONOTONIC */
186 	if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
187 		return RET_FAIL;
188 	res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
189 	test_timeout(res, &ret, "futex_waitv monotonic", ETIMEDOUT);
190 
191 	/* futex_waitv with CLOCK_REALTIME */
192 	if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
193 		return RET_FAIL;
194 	res = futex_waitv(&waitv, 1, 0, &to, CLOCK_REALTIME);
195 	test_timeout(res, &ret, "futex_waitv realtime", ETIMEDOUT);
196 
197 	ksft_print_cnts();
198 	return ret;
199 }
200