linux/tools/testing/selftests/futex/functional/futex_wait_timeout.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/******************************************************************************
   3 *
   4 *   Copyright © International Business Machines  Corp., 2009
   5 *
   6 * DESCRIPTION
   7 *      Block on a futex and wait for timeout.
   8 *
   9 * AUTHOR
  10 *      Darren Hart <dvhart@linux.intel.com>
  11 *
  12 * HISTORY
  13 *      2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com>
  14 *      2021-Apr-26: More test cases by André Almeida <andrealmeid@collabora.com>
  15 *
  16 *****************************************************************************/
  17
  18#include <pthread.h>
  19#include "futextest.h"
  20#include "logging.h"
  21
  22#define TEST_NAME "futex-wait-timeout"
  23
  24static long timeout_ns = 100000;        /* 100us default timeout */
  25static futex_t futex_pi;
  26
  27void usage(char *prog)
  28{
  29        printf("Usage: %s\n", prog);
  30        printf("  -c    Use color\n");
  31        printf("  -h    Display this help message\n");
  32        printf("  -t N  Timeout in nanoseconds (default: 100,000)\n");
  33        printf("  -v L  Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
  34               VQUIET, VCRITICAL, VINFO);
  35}
  36
  37/*
  38 * Get a PI lock and hold it forever, so the main thread lock_pi will block
  39 * and we can test the timeout
  40 */
  41void *get_pi_lock(void *arg)
  42{
  43        int ret;
  44        volatile futex_t lock = 0;
  45
  46        ret = futex_lock_pi(&futex_pi, NULL, 0, 0);
  47        if (ret != 0)
  48                error("futex_lock_pi failed\n", ret);
  49
  50        /* Blocks forever */
  51        ret = futex_wait(&lock, 0, NULL, 0);
  52        error("futex_wait failed\n", ret);
  53
  54        return NULL;
  55}
  56
  57/*
  58 * Check if the function returned the expected error
  59 */
  60static void test_timeout(int res, int *ret, char *test_name, int err)
  61{
  62        if (!res || errno != err) {
  63                ksft_test_result_fail("%s returned %d\n", test_name,
  64                                      res < 0 ? errno : res);
  65                *ret = RET_FAIL;
  66        } else {
  67                ksft_test_result_pass("%s succeeds\n", test_name);
  68        }
  69}
  70
  71/*
  72 * Calculate absolute timeout and correct overflow
  73 */
  74static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
  75                                 long timeout_ns)
  76{
  77        if (clock_gettime(clockid, to)) {
  78                error("clock_gettime failed\n", errno);
  79                return errno;
  80        }
  81
  82        to->tv_nsec += timeout_ns;
  83
  84        if (to->tv_nsec >= 1000000000) {
  85                to->tv_sec++;
  86                to->tv_nsec -= 1000000000;
  87        }
  88
  89        return 0;
  90}
  91
  92int main(int argc, char *argv[])
  93{
  94        futex_t f1 = FUTEX_INITIALIZER;
  95        int res, ret = RET_PASS;
  96        struct timespec to;
  97        pthread_t thread;
  98        int c;
  99
 100        while ((c = getopt(argc, argv, "cht:v:")) != -1) {
 101                switch (c) {
 102                case 'c':
 103                        log_color(1);
 104                        break;
 105                case 'h':
 106                        usage(basename(argv[0]));
 107                        exit(0);
 108                case 't':
 109                        timeout_ns = atoi(optarg);
 110                        break;
 111                case 'v':
 112                        log_verbosity(atoi(optarg));
 113                        break;
 114                default:
 115                        usage(basename(argv[0]));
 116                        exit(1);
 117                }
 118        }
 119
 120        ksft_print_header();
 121        ksft_set_plan(7);
 122        ksft_print_msg("%s: Block on a futex and wait for timeout\n",
 123               basename(argv[0]));
 124        ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
 125
 126        pthread_create(&thread, NULL, get_pi_lock, NULL);
 127
 128        /* initialize relative timeout */
 129        to.tv_sec = 0;
 130        to.tv_nsec = timeout_ns;
 131
 132        res = futex_wait(&f1, f1, &to, 0);
 133        test_timeout(res, &ret, "futex_wait relative", ETIMEDOUT);
 134
 135        /* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
 136        if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
 137                return RET_FAIL;
 138        res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
 139        test_timeout(res, &ret, "futex_wait_bitset realtime", ETIMEDOUT);
 140
 141        /* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
 142        if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
 143                return RET_FAIL;
 144        res = futex_wait_bitset(&f1, f1, &to, 1, 0);
 145        test_timeout(res, &ret, "futex_wait_bitset monotonic", ETIMEDOUT);
 146
 147        /* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
 148        if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
 149                return RET_FAIL;
 150        res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
 151        test_timeout(res, &ret, "futex_wait_requeue_pi realtime", ETIMEDOUT);
 152
 153        /* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
 154        if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
 155                return RET_FAIL;
 156        res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
 157        test_timeout(res, &ret, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
 158
 159        /*
 160         * FUTEX_LOCK_PI with CLOCK_REALTIME
 161         * Due to historical reasons, FUTEX_LOCK_PI supports only realtime
 162         * clock, but requires the caller to not set CLOCK_REALTIME flag.
 163         *
 164         * If you call FUTEX_LOCK_PI with a monotonic clock, it'll be
 165         * interpreted as a realtime clock, and (unless you mess your machine's
 166         * time or your time machine) the monotonic clock value is always
 167         * smaller than realtime and the syscall will timeout immediately.
 168         */
 169        if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
 170                return RET_FAIL;
 171        res = futex_lock_pi(&futex_pi, &to, 0, 0);
 172        test_timeout(res, &ret, "futex_lock_pi realtime", ETIMEDOUT);
 173
 174        /* Test operations that don't support FUTEX_CLOCK_REALTIME */
 175        res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
 176        test_timeout(res, &ret, "futex_lock_pi invalid timeout flag", ENOSYS);
 177
 178        ksft_print_cnts();
 179        return ret;
 180}
 181