1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Description: test ring resizing with fixed large sqes/cqes, and with
* mixed mode sqes/cqes.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "liburing.h"
#include "helpers.h"
static int test_cqe32_resize(void)
{
struct io_uring ring;
struct io_uring_params p = { }, new_p = { };
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret, i;
p.flags = IORING_SETUP_CQE32 | IORING_SETUP_DEFER_TASKRUN |
IORING_SETUP_SINGLE_ISSUER;
p.sq_entries = 8;
p.cq_entries = 8;
ret = io_uring_queue_init_params(8, &ring, &p);
if (ret) {
if (ret == -EINVAL)
return T_EXIT_SKIP;
fprintf(stderr, "queue_init: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
for (i = 0; i < 4; i++) {
sqe = io_uring_get_sqe(&ring);
io_uring_prep_nop(sqe);
sqe->user_data = 0xAAAA0000ULL + i;
}
ret = io_uring_submit(&ring);
if (ret != 4) {
fprintf(stderr, "submit: %d\n", ret);
return T_EXIT_FAIL;
}
/* Resize the ring */
new_p.sq_entries = 8;
new_p.cq_entries = 16;
new_p.flags = IORING_SETUP_CQSIZE;
ret = io_uring_resize_rings(&ring, &new_p);
if (ret) {
if (ret == -EINVAL)
return T_EXIT_SKIP;
fprintf(stderr, "resize failed: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
for (i = 0; i < 4; i++) {
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
if (cqe->user_data != (0xAAAA0000ULL + i)) {
fprintf(stderr, " *** CQE32 CORRUPTION DETECTED ***\n");
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&ring, cqe);
}
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
}
static int test_cqe_mixed_resize(void)
{
struct io_uring ring;
struct io_uring_params p = { }, new_p = { };
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret, i;
/* Setup ring with CQE_MIXED, DEFER_TASKRUN, SINGLE_ISSUER */
p.flags = IORING_SETUP_CQE_MIXED | IORING_SETUP_DEFER_TASKRUN |
IORING_SETUP_SINGLE_ISSUER;
p.sq_entries = 8;
p.cq_entries = 8;
ret = io_uring_queue_init_params(8, &ring, &p);
if (ret) {
if (ret == -EINVAL)
return T_EXIT_SKIP;
fprintf(stderr, "ring_init: %d\n", ret);
return T_EXIT_FAIL;
}
for (i = 0; i < 4; i++) {
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "get_sqe failed\n");
return T_EXIT_FAIL;
}
if (i % 2 == 0) {
io_uring_prep_nop(sqe);
sqe->user_data = 0xBBBB0000ULL + i;
} else {
io_uring_prep_nop(sqe); /* Still NOP but different pattern */
sqe->user_data = 0xCCCC0000ULL + i;
}
}
ret = io_uring_submit(&ring);
if (ret != 4) {
fprintf(stderr, "submit: %d\n", ret);
return T_EXIT_FAIL;
}
/* Resize the ring */
new_p.sq_entries = 8;
new_p.cq_entries = 16;
new_p.flags = IORING_SETUP_CQSIZE;
ret = io_uring_resize_rings(&ring, &new_p);
if (ret) {
fprintf(stderr, "resize failed: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
/* Reap and verify CQEs */
for (i = 0; i < 4; i++) {
uint64_t expected;
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
expected = (i % 2 == 0) ? (0xBBBB0000ULL + i) : (0xCCCC0000ULL + i);
if (cqe->user_data != expected) {
fprintf(stderr, " *** CQE_MIXED CORRUPTION DETECTED ***\n");
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&ring, cqe);
}
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
}
static int test_ring_wrapping(void)
{
struct io_uring ring;
struct io_uring_params p = { }, new_p = { };
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret, i, consumed_count;
int remaining_expected[] = {6, 7};
int remaining_count = 0;
/* Setup small ring to force wrapping */
p.flags = IORING_SETUP_CQE32 | IORING_SETUP_DEFER_TASKRUN |
IORING_SETUP_SINGLE_ISSUER;
p.sq_entries = 4;
p.cq_entries = 4;
ret = io_uring_queue_init_params(4, &ring, &p);
if (ret) {
if (ret == -EINVAL)
return T_EXIT_SKIP;
fprintf(stderr, "queue_init: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
/* Submit more entries to force head/tail wrapping */
consumed_count = 0;
for (i = 0; i < 8; i++) {
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "get_sqe failed at %d\n", i);
return T_EXIT_FAIL;
}
io_uring_prep_nop(sqe);
sqe->user_data = 0xDDDD0000ULL + i;
ret = io_uring_submit(&ring);
if (ret != 1) {
fprintf(stderr, "submit failed: %d\n", ret);
return T_EXIT_FAIL;
}
/* Consume some to create wrap scenario */
if (i >= 2) {
uint64_t expected;
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
/* Verify early consumed CQE */
expected = 0xDDDD0000ULL + consumed_count;
if (cqe->user_data != expected) {
fprintf(stderr, " *** EARLY CQE CORRUPTION DETECTED ***\n");
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&ring, cqe);
consumed_count++;
}
}
/* Now resize with pending wrapped entries */
new_p.sq_entries = 4;
new_p.cq_entries = 16;
ret = io_uring_resize_rings(&ring, &new_p);
if (ret) {
fprintf(stderr, "resize failed: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
/* Consume remaining entries (should be operations 6 and 7) */
while (true) {
uint64_t expected;
ret = io_uring_peek_cqe(&ring, &cqe);
if (ret == -EAGAIN)
break;
if (ret) {
fprintf(stderr, "peek_cqe: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
if (remaining_count >= 2) {
printf(" *** TOO MANY REMAINING CQES ***\n");
return T_EXIT_FAIL;
}
expected = 0xDDDD0000ULL + remaining_expected[remaining_count];
if (cqe->user_data != expected) {
fprintf(stderr, " *** REMAINING CQE CORRUPTION DETECTED ***\n");
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&ring, cqe);
remaining_count++;
}
if (remaining_count != 2) {
fprintf(stderr, " *** WRONG NUMBER OF REMAINING CQES: got %d, expected 2 ***\n", remaining_count);
return T_EXIT_FAIL;
}
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
}
static int test_sqe128_resize(void)
{
struct io_uring ring;
struct io_uring_params p = { }, new_p = { };
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret, i;
p.flags = IORING_SETUP_SQE128 | IORING_SETUP_DEFER_TASKRUN |
IORING_SETUP_SINGLE_ISSUER;
p.sq_entries = 8;
p.cq_entries = 8;
ret = io_uring_queue_init_params(8, &ring, &p);
if (ret) {
if (ret == -EINVAL)
return T_EXIT_SKIP;
fprintf(stderr, "queue_init: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
/* Prepare 4 large SQEs but don't submit them yet */
for (i = 0; i < 4; i++) {
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "get_sqe failed\n");
return 1;
}
io_uring_prep_nop(sqe);
sqe->user_data = 0xEEEE0000ULL + i;
/* Fill some extended SQE data to ensure 128-byte copy works */
if (ring.sq.ring_ptr) {
/* Access extended part of SQE128 if available */
memset(sqe->cmd, 0x55 + i, sizeof(sqe->cmd));
}
}
__io_uring_flush_sq(&ring);
/* Resize the ring while SQEs are pending */
new_p.sq_entries = 16;
new_p.cq_entries = 16;
new_p.flags = IORING_SETUP_CQSIZE;
ret = io_uring_resize_rings(&ring, &new_p);
if (ret) {
fprintf(stderr, "resize failed: %s\n", strerror(-ret));
return 1;
}
/* Now submit the pending SQEs */
ret = io_uring_submit(&ring);
if (ret != 4) {
fprintf(stderr, "submit after resize: %d (expected 4)\n", ret);
return T_EXIT_FAIL;
}
/* Reap and verify CQEs */
for (i = 0; i < 4; i++) {
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
if (cqe->user_data != (0xEEEE0000ULL + i)) {
printf(" *** SQE128 CORRUPTION DETECTED ***\n");
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&ring, cqe);
}
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
}
static int test_sqe_mixed_resize(void)
{
struct io_uring ring;
struct io_uring_params p = { }, new_p = { };
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret, i;
p.flags = IORING_SETUP_SQE_MIXED | IORING_SETUP_DEFER_TASKRUN |
IORING_SETUP_SINGLE_ISSUER;
p.sq_entries = 8;
p.cq_entries = 8;
ret = io_uring_queue_init_params(8, &ring, &p);
if (ret) {
if (ret == -EINVAL)
return T_EXIT_SKIP;
return T_EXIT_FAIL;
}
/* Prepare mix of regular and large SQEs but don't submit them yet */
for (i = 0; i < 4; i++) {
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "get_sqe failed\n");
return T_EXIT_FAIL;
}
/* Alternate between regular NOPs and operations that might use large SQEs */
if (i % 2 == 0) {
io_uring_prep_nop(sqe);
sqe->user_data = 0xFFFF0000ULL + i;
} else {
io_uring_prep_nop(sqe); /* Still NOP but different pattern */
sqe->user_data = 0x11110000ULL + i;
/* Fill extended data for mixed SQE */
memset(sqe->cmd, 0xAA + i, sizeof(sqe->cmd));
}
}
__io_uring_flush_sq(&ring);
/* Resize the ring while mixed SQEs are pending */
new_p.sq_entries = 16;
new_p.cq_entries = 16;
new_p.flags = IORING_SETUP_CQSIZE;
ret = io_uring_resize_rings(&ring, &new_p);
if (ret) {
fprintf(stderr, "resize failed: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
/* Now submit the pending mixed SQEs */
ret = io_uring_submit(&ring);
if (ret != 4) {
fprintf(stderr, "submit after resize: %d (expected 4)\n", ret);
return T_EXIT_FAIL;
}
/* Reap and verify CQEs */
for (i = 0; i < 4; i++) {
uint64_t expected;
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %s\n", strerror(-ret));
return T_EXIT_FAIL;
}
expected = (i % 2 == 0) ? (0xFFFF0000ULL + i) : (0x11110000ULL + i);
if (cqe->user_data != expected) {
fprintf(stderr, " *** SQE_MIXED CORRUPTION DETECTED ***\n");
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&ring, cqe);
}
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
}
int main(void)
{
int ret;
ret = test_cqe32_resize();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "cqe32_resize failed\n");
return T_EXIT_FAIL;
} else if (ret == T_EXIT_SKIP) {
return T_EXIT_SKIP;
}
ret = test_cqe_mixed_resize();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "cqe_mixed_resize failed\n");
return T_EXIT_FAIL;
} else if (ret == T_EXIT_SKIP) {
return T_EXIT_SKIP;
}
ret = test_sqe128_resize();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "sqe128_resize failed\n");
return T_EXIT_FAIL;
} else if (ret == T_EXIT_SKIP) {
return T_EXIT_SKIP;
}
ret = test_ring_wrapping();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "ring_wrapping failed\n");
return T_EXIT_FAIL;
} else if (ret == T_EXIT_SKIP) {
return T_EXIT_SKIP;
}
ret = test_sqe_mixed_resize();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "sqe_mixed_resize failed\n");
return T_EXIT_FAIL;
}
return T_EXIT_PASS;
}
|