ublk: one selftest fix and two zero copy fixes
Subsystems (3)
v2 cover ublk: one selftest fix and two zero copy fixes
Hello Jens,
The 1st patch fixes UBLK_F_NEED_GET_DATA support in ublk selftest side.
The other two patches enhances check for zero copy feature.
The final patch removes one unnecessary check.
Thanks,
Ming
V2:
- add have_program check in new added generic_07 test(Caleb Sander Mateos)
- fixe usage help of ublk utility(Caleb Sander Mateos)
- add the 4th patch for removing one unnecessary check(Caleb Sander Mateos)
Ming Lei (4):
selftests: ublk: fix UBLK_F_NEED_GET_DATA
ublk: decouple zero copy from user copy
ublk: enhance check for register/unregister io buffer command
ublk: remove the check of ublk_need_req_ref() from
__ublk_check_and_get_req
drivers/block/ublk_drv.c | 62 +++++++++++++------ tools/testing/selftests/ublk/Makefile | 1 + tools/testing/selftests/ublk/kublk.c | 22 ++++--- tools/testing/selftests/ublk/kublk.h | 1 + .../testing/selftests/ublk/test_generic_07.sh | 28 +++++++++ .../testing/selftests/ublk/test_stress_05.sh | 8 +-- 6 files changed, 91 insertions(+), 31 deletions(-) create mode 100755 tools/testing/selftests/ublk/test_generic_07.sh
Signature · 1 line
2.47.0
PATCH v2 1/4 selftests: ublk: fix UBLK_F_NEED_GET_DATA
Commit 57e13a2e8cd2 ("selftests: ublk: support user recovery") starts to support UBLK_F_NEED_GET_DATA for covering recovery feature, however the ublk utility implementation isn't done correctly.
Fix it by supporting UBLK_F_NEED_GET_DATA correctly.
Also add test generic_07 for covering UBLK_F_NEED_GET_DATA.
--- tools/testing/selftests/ublk/Makefile | 1 + tools/testing/selftests/ublk/kublk.c | 22 +++++++++------ tools/testing/selftests/ublk/kublk.h | 1 + .../testing/selftests/ublk/test_generic_07.sh | 28 +++++++++++++++++++ .../testing/selftests/ublk/test_stress_05.sh | 8 +++--- 5 files changed, 48 insertions(+), 12 deletions(-) create mode 100755 tools/testing/selftests/ublk/test_generic_07.sh
tools/testing/selftests/ublk/Makefile +1 -0
@@ -9,6 +9,7 @@ TEST_PROGS += test_generic_03.sh TEST_PROGS += test_generic_04.sh TEST_PROGS += test_generic_05.sh TEST_PROGS += test_generic_06.sh +TEST_PROGS += test_generic_07.sh TEST_PROGS += test_null_01.sh TEST_PROGS += test_null_02.sh
tools/testing/selftests/ublk/kublk.c +14 -8
@@ -536,12 +536,17 @@ int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)if (!(io->flags &UBLKSRV_IO_FREE ))return 0 ; -/* we issue because we need either fetching or committing */ +/* +* we issue because we need either fetching or committing or +* getting data +*/ if (!(io->flags & - (UBLKSRV_NEED_FETCH_RQ |UBLKSRV_NEED_COMMIT_RQ_COMP ))) + (UBLKSRV_NEED_FETCH_RQ |UBLKSRV_NEED_COMMIT_RQ_COMP |UBLKSRV_NEED_GET_DATA )))return 0 ; -if (io->flags &UBLKSRV_NEED_COMMIT_RQ_COMP ) +if (io->flags &UBLKSRV_NEED_GET_DATA ) + cmd_op =UBLK_U_IO_NEED_GET_DATA ; +else if (io->flags &UBLKSRV_NEED_COMMIT_RQ_COMP ) cmd_op =UBLK_U_IO_COMMIT_AND_FETCH_REQ ;else if (io->flags &UBLKSRV_NEED_FETCH_RQ ) cmd_op =UBLK_U_IO_FETCH_REQ ;
@@ -658,6 +663,9 @@ static void ublk_handle_cqe(struct io_uring *r,assert (tag < q->q_depth);if (q->tgt_ops->queue_io) q->tgt_ops->queue_io (q, tag); + }else if (cqe->res ==UBLK_IO_RES_NEED_GET_DATA ) { + io->flags |=UBLKSRV_NEED_GET_DATA |UBLKSRV_IO_FREE ; +ublk_queue_io_cmd (q, io, tag); }else {/* * COMMIT_REQ will be completed immediately since no fetching
@@ -1237,7 +1245,7 @@ static void __cmd_create_help(char *exe, bool recovery)printf ("%s %s -t [null|loop|stripe|fault_inject] [-q nr_queues] [-d depth] [-n dev_id]\n" , exe, recovery ?"recover" :"add" ); -printf ("\t[--foreground] [--quiet] [-z] [--debug_mask mask] [-r 0|1 ] [-g 0|1]\n" ); +printf ("\t[--foreground] [--quiet] [-z] [--debug_mask mask] [-r 0|1 ] [-g]\n" );printf ("\t[-e 0|1 ] [-i 0|1]\n" );printf ("\t[target options] [backfile1] [backfile2] ...\n" );printf ("\tdefault: nr_queues=2(max 32), depth=128(max 1024), dev_id=-1(auto allocation)\n" );
@@ -1313,7 +1321,7 @@ int main(int argc, char *argv[]) opterr =0 ; optind =2 ; -while ((opt =getopt_long (argc, argv,"t:n:d:q:r:e:i:az" , +while ((opt =getopt_long (argc, argv,"t:n:d:q:r:e:i:gaz" , longopts, &option_idx)) !=-1 ) {switch (opt) {case 'a' :
@@ -1351,9 +1359,7 @@ int main(int argc, char *argv[]) ctx.flags |=UBLK_F_USER_RECOVERY |UBLK_F_USER_RECOVERY_REISSUE ;break ;case 'g' : - value =strtol (optarg,NULL ,10 ); -if (value) - ctx.flags |=UBLK_F_NEED_GET_DATA ; + ctx.flags |=UBLK_F_NEED_GET_DATA ;break ;case 0 :if (!strcmp (longopts[option_idx].name,"debug_mask" ))
tools/testing/selftests/ublk/kublk.h +1 -0
@@ -115,6 +115,7 @@ struct ublk_io {#define UBLKSRV_NEED_FETCH_RQ (1UL << 0)#define UBLKSRV_NEED_COMMIT_RQ_COMP (1UL << 1)#define UBLKSRV_IO_FREE (1UL << 2) +#define UBLKSRV_NEED_GET_DATA (1UL << 3)unsigned short flags;unsigned short refs;/* used by target code only */
tools/testing/selftests/ublk/test_generic_07.sh new file +28 -0
@@ -0,0 +1,28 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh + +TID="generic_07" +ERR_CODE=0 + +if ! _have_program fio; then + exit "$UBLK_SKIP_CODE" +fi + +_prep_test "generic" "test UBLK_F_NEED_GET_DATA" + +_create_backfile 0 256M +dev_id=$(_add_ublk_dev -t loop -q 2 -g "${UBLK_BACKFILES[0]}") +_check_add_dev $TID $? + +# run fio over the ublk disk +_run_fio_verify_io --filename=/dev/ublkb"${dev_id}" --size=256M +ERR_CODE=$? +if [ "$ERR_CODE" -eq 0 ]; then + _mkfs_mount_test /dev/ublkb"${dev_id}" + ERR_CODE=$? +fi + +_cleanup_test "generic" +_show_result $TID $ERR_CODE
tools/testing/selftests/ublk/test_stress_05.sh +4 -4
@@ -47,15 +47,15 @@ _create_backfile 0 256M _create_backfile 1 256M for reissue in $(seq 0 1); do - ublk_io_and_remove 8G -t null -q 4 -g 1 -r 1 -i "$reissue" & - ublk_io_and_remove 256M -t loop -q 4 -g 1 -r 1 -i "$reissue" "${UBLK_BACKFILES[0]}" & + ublk_io_and_remove 8G -t null -q 4 -g -r 1 -i "$reissue" & + ublk_io_and_remove 256M -t loop -q 4 -g -r 1 -i "$reissue" "${UBLK_BACKFILES[0]}" & wait done if _have_feature "ZERO_COPY"; then for reissue in $(seq 0 1); do - ublk_io_and_remove 8G -t null -q 4 -g 1 -z -r 1 -i "$reissue" & - ublk_io_and_remove 256M -t loop -q 4 -g 1 -z -r 1 -i "$reissue" "${UBLK_BACKFILES[1]}" & + ublk_io_and_remove 8G -t null -q 4 -g -z -r 1 -i "$reissue" & + ublk_io_and_remove 256M -t loop -q 4 -g -z -r 1 -i "$reissue" "${UBLK_BACKFILES[1]}" & wait done fi
Signature · 1 line
2.47.0
PATCH v2 2/4 ublk: decouple zero copy from user copy
In reply to Ming LeiUBLK_F_USER_COPY and UBLK_F_SUPPORT_ZERO_COPY are two different features, and shouldn't be coupled together.
Commit 1f6540e2aabb ("ublk: zc register/unregister bvec") enables user copy automatically in case of UBLK_F_SUPPORT_ZERO_COPY, this way isn't correct.
So decouple zero copy from user copy, and use independent helper to check each one.
--- drivers/block/ublk_drv.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-)
drivers/block/ublk_drv.c +23 -12
@@ -205,11 +205,6 @@ static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub,static inline unsigned int ublk_req_build_flags (struct request *req);static inline struct ublksrv_io_desc *ublk_get_iod (struct ublk_queue *ubq,int tag); -static inline bool ublk_dev_is_user_copy (const struct ublk_device *ub) -{ -return ub->dev_info.flags & (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY ); -} -static inline bool ublk_dev_is_zoned (const struct ublk_device *ub) {return ub->dev_info.flags &UBLK_F_ZONED ;
@@ -609,14 +604,19 @@ static void ublk_apply_params(struct ublk_device *ub)ublk_dev_param_zoned_apply (ub); } +static inline bool ublk_support_zero_copy (const struct ublk_queue *ubq) +{ +return ubq->flags &UBLK_F_SUPPORT_ZERO_COPY ; +} +static inline bool ublk_support_user_copy (const struct ublk_queue *ubq) { -return ubq->flags & (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY ); +return ubq->flags &UBLK_F_USER_COPY ; }static inline bool ublk_need_map_io (const struct ublk_queue *ubq) { -return !ublk_support_user_copy (ubq); +return !ublk_support_user_copy (ubq) && !ublk_support_zero_copy (ubq); }static inline bool ublk_need_req_ref (const struct ublk_queue *ubq)
@@ -624,8 +624,11 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)/* * read()/write() is involved in user copy, so request reference * has to be grabbed +* +* for zero copy, request buffer need to be registered to io_uring +* buffer table, so reference is needed */ -return ublk_support_user_copy (ubq); +return ublk_support_user_copy (ubq) ||ublk_support_zero_copy (ubq); } static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
@@ -2245,6 +2248,9 @@ static struct request *ublk_check_and_get_req(struct kiocb *iocb,if (!ubq)return ERR_PTR (-EINVAL ); +if (!ublk_support_user_copy (ubq)) +return ERR_PTR (-EACCES ); +if (tag >= ubq->q_depth)return ERR_PTR (-EINVAL );
@@ -2783,13 +2789,18 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header) ub->dev_info.flags |=UBLK_F_CMD_IOCTL_ENCODE |UBLK_F_URING_CMD_COMP_IN_TASK ; -/* GET_DATA isn't needed any more with USER_COPY */ -if (ublk_dev_is_user_copy (ub)) +/* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */ +if (ub->dev_info.flags & (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY )) ub->dev_info.flags &= ~UBLK_F_NEED_GET_DATA ; -/* Zoned storage support requires user copy feature */ +/* +* Zoned storage support requires reuse `ublksrv_io_cmd->addr` for +* returning write_append_lba, which is only allowed in case of +* user copy or zero copy +*/ if (ublk_dev_is_zoned (ub) && - (!IS_ENABLED (CONFIG_BLK_DEV_ZONED ) || !ublk_dev_is_user_copy (ub))) { + (!IS_ENABLED (CONFIG_BLK_DEV_ZONED ) || !(ub->dev_info.flags & + (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY )))) { ret = -EINVAL ; goto out_free_dev_number; }
Signature · 1 line
2.47.0
PATCH v2 3/4 ublk: enhance check for register/unregister io buffer command
In reply to Ming LeiThe simple check of UBLK_IO_FLAG_OWNED_BY_SRV can avoid incorrect register/unregister io buffer easily, so check it before calling starting to register/un-register io buffer.
Also only allow io buffer register/unregister uring_cmd in case of UBLK_F_SUPPORT_ZERO_COPY.
Also mark argument 'ublk_queue *' of ublk_register_io_buf as const.
--- drivers/block/ublk_drv.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/block/ublk_drv.c +20 -4
@@ -201,7 +201,7 @@ struct ublk_params_header {static void ublk_stop_dev_unlocked (struct ublk_device *ub);static void ublk_abort_queue (struct ublk_device *ub,struct ublk_queue *ubq);static inline struct request *__ublk_check_and_get_req (struct ublk_device *ub, -struct ublk_queue *ubq,int tag,size_t offset); +const struct ublk_queue *ubq,int tag,size_t offset);static inline unsigned int ublk_req_build_flags (struct request *req);static inline struct ublksrv_io_desc *ublk_get_iod (struct ublk_queue *ubq,int tag);
@@ -1949,13 +1949,20 @@ static void ublk_io_release(void *priv) }static int ublk_register_io_buf (struct io_uring_cmd *cmd, -struct ublk_queue *ubq,unsigned int tag, +const struct ublk_queue *ubq,unsigned int tag,unsigned int index,unsigned int issue_flags) {struct ublk_device *ub = cmd->file->private_data; +const struct ublk_io *io = &ubq->ios[tag];struct request *req;int ret; +if (!ublk_support_zero_copy (ubq)) +return -EINVAL ; + +if (!(io->flags &UBLK_IO_FLAG_OWNED_BY_SRV )) +return -EINVAL ; + req =__ublk_check_and_get_req (ub, ubq, tag,0 );if (!req)return -EINVAL ;
@@ -1971,8 +1978,17 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd, }static int ublk_unregister_io_buf (struct io_uring_cmd *cmd, +const struct ublk_queue *ubq,unsigned int tag,unsigned int index,unsigned int issue_flags) { +const struct ublk_io *io = &ubq->ios[tag]; + +if (!ublk_support_zero_copy (ubq)) +return -EINVAL ; + +if (!(io->flags &UBLK_IO_FLAG_OWNED_BY_SRV )) +return -EINVAL ; +return io_buffer_unregister_bvec (cmd, index, issue_flags); }
@@ -2076,7 +2092,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,case UBLK_IO_REGISTER_IO_BUF :return ublk_register_io_buf (cmd, ubq, tag, ub_cmd->addr, issue_flags);case UBLK_IO_UNREGISTER_IO_BUF : -return ublk_unregister_io_buf (cmd, ub_cmd->addr, issue_flags); +return ublk_unregister_io_buf (cmd, ubq, tag, ub_cmd->addr, issue_flags);case UBLK_IO_FETCH_REQ : ret =ublk_fetch (cmd, ubq, io, ub_cmd->addr);if (ret)
@@ -2128,7 +2144,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, }static inline struct request *__ublk_check_and_get_req (struct ublk_device *ub, -struct ublk_queue *ubq,int tag,size_t offset) +const struct ublk_queue *ubq,int tag,size_t offset) {struct request *req;
Signature · 1 line
2.47.0
PATCH v2 4/4 ublk: remove the check of ublk_need_req_ref() from __ublk_check_and_get_req
In reply to Ming Lei__ublk_check_and_get_req() is only called from ublk_check_and_get_req() and ublk_register_io_buf(), the same check has been covered in the two calling sites.
So remove the check from __ublk_check_and_get_req().
--- drivers/block/ublk_drv.c | 3 --- 1 file changed, 3 deletions(-)
drivers/block/ublk_drv.c +0 -3
@@ -2148,9 +2148,6 @@ static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub, {struct request *req; -if (!ublk_need_req_ref (ubq)) -return NULL ; - req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag);if (!req)return NULL ;
Signature · 1 line
2.47.0
Quoted 10 lines · Ming Lei
The 1st patch fixes
UBLK_F_NEED_GET_DATAsupport in ublk selftest side.The other two patches enhances check for zero copy feature.
The final patch removes one unnecessary check.
Thanks,
Ming[...]
Applied, thanks!
[1/4] selftests: ublk: fix UBLK_F_NEED_GET_DATA
commit: 730d837979bac203c786f2c5b0707f5426275c0d
[2/4] ublk: decouple zero copy from user copy
commit: 69edf98be844375807f299397c516fb1e962b3cc
[3/4] ublk: enhance check for register/unregister io buffer command
commit: 6240f43b29f285a40eebeb789756673af7a7d67c
[4/4] ublk: remove the check of ublk_need_req_ref() from __ublk_check_and_get_req
commit: a584b2630b0d31f8a20e4ccb4de370b160177b8aBest regards,
Signature · 1 line
Jens Axboe