• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "logging.h"
21 
22 #define TEST_NAME "futex-wait-timeout"
23 
24 static long timeout_ns = 100000;	/* 100us default timeout */
25 static futex_t futex_pi;
26 static pthread_barrier_t barrier;
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 	pthread_barrier_wait(&barrier);
52 
53 	/* Blocks forever */
54 	ret = futex_wait(&lock, 0, NULL, 0);
55 	error("futex_wait failed\n", ret);
56 
57 	return NULL;
58 }
59 
60 /*
61  * Check if the function returned the expected error
62  */
test_timeout(int res,int * ret,char * test_name,int err)63 static void test_timeout(int res, int *ret, char *test_name, int err)
64 {
65 	if (!res || errno != err) {
66 		ksft_test_result_fail("%s returned %d\n", test_name,
67 				      res < 0 ? errno : res);
68 		*ret = RET_FAIL;
69 	} else {
70 		ksft_test_result_pass("%s succeeds\n", test_name);
71 	}
72 }
73 
74 /*
75  * Calculate absolute timeout and correct overflow
76  */
futex_get_abs_timeout(clockid_t clockid,struct timespec * to,long timeout_ns)77 static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
78 				 long timeout_ns)
79 {
80 	if (clock_gettime(clockid, to)) {
81 		error("clock_gettime failed\n", errno);
82 		return errno;
83 	}
84 
85 	to->tv_nsec += timeout_ns;
86 
87 	if (to->tv_nsec >= 1000000000) {
88 		to->tv_sec++;
89 		to->tv_nsec -= 1000000000;
90 	}
91 
92 	return 0;
93 }
94 
main(int argc,char * argv[])95 int main(int argc, char *argv[])
96 {
97 	futex_t f1 = FUTEX_INITIALIZER;
98 	int res, ret = RET_PASS;
99 	struct timespec to;
100 	pthread_t thread;
101 	int c;
102 
103 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
104 		switch (c) {
105 		case 'c':
106 			log_color(1);
107 			break;
108 		case 'h':
109 			usage(basename(argv[0]));
110 			exit(0);
111 		case 't':
112 			timeout_ns = atoi(optarg);
113 			break;
114 		case 'v':
115 			log_verbosity(atoi(optarg));
116 			break;
117 		default:
118 			usage(basename(argv[0]));
119 			exit(1);
120 		}
121 	}
122 
123 	ksft_print_header();
124 	ksft_set_plan(7);
125 	ksft_print_msg("%s: Block on a futex and wait for timeout\n",
126 	       basename(argv[0]));
127 	ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
128 
129 	pthread_barrier_init(&barrier, NULL, 2);
130 	pthread_create(&thread, NULL, get_pi_lock, NULL);
131 
132 	/* initialize relative timeout */
133 	to.tv_sec = 0;
134 	to.tv_nsec = timeout_ns;
135 
136 	res = futex_wait(&f1, f1, &to, 0);
137 	test_timeout(res, &ret, "futex_wait relative", ETIMEDOUT);
138 
139 	/* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
140 	if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
141 		return RET_FAIL;
142 	res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
143 	test_timeout(res, &ret, "futex_wait_bitset realtime", ETIMEDOUT);
144 
145 	/* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
146 	if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
147 		return RET_FAIL;
148 	res = futex_wait_bitset(&f1, f1, &to, 1, 0);
149 	test_timeout(res, &ret, "futex_wait_bitset monotonic", ETIMEDOUT);
150 
151 	/* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
152 	if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
153 		return RET_FAIL;
154 	res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
155 	test_timeout(res, &ret, "futex_wait_requeue_pi realtime", ETIMEDOUT);
156 
157 	/* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
158 	if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
159 		return RET_FAIL;
160 	res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
161 	test_timeout(res, &ret, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
162 
163 	/* Wait until the other thread calls futex_lock_pi() */
164 	pthread_barrier_wait(&barrier);
165 	pthread_barrier_destroy(&barrier);
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 	ksft_print_cnts();
186 	return ret;
187 }
188