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