~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/futex/functional/futex_requeue_pi_mismatched_ops.c

Version: ~ [ linux-5.2-rc4 ] ~ [ linux-5.1.9 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.50 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.125 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.181 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.181 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.68 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /******************************************************************************
  2  *
  3  *   Copyright © International Business Machines  Corp., 2009
  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  *      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.
 14  *
 15  * AUTHOR
 16  *      Darren Hart <dvhart@linux.intel.com>
 17  *
 18  * HISTORY
 19  *      2009-Nov-9: Initial version by Darren Hart <dvhart@linux.intel.com>
 20  *
 21  *****************************************************************************/
 22 
 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"
 32 
 33 #define TEST_NAME "futex-requeue-pi-mismatched-ops"
 34 
 35 futex_t f1 = FUTEX_INITIALIZER;
 36 futex_t f2 = FUTEX_INITIALIZER;
 37 int child_ret = 0;
 38 
 39 void usage(char *prog)
 40 {
 41         printf("Usage: %s\n", prog);
 42         printf("  -c    Use color\n");
 43         printf("  -h    Display this help message\n");
 44         printf("  -v L  Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
 45                VQUIET, VCRITICAL, VINFO);
 46 }
 47 
 48 void *blocking_child(void *arg)
 49 {
 50         child_ret = futex_wait(&f1, f1, NULL, FUTEX_PRIVATE_FLAG);
 51         if (child_ret < 0) {
 52                 child_ret = -errno;
 53                 error("futex_wait\n", errno);
 54         }
 55         return (void *)&child_ret;
 56 }
 57 
 58 int main(int argc, char *argv[])
 59 {
 60         int ret = RET_PASS;
 61         pthread_t child;
 62         int c;
 63 
 64         while ((c = getopt(argc, argv, "chv:")) != -1) {
 65                 switch (c) {
 66                 case 'c':
 67                         log_color(1);
 68                         break;
 69                 case 'h':
 70                         usage(basename(argv[0]));
 71                         exit(0);
 72                 case 'v':
 73                         log_verbosity(atoi(optarg));
 74                         break;
 75                 default:
 76                         usage(basename(argv[0]));
 77                         exit(1);
 78                 }
 79         }
 80 
 81         ksft_print_header();
 82         ksft_print_msg("%s: Detect mismatched requeue_pi operations\n",
 83                basename(argv[0]));
 84 
 85         if (pthread_create(&child, NULL, blocking_child, NULL)) {
 86                 error("pthread_create\n", errno);
 87                 ret = RET_ERROR;
 88                 goto out;
 89         }
 90         /* Allow the child to block in the kernel. */
 91         sleep(1);
 92 
 93         /*
 94          * The kernel should detect the waiter did not setup the
 95          * q->requeue_pi_key and return -EINVAL. If it does not,
 96          * it likely gave the lock to the child, which is now hung
 97          * in the kernel.
 98          */
 99         ret = futex_cmp_requeue_pi(&f1, f1, &f2, 1, 0, FUTEX_PRIVATE_FLAG);
100         if (ret < 0) {
101                 if (errno == EINVAL) {
102                         /*
103                          * The kernel correctly detected the mismatched
104                          * requeue_pi target and aborted. Wake the child with
105                          * FUTEX_WAKE.
106                          */
107                         ret = futex_wake(&f1, 1, FUTEX_PRIVATE_FLAG);
108                         if (ret == 1) {
109                                 ret = RET_PASS;
110                         } else if (ret < 0) {
111                                 error("futex_wake\n", errno);
112                                 ret = RET_ERROR;
113                         } else {
114                                 error("futex_wake did not wake the child\n", 0);
115                                 ret = RET_ERROR;
116                         }
117                 } else {
118                         error("futex_cmp_requeue_pi\n", errno);
119                         ret = RET_ERROR;
120                 }
121         } else if (ret > 0) {
122                 fail("futex_cmp_requeue_pi failed to detect the mismatch\n");
123                 ret = RET_FAIL;
124         } else {
125                 error("futex_cmp_requeue_pi found no waiters\n", 0);
126                 ret = RET_ERROR;
127         }
128 
129         pthread_join(child, NULL);
130 
131         if (!ret)
132                 ret = child_ret;
133 
134  out:
135         /* If the kernel crashes, we shouldn't return at all. */
136         print_result(TEST_NAME, ret);
137         return ret;
138 }
139 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp