1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
4 * Copyright © International Business Machines Corp., 2009
7 * 1. Block a thread using FUTEX_WAIT
8 * 2. Attempt to use FUTEX_CMP_REQUEUE_PI on the futex from 1.
9 * 3. The kernel must detect the mismatch and return -EINVAL.
12 * Darren Hart <dvhart@linux.intel.com>
15 * 2009-Nov-9: Initial version by Darren Hart <dvhart@linux.intel.com>
17 *****************************************************************************/
26 #include "futextest.h"
29 #define TEST_NAME "futex-requeue-pi-mismatched-ops"
31 futex_t f1
= FUTEX_INITIALIZER
;
32 futex_t f2
= FUTEX_INITIALIZER
;
35 void usage(char *prog
)
37 printf("Usage: %s\n", prog
);
38 printf(" -c Use color\n");
39 printf(" -h Display this help message\n");
40 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
41 VQUIET
, VCRITICAL
, VINFO
);
44 void *blocking_child(void *arg
)
46 child_ret
= futex_wait(&f1
, f1
, NULL
, FUTEX_PRIVATE_FLAG
);
49 error("futex_wait\n", errno
);
51 return (void *)&child_ret
;
54 int main(int argc
, char *argv
[])
60 while ((c
= getopt(argc
, argv
, "chv:")) != -1) {
66 usage(basename(argv
[0]));
69 log_verbosity(atoi(optarg
));
72 usage(basename(argv
[0]));
79 ksft_print_msg("%s: Detect mismatched requeue_pi operations\n",
82 if (pthread_create(&child
, NULL
, blocking_child
, NULL
)) {
83 error("pthread_create\n", errno
);
87 /* Allow the child to block in the kernel. */
91 * The kernel should detect the waiter did not setup the
92 * q->requeue_pi_key and return -EINVAL. If it does not,
93 * it likely gave the lock to the child, which is now hung
96 ret
= futex_cmp_requeue_pi(&f1
, f1
, &f2
, 1, 0, FUTEX_PRIVATE_FLAG
);
98 if (errno
== EINVAL
) {
100 * The kernel correctly detected the mismatched
101 * requeue_pi target and aborted. Wake the child with
104 ret
= futex_wake(&f1
, 1, FUTEX_PRIVATE_FLAG
);
107 } else if (ret
< 0) {
108 error("futex_wake\n", errno
);
111 error("futex_wake did not wake the child\n", 0);
115 error("futex_cmp_requeue_pi\n", errno
);
118 } else if (ret
> 0) {
119 fail("futex_cmp_requeue_pi failed to detect the mismatch\n");
122 error("futex_cmp_requeue_pi found no waiters\n", 0);
126 pthread_join(child
, NULL
);
132 /* If the kernel crashes, we shouldn't return at all. */
133 print_result(TEST_NAME
, ret
);