init-mem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. memset(ctx->mem, 0, 2*1024*1024);
  39. ctx->pre = ctx->mem + 4096 - sizeof(unsigned long long);
  40. *ctx->pre = PRE_RED;
  41. ctx->ring_mem = ctx->mem + 4096;
  42. p.flags |= IORING_SETUP_CQSIZE | IORING_SETUP_NO_SQARRAY;
  43. p.sq_entries = q->sqes;
  44. p.cq_entries = q->cqes;
  45. ret = io_uring_queue_init_mem(q->sqes, &ctx->ring, &p,
  46. ctx->ring_mem, 2*1024*1024);
  47. if (ret < 0) {
  48. if (ret == -EINVAL)
  49. return T_EXIT_SKIP;
  50. fprintf(stderr, "queue init: %d\n", ret);
  51. return T_EXIT_FAIL;
  52. }
  53. ctx->post = ctx->ring_mem + ret;
  54. *ctx->post = POST_RED;
  55. return 0;
  56. }
  57. static void clean_ctx(struct ctx *ctx)
  58. {
  59. io_uring_queue_exit(&ctx->ring);
  60. free(ctx->mem);
  61. }
  62. static int check_red(struct ctx *ctx, unsigned long i)
  63. {
  64. int fail = 0;
  65. if (*ctx->pre != PRE_RED) {
  66. printf("pre redzone=%llx at i=%lu\n", *ctx->pre, i);
  67. fail = 1;
  68. }
  69. if (*ctx->post != POST_RED) {
  70. printf("post redzone=%llx at i=%lu\n", *ctx->post, i);
  71. fail = 1;
  72. }
  73. return fail;
  74. }
  75. static int test(struct q_entries *q)
  76. {
  77. struct io_uring_sqe *sqe;
  78. struct io_uring_cqe *cqe;
  79. struct ctx ctx = { };
  80. unsigned long i, ud;
  81. int j, ret, batch;
  82. ret = setup_ctx(&ctx, q);
  83. if (ret == T_EXIT_SKIP) {
  84. clean_ctx(&ctx);
  85. return T_EXIT_SKIP;
  86. } else if (ret != T_EXIT_PASS) {
  87. return ret;
  88. }
  89. batch = 64;
  90. if (batch > q->sqes)
  91. batch = q->sqes;
  92. i = ud = 0;
  93. while (i < q->cqes * 2) {
  94. if (check_red(&ctx, i))
  95. return T_EXIT_FAIL;
  96. for (j = 0; j < batch; j++) {
  97. sqe = io_uring_get_sqe(&ctx.ring);
  98. io_uring_prep_nop(sqe);
  99. sqe->user_data = j + (unsigned long) i;
  100. }
  101. io_uring_submit(&ctx.ring);
  102. for (j = 0; j < batch; j++) {
  103. ret = io_uring_wait_cqe(&ctx.ring, &cqe);
  104. if (ret)
  105. goto err;
  106. if (cqe->user_data != ud) {
  107. fprintf(stderr, "ud=%lu, wanted %lu\n", (unsigned long) cqe->user_data, ud);
  108. goto err;
  109. }
  110. ud++;
  111. io_uring_cqe_seen(&ctx.ring, cqe);
  112. }
  113. i += batch;
  114. }
  115. clean_ctx(&ctx);
  116. return T_EXIT_PASS;
  117. err:
  118. clean_ctx(&ctx);
  119. return T_EXIT_FAIL;
  120. }
  121. int main(int argc, char *argv[])
  122. {
  123. struct q_entries q_entries[] = {
  124. { 256, 16384 },
  125. { 32, 4096 },
  126. { 128, 8192 },
  127. { 4096, 32768 },
  128. { 1, 8 },
  129. { 2, 1024 },
  130. };
  131. int i, ret;
  132. if (argc > 1)
  133. return T_EXIT_SKIP;
  134. for (i = 0; i < ARRAY_SIZE(q_entries); i++) {
  135. ret = test(&q_entries[i]);
  136. if (ret == T_EXIT_SKIP) {
  137. return T_EXIT_SKIP;
  138. } else if (ret != T_EXIT_PASS) {
  139. fprintf(stderr, "Failed at %d/%d\n", q_entries[i].sqes,
  140. q_entries[i].cqes);
  141. return T_EXIT_FAIL;
  142. }
  143. }
  144. return T_EXIT_PASS;
  145. }