Skip to content

Commit f2639ed

Browse files
Ming Leiaxboe
authored andcommitted
selftests: ublk: add single sqe allocator helper
Unify the sqe allocator helper, and we will use it for supporting more cases, such as ublk stripe, in which variable sqe allocation is required. Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://lore.kernel.org/r/20250322093218.431419-3-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 723977c commit f2639ed

3 files changed

Lines changed: 44 additions & 52 deletions

File tree

tools/testing/selftests/ublk/file_backed.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,44 +69,42 @@ static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_de
6969
{
7070
int zc = ublk_queue_use_zc(q);
7171
enum io_uring_op op = ublk_to_uring_op(iod, zc);
72-
struct io_uring_sqe *reg;
73-
struct io_uring_sqe *rw;
74-
struct io_uring_sqe *ureg;
72+
struct io_uring_sqe *sqe[3];
7573

7674
if (!zc) {
77-
rw = ublk_queue_alloc_sqe(q);
78-
if (!rw)
75+
ublk_queue_alloc_sqes(q, sqe, 1);
76+
if (!sqe[0])
7977
return -ENOMEM;
8078

81-
io_uring_prep_rw(op, rw, 1 /*fds[1]*/,
79+
io_uring_prep_rw(op, sqe[0], 1 /*fds[1]*/,
8280
(void *)iod->addr,
8381
iod->nr_sectors << 9,
8482
iod->start_sector << 9);
85-
io_uring_sqe_set_flags(rw, IOSQE_FIXED_FILE);
83+
io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
8684
q->io_inflight++;
8785
/* bit63 marks us as tgt io */
88-
rw->user_data = build_user_data(tag, op, UBLK_IO_TGT_NORMAL, 1);
86+
sqe[0]->user_data = build_user_data(tag, op, UBLK_IO_TGT_NORMAL, 1);
8987
return 0;
9088
}
9189

92-
ublk_queue_alloc_sqe3(q, &reg, &rw, &ureg);
90+
ublk_queue_alloc_sqes(q, sqe, 3);
9391

94-
io_uring_prep_buf_register(reg, 0, tag, q->q_id, tag);
95-
reg->user_data = build_user_data(tag, 0xfe, 1, 1);
96-
reg->flags |= IOSQE_CQE_SKIP_SUCCESS;
97-
reg->flags |= IOSQE_IO_LINK;
92+
io_uring_prep_buf_register(sqe[0], 0, tag, q->q_id, tag);
93+
sqe[0]->user_data = build_user_data(tag, 0xfe, 1, 1);
94+
sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS;
95+
sqe[0]->flags |= IOSQE_IO_LINK;
9896

99-
io_uring_prep_rw(op, rw, 1 /*fds[1]*/, 0,
97+
io_uring_prep_rw(op, sqe[1], 1 /*fds[1]*/, 0,
10098
iod->nr_sectors << 9,
10199
iod->start_sector << 9);
102-
rw->buf_index = tag;
103-
rw->flags |= IOSQE_FIXED_FILE;
104-
rw->flags |= IOSQE_IO_LINK;
105-
rw->user_data = build_user_data(tag, op, UBLK_IO_TGT_ZC_OP, 1);
100+
sqe[1]->buf_index = tag;
101+
sqe[1]->flags |= IOSQE_FIXED_FILE;
102+
sqe[1]->flags |= IOSQE_IO_LINK;
103+
sqe[1]->user_data = build_user_data(tag, op, UBLK_IO_TGT_ZC_OP, 1);
106104
q->io_inflight++;
107105

108-
io_uring_prep_buf_unregister(ureg, 0, tag, q->q_id, tag);
109-
ureg->user_data = build_user_data(tag, 0xff, UBLK_IO_TGT_ZC_BUF, 1);
106+
io_uring_prep_buf_unregister(sqe[2], 0, tag, q->q_id, tag);
107+
sqe[2]->user_data = build_user_data(tag, 0xff, UBLK_IO_TGT_ZC_BUF, 1);
110108
q->io_inflight++;
111109

112110
return 0;
@@ -116,17 +114,17 @@ static int loop_queue_tgt_io(struct ublk_queue *q, int tag)
116114
{
117115
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
118116
unsigned ublk_op = ublksrv_get_op(iod);
119-
struct io_uring_sqe *sqe;
117+
struct io_uring_sqe *sqe[1];
120118

121119
switch (ublk_op) {
122120
case UBLK_IO_OP_FLUSH:
123-
sqe = ublk_queue_alloc_sqe(q);
124-
if (!sqe)
121+
ublk_queue_alloc_sqes(q, sqe, 1);
122+
if (!sqe[0])
125123
return -ENOMEM;
126-
io_uring_prep_fsync(sqe, 1 /*fds[1]*/, IORING_FSYNC_DATASYNC);
127-
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
124+
io_uring_prep_fsync(sqe[0], 1 /*fds[1]*/, IORING_FSYNC_DATASYNC);
125+
io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
128126
q->io_inflight++;
129-
sqe->user_data = build_user_data(tag, ublk_op, UBLK_IO_TGT_NORMAL, 1);
127+
sqe[0]->user_data = build_user_data(tag, ublk_op, UBLK_IO_TGT_NORMAL, 1);
130128
break;
131129
case UBLK_IO_OP_WRITE_ZEROES:
132130
case UBLK_IO_OP_DISCARD:

tools/testing/selftests/ublk/kublk.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static void ublk_dev_unprep(struct ublk_dev *dev)
420420
int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)
421421
{
422422
struct ublksrv_io_cmd *cmd;
423-
struct io_uring_sqe *sqe;
423+
struct io_uring_sqe *sqe[1];
424424
unsigned int cmd_op = 0;
425425
__u64 user_data;
426426

@@ -441,24 +441,24 @@ int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)
441441
if (io_uring_sq_space_left(&q->ring) < 1)
442442
io_uring_submit(&q->ring);
443443

444-
sqe = ublk_queue_alloc_sqe(q);
445-
if (!sqe) {
444+
ublk_queue_alloc_sqes(q, sqe, 1);
445+
if (!sqe[0]) {
446446
ublk_err("%s: run out of sqe %d, tag %d\n",
447447
__func__, q->q_id, tag);
448448
return -1;
449449
}
450450

451-
cmd = (struct ublksrv_io_cmd *)ublk_get_sqe_cmd(sqe);
451+
cmd = (struct ublksrv_io_cmd *)ublk_get_sqe_cmd(sqe[0]);
452452

453453
if (cmd_op == UBLK_U_IO_COMMIT_AND_FETCH_REQ)
454454
cmd->result = io->result;
455455

456456
/* These fields should be written once, never change */
457-
ublk_set_sqe_cmd_op(sqe, cmd_op);
458-
sqe->fd = 0; /* dev->fds[0] */
459-
sqe->opcode = IORING_OP_URING_CMD;
460-
sqe->flags = IOSQE_FIXED_FILE;
461-
sqe->rw_flags = 0;
457+
ublk_set_sqe_cmd_op(sqe[0], cmd_op);
458+
sqe[0]->fd = 0; /* dev->fds[0] */
459+
sqe[0]->opcode = IORING_OP_URING_CMD;
460+
sqe[0]->flags = IOSQE_FIXED_FILE;
461+
sqe[0]->rw_flags = 0;
462462
cmd->tag = tag;
463463
cmd->q_id = q->q_id;
464464
if (!(q->state & UBLKSRV_NO_BUF))
@@ -467,7 +467,7 @@ int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)
467467
cmd->addr = 0;
468468

469469
user_data = build_user_data(tag, _IOC_NR(cmd_op), 0, 0);
470-
io_uring_sqe_set_data64(sqe, user_data);
470+
io_uring_sqe_set_data64(sqe[0], user_data);
471471

472472
io->flags = 0;
473473

tools/testing/selftests/ublk/kublk.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,22 @@ static inline void ublk_dbg(int level, const char *fmt, ...)
221221
}
222222
}
223223

224-
static inline struct io_uring_sqe *ublk_queue_alloc_sqe(struct ublk_queue *q)
224+
static inline int ublk_queue_alloc_sqes(struct ublk_queue *q,
225+
struct io_uring_sqe *sqes[], int nr_sqes)
225226
{
226227
unsigned left = io_uring_sq_space_left(&q->ring);
228+
int i;
227229

228-
if (left < 1)
230+
if (left < nr_sqes)
229231
io_uring_submit(&q->ring);
230-
return io_uring_get_sqe(&q->ring);
231-
}
232-
233-
static inline void ublk_queue_alloc_sqe3(struct ublk_queue *q,
234-
struct io_uring_sqe **sqe1, struct io_uring_sqe **sqe2,
235-
struct io_uring_sqe **sqe3)
236-
{
237-
struct io_uring *r = &q->ring;
238-
unsigned left = io_uring_sq_space_left(r);
239232

240-
if (left < 3)
241-
io_uring_submit(r);
233+
for (i = 0; i < nr_sqes; i++) {
234+
sqes[i] = io_uring_get_sqe(&q->ring);
235+
if (!sqes[i])
236+
return i;
237+
}
242238

243-
*sqe1 = io_uring_get_sqe(r);
244-
*sqe2 = io_uring_get_sqe(r);
245-
*sqe3 = io_uring_get_sqe(r);
239+
return nr_sqes;
246240
}
247241

248242
static inline void io_uring_prep_buf_register(struct io_uring_sqe *sqe,

0 commit comments

Comments
 (0)