1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
3  *
4  *   Copyright © International Business Machines  Corp., 2006-2008
5  *
6  * DESCRIPTION
7  *      This test exercises the futex_wait_requeue_pi() signal handling both
8  *      before and after the requeue. The first should be restarted by the
9  *      kernel. The latter should return EWOULDBLOCK to the waiter.
10  *
11  * AUTHORS
12  *      Darren Hart <dvhart@linux.intel.com>
13  *
14  * HISTORY
15  *      2008-May-5: Initial version by Darren Hart <dvhart@linux.intel.com>
16  *
17  *****************************************************************************/
18 
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "atomic.h"
28 #include "futextest.h"
29 #include "logging.h"
30 
31 #define TEST_NAME "futex-requeue-pi-signal-restart"
32 #define DELAY_US 100
33 
34 futex_t f1 = FUTEX_INITIALIZER;
35 futex_t f2 = FUTEX_INITIALIZER;
36 atomic_t requeued = ATOMIC_INITIALIZER;
37 
38 int waiter_ret = 0;
39 
usage(char * prog)40 void usage(char *prog)
41 {
42 	printf("Usage: %s\n", prog);
43 	printf("  -c	Use color\n");
44 	printf("  -h	Display this help message\n");
45 	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
46 	       VQUIET, VCRITICAL, VINFO);
47 }
48 
create_rt_thread(pthread_t * pth,void * (* func)(void *),void * arg,int policy,int prio)49 int create_rt_thread(pthread_t *pth, void*(*func)(void *), void *arg,
50 		     int policy, int prio)
51 {
52 	struct sched_param schedp;
53 	pthread_attr_t attr;
54 	int ret;
55 
56 	pthread_attr_init(&attr);
57 	memset(&schedp, 0, sizeof(schedp));
58 
59 	ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
60 	if (ret) {
61 		error("pthread_attr_setinheritsched\n", ret);
62 		return -1;
63 	}
64 
65 	ret = pthread_attr_setschedpolicy(&attr, policy);
66 	if (ret) {
67 		error("pthread_attr_setschedpolicy\n", ret);
68 		return -1;
69 	}
70 
71 	schedp.sched_priority = prio;
72 	ret = pthread_attr_setschedparam(&attr, &schedp);
73 	if (ret) {
74 		error("pthread_attr_setschedparam\n", ret);
75 		return -1;
76 	}
77 
78 	ret = pthread_create(pth, &attr, func, arg);
79 	if (ret) {
80 		error("pthread_create\n", ret);
81 		return -1;
82 	}
83 	return 0;
84 }
85 
handle_signal(int signo)86 void handle_signal(int signo)
87 {
88 	info("signal received %s requeue\n",
89 	     requeued.val ? "after" : "prior to");
90 }
91 
waiterfn(void * arg)92 void *waiterfn(void *arg)
93 {
94 	unsigned int old_val;
95 	int res;
96 
97 	waiter_ret = RET_PASS;
98 
99 	info("Waiter running\n");
100 	info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
101 	old_val = f1;
102 	res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL,
103 				    FUTEX_PRIVATE_FLAG);
104 	if (!requeued.val || errno != EWOULDBLOCK) {
105 		fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n",
106 		     res, strerror(errno));
107 		info("w2:futex: %x\n", f2);
108 		if (!res)
109 			futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
110 		waiter_ret = RET_FAIL;
111 	}
112 
113 	info("Waiter exiting with %d\n", waiter_ret);
114 	pthread_exit(NULL);
115 }
116 
117 
main(int argc,char * argv[])118 int main(int argc, char *argv[])
119 {
120 	unsigned int old_val;
121 	struct sigaction sa;
122 	pthread_t waiter;
123 	int c, res, ret = RET_PASS;
124 
125 	while ((c = getopt(argc, argv, "chv:")) != -1) {
126 		switch (c) {
127 		case 'c':
128 			log_color(1);
129 			break;
130 		case 'h':
131 			usage(basename(argv[0]));
132 			exit(0);
133 		case 'v':
134 			log_verbosity(atoi(optarg));
135 			break;
136 		default:
137 			usage(basename(argv[0]));
138 			exit(1);
139 		}
140 	}
141 
142 	ksft_print_header();
143 	ksft_set_plan(1);
144 	ksft_print_msg("%s: Test signal handling during requeue_pi\n",
145 	       basename(argv[0]));
146 	ksft_print_msg("\tArguments: <none>\n");
147 
148 	sa.sa_handler = handle_signal;
149 	sigemptyset(&sa.sa_mask);
150 	sa.sa_flags = 0;
151 	if (sigaction(SIGUSR1, &sa, NULL)) {
152 		error("sigaction\n", errno);
153 		exit(1);
154 	}
155 
156 	info("m1:f2: %x\n", f2);
157 	info("Creating waiter\n");
158 	res = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1);
159 	if (res) {
160 		error("Creating waiting thread failed", res);
161 		ret = RET_ERROR;
162 		goto out;
163 	}
164 
165 	info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
166 	info("m2:f2: %x\n", f2);
167 	futex_lock_pi(&f2, 0, 0, FUTEX_PRIVATE_FLAG);
168 	info("m3:f2: %x\n", f2);
169 
170 	while (1) {
171 		/*
172 		 * signal the waiter before requeue, waiter should automatically
173 		 * restart futex_wait_requeue_pi() in the kernel. Wait for the
174 		 * waiter to block on f1 again.
175 		 */
176 		info("Issuing SIGUSR1 to waiter\n");
177 		pthread_kill(waiter, SIGUSR1);
178 		usleep(DELAY_US);
179 
180 		info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
181 		old_val = f1;
182 		res = futex_cmp_requeue_pi(&f1, old_val, &(f2), 1, 0,
183 					   FUTEX_PRIVATE_FLAG);
184 		/*
185 		 * If res is non-zero, we either requeued the waiter or hit an
186 		 * error, break out and handle it. If it is zero, then the
187 		 * signal may have hit before the waiter was blocked on f1.
188 		 * Try again.
189 		 */
190 		if (res > 0) {
191 			atomic_set(&requeued, 1);
192 			break;
193 		} else if (res < 0) {
194 			error("FUTEX_CMP_REQUEUE_PI failed\n", errno);
195 			ret = RET_ERROR;
196 			break;
197 		}
198 	}
199 	info("m4:f2: %x\n", f2);
200 
201 	/*
202 	 * Signal the waiter after requeue, waiter should return from
203 	 * futex_wait_requeue_pi() with EWOULDBLOCK. Join the thread here so the
204 	 * futex_unlock_pi() can't happen before the signal wakeup is detected
205 	 * in the kernel.
206 	 */
207 	info("Issuing SIGUSR1 to waiter\n");
208 	pthread_kill(waiter, SIGUSR1);
209 	info("Waiting for waiter to return\n");
210 	pthread_join(waiter, NULL);
211 
212 	info("Calling FUTEX_UNLOCK_PI on mutex=%x @ %p\n", f2, &f2);
213 	futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
214 	info("m5:f2: %x\n", f2);
215 
216  out:
217 	if (ret == RET_PASS && waiter_ret)
218 		ret = waiter_ret;
219 
220 	print_result(TEST_NAME, ret);
221 	return ret;
222 }
223