1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
4 * Copyright © International Business Machines Corp., 2006-2008
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.
12 * Darren Hart <dvhart@linux.intel.com>
15 * 2008-May-5: Initial version by Darren Hart <dvhart@linux.intel.com>
17 *****************************************************************************/
28 #include "futextest.h"
31 #define TEST_NAME "futex-requeue-pi-signal-restart"
34 futex_t f1
= FUTEX_INITIALIZER
;
35 futex_t f2
= FUTEX_INITIALIZER
;
36 atomic_t requeued
= ATOMIC_INITIALIZER
;
40 void usage(char *prog
)
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
);
49 int create_rt_thread(pthread_t
*pth
, void*(*func
)(void *), void *arg
,
52 struct sched_param schedp
;
56 pthread_attr_init(&attr
);
57 memset(&schedp
, 0, sizeof(schedp
));
59 ret
= pthread_attr_setinheritsched(&attr
, PTHREAD_EXPLICIT_SCHED
);
61 error("pthread_attr_setinheritsched\n", ret
);
65 ret
= pthread_attr_setschedpolicy(&attr
, policy
);
67 error("pthread_attr_setschedpolicy\n", ret
);
71 schedp
.sched_priority
= prio
;
72 ret
= pthread_attr_setschedparam(&attr
, &schedp
);
74 error("pthread_attr_setschedparam\n", ret
);
78 ret
= pthread_create(pth
, &attr
, func
, arg
);
80 error("pthread_create\n", ret
);
86 void handle_signal(int signo
)
88 info("signal received %s requeue\n",
89 requeued
.val
? "after" : "prior to");
92 void *waiterfn(void *arg
)
97 waiter_ret
= RET_PASS
;
99 info("Waiter running\n");
100 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2
, &f2
);
102 res
= futex_wait_requeue_pi(&f1
, old_val
, &(f2
), NULL
,
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
);
109 futex_unlock_pi(&f2
, FUTEX_PRIVATE_FLAG
);
110 waiter_ret
= RET_FAIL
;
113 info("Waiter exiting with %d\n", waiter_ret
);
118 int main(int argc
, char *argv
[])
120 unsigned int old_val
;
123 int c
, res
, ret
= RET_PASS
;
125 while ((c
= getopt(argc
, argv
, "chv:")) != -1) {
131 usage(basename(argv
[0]));
134 log_verbosity(atoi(optarg
));
137 usage(basename(argv
[0]));
144 ksft_print_msg("%s: Test signal handling during requeue_pi\n",
146 ksft_print_msg("\tArguments: <none>\n");
148 sa
.sa_handler
= handle_signal
;
149 sigemptyset(&sa
.sa_mask
);
151 if (sigaction(SIGUSR1
, &sa
, NULL
)) {
152 error("sigaction\n", errno
);
156 info("m1:f2: %x\n", f2
);
157 info("Creating waiter\n");
158 res
= create_rt_thread(&waiter
, waiterfn
, NULL
, SCHED_FIFO
, 1);
160 error("Creating waiting thread failed", res
);
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
);
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.
176 info("Issuing SIGUSR1 to waiter\n");
177 pthread_kill(waiter
, SIGUSR1
);
180 info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
182 res
= futex_cmp_requeue_pi(&f1
, old_val
, &(f2
), 1, 0,
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 the waiter was blocked on f1.
191 atomic_set(&requeued
, 1);
193 } else if (res
< 0) {
194 error("FUTEX_CMP_REQUEUE_PI failed\n", errno
);
199 info("m4:f2: %x\n", f2
);
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
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
);
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
);
217 if (ret
== RET_PASS
&& waiter_ret
)
220 print_result(TEST_NAME
, ret
);