~ [ 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.8 ] ~ [ linux-5.7.14 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.57 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.138 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.193 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ 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.85 ] ~ [ 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-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 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /******************************************************************************
  3  *
  4  *   Copyright © International Business Machines  Corp., 2009
  5  *
  6  * DESCRIPTION
  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.
 10  *
 11  * AUTHOR
 12  *      Darren Hart <dvhart@linux.intel.com>
 13  *
 14  * HISTORY
 15  *      2009-Nov-9: Initial version by Darren Hart <dvhart@linux.intel.com>
 16  *
 17  *****************************************************************************/
 18 
 19 #include <errno.h>
 20 #include <getopt.h>
 21 #include <pthread.h>
 22 #include <stdio.h>
 23 #include <stdlib.h>
 24 #include <string.h>
 25 #include <time.h>
 26 #include "futextest.h"
 27 #include "logging.h"
 28 
 29 #define TEST_NAME "futex-requeue-pi-mismatched-ops"
 30 
 31 futex_t f1 = FUTEX_INITIALIZER;
 32 futex_t f2 = FUTEX_INITIALIZER;
 33 int child_ret = 0;
 34 
 35 void usage(char *prog)
 36 {
 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);
 42 }
 43 
 44 void *blocking_child(void *arg)
 45 {
 46         child_ret = futex_wait(&f1, f1, NULL, FUTEX_PRIVATE_FLAG);
 47         if (child_ret < 0) {
 48                 child_ret = -errno;
 49                 error("futex_wait\n", errno);
 50         }
 51         return (void *)&child_ret;
 52 }
 53 
 54 int main(int argc, char *argv[])
 55 {
 56         int ret = RET_PASS;
 57         pthread_t child;
 58         int c;
 59 
 60         while ((c = getopt(argc, argv, "chv:")) != -1) {
 61                 switch (c) {
 62                 case 'c':
 63                         log_color(1);
 64                         break;
 65                 case 'h':
 66                         usage(basename(argv[0]));
 67                         exit(0);
 68                 case 'v':
 69                         log_verbosity(atoi(optarg));
 70                         break;
 71                 default:
 72                         usage(basename(argv[0]));
 73                         exit(1);
 74                 }
 75         }
 76 
 77         ksft_print_header();
 78         ksft_set_plan(1);
 79         ksft_print_msg("%s: Detect mismatched requeue_pi operations\n",
 80                basename(argv[0]));
 81 
 82         if (pthread_create(&child, NULL, blocking_child, NULL)) {
 83                 error("pthread_create\n", errno);
 84                 ret = RET_ERROR;
 85                 goto out;
 86         }
 87         /* Allow the child to block in the kernel. */
 88         sleep(1);
 89 
 90         /*
 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.
 95          */
 96         ret = futex_cmp_requeue_pi(&f1, f1, &f2, 1, 0, FUTEX_PRIVATE_FLAG);
 97         if (ret < 0) {
 98                 if (errno == EINVAL) {
 99                         /*
100                          * The kernel correctly detected the mismatched
101                          * requeue_pi target and aborted. Wake the child with
102                          * FUTEX_WAKE.
103                          */
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;
113                         }
114                 } else {
115                         error("futex_cmp_requeue_pi\n", errno);
116                         ret = RET_ERROR;
117                 }
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;
124         }
125 
126         pthread_join(child, NULL);
127 
128         if (!ret)
129                 ret = child_ret;
130 
131  out:
132         /* If the kernel crashes, we shouldn't return at all. */
133         print_result(TEST_NAME, ret);
134         return ret;
135 }
136 

~ [ 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