-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathio_uring.c
1731 lines (1499 loc) · 42.4 KB
/
io_uring.c
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* io_uring engine
*
* IO engine using the new native Linux aio io_uring interface. See:
*
* http://git.kernel.dk/cgit/linux-block/log/?h=io_uring
*
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "../fio.h"
#include "../lib/pow2.h"
#include "../optgroup.h"
#include "../lib/memalign.h"
#include "../lib/fls.h"
#include "../lib/roundup.h"
#include "../verify.h"
#ifdef ARCH_HAVE_IOURING
#include "../lib/types.h"
#include "../os/linux/io_uring.h"
#include "cmdprio.h"
#include "zbd.h"
#include "nvme.h"
#include <sys/stat.h>
enum uring_cmd_type {
FIO_URING_CMD_NVME = 1,
};
enum uring_cmd_write_mode {
FIO_URING_CMD_WMODE_WRITE = 1,
FIO_URING_CMD_WMODE_UNCOR,
FIO_URING_CMD_WMODE_ZEROES,
FIO_URING_CMD_WMODE_VERIFY,
};
enum uring_cmd_verify_mode {
FIO_URING_CMD_VMODE_READ = 1,
FIO_URING_CMD_VMODE_COMPARE,
};
struct io_sq_ring {
unsigned *head;
unsigned *tail;
unsigned *ring_mask;
unsigned *ring_entries;
unsigned *flags;
unsigned *array;
};
struct io_cq_ring {
unsigned *head;
unsigned *tail;
unsigned *ring_mask;
unsigned *ring_entries;
struct io_uring_cqe *cqes;
};
struct ioring_mmap {
void *ptr;
size_t len;
};
struct ioring_data {
int ring_fd;
struct io_u **io_u_index;
char *md_buf;
int *fds;
struct io_sq_ring sq_ring;
struct io_uring_sqe *sqes;
struct iovec *iovecs;
unsigned sq_ring_mask;
struct io_cq_ring cq_ring;
unsigned cq_ring_mask;
int async_trim_fail;
int queued;
int cq_ring_off;
unsigned iodepth;
int prepped;
struct ioring_mmap mmap[3];
struct cmdprio cmdprio;
struct nvme_dsm *dsm;
uint32_t cdw12_flags[DDIR_RWDIR_CNT];
uint8_t write_opcode;
};
struct ioring_options {
struct thread_data *td;
unsigned int hipri;
unsigned int readfua;
unsigned int writefua;
unsigned int deac;
unsigned int write_mode;
unsigned int verify_mode;
struct cmdprio_options cmdprio_options;
unsigned int fixedbufs;
unsigned int registerfiles;
unsigned int sqpoll_thread;
unsigned int sqpoll_set;
unsigned int sqpoll_cpu;
unsigned int nonvectored;
unsigned int nowait;
unsigned int force_async;
unsigned int md_per_io_size;
unsigned int pi_act;
unsigned int apptag;
unsigned int apptag_mask;
unsigned int prchk;
char *pi_chk;
enum uring_cmd_type cmd_type;
};
static const int ddir_to_op[2][2] = {
{ IORING_OP_READV, IORING_OP_READ },
{ IORING_OP_WRITEV, IORING_OP_WRITE }
};
static const int fixed_ddir_to_op[2] = {
IORING_OP_READ_FIXED,
IORING_OP_WRITE_FIXED
};
static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
{
struct ioring_options *o = data;
o->sqpoll_cpu = *val;
o->sqpoll_set = 1;
return 0;
}
static struct fio_option options[] = {
{
.name = "hipri",
.lname = "High Priority",
.type = FIO_OPT_STR_SET,
.off1 = offsetof(struct ioring_options, hipri),
.help = "Use polled IO completions",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "readfua",
.lname = "Read fua flag support",
.type = FIO_OPT_BOOL,
.off1 = offsetof(struct ioring_options, readfua),
.help = "Set FUA flag (force unit access) for all Read operations",
.def = "0",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "writefua",
.lname = "Write fua flag support",
.type = FIO_OPT_BOOL,
.off1 = offsetof(struct ioring_options, writefua),
.help = "Set FUA flag (force unit access) for all Write operations",
.def = "0",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "write_mode",
.lname = "Additional Write commands support (Write Uncorrectable, Write Zeores)",
.type = FIO_OPT_STR,
.off1 = offsetof(struct ioring_options, write_mode),
.help = "Issue Write Uncorrectable or Zeroes command instaed of Write command",
.def = "write",
.posval = {
{ .ival = "write",
.oval = FIO_URING_CMD_WMODE_WRITE,
.help = "Issue Write commands for write operations"
},
{ .ival = "uncor",
.oval = FIO_URING_CMD_WMODE_UNCOR,
.help = "Issue Write Uncorrectable commands for write operations"
},
{ .ival = "zeroes",
.oval = FIO_URING_CMD_WMODE_ZEROES,
.help = "Issue Write Zeroes commands for write operations"
},
{ .ival = "verify",
.oval = FIO_URING_CMD_WMODE_VERIFY,
.help = "Issue Verify commands for write operations"
},
},
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "verify_mode",
.lname = "Do verify based on the configured command (e.g., Read or Compare command)",
.type = FIO_OPT_STR,
.off1 = offsetof(struct ioring_options, verify_mode),
.help = "Issue Read or Compare command in the verification phase",
.def = "read",
.posval = {
{ .ival = "read",
.oval = FIO_URING_CMD_VMODE_READ,
.help = "Issue Read commands in the verification phase"
},
{ .ival = "compare",
.oval = FIO_URING_CMD_VMODE_COMPARE,
.help = "Issue Compare commands in the verification phase"
},
},
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "fixedbufs",
.lname = "Fixed (pre-mapped) IO buffers",
.type = FIO_OPT_STR_SET,
.off1 = offsetof(struct ioring_options, fixedbufs),
.help = "Pre map IO buffers",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "registerfiles",
.lname = "Register file set",
.type = FIO_OPT_STR_SET,
.off1 = offsetof(struct ioring_options, registerfiles),
.help = "Pre-open/register files",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "sqthread_poll",
.lname = "Kernel SQ thread polling",
.type = FIO_OPT_STR_SET,
.off1 = offsetof(struct ioring_options, sqpoll_thread),
.help = "Offload submission/completion to kernel thread",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "sqthread_poll_cpu",
.lname = "SQ Thread Poll CPU",
.type = FIO_OPT_INT,
.cb = fio_ioring_sqpoll_cb,
.help = "What CPU to run SQ thread polling on",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "nonvectored",
.lname = "Non-vectored",
.type = FIO_OPT_INT,
.off1 = offsetof(struct ioring_options, nonvectored),
.def = "-1",
.help = "Use non-vectored read/write commands",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "uncached",
.lname = "Uncached",
.type = FIO_OPT_SOFT_DEPRECATED,
},
{
.name = "nowait",
.lname = "RWF_NOWAIT",
.type = FIO_OPT_BOOL,
.off1 = offsetof(struct ioring_options, nowait),
.help = "Use RWF_NOWAIT for reads/writes",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "force_async",
.lname = "Force async",
.type = FIO_OPT_INT,
.off1 = offsetof(struct ioring_options, force_async),
.help = "Set IOSQE_ASYNC every N requests",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "cmd_type",
.lname = "Uring cmd type",
.type = FIO_OPT_STR,
.off1 = offsetof(struct ioring_options, cmd_type),
.help = "Specify uring-cmd type",
.def = "nvme",
.posval = {
{ .ival = "nvme",
.oval = FIO_URING_CMD_NVME,
.help = "Issue nvme-uring-cmd",
},
},
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
CMDPRIO_OPTIONS(struct ioring_options, FIO_OPT_G_IOURING),
{
.name = "md_per_io_size",
.lname = "Separate Metadata Buffer Size per I/O",
.type = FIO_OPT_INT,
.off1 = offsetof(struct ioring_options, md_per_io_size),
.def = "0",
.help = "Size of separate metadata buffer per I/O (Default: 0)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "pi_act",
.lname = "Protection Information Action",
.type = FIO_OPT_BOOL,
.off1 = offsetof(struct ioring_options, pi_act),
.def = "1",
.help = "Protection Information Action bit (pi_act=1 or pi_act=0)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "pi_chk",
.lname = "Protection Information Check",
.type = FIO_OPT_STR_STORE,
.off1 = offsetof(struct ioring_options, pi_chk),
.def = NULL,
.help = "Control of Protection Information Checking (pi_chk=GUARD,REFTAG,APPTAG)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "apptag",
.lname = "Application Tag used in Protection Information",
.type = FIO_OPT_INT,
.off1 = offsetof(struct ioring_options, apptag),
.def = "0x1234",
.help = "Application Tag used in Protection Information field (Default: 0x1234)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "apptag_mask",
.lname = "Application Tag Mask",
.type = FIO_OPT_INT,
.off1 = offsetof(struct ioring_options, apptag_mask),
.def = "0xffff",
.help = "Application Tag Mask used with Application Tag (Default: 0xffff)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = "deac",
.lname = "Deallocate bit for write zeroes command",
.type = FIO_OPT_BOOL,
.off1 = offsetof(struct ioring_options, deac),
.help = "Set DEAC (deallocate) flag for write zeroes command",
.def = "0",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_IOURING,
},
{
.name = NULL,
},
};
static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
unsigned int min_complete, unsigned int flags)
{
#ifdef FIO_ARCH_HAS_SYSCALL
return __do_syscall6(__NR_io_uring_enter, ld->ring_fd, to_submit,
min_complete, flags, NULL, 0);
#else
return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
min_complete, flags, NULL, 0);
#endif
}
#ifndef BLOCK_URING_CMD_DISCARD
#define BLOCK_URING_CMD_DISCARD _IO(0x12, 0)
#endif
static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
{
struct ioring_data *ld = td->io_ops_data;
struct ioring_options *o = td->eo;
struct fio_file *f = io_u->file;
struct io_uring_sqe *sqe;
sqe = &ld->sqes[io_u->index];
if (o->registerfiles) {
sqe->fd = f->engine_pos;
sqe->flags = IOSQE_FIXED_FILE;
} else {
sqe->fd = f->fd;
sqe->flags = 0;
}
if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
if (o->fixedbufs) {
sqe->opcode = fixed_ddir_to_op[io_u->ddir];
sqe->addr = (unsigned long) io_u->xfer_buf;
sqe->len = io_u->xfer_buflen;
sqe->buf_index = io_u->index;
} else {
struct iovec *iov = &ld->iovecs[io_u->index];
/*
* Update based on actual io_u, requeue could have
* adjusted these
*/
iov->iov_base = io_u->xfer_buf;
iov->iov_len = io_u->xfer_buflen;
sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
if (o->nonvectored) {
sqe->addr = (unsigned long) iov->iov_base;
sqe->len = iov->iov_len;
} else {
sqe->addr = (unsigned long) iov;
sqe->len = 1;
}
}
sqe->rw_flags = 0;
if (o->nowait)
sqe->rw_flags |= RWF_NOWAIT;
if (td->o.oatomic && io_u->ddir == DDIR_WRITE)
sqe->rw_flags |= RWF_ATOMIC;
/*
* Since io_uring can have a submission context (sqthread_poll)
* that is different from the process context, we cannot rely on
* the IO priority set by ioprio_set() (options prio, prioclass,
* and priohint) to be inherited.
* td->ioprio will have the value of the "default prio", so set
* this unconditionally. This value might get overridden by
* fio_ioring_cmdprio_prep() if the option cmdprio_percentage or
* cmdprio_bssplit is used.
*/
sqe->ioprio = td->ioprio;
sqe->off = io_u->offset;
} else if (ddir_sync(io_u->ddir)) {
sqe->ioprio = 0;
if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
sqe->off = f->first_write;
sqe->len = f->last_write - f->first_write;
sqe->sync_range_flags = td->o.sync_file_range;
sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
} else {
sqe->off = 0;
sqe->addr = 0;
sqe->len = 0;
if (io_u->ddir == DDIR_DATASYNC)
sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
sqe->opcode = IORING_OP_FSYNC;
}
} else if (io_u->ddir == DDIR_TRIM) {
sqe->opcode = IORING_OP_URING_CMD;
sqe->addr = io_u->offset;
sqe->addr3 = io_u->xfer_buflen;
sqe->rw_flags = 0;
sqe->len = sqe->off = 0;
sqe->ioprio = 0;
sqe->cmd_op = BLOCK_URING_CMD_DISCARD;
sqe->__pad1 = 0;
sqe->file_index = 0;
}
if (o->force_async && ++ld->prepped == o->force_async) {
ld->prepped = 0;
sqe->flags |= IOSQE_ASYNC;
}
sqe->user_data = (unsigned long) io_u;
return 0;
}
static int fio_ioring_cmd_prep(struct thread_data *td, struct io_u *io_u)
{
struct ioring_data *ld = td->io_ops_data;
struct ioring_options *o = td->eo;
struct fio_file *f = io_u->file;
struct nvme_uring_cmd *cmd;
struct io_uring_sqe *sqe;
struct nvme_dsm *dsm;
void *ptr = ld->dsm;
unsigned int dsm_size;
uint8_t read_opcode = nvme_cmd_read;
/* only supports nvme_uring_cmd */
if (o->cmd_type != FIO_URING_CMD_NVME)
return -EINVAL;
if (io_u->ddir == DDIR_TRIM && td->io_ops->flags & FIO_ASYNCIO_SYNC_TRIM)
return 0;
sqe = &ld->sqes[(io_u->index) << 1];
if (o->registerfiles) {
sqe->fd = f->engine_pos;
sqe->flags = IOSQE_FIXED_FILE;
} else {
sqe->fd = f->fd;
}
sqe->rw_flags = 0;
if (o->nowait)
sqe->rw_flags |= RWF_NOWAIT;
sqe->opcode = IORING_OP_URING_CMD;
sqe->user_data = (unsigned long) io_u;
if (o->nonvectored)
sqe->cmd_op = NVME_URING_CMD_IO;
else
sqe->cmd_op = NVME_URING_CMD_IO_VEC;
if (o->force_async && ++ld->prepped == o->force_async) {
ld->prepped = 0;
sqe->flags |= IOSQE_ASYNC;
}
if (o->fixedbufs) {
sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
sqe->buf_index = io_u->index;
}
cmd = (struct nvme_uring_cmd *)sqe->cmd;
dsm_size = sizeof(*ld->dsm) + td->o.num_range * sizeof(struct nvme_dsm_range);
ptr += io_u->index * dsm_size;
dsm = (struct nvme_dsm *)ptr;
/*
* If READ command belongs to the verification phase and the
* verify_mode=compare, convert READ to COMPARE command.
*/
if (io_u->flags & IO_U_F_VER_LIST && io_u->ddir == DDIR_READ &&
o->verify_mode == FIO_URING_CMD_VMODE_COMPARE) {
populate_verify_io_u(td, io_u);
read_opcode = nvme_cmd_compare;
io_u_set(td, io_u, IO_U_F_VER_IN_DEV);
}
return fio_nvme_uring_cmd_prep(cmd, io_u,
o->nonvectored ? NULL : &ld->iovecs[io_u->index],
dsm, read_opcode, ld->write_opcode,
ld->cdw12_flags[io_u->ddir]);
}
static struct io_u *fio_ioring_event(struct thread_data *td, int event)
{
struct ioring_data *ld = td->io_ops_data;
struct io_uring_cqe *cqe;
struct io_u *io_u;
unsigned index;
index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
cqe = &ld->cq_ring.cqes[index];
io_u = (struct io_u *) (uintptr_t) cqe->user_data;
/* trim returns 0 on success */
if (cqe->res == io_u->xfer_buflen ||
(io_u->ddir == DDIR_TRIM && !cqe->res)) {
io_u->error = 0;
return io_u;
}
if (cqe->res != io_u->xfer_buflen) {
if (io_u->ddir == DDIR_TRIM) {
ld->async_trim_fail = 1;
cqe->res = 0;
}
if (cqe->res > io_u->xfer_buflen)
io_u->error = -cqe->res;
else
io_u->resid = io_u->xfer_buflen - cqe->res;
}
return io_u;
}
static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event)
{
struct ioring_data *ld = td->io_ops_data;
struct ioring_options *o = td->eo;
struct io_uring_cqe *cqe;
struct io_u *io_u;
struct nvme_data *data;
unsigned index;
int ret;
index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
if (o->cmd_type == FIO_URING_CMD_NVME)
index <<= 1;
cqe = &ld->cq_ring.cqes[index];
io_u = (struct io_u *) (uintptr_t) cqe->user_data;
io_u->error = cqe->res;
if (io_u->error != 0)
goto ret;
if (o->cmd_type == FIO_URING_CMD_NVME) {
data = FILE_ENG_DATA(io_u->file);
if (data->pi_type && (io_u->ddir == DDIR_READ) && !o->pi_act) {
ret = fio_nvme_pi_verify(data, io_u);
if (ret)
io_u->error = ret;
}
}
ret:
/*
* If IO_U_F_DEVICE_ERROR is not set, io_u->error will be parsed as an
* errno, otherwise device-specific error value (status value in CQE).
*/
if ((int)io_u->error > 0)
io_u_set(td, io_u, IO_U_F_DEVICE_ERROR);
else
io_u_clear(td, io_u, IO_U_F_DEVICE_ERROR);
io_u->error = abs((int)io_u->error);
return io_u;
}
static char *fio_ioring_cmd_errdetails(struct thread_data *td,
struct io_u *io_u)
{
struct ioring_options *o = td->eo;
unsigned int sct = (io_u->error >> 8) & 0x7;
unsigned int sc = io_u->error & 0xff;
#define MAXERRDETAIL 1024
#define MAXMSGCHUNK 128
char *msg, msgchunk[MAXMSGCHUNK];
if (!(io_u->flags & IO_U_F_DEVICE_ERROR))
return NULL;
msg = calloc(1, MAXERRDETAIL);
strcpy(msg, "io_uring_cmd: ");
snprintf(msgchunk, MAXMSGCHUNK, "%s: ", io_u->file->file_name);
strlcat(msg, msgchunk, MAXERRDETAIL);
if (o->cmd_type == FIO_URING_CMD_NVME) {
strlcat(msg, "cq entry status (", MAXERRDETAIL);
snprintf(msgchunk, MAXMSGCHUNK, "sct=0x%02x; ", sct);
strlcat(msg, msgchunk, MAXERRDETAIL);
snprintf(msgchunk, MAXMSGCHUNK, "sc=0x%02x)", sc);
strlcat(msg, msgchunk, MAXERRDETAIL);
} else {
/* Print status code in generic */
snprintf(msgchunk, MAXMSGCHUNK, "status=0x%x", io_u->error);
strlcat(msg, msgchunk, MAXERRDETAIL);
}
return msg;
}
static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
unsigned int max)
{
struct ioring_data *ld = td->io_ops_data;
struct io_cq_ring *ring = &ld->cq_ring;
unsigned head, reaped = 0;
head = *ring->head;
do {
if (head == atomic_load_acquire(ring->tail))
break;
reaped++;
head++;
} while (reaped + events < max);
if (reaped)
atomic_store_release(ring->head, head);
return reaped;
}
static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
unsigned int max, const struct timespec *t)
{
struct ioring_data *ld = td->io_ops_data;
unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
struct ioring_options *o = td->eo;
struct io_cq_ring *ring = &ld->cq_ring;
unsigned events = 0;
int r;
ld->cq_ring_off = *ring->head;
do {
r = fio_ioring_cqring_reap(td, events, max);
if (r) {
events += r;
max -= r;
if (actual_min != 0)
actual_min -= r;
continue;
}
if (!o->sqpoll_thread) {
r = io_uring_enter(ld, 0, actual_min,
IORING_ENTER_GETEVENTS);
if (r < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
r = -errno;
td_verror(td, errno, "io_uring_enter");
break;
}
}
} while (events < min);
return r < 0 ? r : events;
}
static inline void fio_ioring_cmd_nvme_pi(struct thread_data *td,
struct io_u *io_u)
{
struct ioring_data *ld = td->io_ops_data;
struct ioring_options *o = td->eo;
struct nvme_uring_cmd *cmd;
struct io_uring_sqe *sqe;
struct nvme_cmd_ext_io_opts ext_opts = {0};
struct nvme_data *data = FILE_ENG_DATA(io_u->file);
if (io_u->ddir == DDIR_TRIM)
return;
sqe = &ld->sqes[(io_u->index) << 1];
cmd = (struct nvme_uring_cmd *)sqe->cmd;
if (data->pi_type) {
if (o->pi_act)
ext_opts.io_flags |= NVME_IO_PRINFO_PRACT;
ext_opts.io_flags |= o->prchk;
ext_opts.apptag = o->apptag;
ext_opts.apptag_mask = o->apptag_mask;
}
fio_nvme_pi_fill(cmd, io_u, &ext_opts);
}
static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
struct io_u *io_u)
{
struct ioring_data *ld = td->io_ops_data;
struct cmdprio *cmdprio = &ld->cmdprio;
if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
ld->sqes[io_u->index].ioprio = io_u->ioprio;
}
static enum fio_q_status fio_ioring_queue(struct thread_data *td,
struct io_u *io_u)
{
struct ioring_data *ld = td->io_ops_data;
struct ioring_options *o = td->eo;
struct io_sq_ring *ring = &ld->sq_ring;
unsigned tail, next_tail;
fio_ro_check(td, io_u);
/* should not hit... */
if (ld->queued == td->o.iodepth)
return FIO_Q_BUSY;
/* if async trim has been tried and failed, punt to sync */
if (io_u->ddir == DDIR_TRIM && ld->async_trim_fail) {
if (ld->queued)
return FIO_Q_BUSY;
do_io_u_trim(td, io_u);
io_u_mark_submit(td, 1);
io_u_mark_complete(td, 1);
return FIO_Q_COMPLETED;
}
tail = *ring->tail;
next_tail = tail + 1;
if (next_tail == atomic_load_relaxed(ring->head))
return FIO_Q_BUSY;
if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
fio_ioring_cmdprio_prep(td, io_u);
if (!strcmp(td->io_ops->name, "io_uring_cmd") &&
o->cmd_type == FIO_URING_CMD_NVME)
fio_ioring_cmd_nvme_pi(td, io_u);
ring->array[tail & ld->sq_ring_mask] = io_u->index;
atomic_store_release(ring->tail, next_tail);
ld->queued++;
return FIO_Q_QUEUED;
}
static void fio_ioring_queued(struct thread_data *td, int start, int nr)
{
struct ioring_data *ld = td->io_ops_data;
struct timespec now;
if (!fio_fill_issue_time(td))
return;
fio_gettime(&now, NULL);
while (nr--) {
struct io_sq_ring *ring = &ld->sq_ring;
int index = ring->array[start & ld->sq_ring_mask];
struct io_u *io_u = ld->io_u_index[index];
memcpy(&io_u->issue_time, &now, sizeof(now));
io_u_queued(td, io_u);
start++;
}
/*
* only used for iolog
*/
if (td->o.read_iolog_file)
memcpy(&td->last_issue, &now, sizeof(now));
}
static int fio_ioring_commit(struct thread_data *td)
{
struct ioring_data *ld = td->io_ops_data;
struct ioring_options *o = td->eo;
int ret;
if (!ld->queued)
return 0;
/*
* Kernel side does submission. just need to check if the ring is
* flagged as needing a kick, if so, call io_uring_enter(). This
* only happens if we've been idle too long.
*/
if (o->sqpoll_thread) {
struct io_sq_ring *ring = &ld->sq_ring;
unsigned start = *ld->sq_ring.tail - ld->queued;
unsigned flags;
flags = atomic_load_relaxed(ring->flags);
if (flags & IORING_SQ_NEED_WAKEUP)
io_uring_enter(ld, ld->queued, 0,
IORING_ENTER_SQ_WAKEUP);
fio_ioring_queued(td, start, ld->queued);
io_u_mark_submit(td, ld->queued);
ld->queued = 0;
return 0;
}
do {
unsigned start = *ld->sq_ring.head;
long nr = ld->queued;
ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
if (ret > 0) {
fio_ioring_queued(td, start, ret);
io_u_mark_submit(td, ret);
ld->queued -= ret;
ret = 0;
} else if (!ret) {
io_u_mark_submit(td, ret);
continue;
} else {
if (errno == EAGAIN || errno == EINTR) {
ret = fio_ioring_cqring_reap(td, 0, ld->queued);
if (ret)
continue;
/* Shouldn't happen */
usleep(1);
continue;
}
ret = -errno;
td_verror(td, errno, "io_uring_enter submit");
break;
}
} while (ld->queued);
return ret;
}
static void fio_ioring_unmap(struct ioring_data *ld)
{
int i;
for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
munmap(ld->mmap[i].ptr, ld->mmap[i].len);
close(ld->ring_fd);
}
static void fio_ioring_cleanup(struct thread_data *td)
{
struct ioring_data *ld = td->io_ops_data;
if (ld) {
if (!(td->flags & TD_F_CHILD))
fio_ioring_unmap(ld);
fio_cmdprio_cleanup(&ld->cmdprio);
free(ld->io_u_index);
free(ld->md_buf);
free(ld->iovecs);
free(ld->fds);
free(ld->dsm);
free(ld);
}
}
static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
{
struct io_sq_ring *sring = &ld->sq_ring;
struct io_cq_ring *cring = &ld->cq_ring;
void *ptr;
ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, ld->ring_fd,
IORING_OFF_SQ_RING);
ld->mmap[0].ptr = ptr;
sring->head = ptr + p->sq_off.head;
sring->tail = ptr + p->sq_off.tail;
sring->ring_mask = ptr + p->sq_off.ring_mask;
sring->ring_entries = ptr + p->sq_off.ring_entries;
sring->flags = ptr + p->sq_off.flags;
sring->array = ptr + p->sq_off.array;
ld->sq_ring_mask = *sring->ring_mask;
if (p->flags & IORING_SETUP_SQE128)
ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe);
else
ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, ld->ring_fd,
IORING_OFF_SQES);
ld->mmap[1].ptr = ld->sqes;
if (p->flags & IORING_SETUP_CQE32) {
ld->mmap[2].len = p->cq_off.cqes +
2 * p->cq_entries * sizeof(struct io_uring_cqe);
} else {
ld->mmap[2].len = p->cq_off.cqes +
p->cq_entries * sizeof(struct io_uring_cqe);
}
ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, ld->ring_fd,
IORING_OFF_CQ_RING);
ld->mmap[2].ptr = ptr;
cring->head = ptr + p->cq_off.head;
cring->tail = ptr + p->cq_off.tail;
cring->ring_mask = ptr + p->cq_off.ring_mask;
cring->ring_entries = ptr + p->cq_off.ring_entries;
cring->cqes = ptr + p->cq_off.cqes;
ld->cq_ring_mask = *cring->ring_mask;
return 0;
}
static void fio_ioring_probe(struct thread_data *td)
{
struct ioring_data *ld = td->io_ops_data;
struct ioring_options *o = td->eo;
struct io_uring_probe *p;
int ret;
/* already set by user, don't touch */
if (o->nonvectored != -1)
return;
/* default to off, as that's always safe */
o->nonvectored = 0;
p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
if (!p)
return;
ret = syscall(__NR_io_uring_register, ld->ring_fd,
IORING_REGISTER_PROBE, p, 256);
if (ret < 0)
goto out;
if (IORING_OP_WRITE > p->ops_len)
goto out;
if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
(p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))