d4ae271dfaae.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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;
  21. struct io_uring_sqe *sqe;
  22. struct io_uring_cqe *cqe;
  23. struct iovec *iovecs;
  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. if (fname != argv[1])
  42. unlink(fname);
  43. if (fd < 0) {
  44. perror("open");
  45. goto out;
  46. }
  47. iovecs = t_calloc(10, sizeof(struct iovec));
  48. for (i = 0; i < 10; i++) {
  49. t_posix_memalign(&buf, 4096, 4096);
  50. iovecs[i].iov_base = buf;
  51. iovecs[i].iov_len = 4096;
  52. }
  53. ret = io_uring_register_files(&ring, &fd, 1);
  54. if (ret < 0) {
  55. fprintf(stderr, "register files %d\n", ret);
  56. goto out;
  57. }
  58. for (i = 0; i < 10; i++) {
  59. sqe = io_uring_get_sqe(&ring);
  60. if (!sqe)
  61. break;
  62. io_uring_prep_readv(sqe, 0, &iovecs[i], 1, 0);
  63. sqe->flags |= IOSQE_FIXED_FILE;
  64. ret = io_uring_submit(&ring);
  65. usleep(1000);
  66. }
  67. for (i = 0; i < 10; i++) {
  68. ret = io_uring_wait_cqe(&ring, &cqe);
  69. if (ret) {
  70. fprintf(stderr, "wait_cqe=%d\n", ret);
  71. break;
  72. }
  73. if (cqe->res != 4096) {
  74. fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
  75. ret = 1;
  76. break;
  77. }
  78. io_uring_cqe_seen(&ring, cqe);
  79. }
  80. close(fd);
  81. out:
  82. io_uring_queue_exit(&ring);
  83. return ret;
  84. }