Skip to content

Commit 6f1a182

Browse files
Ming Leiaxboe
authored andcommitted
selftests: ublk: add test for covering UBLK_AUTO_BUF_REG_FALLBACK
Add test for covering UBLK_AUTO_BUF_REG_FALLBACK: - pass '--auto_zc_fallback' to null target, which requires both F_AUTO_BUF_REG and F_SUPPORT_ZERO_COPY for handling UBLK_AUTO_BUF_REG_FALLBACK - add ->buf_index() method for returning invalid buffer index to trigger UBLK_AUTO_BUF_REG_FALLBACK - add generic_09 for running the test - add --auto_zc_fallback test in stress_03/stress_04/stress_05 Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 8ccebc1 commit 6f1a182

File tree

11 files changed

+108
-13
lines changed

11 files changed

+108
-13
lines changed

tools/testing/selftests/ublk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TEST_PROGS += test_generic_06.sh
1616
TEST_PROGS += test_generic_07.sh
1717

1818
TEST_PROGS += test_generic_08.sh
19+
TEST_PROGS += test_generic_09.sh
1920

2021
TEST_PROGS += test_null_01.sh
2122
TEST_PROGS += test_null_02.sh

tools/testing/selftests/ublk/fault_inject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ static int ublk_fault_inject_tgt_init(const struct dev_ctx *ctx,
1616
const struct ublksrv_ctrl_dev_info *info = &dev->dev_info;
1717
unsigned long dev_size = 250UL << 30;
1818

19+
if (ctx->auto_zc_fallback) {
20+
ublk_err("%s: not support auto_zc_fallback\n", __func__);
21+
return -EINVAL;
22+
}
23+
1924
dev->tgt.dev_size = dev_size;
2025
dev->tgt.params = (struct ublk_params) {
2126
.types = UBLK_PARAM_TYPE_BASIC,

tools/testing/selftests/ublk/file_backed.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ static int ublk_loop_tgt_init(const struct dev_ctx *ctx, struct ublk_dev *dev)
149149
},
150150
};
151151

152+
if (ctx->auto_zc_fallback) {
153+
ublk_err("%s: not support auto_zc_fallback\n", __func__);
154+
return -EINVAL;
155+
}
156+
152157
ret = backing_file_tgt_init(dev);
153158
if (ret)
154159
return ret;

tools/testing/selftests/ublk/kublk.c

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static void ublk_queue_deinit(struct ublk_queue *q)
405405
free(q->ios[i].buf_addr);
406406
}
407407

408-
static int ublk_queue_init(struct ublk_queue *q)
408+
static int ublk_queue_init(struct ublk_queue *q, unsigned extra_flags)
409409
{
410410
struct ublk_dev *dev = q->dev;
411411
int depth = dev->dev_info.queue_depth;
@@ -427,6 +427,7 @@ static int ublk_queue_init(struct ublk_queue *q)
427427
if (dev->dev_info.flags & UBLK_F_AUTO_BUF_REG)
428428
q->state |= UBLKSRV_AUTO_BUF_REG;
429429
}
430+
q->state |= extra_flags;
430431

431432
cmd_buf_size = ublk_queue_cmd_buf_sz(q);
432433
off = UBLKSRV_CMD_BUF_OFFSET + q->q_id * ublk_queue_max_cmd_buf_sz();
@@ -528,14 +529,19 @@ static void ublk_dev_unprep(struct ublk_dev *dev)
528529
close(dev->fds[0]);
529530
}
530531

531-
static void ublk_set_auto_buf_reg(struct io_uring_sqe *sqe,
532-
unsigned short buf_idx,
533-
unsigned char flags)
532+
static void ublk_set_auto_buf_reg(const struct ublk_queue *q,
533+
struct io_uring_sqe *sqe,
534+
unsigned short tag)
534535
{
535-
struct ublk_auto_buf_reg buf = {
536-
.index = buf_idx,
537-
.flags = flags,
538-
};
536+
struct ublk_auto_buf_reg buf = {};
537+
538+
if (q->tgt_ops->buf_index)
539+
buf.index = q->tgt_ops->buf_index(q, tag);
540+
else
541+
buf.index = tag;
542+
543+
if (q->state & UBLKSRV_AUTO_BUF_REG_FALLBACK)
544+
buf.flags = UBLK_AUTO_BUF_REG_FALLBACK;
539545

540546
sqe->addr = ublk_auto_buf_reg_to_sqe_addr(&buf);
541547
}
@@ -595,7 +601,7 @@ int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)
595601
cmd->addr = 0;
596602

597603
if (q->state & UBLKSRV_AUTO_BUF_REG)
598-
ublk_set_auto_buf_reg(sqe[0], tag, 0);
604+
ublk_set_auto_buf_reg(q, sqe[0], tag);
599605

600606
user_data = build_user_data(tag, _IOC_NR(cmd_op), 0, 0);
601607
io_uring_sqe_set_data64(sqe[0], user_data);
@@ -747,16 +753,21 @@ struct ublk_queue_info {
747753
struct ublk_queue *q;
748754
sem_t *queue_sem;
749755
cpu_set_t *affinity;
756+
unsigned char auto_zc_fallback;
750757
};
751758

