init-mem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: Check that io_uring_queue_init_mem() doesn't underestimate
  5. * the memory required for various size rings.
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sys/mman.h>
  11. #include <linux/mman.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <netinet/udp.h>
  15. #include <arpa/inet.h>
  16. #include <net/if.h>
  17. #include "liburing.h"
  18. #include "helpers.h"
  19. #define PRE_RED 0x5aa55aa55aa55aa5ULL
  20. #define POST_RED 0xa55aa55aa55aa55aULL
  21. struct ctx {
  22. struct io_uring ring;
  23. void *ring_mem;
  24. void *mem;
  25. unsigned long long *pre;
  26. unsigned long long *post;
  27. };
  28. struct q_entries {
  29. unsigned int sqes;
  30. unsigned int cqes;
  31. };
  32. static int setup_ctx(struct ctx *ctx, struct q_entries *q)
  33. {
  34. struct io_uring_params p = { };
  35. int ret;
  36. if (posix_memalign(&ctx->mem, 4096, 2*1024*1024))
  37. return T_EXIT_FAIL;
  38. ctx->pre = ctx->mem + 4096 - sizeof(unsigned long long);
  39. *ctx->pre = PRE_RED;
  40. ctx->ring_mem = ctx->mem + 4096;
  41. p.flags |= IORING_SETUP_CQSIZE | IORING_SETUP_NO_SQARRAY;
  42. p.sq_entries = q->sqes;
  43. p.cq_entries = q->cqes;
  44. ret = io_uring_queue_init_mem(q->sqes, &ctx->ring, &p,
  45. ctx->ring_mem, 2*1024*1024);
  46. if (ret < 0) {
  47. if (ret == -EINVAL)
  48. return T_EXIT_SKIP;
  49. fprintf(stderr, "queue init: %d\n", ret);
  50. return T_EXIT_FAIL;
  51. }
  52. ctx->post = ctx->ring_mem + ret;
  53. *ctx->post = POST_RED;
  54. return 0;
  55. }
  56. static void clean_ctx(struct ctx *ctx)
  57. {
  58. io_uring_queue_exit(&ctx->ring);
  59. free(ctx->mem);
  60. }
  61. static int check_red(struct ctx *ctx, unsigned long i)
  62. {
  63. int fail = 0;
  64. if (*ctx->pre != PRE_RED) {
  65. printf("pre redzone=%llx at i=%lu\n", *ctx->pre, i);
  66. fail = 1;
  67. }
  68. if (*ctx->post != POST_RED) {
  69. printf("post redzone=%llx at i=%lu\n", *ctx->post, i);
  70. fail = 1;
  71. }
  72. return fail;
  73. }
  74. static int test(struct q_entries *q)
  75. {
  76. struct io_uring_sqe *sqe;
  77. struct io_uring_cqe *cqe;
  78. struct ctx ctx = { };
  79. unsigned long i, ud;
  80. int j, ret, batch;
  81. ret = setup_ctx(&ctx, q);
  82. if (ret == T_EXIT_SKIP) {
  83. clean_ctx(&ctx);
  84. return T_EXIT_SKIP;
  85. } else if (ret != T_EXIT_PASS) {
  86. return ret;
  87. }
  88. batch = 64;
  89. if (batch > q->sqes)
  90. batch = q->sqes;
  91. i = ud = 0;
  92. while (i < q->cqes * 2) {
  93. if (check_red(&ctx, i))
  94. return T_EXIT_FAIL;
  95. for (j = 0; j < batch; j++) {
  96. sqe = io_uring_get_sqe(&ctx.ring);
  97. io_uring_prep_nop(sqe);
  98. sqe->user_data = j + (unsigned long) i;
  99. }
  100. io_uring_submit(&ctx.ring);
  101. for (j = 0; j < batch; j++) {
  102. ret = io_uring_wait_cqe(&ctx.ring, &cqe);
  103. if (ret)
  104. goto err;
  105. if (cqe->user_data != ud) {
  106. fprintf(stderr, "ud=%lu, wanted %lu\n", (unsigned long) cqe->user_data, ud);
  107. goto err;
  108. }
  109. ud++;
  110. io_uring_cqe_seen(&ctx.ring, cqe);
  111. }
  112. i += batch;
  113. }
  114. clean_ctx(&ctx);
  115. return T_EXIT_PASS;
  116. err:
  117. clean_ctx(&ctx);
  118. return T_EXIT_FAIL;
  119. }
  120. int main(int argc, char *argv[])
  121. {
  122. struct q_entries q_entries[] = {
  123. { 256, 16384 },
  124. { 32, 4096 },
  125. { 128, 8192 },
  126. { 4096, 32768 },
  127. { 1, 8 },
  128. { 2, 1024 },
  129. };
  130. int i, ret;
  131. if (argc > 1)
  132. return T_EXIT_SKIP;
  133. for (i = 0; i < ARRAY_SIZE(q_entries); i++) {
  134. ret = test(&q_entries[i]);
  135. if (ret == T_EXIT_SKIP) {
  136. return T_EXIT_SKIP;
  137. } else if (ret != T_EXIT_PASS) {
  138. fprintf(stderr, "Failed at %d/%d\n", q_entries[i].sqes,
  139. q_entries[i].cqes);
  140. return T_EXIT_FAIL;
  141. }
  142. }
  143. return T_EXIT_PASS;
  144. }