Skip to content

Commit 63d364f

Browse files
Ming Leikawasaki
authored andcommitted
selftests: ublk: pass 'ublk_thread *' to ->queue_io() and ->tgt_io_done()
'struct thread' is task local structure, and the related code will become more readable if we pass it via parameter. Meantime pass 'ublk_thread *' to ublk_io_alloc_sqes(), and this way is natural since we use per-thread io_uring for handling IO. More importantly it helps much for removing the current ubq_daemon or per-io-task limit. Signed-off-by: Ming Lei <[email protected]>
1 parent 678ed5f commit 63d364f

6 files changed

Lines changed: 58 additions & 44 deletions

File tree

tools/testing/selftests/ublk/fault_inject.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ static int ublk_fault_inject_tgt_init(const struct dev_ctx *ctx,
3838
return 0;
3939
}
4040

41-
static int ublk_fault_inject_queue_io(struct ublk_queue *q, int tag)
41+
static int ublk_fault_inject_queue_io(struct ublk_thread *t,
42+
struct ublk_queue *q, int tag)
4243
{
4344
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
4445
struct io_uring_sqe *sqe;
4546
struct __kernel_timespec ts = {
4647
.tv_nsec = (long long)q->dev->private_data,
4748
};
4849

49-
ublk_io_alloc_sqes(ublk_get_io(q, tag), &sqe, 1);
50+
ublk_io_alloc_sqes(t, &sqe, 1);
5051
io_uring_prep_timeout(sqe, &ts, 1, 0);
5152
sqe->user_data = build_user_data(tag, ublksrv_get_op(iod), 0, q->q_id, 1);
5253

@@ -55,7 +56,8 @@ static int ublk_fault_inject_queue_io(struct ublk_queue *q, int tag)
5556
return 0;
5657
}
5758

58-
static void ublk_fault_inject_tgt_io_done(struct ublk_queue *q,
59+
static void ublk_fault_inject_tgt_io_done(struct ublk_thread *t,
60+
struct ublk_queue *q,
5961
const struct io_uring_cqe *cqe)
6062
{
6163
unsigned tag = user_data_to_tag(cqe->user_data);

tools/testing/selftests/ublk/file_backed.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +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)
16+
static int loop_queue_flush_io(struct ublk_thread *t, struct ublk_queue *q,
17+
const struct ublksrv_io_desc *iod, int tag)
1718
{
1819
unsigned ublk_op = ublksrv_get_op(iod);
1920
struct io_uring_sqe *sqe[1];
2021

21-
ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, 1);
22+
ublk_io_alloc_sqes(t, sqe, 1);
2223
io_uring_prep_fsync(sqe[0], 1 /*fds[1]*/, IORING_FSYNC_DATASYNC);
2324
io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
2425
/* bit63 marks us as tgt io */
2526
sqe[0]->user_data = build_user_data(tag, ublk_op, 0, q->q_id, 1);
2627
return 1;
2728
}
2829

29-
static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag)
30+
static int loop_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
31+
const struct ublksrv_io_desc *iod, int tag)
3032
{
3133
unsigned ublk_op = ublksrv_get_op(iod);
3234
unsigned zc = ublk_queue_use_zc(q);
@@ -36,7 +38,7 @@ static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_de
3638
void *addr = (zc | auto_zc) ? NULL : (void *)iod->addr;
3739

3840
if (!zc || auto_zc) {
39-
ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, 1);
41+
ublk_io_alloc_sqes(t, sqe, 1);
4042
if (!sqe[0])
4143
return -ENOMEM;
4244

@@ -52,7 +54,7 @@ static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_de
5254
return 1;
5355
}
5456

55-
ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, 3);
57+
ublk_io_alloc_sqes(t, sqe, 3);
5658

5759
io_uring_prep_buf_register(sqe[0], 0, tag, q->q_id, ublk_get_io(q, tag)->buf_index);
5860
sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK;
@@ -72,23 +74,23 @@ static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_de
7274
return 2;
7375
}
7476