752759
static void *ublk_io_handler_fn(void *data)
753760
{
754761
struct ublk_queue_info *info = data;
755762
struct ublk_queue *q = info->q;
756763
int dev_id = q->dev->dev_info.dev_id;
764+
unsigned extra_flags = 0;
757765
int ret;
758766

759-
ret = ublk_queue_init(q);
767+
if (info->auto_zc_fallback)
768+
extra_flags = UBLKSRV_AUTO_BUF_REG_FALLBACK;
769+
770+
ret = ublk_queue_init(q, extra_flags);
760771
if (ret) {
761772
ublk_err("ublk dev %d queue %d init queue failed\n",
762773
dev_id, q->q_id);
@@ -849,6 +860,7 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
849860
qinfo[i].q = &dev->q[i];
850861
qinfo[i].queue_sem = &queue_sem;
851862
qinfo[i].affinity = &affinity_buf[i];
863+
qinfo[i].auto_zc_fallback = ctx->auto_zc_fallback;
852864
pthread_create(&dev->q[i].thread, NULL,
853865
ublk_io_handler_fn,
854866
&qinfo[i]);
@@ -1264,7 +1276,7 @@ static void __cmd_create_help(char *exe, bool recovery)
12641276

12651277
printf("%s %s -t [null|loop|stripe|fault_inject] [-q nr_queues] [-d depth] [-n dev_id]\n",
12661278
exe, recovery ? "recover" : "add");
1267-
printf("\t[--foreground] [--quiet] [-z] [--auto_zc] [--debug_mask mask] [-r 0|1 ] [-g]\n");
1279+
printf("\t[--foreground] [--quiet] [-z] [--auto_zc] [--auto_zc_fallback] [--debug_mask mask] [-r 0|1 ] [-g]\n");
12681280
printf("\t[-e 0|1 ] [-i 0|1]\n");
12691281
printf("\t[target options] [backfile1] [backfile2] ...\n");
12701282
printf("\tdefault: nr_queues=2(max 32), depth=128(max 1024), dev_id=-1(auto allocation)\n");
@@ -1319,7 +1331,8 @@ int main(int argc, char *argv[])
13191331
{ "recovery_fail_io", 1, NULL, 'e'},
13201332
{ "recovery_reissue", 1, NULL, 'i'},
13211333
{ "get_data", 1, NULL, 'g'},
1322-
{ "auto_zc", 0, NULL, 0},
1334+
{ "auto_zc", 0, NULL, 0 },
1335+
{ "auto_zc_fallback", 0, NULL, 0 },
13231336
{ 0, 0, 0, 0 }
13241337
};
13251338
const struct ublk_tgt_ops *ops = NULL;
@@ -1390,6 +1403,8 @@ int main(int argc, char *argv[])
13901403
ctx.fg = 1;
13911404
if (!strcmp(longopts[option_idx].name, "auto_zc"))
13921405
ctx.flags |= UBLK_F_AUTO_BUF_REG;
1406+
if (!strcmp(longopts[option_idx].name, "auto_zc_fallback"))
1407+
ctx.auto_zc_fallback = 1;
13931408
break;
13941409
case '?':
13951410
/*
@@ -1413,6 +1428,16 @@ int main(int argc, char *argv[])
14131428
}
14141429
}
14151430

1431+
/* auto_zc_fallback depends on F_AUTO_BUF_REG & F_SUPPORT_ZERO_COPY */
1432+
if (ctx.auto_zc_fallback &&
1433+
!((ctx.flags & UBLK_F_AUTO_BUF_REG) &&
1434+
(ctx.flags & UBLK_F_SUPPORT_ZERO_COPY))) {
1435+
ublk_err("%s: auto_zc_fallback is set but neither "
1436+
"F_AUTO_BUF_REG nor F_SUPPORT_ZERO_COPY is enabled\n",
1437+
__func__);
1438+
return -EINVAL;
1439+
}
1440+
14161441
i = optind;
14171442
while (i < argc && ctx.nr_files < MAX_BACK_FILES) {
14181443
ctx.files[ctx.nr_files++] = argv[i++];

tools/testing/selftests/ublk/kublk.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct dev_ctx {
8484
unsigned int all:1;
8585
unsigned int fg:1;
8686
unsigned int recovery:1;
87+
unsigned int auto_zc_fallback:1;
8788

8889
int _evtfd;
8990
int _shmid;
@@ -141,6 +142,9 @@ struct ublk_tgt_ops {
141142
*/
142143
void (*parse_cmd_line)(struct dev_ctx *ctx, int argc, char *argv[]);
143144
void (*usage)(const struct ublk_tgt_ops *ops);
145+
146+
/* return buffer index for UBLK_F_AUTO_BUF_REG */
147+
unsigned short (*buf_index)(const struct ublk_queue *, int tag);
144148
};
145149

146150
struct ublk_tgt {
@@ -170,6 +174,7 @@ struct ublk_queue {
170174
#define UBLKSRV_NO_BUF (1U << 2)
171175
#define UBLKSRV_ZC (1U << 3)
172176
#define UBLKSRV_AUTO_BUF_REG (1U << 4)
177+
#define UBLKSRV_AUTO_BUF_REG_FALLBACK (1U << 5)
173178
unsigned state;
174179
pid_t tid;
175180
pthread_t thread;
@@ -205,6 +210,12 @@ struct ublk_dev {
205210
extern unsigned int ublk_dbg_mask;
206211
extern int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag);
207212

213+
214+
static inline int ublk_io_auto_zc_fallback(const struct ublksrv_io_desc *iod)
215+
{
216+
return !!(iod->op_flags & UBLK_IO_F_NEED_REG_BUF);
217+
}
218+
208219
static inline int is_target_io(__u64 user_data)
209220
{
210221
return (user_data & (1ULL << 63)) != 0;

tools/testing/selftests/ublk/null.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static int ublk_null_queue_io(struct ublk_queue *q, int tag)
116116
unsigned zc = ublk_queue_use_zc(q);
117117
int queued;
118118

119-
if (auto_zc)
119+
if (auto_zc && !ublk_io_auto_zc_fallback(iod))
120120
queued = null_queue_auto_zc_io(q, tag);
121121
else if (zc)
122122
queued = null_queue_zc_io(q, tag);
@@ -128,9 +128,21 @@ static int ublk_null_queue_io(struct ublk_queue *q, int tag)
128128
return 0;
129129
}
130130

131+
/*
132+
* return invalid buffer index for triggering auto buffer register failure,
133+
* then UBLK_IO_RES_NEED_REG_BUF handling is covered
134+
*/
135+
static unsigned short ublk_null_buf_index(const struct ublk_queue *q, int tag)
136+
{
137+
if (q->state & UBLKSRV_AUTO_BUF_REG_FALLBACK)
138+
return (unsigned short)-1;
139+
return tag;
140+
}
141+
131142
const struct ublk_tgt_ops null_tgt_ops = {
132143
.name = "null",
133144
.init_tgt = ublk_null_tgt_init,
134145
.queue_io = ublk_null_queue_io,
135146
.tgt_io_done = ublk_null_io_done,
147+
.buf_index = ublk_null_buf_index,
136148
};

tools/testing/selftests/ublk/stripe.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ static int ublk_stripe_tgt_init(const struct dev_ctx *ctx, struct ublk_dev *dev)
288288
loff_t bytes = 0;
289289
int ret, i, mul = 1;
290290

291+
if (ctx->auto_zc_fallback) {
292+
ublk_err("%s: not support auto_zc_fallback\n", __func__);
293+
return -EINVAL;
294+
}
295+
291296
if ((chunk_size & (chunk_size - 1)) || !chunk_size) {
292297
ublk_err("invalid chunk size %u\n", chunk_size);
293298
return -EINVAL;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
5+
6+
TID="generic_09"
7+
ERR_CODE=0
8+
9+
if ! _have_feature "AUTO_BUF_REG"; then
10+
exit "$UBLK_SKIP_CODE"
11+
fi
12+
13+
if ! _have_program fio; then
14+
exit "$UBLK_SKIP_CODE"
15+
fi
16+
17+
_prep_test "null" "basic IO test"
18+
19+
dev_id=$(_add_ublk_dev -t null -z --auto_zc --auto_zc_fallback)
20+
_check_add_dev $TID $?
21+
22+
# run fio over the two disks
23+
fio --name=job1 --filename=/dev/ublkb"${dev_id}" --ioengine=libaio --rw=readwrite --iodepth=32 --size=256M > /dev/null 2>&1
24+
ERR_CODE=$?
25+
26+
_cleanup_test "null"
27+
28+
_show_result $TID $ERR_CODE

tools/testing/selftests/ublk/test_stress_03.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ if _have_feature "AUTO_BUF_REG"; then
3737
ublk_io_and_remove 8G -t null -q 4 --auto_zc &
3838
ublk_io_and_remove 256M -t loop -q 4 --auto_zc "${UBLK_BACKFILES[0]}" &
3939
ublk_io_and_remove 256M -t stripe -q 4 --auto_zc "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
40+
ublk_io_and_remove 8G -t null -q 4 -z --auto_zc --auto_zc_fallback &
4041
fi
4142
wait
4243

tools/testing/selftests/ublk/test_stress_04.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ if _have_feature "AUTO_BUF_REG"; then
3636
ublk_io_and_kill_daemon 8G -t null -q 4 --auto_zc &
3737
ublk_io_and_kill_daemon 256M -t loop -q 4 --auto_zc "${UBLK_BACKFILES[0]}" &
3838
ublk_io_and_kill_daemon 256M -t stripe -q 4 --auto_zc "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
39+
ublk_io_and_kill_daemon 8G -t null -q 4 -z --auto_zc --auto_zc_fallback &
3940
fi
4041
wait
4142

0 commit comments

Comments
 (0)