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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Description: run various CQ ring overflow tests
*
*/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include "helpers.h"
#include "liburing.h"
#define FILE_SIZE (256 * 1024)
#define BS 4096
#define BUFFERS (FILE_SIZE / BS)
static struct iovec *vecs;
#define ENTRIES 8
/*
* io_uring has rare cases where CQEs are lost.
* This happens when there is no space in the CQ ring, and also there is no
* GFP_ATOMIC memory available. In reality this probably means that the process
* is about to be killed as many other things might start failing, but we still
* want to test that liburing and the kernel deal with this properly. The fault
* injection framework allows us to test this scenario. Unfortunately this
* requires some system wide changes and so we do not enable this by default.
* The tests in this file should work in both cases (where overflows are queued
* and where they are dropped) on recent kernels.
*
* In order to test dropped CQEs you should enable fault injection in the kernel
* config:
*
* CONFIG_FAULT_INJECTION=y
* CONFIG_FAILSLAB=y
* CONFIG_FAULT_INJECTION_DEBUG_FS=y
*
* and then run the test as follows:
* echo Y > /sys/kernel/debug/failslab/task-filter
* echo 100 > /sys/kernel/debug/failslab/probability
* echo 0 > /sys/kernel/debug/failslab/verbose
* echo 100000 > /sys/kernel/debug/failslab/times
* bash -c "echo 1 > /proc/self/make-it-fail && exec ./cq-overflow.t"
*/
static int test_io(const char *file, unsigned long usecs, unsigned *drops,
int fault)
{
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
struct io_uring_params p;
unsigned reaped, total;
struct io_uring ring;
int nodrop, i, fd, ret;
bool cqe_dropped = false;
fd = open(file, O_RDONLY | O_DIRECT);
if (fd < 0) {
if (errno == EINVAL)
return T_EXIT_SKIP;
perror("file open");
return T_EXIT_FAIL;
}
memset(&p, 0, sizeof(p));
ret = io_uring_queue_init_params(ENTRIES, &ring, &p);
if (ret) {
close(fd);
fprintf(stderr, "ring create failed: %d\n", ret);
return T_EXIT_FAIL;
}
nodrop = 0;
if (p.features & IORING_FEAT_NODROP)
nodrop = 1;
total = 0;
for (i = 0; i < BUFFERS / 2; i++) {
off_t offset;
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "sqe get failed\n");
goto err;
}
offset = BS * (rand() % BUFFERS);
if (fault && i == ENTRIES + 4) {
free(vecs[i].iov_base);
vecs[i].iov_base = NULL;
}
io_uring_prep_readv(sqe, fd, &vecs[i], 1, offset);
ret = io_uring_submit(&ring);
if (nodrop && ret == -EBUSY) {
*drops = 1;
total = i;
break;
} else if (ret != 1) {
fprintf(stderr, "submit got %d, wanted %d\n", ret, 1);
total = i;
break;
}
total++;
}
if (*drops)
goto reap_it;
usleep(usecs);
for (i = total; i < BUFFERS; i++) {
off_t offset;
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "sqe get failed\n");
goto err;
}
offset = BS * (rand() % BUFFERS);
io_uring_prep_readv(sqe, fd, &vecs[i], 1, offset);
ret = io_uring_submit(&ring);
if (nodrop && ret == -EBUSY) {
*drops = 1;
break;
} else if (ret != 1) {
fprintf(stderr, "submit got %d, wanted %d\n", ret, 1);
break;
}
total++;
}
reap_it:
reaped = 0;
do {
if (nodrop && !cqe_dropped) {
/* nodrop should never lose events unless cqe_dropped */
if (reaped == total)
break;
} else {
if (reaped + *ring.cq.koverflow == total)
break;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (nodrop && ret == -EBADR) {
cqe_dropped = true;
continue;
} else if (ret) {
fprintf(stderr, "wait_cqe=%d\n", ret);
goto err;
}
if (cqe->res != BS) {
if (!(fault && cqe->res == -EFAULT)) {
fprintf(stderr, "cqe res %d, wanted %d\n",
cqe->res, BS);
goto err;
}
}
io_uring_cqe_seen(&ring, cqe);
reaped++;
} while (1);
if (!io_uring_peek_cqe(&ring, &cqe)) {
fprintf(stderr, "found unexpected completion\n");
goto err;
}
if (!nodrop || cqe_dropped) {
*drops = *ring.cq.koverflow;
} else if (*ring.cq.koverflow) {
fprintf(stderr, "Found %u overflows\n", *ring.cq.koverflow);
goto err;
}
io_uring_queue_exit(&ring);
close(fd);
return T_EXIT_PASS;
err:
if (fd != -1)
close(fd);
io_uring_queue_exit(&ring);
return T_EXIT_SKIP;
}
static int reap_events(struct io_uring *ring, unsigned nr_events, int do_wait)
{
struct io_uring_cqe *cqe;
int i, ret = 0, seq = 0;
unsigned int start_overflow = *ring->cq.koverflow;
bool dropped = false;
for (i = 0; i < nr_events; i++) {
if (do_wait)
ret = io_uring_wait_cqe(ring, &cqe);
else
ret = io_uring_peek_cqe(ring, &cqe);
if (do_wait && ret == -EBADR) {
unsigned int this_drop = *ring->cq.koverflow -
start_overflow;
dropped = true;
start_overflow = *ring->cq.koverflow;
assert(this_drop > 0);
i += (this_drop - 1);
continue;
} else if (ret) {
if (ret != -EAGAIN)
fprintf(stderr, "cqe peek failed: %d\n", ret);
break;
}
if (!dropped && cqe->user_data != seq) {
fprintf(stderr, "cqe sequence out-of-order\n");
fprintf(stderr, "got %d, wanted %d\n", (int) cqe->user_data,
seq);
return -EINVAL;
}
seq++;
io_uring_cqe_seen(ring, cqe);
}
return i ? i : ret;
}
/*
* Submit some NOPs and watch if the overflow is correct
*/
static int test_overflow(void)
{
struct io_uring ring;
struct io_uring_params p;
struct io_uring_sqe *sqe;
unsigned pending;
int ret, i, j;
memset(&p, 0, sizeof(p));
ret = io_uring_queue_init_params(4, &ring, &p);
if (ret) {
fprintf(stderr, "io_uring_queue_init failed %d\n", ret);
return 1;
}
/* submit 4x4 SQEs, should overflow the ring by 8 */
pending = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "get sqe failed\n");
goto err;
}
io_uring_prep_nop(sqe);
sqe->user_data = (i * 4) + j;
}
ret = io_uring_submit(&ring);
if (ret == 4) {
pending += 4;
continue;
}
if (p.features & IORING_FEAT_NODROP) {
if (ret == -EBUSY)
break;
}
fprintf(stderr, "sqe submit failed: %d\n", ret);
goto err;
}
/* we should now have 8 completions ready */
ret = reap_events(&ring, pending, 0);
if (ret < 0)
goto err;
if (!(p.features & IORING_FEAT_NODROP)) {
if (*ring.cq.koverflow != 8) {
fprintf(stderr, "cq ring overflow %d, expected 8\n",
*ring.cq.koverflow);
goto err;
}
}
io_uring_queue_exit(&ring);
return 0;
err:
io_uring_queue_exit(&ring);
return 1;
}
static void submit_one_nop(struct io_uring *ring, int ud)
{
struct io_uring_sqe *sqe;
int ret;
sqe = io_uring_get_sqe(ring);
assert(sqe);
io_uring_prep_nop(sqe);
sqe->user_data = ud;
ret = io_uring_submit(ring);
assert(ret == 1);
}
/*
* Create an overflow condition and ensure that SQEs are still processed
*/
static int test_overflow_handling(bool batch, int cqe_multiple, bool poll,
bool defer)
{
struct io_uring ring;
struct io_uring_params p;
int ret, i, j, ud, cqe_count;
unsigned int count;
int const N = 8;
int const LOOPS = 128;
int const QUEUE_LENGTH = 1024;
int completions[N];
int queue[QUEUE_LENGTH];
int queued = 0;
int outstanding = 0;
bool cqe_dropped = false;
memset(&completions, 0, sizeof(int) * N);
memset(&p, 0, sizeof(p));
p.cq_entries = 2 * cqe_multiple;
p.flags |= IORING_SETUP_CQSIZE;
if (poll)
p.flags |= IORING_SETUP_IOPOLL;
if (defer)
p.flags |= IORING_SETUP_SINGLE_ISSUER |
IORING_SETUP_DEFER_TASKRUN;
ret = io_uring_queue_init_params(2, &ring, &p);
if (ret) {
fprintf(stderr, "io_uring_queue_init failed %d\n", ret);
return 1;
}
assert(p.cq_entries < N);
/* submit N SQEs, some should overflow */
for (i = 0; i < N; i++) {
submit_one_nop(&ring, i);
outstanding++;
}
for (i = 0; i < LOOPS; i++) {
struct io_uring_cqe *cqes[N];
if (io_uring_cq_has_overflow(&ring)) {
/*
* Flush any overflowed CQEs and process those. Actively
* flush these to make sure CQEs arrive in vague order
* of being sent.
*/
ret = io_uring_get_events(&ring);
if (ret != 0) {
fprintf(stderr,
"io_uring_get_events returned %d\n",
ret);
goto err;
}
} else if (!cqe_dropped) {
for (j = 0; j < queued; j++) {
submit_one_nop(&ring, queue[j]);
outstanding++;
}
queued = 0;
}
/* We have lost some random cqes, stop if no remaining. */
if (cqe_dropped && outstanding == *ring.cq.koverflow)
break;
ret = io_uring_wait_cqe(&ring, &cqes[0]);
if (ret == -EBADR) {
cqe_dropped = true;
fprintf(stderr, "CQE dropped\n");
continue;
} else if (ret != 0) {
fprintf(stderr, "io_uring_wait_cqes failed %d\n", ret);
goto err;
}
cqe_count = 1;
if (batch) {
ret = io_uring_peek_batch_cqe(&ring, &cqes[0], 2);
if (ret < 0) {
fprintf(stderr,
"io_uring_peek_batch_cqe failed %d\n",
ret);
goto err;
}
cqe_count = ret;
}
for (j = 0; j < cqe_count; j++) {
assert(cqes[j]->user_data < N);
ud = cqes[j]->user_data;
completions[ud]++;
assert(queued < QUEUE_LENGTH);
queue[queued++] = (int)ud;
}
io_uring_cq_advance(&ring, cqe_count);
outstanding -= cqe_count;
}
/* See if there were any drops by flushing the CQ ring *and* overflow */
do {
struct io_uring_cqe *cqe;
ret = io_uring_get_events(&ring);
if (ret < 0) {
if (ret == -EBADR) {
fprintf(stderr, "CQE dropped\n");
cqe_dropped = true;
break;
}
goto err;
}
if (outstanding && !io_uring_cq_ready(&ring))
ret = io_uring_wait_cqe_timeout(&ring, &cqe, NULL);
if (ret && ret != -ETIME) {
if (ret == -EBADR) {
fprintf(stderr, "CQE dropped\n");
cqe_dropped = true;
break;
}
fprintf(stderr, "wait_cqe_timeout = %d\n", ret);
goto err;
}
count = io_uring_cq_ready(&ring);
io_uring_cq_advance(&ring, count);
outstanding -= count;
} while (count);
io_uring_queue_exit(&ring);
/* Make sure that completions come back in the same order they were
* sent. If they come back unfairly then this will concentrate on a
* couple of indices.
*/
for (i = 1; !cqe_dropped && i < N; i++) {
if (abs(completions[i] - completions[i - 1]) > 1) {
fprintf(stderr, "bad completion size %d %d\n",
completions[i], completions[i - 1]);
goto err;
}
}
return 0;
err:
io_uring_queue_exit(&ring);
return 1;
}
int main(int argc, char *argv[])
{
const char *fname = ".cq-overflow";
unsigned iters, drops;
unsigned long usecs;
int ret;
int i;
bool can_defer;
if (argc > 1)
return T_EXIT_SKIP;
can_defer = t_probe_defer_taskrun();
for (i = 0; i < 16; i++) {
bool batch = i & 1;
int mult = (i & 2) ? 1 : 2;
bool poll = i & 4;
bool defer = i & 8;
if (defer && !can_defer)
continue;
ret = test_overflow_handling(batch, mult, poll, defer);
if (ret) {
fprintf(stderr, "test_overflow_handling("
"batch=%d, mult=%d, poll=%d, defer=%d) failed\n",
batch, mult, poll, defer);
goto err;
}
}
ret = test_overflow();
if (ret) {
fprintf(stderr, "test_overflow failed\n");
return ret;
}
t_create_file(fname, FILE_SIZE);
vecs = t_create_buffers(BUFFERS, BS);
iters = 0;
usecs = 1000;
do {
drops = 0;
ret = test_io(fname, usecs, &drops, 0);
if (ret == T_EXIT_SKIP)
break;
else if (ret != T_EXIT_PASS) {
fprintf(stderr, "test_io nofault failed\n");
goto err;
}
if (drops)
break;
usecs = (usecs * 12) / 10;
iters++;
} while (iters < 40);
if (test_io(fname, usecs, &drops, 0) == T_EXIT_FAIL) {
fprintf(stderr, "test_io nofault failed\n");
goto err;
}
if (test_io(fname, usecs, &drops, 1) == T_EXIT_FAIL) {
fprintf(stderr, "test_io fault failed\n");
goto err;
}
unlink(fname);
if(vecs != NULL) {
for (i = 0; i < BUFFERS; i++)
free(vecs[i].iov_base);
}
free(vecs);
return T_EXIT_PASS;
err:
unlink(fname);
if(vecs != NULL) {
for (i = 0; i < BUFFERS; i++)
free(vecs[i].iov_base);
}
free(vecs);
return T_EXIT_FAIL;
}
|