d4ae271dfaae.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Test case for SQPOLL missing a 'ret' clear in case of busy.
  5. *
  6. * Heavily based on a test case from
  7. * Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include "helpers.h"
  15. #include "liburing.h"
  16. #define FILE_SIZE (128 * 1024)
  17. int main(int argc, char *argv[])
  18. {
  19. struct io_uring ring;
  20. int i, fd, ret, __e;
  21. struct io_uring_sqe *sqe;
  22. struct io_uring_cqe *cqe;
  23. struct iovec *iovecs = NULL;
  24. struct io_uring_params p;
  25. char *fname;
  26. void *buf;
  27. memset(&p, 0, sizeof(p));
  28. p.flags = IORING_SETUP_SQPOLL;
  29. ret = t_create_ring_params(16, &ring, &p);
  30. if (ret == T_SETUP_SKIP)
  31. return T_EXIT_SKIP;
  32. else if (ret < 0)
  33. return T_EXIT_FAIL;
  34. if (argc > 1) {
  35. fname = argv[1];
  36. } else {
  37. fname = ".sqpoll.tmp";
  38. t_create_file(fname, FILE_SIZE);
  39. }
  40. fd = open(fname, O_RDONLY | O_DIRECT);
  41. __e = errno;
  42. if (fname != argv[1])
  43. unlink(fname);
  44. if (fd < 0) {
  45. if (__e == EINVAL || __e == EPERM || __e == EACCES)
  46. return T_EXIT_SKIP;
  47. fprintf(stderr, "open: %s\n", strerror(__e));
  48. goto out;
  49. }
  50. iovecs = t_calloc(10, sizeof(struct iovec));
  51. for (i = 0; i < 10; i++) {
  52. t_posix_memalign(&buf, 4096, 4096);
  53. iovecs[i].iov_base = buf;
  54. iovecs[i].iov_len = 4096;
  55. }
  56. ret = io_uring_register_files(&ring, &fd, 1);
  57. if (ret < 0) {
  58. fprintf(stderr, "register files %d\n", ret);
  59. goto out;
  60. }
  61. for (i = 0; i < 10; i++) {
  62. sqe = io_uring_get_sqe(&ring);
  63. if (!sqe)
  64. break;
  65. io_uring_prep_readv(sqe, 0, &iovecs[i], 1, 0);
  66. sqe->flags |= IOSQE_FIXED_FILE;
  67. ret = io_uring_submit(&ring);
  68. usleep(1000);
  69. }
  70. for (i = 0; i < 10; i++) {
  71. ret = io_uring_wait_cqe(&ring, &cqe);
  72. if (ret) {
  73. fprintf(stderr, "wait_cqe=%d\n", ret);
  74. break;
  75. }
  76. if (cqe->res != 4096) {
  77. fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
  78. ret = 1;
  79. break;
  80. }
  81. io_uring_cqe_seen(&ring, cqe);
  82. }
  83. close(fd);
  84. out:
  85. io_uring_queue_exit(&ring);
  86. if (iovecs != NULL) { //
  87. for (i = 0; i < 10; i++)
  88. free(iovecs[i].iov_base);
  89. free(iovecs);
  90. }
  91. return ret;
  92. }