futex_wait_timeout.c 5.7 KB

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