linux/tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 *   Copyright © International Business Machines  Corp., 2006-2008
   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 *      This test exercises the futex_wait_requeue_pi() signal handling both
  12 *      before and after the requeue. The first should be restarted by the
  13 *      kernel. The latter should return EWOULDBLOCK to the waiter.
  14 *
  15 * AUTHORS
  16 *      Darren Hart <dvhart@linux.intel.com>
  17 *
  18 * HISTORY
  19 *      2008-May-5: Initial version by Darren Hart <dvhart@linux.intel.com>
  20 *
  21 *****************************************************************************/
  22
  23#include <errno.h>
  24#include <getopt.h>
  25#include <limits.h>
  26#include <pthread.h>
  27#include <signal.h>
  28#include <stdio.h>
  29#include <stdlib.h>
  30#include <string.h>
  31#include "atomic.h"
  32#include "futextest.h"
  33#include "logging.h"
  34
  35#define TEST_NAME "futex-requeue-pi-signal-restart"
  36#define DELAY_US 100
  37
  38futex_t f1 = FUTEX_INITIALIZER;
  39futex_t f2 = FUTEX_INITIALIZER;
  40atomic_t requeued = ATOMIC_INITIALIZER;
  41
  42int waiter_ret = 0;
  43
  44void usage(char *prog)
  45{
  46        printf("Usage: %s\n", prog);
  47        printf("  -c    Use color\n");
  48        printf("  -h    Display this help message\n");
  49        printf("  -v L  Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
  50               VQUIET, VCRITICAL, VINFO);
  51}
  52
  53int create_rt_thread(pthread_t *pth, void*(*func)(void *), void *arg,
  54                     int policy, int prio)
  55{
  56        struct sched_param schedp;
  57        pthread_attr_t attr;
  58        int ret;
  59
  60        pthread_attr_init(&attr);
  61        memset(&schedp, 0, sizeof(schedp));
  62
  63        ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  64        if (ret) {
  65                error("pthread_attr_setinheritsched\n", ret);
  66                return -1;
  67        }
  68
  69        ret = pthread_attr_setschedpolicy(&attr, policy);
  70        if (ret) {
  71                error("pthread_attr_setschedpolicy\n", ret);
  72                return -1;
  73        }
  74
  75        schedp.sched_priority = prio;
  76        ret = pthread_attr_setschedparam(&attr, &schedp);
  77        if (ret) {
  78                error("pthread_attr_setschedparam\n", ret);
  79                return -1;
  80        }
  81
  82        ret = pthread_create(pth, &attr, func, arg);
  83        if (ret) {
  84                error("pthread_create\n", ret);
  85                return -1;
  86        }
  87        return 0;
  88}
  89
  90void handle_signal(int signo)
  91{
  92        info("signal received %s requeue\n",
  93             requeued.val ? "after" : "prior to");
  94}
  95
  96void *waiterfn(void *arg)
  97{
  98        unsigned int old_val;
  99        int res;
 100
 101        waiter_ret = RET_PASS;
 102
 103        info("Waiter running\n");
 104        info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
 105        old_val = f1;
 106        res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL,
 107                                    FUTEX_PRIVATE_FLAG);
 108        if (!requeued.val || errno != EWOULDBLOCK) {
 109                fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n",
 110                     res, strerror(errno));
 111                info("w2:futex: %x\n", f2);
 112                if (!res)
 113                        futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
 114                waiter_ret = RET_FAIL;
 115        }
 116
 117        info("Waiter exiting with %d\n", waiter_ret);
 118        pthread_exit(NULL);
 119}
 120
 121
 122int main(int argc, char *argv[])
 123{
 124        unsigned int old_val;
 125        struct sigaction sa;
 126        pthread_t waiter;
 127        int c, res, ret = RET_PASS;
 128
 129        while ((c = getopt(argc, argv, "chv:")) != -1) {
 130                switch (c) {
 131                case 'c':
 132                        log_color(1);
 133                        break;
 134                case 'h':
 135                        usage(basename(argv[0]));
 136                        exit(0);
 137                case 'v':
 138                        log_verbosity(atoi(optarg));
 139                        break;
 140                default:
 141                        usage(basename(argv[0]));
 142                        exit(1);
 143                }
 144        }
 145
 146        ksft_print_header();
 147        ksft_print_msg("%s: Test signal handling during requeue_pi\n",
 148               basename(argv[0]));
 149        ksft_print_msg("\tArguments: <none>\n");
 150
 151        sa.sa_handler = handle_signal;
 152        sigemptyset(&sa.sa_mask);
 153        sa.sa_flags = 0;
 154        if (sigaction(SIGUSR1, &sa, NULL)) {
 155                error("sigaction\n", errno);
 156                exit(1);
 157        }
 158
 159        info("m1:f2: %x\n", f2);
 160        info("Creating waiter\n");
 161        res = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1);
 162        if (res) {
 163                error("Creating waiting thread failed", res);
 164                ret = RET_ERROR;
 165                goto out;
 166        }
 167
 168        info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
 169        info("m2:f2: %x\n", f2);
 170        futex_lock_pi(&f2, 0, 0, FUTEX_PRIVATE_FLAG);
 171        info("m3:f2: %x\n", f2);
 172
 173        while (1) {
 174                /*
 175                 * signal the waiter before requeue, waiter should automatically
 176                 * restart futex_wait_requeue_pi() in the kernel. Wait for the
 177                 * waiter to block on f1 again.
 178                 */
 179                info("Issuing SIGUSR1 to waiter\n");
 180                pthread_kill(waiter, SIGUSR1);
 181                usleep(DELAY_US);
 182
 183                info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
 184                old_val = f1;
 185                res = futex_cmp_requeue_pi(&f1, old_val, &(f2), 1, 0,
 186                                           FUTEX_PRIVATE_FLAG);
 187                /*
 188                 * If res is non-zero, we either requeued the waiter or hit an
 189                 * error, break out and handle it. If it is zero, then the
 190                 * signal may have hit before the the waiter was blocked on f1.
 191                 * Try again.
 192                 */
 193                if (res > 0) {
 194                        atomic_set(&requeued, 1);
 195                        break;
 196                } else if (res < 0) {
 197                        error("FUTEX_CMP_REQUEUE_PI failed\n", errno);
 198                        ret = RET_ERROR;
 199                        break;
 200                }
 201        }
 202        info("m4:f2: %x\n", f2);
 203
 204        /*
 205         * Signal the waiter after requeue, waiter should return from
 206         * futex_wait_requeue_pi() with EWOULDBLOCK. Join the thread here so the
 207         * futex_unlock_pi() can't happen before the signal wakeup is detected
 208         * in the kernel.
 209         */
 210        info("Issuing SIGUSR1 to waiter\n");
 211        pthread_kill(waiter, SIGUSR1);
 212        info("Waiting for waiter to return\n");
 213        pthread_join(waiter, NULL);
 214
 215        info("Calling FUTEX_UNLOCK_PI on mutex=%x @ %p\n", f2, &f2);
 216        futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
 217        info("m5:f2: %x\n", f2);
 218
 219 out:
 220        if (ret == RET_PASS && waiter_ret)
 221                ret = waiter_ret;
 222
 223        print_result(TEST_NAME, ret);
 224        return ret;
 225}
 226