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