irqchip: Fix dependencies for archs w/o HAS_IOMEM
[linux/fpc-iii.git] / tools / testing / selftests / futex / functional / futex_requeue_pi_mismatched_ops.c
blobd5e4f2c4da2a2d22bc8dc7ced47611b965b6b350
1 /******************************************************************************
3 * Copyright © International Business Machines Corp., 2009
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.
10 * DESCRIPTION
11 * 1. Block a thread using FUTEX_WAIT
12 * 2. Attempt to use FUTEX_CMP_REQUEUE_PI on the futex from 1.
13 * 3. The kernel must detect the mismatch and return -EINVAL.
15 * AUTHOR
16 * Darren Hart <dvhart@linux.intel.com>
18 * HISTORY
19 * 2009-Nov-9: Initial version by Darren Hart <dvhart@linux.intel.com>
21 *****************************************************************************/
23 #include <errno.h>
24 #include <getopt.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include "futextest.h"
31 #include "logging.h"
33 futex_t f1 = FUTEX_INITIALIZER;
34 futex_t f2 = FUTEX_INITIALIZER;
35 int child_ret = 0;
37 void usage(char *prog)
39 printf("Usage: %s\n", prog);
40 printf(" -c Use color\n");
41 printf(" -h Display this help message\n");
42 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
43 VQUIET, VCRITICAL, VINFO);
46 void *blocking_child(void *arg)
48 child_ret = futex_wait(&f1, f1, NULL, FUTEX_PRIVATE_FLAG);
49 if (child_ret < 0) {
50 child_ret = -errno;
51 error("futex_wait\n", errno);
53 return (void *)&child_ret;
56 int main(int argc, char *argv[])
58 int ret = RET_PASS;
59 pthread_t child;
60 int c;
62 while ((c = getopt(argc, argv, "chv:")) != -1) {
63 switch (c) {
64 case 'c':
65 log_color(1);
66 break;
67 case 'h':
68 usage(basename(argv[0]));
69 exit(0);
70 case 'v':
71 log_verbosity(atoi(optarg));
72 break;
73 default:
74 usage(basename(argv[0]));
75 exit(1);
79 printf("%s: Detect mismatched requeue_pi operations\n",
80 basename(argv[0]));
82 if (pthread_create(&child, NULL, blocking_child, NULL)) {
83 error("pthread_create\n", errno);
84 ret = RET_ERROR;
85 goto out;
87 /* Allow the child to block in the kernel. */
88 sleep(1);
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
94 * in the kernel.
96 ret = futex_cmp_requeue_pi(&f1, f1, &f2, 1, 0, FUTEX_PRIVATE_FLAG);
97 if (ret < 0) {
98 if (errno == EINVAL) {
100 * The kernel correctly detected the mismatched
101 * requeue_pi target and aborted. Wake the child with
102 * FUTEX_WAKE.
104 ret = futex_wake(&f1, 1, FUTEX_PRIVATE_FLAG);
105 if (ret == 1) {
106 ret = RET_PASS;
107 } else if (ret < 0) {
108 error("futex_wake\n", errno);
109 ret = RET_ERROR;
110 } else {
111 error("futex_wake did not wake the child\n", 0);
112 ret = RET_ERROR;
114 } else {
115 error("futex_cmp_requeue_pi\n", errno);
116 ret = RET_ERROR;
118 } else if (ret > 0) {
119 fail("futex_cmp_requeue_pi failed to detect the mismatch\n");
120 ret = RET_FAIL;
121 } else {
122 error("futex_cmp_requeue_pi found no waiters\n", 0);
123 ret = RET_ERROR;
126 pthread_join(child, NULL);
128 if (!ret)
129 ret = child_ret;
131 out:
132 /* If the kernel crashes, we shouldn't return at all. */
133 print_result(ret);
134 return ret;