futex_wait_timeout.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "kselftest_harness.h"
  21. static long timeout_ns = 100000; /* 100us default timeout */
  22. static futex_t futex_pi;
  23. static pthread_barrier_t barrier;
  24. /*
  25. * Get a PI lock and hold it forever, so the main thread lock_pi will block
  26. * and we can test the timeout
  27. */
  28. void *get_pi_lock(void *arg)
  29. {
  30. int ret;
  31. volatile futex_t lock = 0;
  32. ret = futex_lock_pi(&futex_pi, NULL, 0, 0);
  33. if (ret != 0)
  34. ksft_exit_fail_msg("futex_lock_pi failed\n");
  35. pthread_barrier_wait(&barrier);
  36. /* Blocks forever */
  37. ret = futex_wait(&lock, 0, NULL, 0);
  38. ksft_exit_fail_msg("futex_wait failed\n");
  39. return NULL;
  40. }
  41. /*
  42. * Check if the function returned the expected error
  43. */
  44. static void test_timeout(int res, char *test_name, int err)
  45. {
  46. if (!res || errno != err) {
  47. ksft_test_result_fail("%s returned %d\n", test_name,
  48. res < 0 ? errno : res);
  49. } else {
  50. ksft_test_result_pass("%s succeeds\n", test_name);
  51. }
  52. }
  53. /*
  54. * Calculate absolute timeout and correct overflow
  55. */
  56. static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
  57. long timeout_ns)
  58. {
  59. if (clock_gettime(clockid, to))
  60. ksft_exit_fail_msg("clock_gettime failed\n");
  61. to->tv_nsec += timeout_ns;
  62. if (to->tv_nsec >= 1000000000) {
  63. to->tv_sec++;
  64. to->tv_nsec -= 1000000000;
  65. }
  66. return 0;
  67. }
  68. TEST(wait_bitset)
  69. {
  70. futex_t f1 = FUTEX_INITIALIZER;
  71. struct timespec to;
  72. int res;
  73. /* initialize relative timeout */
  74. to.tv_sec = 0;
  75. to.tv_nsec = timeout_ns;
  76. res = futex_wait(&f1, f1, &to, 0);
  77. test_timeout(res, "futex_wait relative", ETIMEDOUT);
  78. /* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
  79. if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
  80. ksft_test_result_error("get_time error");
  81. res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
  82. test_timeout(res, "futex_wait_bitset realtime", ETIMEDOUT);
  83. /* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
  84. if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
  85. ksft_test_result_error("get_time error");
  86. res = futex_wait_bitset(&f1, f1, &to, 1, 0);
  87. test_timeout(res, "futex_wait_bitset monotonic", ETIMEDOUT);
  88. }
  89. TEST(requeue_pi)
  90. {
  91. futex_t f1 = FUTEX_INITIALIZER;
  92. struct timespec to;
  93. int res;
  94. /* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
  95. if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
  96. ksft_test_result_error("get_time error");
  97. res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
  98. test_timeout(res, "futex_wait_requeue_pi realtime", ETIMEDOUT);
  99. /* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
  100. if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
  101. ksft_test_result_error("get_time error");
  102. res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
  103. test_timeout(res, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
  104. }
  105. TEST(lock_pi)
  106. {
  107. struct timespec to;
  108. pthread_t thread;
  109. int res;
  110. /* Create a thread that will lock forever so any waiter will timeout */
  111. pthread_barrier_init(&barrier, NULL, 2);
  112. pthread_create(&thread, NULL, get_pi_lock, NULL);
  113. /* Wait until the other thread calls futex_lock_pi() */
  114. pthread_barrier_wait(&barrier);
  115. pthread_barrier_destroy(&barrier);
  116. /*
  117. * FUTEX_LOCK_PI with CLOCK_REALTIME
  118. * Due to historical reasons, FUTEX_LOCK_PI supports only realtime
  119. * clock, but requires the caller to not set CLOCK_REALTIME flag.
  120. *
  121. * If you call FUTEX_LOCK_PI with a monotonic clock, it'll be
  122. * interpreted as a realtime clock, and (unless you mess your machine's
  123. * time or your time machine) the monotonic clock value is always
  124. * smaller than realtime and the syscall will timeout immediately.
  125. */
  126. if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
  127. ksft_test_result_error("get_time error");
  128. res = futex_lock_pi(&futex_pi, &to, 0, 0);
  129. test_timeout(res, "futex_lock_pi realtime", ETIMEDOUT);
  130. /* Test operations that don't support FUTEX_CLOCK_REALTIME */
  131. res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
  132. test_timeout(res, "futex_lock_pi invalid timeout flag", ENOSYS);
  133. }
  134. TEST(waitv)
  135. {
  136. futex_t f1 = FUTEX_INITIALIZER;
  137. struct futex_waitv waitv = {
  138. .uaddr = (uintptr_t)&f1,
  139. .val = f1,
  140. .flags = FUTEX_32,
  141. .__reserved = 0,
  142. };
  143. struct timespec to;
  144. int res;
  145. /* futex_waitv with CLOCK_MONOTONIC */
  146. if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
  147. ksft_test_result_error("get_time error");
  148. res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
  149. test_timeout(res, "futex_waitv monotonic", ETIMEDOUT);
  150. /* futex_waitv with CLOCK_REALTIME */
  151. if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
  152. ksft_test_result_error("get_time error");
  153. res = futex_waitv(&waitv, 1, 0, &to, CLOCK_REALTIME);
  154. test_timeout(res, "futex_waitv realtime", ETIMEDOUT);
  155. }
  156. TEST_HARNESS_MAIN