defer-tw-timeout.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test waiting for more events than what will be posted with
  5. * a timeout with DEFER_TASKRUN. All kernels should time out,
  6. * but a non-buggy kernel will end up with one CQE available
  7. * for reaping. Buggy kernels will not have processed the
  8. * task_work and will have 0 events.
  9. *
  10. */
  11. #include <errno.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <pthread.h>
  17. #include "liburing.h"
  18. #include "helpers.h"
  19. struct d {
  20. int fd;
  21. };
  22. static void *thread_fn(void *data)
  23. {
  24. struct d *d = data;
  25. int ret;
  26. usleep(100000);
  27. ret = write(d->fd, "Hello", 5);
  28. if (ret < 0)
  29. perror("write");
  30. return NULL;
  31. }
  32. static int test_poll(struct io_uring *ring)
  33. {
  34. struct io_uring_cqe *cqe;
  35. struct io_uring_sqe *sqe;
  36. struct __kernel_timespec ts;
  37. int ret, fds[2], i;
  38. pthread_t thread;
  39. char buf[32];
  40. struct d d;
  41. void *tret;
  42. if (pipe(fds) < 0) {
  43. perror("pipe");
  44. return 1;
  45. }
  46. d.fd = fds[1];
  47. sqe = io_uring_get_sqe(ring);
  48. io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);
  49. pthread_create(&thread, NULL, thread_fn, &d);
  50. ts.tv_sec = 1;
  51. ts.tv_nsec = 0;
  52. ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL);
  53. if (ret != 1) {
  54. fprintf(stderr, "unexpected wait ret %d\n", ret);
  55. return T_EXIT_FAIL;
  56. }
  57. for (i = 0; i < 2; i++) {
  58. ret = io_uring_peek_cqe(ring, &cqe);
  59. if (ret)
  60. break;
  61. io_uring_cqe_seen(ring, cqe);
  62. }
  63. if (i != 1) {
  64. fprintf(stderr, "Got %d request, expected 1\n", i);
  65. return T_EXIT_FAIL;
  66. }
  67. pthread_join(thread, &tret);
  68. return T_EXIT_PASS;
  69. }
  70. static int test_file(struct io_uring *ring, char *__fname)
  71. {
  72. struct io_uring_cqe *cqe;
  73. struct io_uring_sqe *sqe;
  74. struct __kernel_timespec ts;
  75. char filename[64], *fname;
  76. int fd, ret, i;
  77. void *buf;
  78. if (!__fname) {
  79. fname = filename;
  80. sprintf(fname, ".defer-tw-timeout.%d", getpid());
  81. t_create_file(fname, 128*1024);
  82. } else {
  83. fname = __fname;
  84. }
  85. fd = open(fname, O_RDONLY | O_DIRECT);
  86. if (fd < 0) {
  87. if (errno == EINVAL || errno == EPERM || errno == EACCES) {
  88. if (!__fname)
  89. unlink(fname);
  90. return T_EXIT_SKIP;
  91. }
  92. perror("open");
  93. if (!__fname)
  94. unlink(fname);
  95. return T_EXIT_FAIL;
  96. }
  97. if (!__fname)
  98. unlink(fname);
  99. if (posix_memalign(&buf, 4096, 4096)) {
  100. close(fd);
  101. return T_EXIT_FAIL;
  102. }
  103. sqe = io_uring_get_sqe(ring);
  104. io_uring_prep_read(sqe, fd, buf, 4096, 0);
  105. ts.tv_sec = 1;
  106. ts.tv_nsec = 0;
  107. ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL);
  108. if (ret != 1) {
  109. fprintf(stderr, "unexpected wait ret %d\n", ret);
  110. close(fd);
  111. free(buf);
  112. return T_EXIT_FAIL;
  113. }
  114. for (i = 0; i < 2; i++) {
  115. ret = io_uring_peek_cqe(ring, &cqe);
  116. if (ret)
  117. break;
  118. io_uring_cqe_seen(ring, cqe);
  119. }
  120. if (i != 1) {
  121. fprintf(stderr, "Got %d request, expected 1\n", i);
  122. close(fd);
  123. free(buf);
  124. return T_EXIT_FAIL;
  125. }
  126. close(fd);
  127. free(buf);
  128. return T_EXIT_PASS;
  129. }
  130. int main(int argc, char *argv[])
  131. {
  132. struct io_uring ring;
  133. char *fname = NULL;
  134. int ret;
  135. ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN);
  136. if (ret == -EINVAL)
  137. return T_EXIT_SKIP;
  138. if (argc > 1)
  139. fname = argv[1];
  140. ret = test_file(&ring, fname);
  141. if (ret != T_EXIT_PASS)
  142. return ret;
  143. ret = test_poll(&ring);
  144. if (ret != T_EXIT_PASS)
  145. return ret;
  146. return T_EXIT_PASS;
  147. }