75-
static int loop_queue_tgt_io(struct ublk_queue *q, int tag)
77+
static int loop_queue_tgt_io(struct ublk_thread *t, struct ublk_queue *q, int tag)
7678
{
7779
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
7880
unsigned ublk_op = ublksrv_get_op(iod);
7981
int ret;
8082

8183
switch (ublk_op) {
8284
case UBLK_IO_OP_FLUSH:
83-
ret = loop_queue_flush_io(q, iod, tag);
85+
ret = loop_queue_flush_io(t, q, iod, tag);
8486
break;
8587
case UBLK_IO_OP_WRITE_ZEROES:
8688
case UBLK_IO_OP_DISCARD:
8789
ret = -ENOTSUP;
8890
break;
8991
case UBLK_IO_OP_READ:
9092
case UBLK_IO_OP_WRITE:
91-
ret = loop_queue_tgt_rw_io(q, iod, tag);
93+
ret = loop_queue_tgt_rw_io(t, q, iod, tag);
9294
break;
9395
default:
9496
ret = -EINVAL;
@@ -100,15 +102,16 @@ static int loop_queue_tgt_io(struct ublk_queue *q, int tag)
100102
return ret;
101103
}
102104

103-
static int ublk_loop_queue_io(struct ublk_queue *q, int tag)
105+
static int ublk_loop_queue_io(struct ublk_thread *t, struct ublk_queue *q,
106+
int tag)
104107
{
105-
int queued = loop_queue_tgt_io(q, tag);
108+
int queued = loop_queue_tgt_io(t, q, tag);
106109

107110
ublk_queued_tgt_io(q, tag, queued);
108111
return 0;
109112
}
110113

111-
static void ublk_loop_io_done(struct ublk_queue *q,
114+
static void ublk_loop_io_done(struct ublk_thread *t, struct ublk_queue *q,
112115
const struct io_uring_cqe *cqe)
113116
{
114117
unsigned tag = user_data_to_tag(cqe->user_data);

tools/testing/selftests/ublk/kublk.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ int ublk_queue_io_cmd(struct ublk_io *io)
620620
if (io_uring_sq_space_left(&t->ring) < 1)
621621
io_uring_submit(&t->ring);
622622

623-
ublk_io_alloc_sqes(io, sqe, 1);
623+
ublk_io_alloc_sqes(t, sqe, 1);
624624
if (!sqe[0]) {
625625
ublk_err("%s: run out of sqe. thread %u, tag %d\n",
626626
__func__, t->idx, io->tag);
@@ -714,8 +714,9 @@ static int ublk_thread_is_done(struct ublk_thread *t)
714714
return (t->state & UBLKSRV_THREAD_STOPPING) && ublk_thread_is_idle(t);
715715
}
716716

717-
static inline void ublksrv_handle_tgt_cqe(struct ublk_queue *q,
718-
struct io_uring_cqe *cqe)
717+
static inline void ublksrv_handle_tgt_cqe(struct ublk_thread *t,
718+
struct ublk_queue *q,
719+
struct io_uring_cqe *cqe)
719720
{
720721
if (cqe->res < 0 && cqe->res != -EAGAIN)
721722
ublk_err("%s: failed tgt io: res %d qid %u tag %u, cmd_op %u\n",
@@ -724,7 +725,7 @@ static inline void ublksrv_handle_tgt_cqe(struct ublk_queue *q,
724725
user_data_to_op(cqe->user_data));
725726

726727
if (q->tgt_ops->tgt_io_done)
727-
q->tgt_ops->tgt_io_done(q, cqe);
728+
q->tgt_ops->tgt_io_done(t, q, cqe);
728729
}
729730

730731
static void ublk_handle_cqe(struct ublk_thread *t,
@@ -751,7 +752,7 @@ static void ublk_handle_cqe(struct ublk_thread *t,
751752

752753
/* Don't retrieve io in case of target io */
753754
if (is_target_io(cqe->user_data)) {
754-
ublksrv_handle_tgt_cqe(q, cqe);
755+
ublksrv_handle_tgt_cqe(t, q, cqe);
755756
return;
756757
}
757758

@@ -766,7 +767,7 @@ static void ublk_handle_cqe(struct ublk_thread *t,
766767
if (cqe->res == UBLK_IO_RES_OK) {
767768
assert(tag < q->q_depth);
768769
if (q->tgt_ops->queue_io)
769-
q->tgt_ops->queue_io(q, tag);
770+
q->tgt_ops->queue_io(t, q, tag);
770771
} else if (cqe->res == UBLK_IO_RES_NEED_GET_DATA) {
771772
io->flags |= UBLKSRV_NEED_GET_DATA | UBLKSRV_IO_FREE;
772773
ublk_queue_io_cmd(io);

tools/testing/selftests/ublk/kublk.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ struct ublk_tgt_ops {
144144
int (*init_tgt)(const struct dev_ctx *ctx, struct ublk_dev *);
145145
void (*deinit_tgt)(struct ublk_dev *);
146146

147-
int (*queue_io)(struct ublk_queue *, int tag);
148-
void (*tgt_io_done)(struct ublk_queue *, const struct io_uring_cqe *);
147+
int (*queue_io)(struct ublk_thread *, struct ublk_queue *, int tag);
148+
void (*tgt_io_done)(struct ublk_thread *, struct ublk_queue *,
149+
const struct io_uring_cqe *);
149150

150151
/*
151152
* Target specific command line handling
@@ -313,10 +314,10 @@ static inline struct ublk_queue *ublk_io_to_queue(const struct ublk_io *io)
313314
return container_of(io, struct ublk_queue, ios[io->tag]);
314315
}
315316

316-
static inline int ublk_io_alloc_sqes(struct ublk_io *io,
317+
static inline int ublk_io_alloc_sqes(struct ublk_thread *t,
317318
struct io_uring_sqe *sqes[], int nr_sqes)
318319
{
319-
struct io_uring *ring = &io->t->ring;
320+
struct io_uring *ring = &t->ring;
320321
unsigned left = io_uring_sq_space_left(ring);
321322
int i;
322323

tools/testing/selftests/ublk/null.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ static void __setup_nop_io(int tag, const struct ublksrv_io_desc *iod,
5555
sqe->user_data = build_user_data(tag, ublk_op, 0, q_id, 1);
5656
}
5757

58-
static int null_queue_zc_io(struct ublk_queue *q, int tag)
58+
static int null_queue_zc_io(struct ublk_thread *t, struct ublk_queue *q,
59+
int tag)
5960
{
6061
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
6162
struct io_uring_sqe *sqe[3];
6263

63-
ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, 3);
64+
ublk_io_alloc_sqes(t, sqe, 3);
6465

6566
io_uring_prep_buf_register(sqe[0], 0, tag, q->q_id, ublk_get_io(q, tag)->buf_index);
6667
sqe[0]->user_data = build_user_data(tag,
@@ -77,18 +78,19 @@ static int null_queue_zc_io(struct ublk_queue *q, int tag)
7778
return 2;
7879
}
7980

80-
static int null_queue_auto_zc_io(struct ublk_queue *q, int tag)
81+
static int null_queue_auto_zc_io(struct ublk_thread *t, struct ublk_queue *q,
82+
int tag)
8183
{
8284
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
8385
struct io_uring_sqe *sqe[1];
8486

85-
ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, 1);
87+
ublk_io_alloc_sqes(t, sqe, 1);
8688
__setup_nop_io(tag, iod, sqe[0], q->q_id);
8789
return 1;
8890
}
8991

90-
static void ublk_null_io_done(struct ublk_queue *q,
91-
const struct io_uring_cqe *cqe)
92+
static void ublk_null_io_done(struct ublk_thread *t, struct ublk_queue *q,
93+
const struct io_uring_cqe *cqe)
9294
{
9395
unsigned tag = user_data_to_tag(cqe->user_data);
9496
unsigned op = user_data_to_op(cqe->user_data);
@@ -110,17 +112,18 @@ static void ublk_null_io_done(struct ublk_queue *q,
110112
ublk_complete_io(q, tag, io->result);
111113
}
112114

113-
static int ublk_null_queue_io(struct ublk_queue *q, int tag)
115+
static int ublk_null_queue_io(struct ublk_thread *t, struct ublk_queue *q,
116+
int tag)
114117
{
115118
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
116119
unsigned auto_zc = ublk_queue_use_auto_zc(q);
117120
unsigned zc = ublk_queue_use_zc(q);
118121
int queued;
119122

120123
if (auto_zc && !ublk_io_auto_zc_fallback(iod))
121-
queued = null_queue_auto_zc_io(q, tag);
124+
queued = null_queue_auto_zc_io(t, q, tag);
122125
else if (zc)
123-
queued = null_queue_zc_io(q, tag);
126+
queued = null_queue_zc_io(t, q, tag);
124127
else {
125128
ublk_complete_io(q, tag, iod->nr_sectors << 9);
126129
return 0;

tools/testing/selftests/ublk/stripe.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ static inline enum io_uring_op stripe_to_uring_op(
123123
assert(0);
124124
}
125125

126-
static int stripe_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag)
126+
static int stripe_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
127+
const struct ublksrv_io_desc *iod, int tag)
127128
{
128129
const struct stripe_conf *conf = get_chunk_shift(q);
129130
unsigned auto_zc = (ublk_queue_use_auto_zc(q) != 0);
@@ -138,7 +139,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_
138139
io->private_data = s;
139140
calculate_stripe_array(conf, iod, s, base);
140141

141-
ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, s->nr + extra);
142+
ublk_io_alloc_sqes(t, sqe, s->nr + extra);
142143

143144
if (zc) {
144145
io_uring_prep_buf_register(sqe[0], 0, tag, q->q_id, io->buf_index);
@@ -176,13 +177,14 @@ static int stripe_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_
176177
return s->nr + zc;
177178
}
178179

179-
static int handle_flush(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag)
180+
static int handle_flush(struct ublk_thread *t, struct ublk_queue *q,
181+
const struct ublksrv_io_desc *iod, int tag)
180182
{
181183
const struct stripe_conf *conf = get_chunk_shift(q);
182184
struct io_uring_sqe *sqe[NR_STRIPE];
183185
int i;
184186

185-
ublk_io_alloc_sqes(ublk_get_io(q, tag), sqe, conf->nr_files);
187+
ublk_io_alloc_sqes(t, sqe, conf->nr_files);
186188
for (i = 0; i < conf->nr_files; i++) {
187189
io_uring_prep_fsync(sqe[i], i + 1, IORING_FSYNC_DATASYNC);
188190
io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE);
@@ -191,23 +193,24 @@ static int handle_flush(struct ublk_queue *q, const struct ublksrv_io_desc *iod,
191193
return conf->nr_files;
192194
}
193195

194-
static int stripe_queue_tgt_io(struct ublk_queue *q, int tag)
196+
static int stripe_queue_tgt_io(struct ublk_thread *t, struct ublk_queue *q,
197+
int tag)
195198
{
196199
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
197200
unsigned ublk_op = ublksrv_get_op(iod);
198201
int ret = 0;
199202

200203
switch (ublk_op) {
201204
case UBLK_IO_OP_FLUSH:
202-
ret = handle_flush(q, iod, tag);
205+
ret = handle_flush(t, q, iod, tag);
203206
break;
204207
case UBLK_IO_OP_WRITE_ZEROES:
205208
case UBLK_IO_OP_DISCARD:
206209
ret = -ENOTSUP;
207210
break;
208211
case UBLK_IO_OP_READ:
209212
case UBLK_IO_OP_WRITE:
210-
ret = stripe_queue_tgt_rw_io(q, iod, tag);
213+
ret = stripe_queue_tgt_rw_io(t, q, iod, tag);
211214
break;
212215
default:
213216
ret = -EINVAL;
@@ -218,16 +221,17 @@ static int stripe_queue_tgt_io(struct ublk_queue *q, int tag)
218221
return ret;
219222
}
220223

221-
static int ublk_stripe_queue_io(struct ublk_queue *q, int tag)
224+
static int ublk_stripe_queue_io(struct ublk_thread *t, struct ublk_queue *q,
225+
int tag)
222226
{
223-
int queued = stripe_queue_tgt_io(q, tag);
227+
int queued = stripe_queue_tgt_io(t, q, tag);
224228

225229
ublk_queued_tgt_io(q, tag, queued);
226230
return 0;
227231
}
228232

229-
static void ublk_stripe_io_done(struct ublk_queue *q,
230-
const struct io_uring_cqe *cqe)
233+
static void ublk_stripe_io_done(struct ublk_thread *t, struct ublk_queue *q,
234+
const struct io_uring_cqe *cqe)
231235
{
232236
unsigned tag = user_data_to_tag(cqe->user_data);
233237
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);

0 commit comments

Comments
 (0)