Skip to content

Commit 263846e

Browse files
Ming Leiaxboe
authored andcommitted
selftests: ublk: simplify loop io completion
Use the added target io handling helpers for simplifying loop io completion. Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 8cb9b97 commit 263846e

File tree

2 files changed

+47
-48
lines changed

2 files changed

+47
-48
lines changed

tools/testing/selftests/ublk/file_backed.c

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,22 @@ static enum io_uring_op ublk_to_uring_op(const struct ublksrv_io_desc *iod, int
1313
assert(0);
1414
}
1515

16+
static int loop_queue_flush_io(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag)
17+
{
18+
unsigned ublk_op = ublksrv_get_op(iod);
19+
struct io_uring_sqe *sqe[1];
20+
21+
ublk_queue_alloc_sqes(q, sqe, 1);
22+
io_uring_prep_fsync(sqe[0], 1 /*fds[1]*/, IORING_FSYNC_DATASYNC);
23+
io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
24+
/* bit63 marks us as tgt io */
25+
sqe[0]->user_data = build_user_data(tag, ublk_op, 0, 1);
26+
return 1;
27+
}
28+
1629
static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag)
1730
{
31+
unsigned ublk_op = ublksrv_get_op(iod);
1832
int zc = ublk_queue_use_zc(q);
1933
enum io_uring_op op = ublk_to_uring_op(iod, zc);
2034
struct io_uring_sqe *sqe[3];
@@ -29,98 +43,87 @@ static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_de
2943
iod->nr_sectors << 9,
3044
iod->start_sector << 9);
3145
io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
32-
q->io_inflight++;
3346
/* bit63 marks us as tgt io */
34-
sqe[0]->user_data = build_user_data(tag, op, UBLK_IO_TGT_NORMAL, 1);
35-
return 0;
47+
sqe[0]->user_data = build_user_data(tag, ublk_op, 0, 1);
48+
return 1;
3649
}
3750

3851
ublk_queue_alloc_sqes(q, sqe, 3);
3952

4053
io_uring_prep_buf_register(sqe[0], 0, tag, q->q_id, tag);
41-
sqe[0]->user_data = build_user_data(tag, 0xfe, 1, 1);
42-
sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS;
43-
sqe[0]->flags |= IOSQE_IO_LINK;
54+
sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK;
55+
sqe[0]->user_data = build_user_data(tag,
56+
ublk_cmd_op_nr(sqe[0]->cmd_op), 0, 1);
4457

4558
io_uring_prep_rw(op, sqe[1], 1 /*fds[1]*/, 0,
4659
iod->nr_sectors << 9,
4760
iod->start_sector << 9);
4861
sqe[1]->buf_index = tag;
49-
sqe[1]->flags |= IOSQE_FIXED_FILE;
50-
sqe[1]->flags |= IOSQE_IO_LINK;
51-
sqe[1]->user_data = build_user_data(tag, op, UBLK_IO_TGT_ZC_OP, 1);
52-
q->io_inflight++;
62+
sqe[1]->flags |= IOSQE_FIXED_FILE | IOSQE_IO_HARDLINK;
63+
sqe[1]->user_data = build_user_data(tag, ublk_op, 0, 1);
5364

5465
io_uring_prep_buf_unregister(sqe[2], 0, tag, q->q_id, tag);
55-
sqe[2]->user_data = build_user_data(tag, 0xff, UBLK_IO_TGT_ZC_BUF, 1);
56-
q->io_inflight++;
66+
sqe[2]->user_data = build_user_data(tag, ublk_cmd_op_nr(sqe[2]->cmd_op), 0, 1);
5767

58-
return 0;
68+
return 2;
5969
}
6070

