iopoll-overflow.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: IOPOLL with overflow test case
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include <sys/types.h>
  13. #include <poll.h>
  14. #include <sys/eventfd.h>
  15. #include <sys/resource.h>
  16. #include "helpers.h"
  17. #include "liburing.h"
  18. #include "../src/syscall.h"
  19. #define FILE_SIZE (128 * 1024)
  20. #define BS 4096
  21. #define BUFFERS (FILE_SIZE / BS)
  22. static struct iovec *vecs;
  23. static int test(struct io_uring *ring, int fd)
  24. {
  25. struct io_uring_sqe *sqe;
  26. int i, j, ret;
  27. loff_t off;
  28. off = FILE_SIZE - BS;
  29. for (j = 0; j < 8; j++) {
  30. for (i = 0; i < BUFFERS; i++) {
  31. sqe = io_uring_get_sqe(ring);
  32. io_uring_prep_read(sqe, fd, vecs[i].iov_base,
  33. vecs[i].iov_len, off);
  34. if (!off)
  35. off = FILE_SIZE - BS;
  36. else
  37. off -= BS;
  38. }
  39. ret = io_uring_submit(ring);
  40. if (ret != BUFFERS) {
  41. fprintf(stderr, "submitted %d\n", ret);
  42. return T_EXIT_FAIL;
  43. }
  44. }
  45. sleep(1);
  46. ret = __sys_io_uring_enter(ring->ring_fd, 0, BUFFERS * 8,
  47. IORING_ENTER_GETEVENTS, NULL);
  48. for (i = 0; i < BUFFERS * 8; i++) {
  49. struct io_uring_cqe *cqe;
  50. ret = io_uring_wait_cqe(ring, &cqe);
  51. if (ret) {
  52. fprintf(stderr, "wait=%d\n", ret);
  53. return T_EXIT_FAIL;
  54. }
  55. io_uring_cqe_seen(ring, cqe);
  56. }
  57. return T_EXIT_PASS;
  58. }
  59. int main(int argc, char *argv[])
  60. {
  61. struct io_uring_params p = { };
  62. struct io_uring ring;
  63. char buf[256];
  64. char *fname;
  65. int ret, fd;
  66. p.flags = IORING_SETUP_IOPOLL | IORING_SETUP_CQSIZE;
  67. p.cq_entries = 64;
  68. ret = t_create_ring_params(64, &ring, &p);
  69. if (ret == T_SETUP_SKIP)
  70. return 0;
  71. if (ret != T_SETUP_OK) {
  72. fprintf(stderr, "ring create failed: %d\n", ret);
  73. return 1;
  74. }
  75. if (argc > 1) {
  76. fname = argv[1];
  77. } else {
  78. srand((unsigned)time(NULL));
  79. snprintf(buf, sizeof(buf), ".basic-rw-%u-%u",
  80. (unsigned)rand(), (unsigned)getpid());
  81. fname = buf;
  82. t_create_file(fname, FILE_SIZE);
  83. }
  84. fd = open(fname, O_RDONLY | O_DIRECT);
  85. if (fd < 0) {
  86. if (errno == EINVAL || errno == EACCES || errno == EPERM) {
  87. if (fname != argv[1])
  88. unlink(fname);
  89. return T_EXIT_SKIP;
  90. }
  91. perror("open");
  92. goto err;
  93. }
  94. vecs = t_create_buffers(BUFFERS, BS);
  95. ret = test(&ring, fd);
  96. if (fname != argv[1])
  97. unlink(fname);
  98. return ret;
  99. err:
  100. if (fname != argv[1])
  101. unlink(fname);
  102. return T_EXIT_FAIL;
  103. }