ce593a6c480a.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Test 5.7 regression with task_work not being run while a task is
  5. * waiting on another event in the kernel.
  6. */
  7. #include <errno.h>
  8. #include <poll.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/eventfd.h>
  12. #include <unistd.h>
  13. #include <pthread.h>
  14. #include "liburing.h"
  15. #include "helpers.h"
  16. static int use_sqpoll = 0;
  17. static void notify_fd(int fd)
  18. {
  19. char buf[8] = {0, 0, 0, 0, 0, 0, 1};
  20. int ret;
  21. ret = write(fd, &buf, 8);
  22. if (ret < 0)
  23. perror("write");
  24. }
  25. static void *delay_set_fd_from_thread(void *data)
  26. {
  27. int fd = (intptr_t) data;
  28. sleep(1);
  29. notify_fd(fd);
  30. return NULL;
  31. }
  32. int main(int argc, char *argv[])
  33. {
  34. struct io_uring_params p = {};
  35. struct io_uring ring;
  36. int loop_fd, other_fd;
  37. struct io_uring_sqe *sqe;
  38. struct io_uring_cqe *cqe = NULL;
  39. int ret, use_fd;
  40. char buf[8] = {0, 0, 0, 0, 0, 0, 1};
  41. pthread_t tid;
  42. if (argc > 1)
  43. return T_EXIT_SKIP;
  44. /* Create an eventfd to be registered with the loop to be
  45. * notified of events being ready
  46. */
  47. loop_fd = eventfd(0, EFD_CLOEXEC);
  48. if (loop_fd == -1) {
  49. fprintf(stderr, "eventfd errno=%d\n", errno);
  50. return T_EXIT_FAIL;
  51. }
  52. /* Create an eventfd that can create events */
  53. use_fd = other_fd = eventfd(0, EFD_CLOEXEC);
  54. if (other_fd == -1) {
  55. fprintf(stderr, "eventfd errno=%d\n", errno);
  56. return T_EXIT_FAIL;
  57. }
  58. if (use_sqpoll)
  59. p.flags = IORING_SETUP_SQPOLL;
  60. /* Setup the ring with a registered event fd to be notified on events */
  61. ret = t_create_ring_params(8, &ring, &p);
  62. if (ret == T_SETUP_SKIP)
  63. return T_EXIT_PASS;
  64. else if (ret < 0)
  65. return ret;
  66. ret = io_uring_register_eventfd(&ring, loop_fd);
  67. if (ret < 0) {
  68. fprintf(stderr, "register_eventfd=%d\n", ret);
  69. return T_EXIT_FAIL;
  70. }
  71. if (use_sqpoll) {
  72. ret = io_uring_register_files(&ring, &other_fd, 1);
  73. if (ret < 0) {
  74. fprintf(stderr, "register_files=%d\n", ret);
  75. return T_EXIT_FAIL;
  76. }
  77. use_fd = 0;
  78. }
  79. /* Submit a poll operation to wait on an event in other_fd */
  80. sqe = io_uring_get_sqe(&ring);
  81. io_uring_prep_poll_add(sqe, use_fd, POLLIN);
  82. sqe->user_data = 1;
  83. if (use_sqpoll)
  84. sqe->flags |= IOSQE_FIXED_FILE;
  85. ret = io_uring_submit(&ring);
  86. if (ret != 1) {
  87. fprintf(stderr, "submit=%d\n", ret);
  88. return T_EXIT_FAIL;
  89. }
  90. /*
  91. * CASE 3: Hangs forever in Linux 5.7.5; Works in Linux 5.6.0 When this
  92. * code is uncommented, we don't se a notification on other_fd until
  93. * _after_ we have started the read on loop_fd. In that case, the read() on
  94. * loop_fd seems to hang forever.
  95. */
  96. pthread_create(&tid, NULL, delay_set_fd_from_thread,
  97. (void*) (intptr_t) other_fd);
  98. /* Wait on the event fd for an event to be ready */
  99. do {
  100. ret = read(loop_fd, buf, 8);
  101. } while (ret < 0 && errno == EINTR);
  102. if (ret < 0) {
  103. perror("read");
  104. return T_EXIT_FAIL;
  105. } else if (ret != 8) {
  106. fprintf(stderr, "Odd-sized eventfd read: %d\n", ret);
  107. return T_EXIT_FAIL;
  108. }
  109. ret = io_uring_wait_cqe(&ring, &cqe);
  110. if (ret) {
  111. fprintf(stderr, "wait_cqe=%d\n", ret);
  112. return ret;
  113. }
  114. if (cqe->res < 0) {
  115. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  116. return T_EXIT_FAIL;
  117. }
  118. io_uring_cqe_seen(&ring, cqe);
  119. return T_EXIT_PASS;
  120. }