6171
static int loop_queue_tgt_io(struct ublk_queue *q, int tag)
6272
{
6373
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
6474
unsigned ublk_op = ublksrv_get_op(iod);
65-
struct io_uring_sqe *sqe[1];
75+
int ret;
6676

6777
switch (ublk_op) {
6878
case UBLK_IO_OP_FLUSH:
69-
ublk_queue_alloc_sqes(q, sqe, 1);
70-
if (!sqe[0])
71-
return -ENOMEM;
72-
io_uring_prep_fsync(sqe[0], 1 /*fds[1]*/, IORING_FSYNC_DATASYNC);
73-
io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
74-
q->io_inflight++;
75-
sqe[0]->user_data = build_user_data(tag, ublk_op, UBLK_IO_TGT_NORMAL, 1);
79+
ret = loop_queue_flush_io(q, iod, tag);
7680
break;
7781
case UBLK_IO_OP_WRITE_ZEROES:
7882
case UBLK_IO_OP_DISCARD:
79-
return -ENOTSUP;
83+
ret = -ENOTSUP;
84+
break;
8085
case UBLK_IO_OP_READ:
8186
case UBLK_IO_OP_WRITE:
82-
loop_queue_tgt_rw_io(q, iod, tag);
87+
ret = loop_queue_tgt_rw_io(q, iod, tag);
8388
break;
8489
default:
85-
return -EINVAL;
90+
ret = -EINVAL;
91+
break;
8692
}
8793

8894
ublk_dbg(UBLK_DBG_IO, "%s: tag %d ublk io %x %llx %u\n", __func__, tag,
8995
iod->op_flags, iod->start_sector, iod->nr_sectors << 9);
90-
return 1;
96+
return ret;
9197
}
9298

9399
static int ublk_loop_queue_io(struct ublk_queue *q, int tag)
94100
{
95101
int queued = loop_queue_tgt_io(q, tag);
96102

97-
if (queued < 0)
98-
ublk_complete_io(q, tag, queued);
99-
103+
ublk_queued_tgt_io(q, tag, queued);
100104
return 0;
101105
}
102106

103107
static void ublk_loop_io_done(struct ublk_queue *q, int tag,
104108
const struct io_uring_cqe *cqe)
105109
{
106-
int cqe_tag = user_data_to_tag(cqe->user_data);
107-
unsigned tgt_data = user_data_to_tgt_data(cqe->user_data);
108-
int res = cqe->res;
110+
unsigned op = user_data_to_op(cqe->user_data);
111+
struct ublk_io *io = ublk_get_io(q, tag);
112+
113+
if (cqe->res < 0 || op != ublk_cmd_op_nr(UBLK_U_IO_UNREGISTER_IO_BUF)) {
114+
if (!io->result)
115+
io->result = cqe->res;
116+
if (cqe->res < 0)
117+
ublk_err("%s: io failed op %x user_data %lx\n",
118+
__func__, op, cqe->user_data);
119+
}
109120

110-
if (res < 0 || tgt_data == UBLK_IO_TGT_NORMAL)
111-
goto complete;
121+
/* buffer register op is IOSQE_CQE_SKIP_SUCCESS */
122+
if (op == ublk_cmd_op_nr(UBLK_U_IO_REGISTER_IO_BUF))
123+
io->tgt_ios += 1;
112124

113-
if (tgt_data == UBLK_IO_TGT_ZC_OP) {
114-
ublk_set_io_res(q, tag, cqe->res);
115-
goto exit;
116-
}
117-
assert(tgt_data == UBLK_IO_TGT_ZC_BUF);
118-
res = ublk_get_io_res(q, tag);
119-
complete:
120-
assert(tag == cqe_tag);
121-
ublk_complete_io(q, tag, res);
122-
exit:
123-
q->io_inflight--;
125+
if (ublk_completed_tgt_io(q, tag))
126+
ublk_complete_io(q, tag, io->result);
124127
}
125128

126129
static int ublk_loop_tgt_init(const struct dev_ctx *ctx, struct ublk_dev *dev)

tools/testing/selftests/ublk/kublk.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@
4444
#define UBLK_MAX_QUEUES 4
4545
#define UBLK_QUEUE_DEPTH 128
4646

47-
#define UBLK_IO_TGT_NORMAL 0
48-
#define UBLK_IO_TGT_ZC_BUF 1
49-
#define UBLK_IO_TGT_ZC_OP 2
50-
5147
#define UBLK_DBG_DEV (1U << 0)
5248
#define UBLK_DBG_QUEUE (1U << 1)
5349
#define UBLK_DBG_IO_CMD (1U << 2)

0 commit comments

Comments
 (0)