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
17 #include "liburing.h"
18 #include "helpers.h"
19
20 #define PRE_RED 0x5aa55aa55aa55aa5ULL
21 #define POST_RED 0xa55aa55aa55aa55aULL
22
23 struct ctx {
24 struct io_uring ring;
25 void *ring_mem;
26 void *mem;
27 unsigned long long *pre;
28 unsigned long long *post;
29 };
30
31 struct q_entries {
32 unsigned int sqes;
33 unsigned int cqes;
34 };
35
setup_ctx(struct ctx * ctx,struct q_entries * q)36 static int setup_ctx(struct ctx *ctx, struct q_entries *q)
37 {
38 struct io_uring_params p = { };
39 int ret;
40
41 if (posix_memalign(&ctx->mem, 4096, 2*1024*1024))
42 return T_EXIT_FAIL;
43
44 ctx->pre = ctx->mem + 4096 - sizeof(unsigned long long);
45 *ctx->pre = PRE_RED;
46
47 ctx->ring_mem = ctx->mem + 4096;
48 p.flags |= IORING_SETUP_CQSIZE | IORING_SETUP_NO_SQARRAY;
49 p.sq_entries = q->sqes;
50 p.cq_entries = q->cqes;
51
52 ret = io_uring_queue_init_mem(q->sqes, &ctx->ring, &p,
53 ctx->ring_mem, 2*1024*1024);
54
55 if (ret < 0) {
56 if (ret == -EINVAL)
57 return T_EXIT_SKIP;
58 fprintf(stderr, "queue init: %d\n", ret);
59 return T_EXIT_FAIL;
60 }
61
62 ctx->post = ctx->ring_mem + ret;
63 *ctx->post = POST_RED;
64 return 0;
65 }
66
clean_ctx(struct ctx * ctx)67 static void clean_ctx(struct ctx *ctx)
68 {
69 io_uring_queue_exit(&ctx->ring);
70 free(ctx->mem);
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 clean_ctx(&ctx);
99 return T_EXIT_SKIP;
100 } else if (ret != T_EXIT_PASS) {
101 return ret;
102 }
103
104 batch = 64;
105 if (batch > q->sqes)
106 batch = q->sqes;
107
108 i = ud = 0;
109 while (i < q->cqes * 2) {
110 if (check_red(&ctx, i))
111 return T_EXIT_FAIL;
112 for (j = 0; j < batch; j++) {
113 sqe = io_uring_get_sqe(&ctx.ring);
114 io_uring_prep_nop(sqe);
115 sqe->user_data = j + (unsigned long) i;
116 }
117 io_uring_submit(&ctx.ring);
118 for (j = 0; j < batch; j++) {
119 ret = io_uring_wait_cqe(&ctx.ring, &cqe);
120 if (ret)
121 goto err;
122 if (cqe->user_data != ud) {
123 fprintf(stderr, "ud=%lu, wanted %lu\n", (unsigned long) cqe->user_data, ud);
124 goto err;
125 }
126 ud++;
127 io_uring_cqe_seen(&ctx.ring, cqe);
128 }
129 i += batch;
130 }
131
132 clean_ctx(&ctx);
133 return T_EXIT_PASS;
134 err:
135 clean_ctx(&ctx);
136 return T_EXIT_FAIL;
137 }
138
main(int argc,char * argv[])139 int main(int argc, char *argv[])
140 {
141 struct q_entries q_entries[] = {
142 { 256, 16384 },
143 { 32, 4096 },
144 { 128, 8192 },
145 { 4096, 32768 },
146 { 1, 8 },
147 { 2, 1024 },
148 };
149 int i, ret;
150
151 if (argc > 1)
152 return T_EXIT_SKIP;
153
154 for (i = 0; i < ARRAY_SIZE(q_entries); i++) {
155 ret = test(&q_entries[i]);
156 if (ret == T_EXIT_SKIP) {
157 return T_EXIT_SKIP;
158 } else if (ret != T_EXIT_PASS) {
159 fprintf(stderr, "Failed at %d/%d\n", q_entries[i].sqes,
160 q_entries[i].cqes);
161 return T_EXIT_FAIL;
162 }
163 }
164
165 return T_EXIT_PASS;
166 }
167