sq-poll-kthread.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test if io_uring SQ poll kthread is stopped when the userspace
  5. * process ended with or without closing the io_uring fd
  6. *
  7. */
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <pthread.h>
  13. #include <stdbool.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <signal.h>
  17. #include <poll.h>
  18. #include <sys/wait.h>
  19. #include <sys/epoll.h>
  20. #include "liburing.h"
  21. #include "helpers.h"
  22. #define SQ_THREAD_IDLE 2000
  23. #define BUF_SIZE 128
  24. #define KTHREAD_NAME "io_uring-sq"
  25. enum {
  26. TEST_OK = 0,
  27. TEST_SKIPPED = 1,
  28. TEST_FAILED = 2,
  29. };
  30. static int do_test_sq_poll_kthread_stopped(bool do_exit)
  31. {
  32. int ret = 0, pipe1[2];
  33. struct io_uring_params param;
  34. struct io_uring ring;
  35. struct io_uring_sqe *sqe;
  36. struct io_uring_cqe *cqe;
  37. uint8_t buf[BUF_SIZE];
  38. struct iovec iov;
  39. if (pipe(pipe1) != 0) {
  40. perror("pipe");
  41. return TEST_FAILED;
  42. }
  43. memset(&param, 0, sizeof(param));
  44. param.flags |= IORING_SETUP_SQPOLL;
  45. param.sq_thread_idle = SQ_THREAD_IDLE;
  46. ret = t_create_ring_params(16, &ring, &param);
  47. if (ret == T_SETUP_SKIP) {
  48. ret = TEST_FAILED;
  49. goto err_pipe;
  50. } else if (ret != T_SETUP_OK) {
  51. fprintf(stderr, "ring setup failed\n");
  52. ret = TEST_FAILED;
  53. goto err_pipe;
  54. }
  55. ret = io_uring_register_files(&ring, &pipe1[1], 1);
  56. if (ret) {
  57. fprintf(stderr, "file reg failed: %d\n", ret);
  58. ret = TEST_FAILED;
  59. goto err_uring;
  60. }
  61. iov.iov_base = buf;
  62. iov.iov_len = BUF_SIZE;
  63. sqe = io_uring_get_sqe(&ring);
  64. if (!sqe) {
  65. fprintf(stderr, "io_uring_get_sqe failed\n");
  66. ret = TEST_FAILED;
  67. goto err_uring;
  68. }
  69. io_uring_prep_writev(sqe, 0, &iov, 1, 0);
  70. sqe->flags |= IOSQE_FIXED_FILE;
  71. ret = io_uring_submit(&ring);
  72. if (ret < 0) {
  73. fprintf(stderr, "io_uring_submit failed - ret: %d\n",
  74. ret);
  75. ret = TEST_FAILED;
  76. goto err_uring;
  77. }
  78. ret = io_uring_wait_cqe(&ring, &cqe);
  79. if (ret < 0) {
  80. fprintf(stderr, "io_uring_wait_cqe - ret: %d\n",
  81. ret);
  82. ret = TEST_FAILED;
  83. goto err_uring;
  84. }
  85. if (cqe->res != BUF_SIZE) {
  86. fprintf(stderr, "unexpected cqe->res %d [expected %d]\n",
  87. cqe->res, BUF_SIZE);
  88. ret = TEST_FAILED;
  89. goto err_uring;
  90. }
  91. io_uring_cqe_seen(&ring, cqe);
  92. ret = TEST_OK;
  93. err_uring:
  94. if (do_exit)
  95. io_uring_queue_exit(&ring);
  96. err_pipe:
  97. close(pipe1[0]);
  98. close(pipe1[1]);
  99. return ret;
  100. }
  101. static int test_sq_poll_kthread_stopped(bool do_exit)
  102. {
  103. pid_t pid;
  104. int status = 0;
  105. pid = fork();
  106. if (pid == 0) {
  107. int ret = do_test_sq_poll_kthread_stopped(do_exit);
  108. exit(ret);
  109. }
  110. pid = wait(&status);
  111. if (status != 0)
  112. return WEXITSTATUS(status);
  113. sleep(1);
  114. if (system("ps --ppid 2 | grep " KTHREAD_NAME) == 0) {
  115. fprintf(stderr, "%s kthread still running!\n", KTHREAD_NAME);
  116. return TEST_FAILED;
  117. }
  118. return 0;
  119. }
  120. int main(int argc, char *argv[])
  121. {
  122. int ret;
  123. if (argc > 1)
  124. return 0;
  125. ret = test_sq_poll_kthread_stopped(true);
  126. if (ret == TEST_SKIPPED) {
  127. printf("test_sq_poll_kthread_stopped_exit: skipped\n");
  128. } else if (ret == TEST_FAILED) {
  129. fprintf(stderr, "test_sq_poll_kthread_stopped_exit failed\n");
  130. return ret;
  131. }
  132. ret = test_sq_poll_kthread_stopped(false);
  133. if (ret == TEST_SKIPPED) {
  134. printf("test_sq_poll_kthread_stopped_noexit: skipped\n");
  135. } else if (ret == TEST_FAILED) {
  136. fprintf(stderr, "test_sq_poll_kthread_stopped_noexit failed\n");
  137. return ret;
  138. }
  139. return 0;
  140. }