]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
ublk: prepare for supporting to register request buffer automatically
authorMing Lei <ming.lei@redhat.com>
Tue, 20 May 2025 04:54:32 +0000 (12:54 +0800)
committerJens Axboe <axboe@kernel.dk>
Tue, 20 May 2025 16:24:45 +0000 (10:24 -0600)
UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer
register/unregister uring_cmd for each IO, this way is not only inefficient,
but also introduce dependency between buffer consumer and buffer register/
unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c
in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

Prepare for adding feature UBLK_F_AUTO_BUF_REG for addressing the existing
zero copy limitation:

- register request buffer automatically to ublk uring_cmd's io_uring
  context before delivering io command to ublk server

- unregister request buffer automatically from the ublk uring_cmd's
  io_uring context when completing the request

- io_uring will unregister the buffer automatically when uring is
  exiting, so we needn't worry about accident exit

For using this feature, ublk server has to create one sparse buffer table

Reviewed-by: Caleb Sander Mateos <csander@purestorage.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250520045455.515691-3-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/ublk_drv.c

index ae2f47dc82247a2b17ac72a6c37bd9b2f536276c..3e56a9d267fbfc27efec2800bad78f1adc11550b 100644 (file)
@@ -133,6 +133,14 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
 
+/*
+ * request buffer is registered automatically, so we have to unregister it
+ * before completing this request.
+ *
+ * io_uring will unregister buffer automatically for us during exiting.
+ */
+#define UBLK_IO_FLAG_AUTO_BUF_REG      0x10
+
 /* atomic RW with ubq->cancel_lock */
 #define UBLK_IO_FLAG_CANCELED  0x80000000
 
@@ -205,6 +213,7 @@ struct ublk_params_header {
        __u32   types;
 };
 
+static void ublk_io_release(void *priv);
 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,
@@ -619,6 +628,11 @@ 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_auto_buf_reg(const struct ublk_queue *ubq)
+{
+       return false;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & UBLK_F_USER_COPY;
@@ -626,7 +640,8 @@ static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 
 static inline bool ublk_need_map_io(const struct ublk_queue *ubq)
 {
-       return !ublk_support_user_copy(ubq) && !ublk_support_zero_copy(ubq);
+       return !ublk_support_user_copy(ubq) && !ublk_support_zero_copy(ubq) &&
+               !ublk_support_auto_buf_reg(ubq);
 }
 
 static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
@@ -637,8 +652,13 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
         *
         * for zero copy, request buffer need to be registered to io_uring
         * buffer table, so reference is needed
+        *
+        * For auto buffer register, ublk server still may issue
+        * UBLK_IO_COMMIT_AND_FETCH_REQ before one registered buffer is used up,
+        * so reference is required too.
         */
-       return ublk_support_user_copy(ubq) || ublk_support_zero_copy(ubq);
+       return ublk_support_user_copy(ubq) || ublk_support_zero_copy(ubq) ||
+               ublk_support_auto_buf_reg(ubq);
 }
 
 static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
@@ -1155,6 +1175,35 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq,
                blk_mq_end_request(rq, BLK_STS_IOERR);
 }
 
+static bool ublk_auto_buf_reg(struct request *req, struct ublk_io *io,
+                             unsigned int issue_flags)
+{
+       struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
+       int ret;
+
+       ret = io_buffer_register_bvec(io->cmd, req, ublk_io_release, 0,
+                                     issue_flags);
+       if (ret) {
+               blk_mq_end_request(req, BLK_STS_IOERR);
+               return false;
+       }
+       /* one extra reference is dropped by ublk_io_release */
+       refcount_set(&data->ref, 2);
+       io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
+       return true;
+}
+
+static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq,
+                                  struct request *req, struct ublk_io *io,
+                                  unsigned int issue_flags)
+{
+       if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req))
+               return ublk_auto_buf_reg(req, io, issue_flags);
+
+       ublk_init_req_ref(ubq, req);
+       return true;
+}
+
 static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req,
                          struct ublk_io *io)
 {
@@ -1180,7 +1229,6 @@ static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req,
                        mapped_bytes >> 9;
        }
 
-       ublk_init_req_ref(ubq, req);
        return true;
 }
 
@@ -1226,7 +1274,8 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
        if (!ublk_start_io(ubq, req, io))
                return;
 
-       ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags);
+       if (ublk_prep_auto_buf_reg(ubq, req, io, issue_flags))
+               ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags);
 }
 
 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
@@ -1994,7 +2043,8 @@ out:
 
 static int ublk_commit_and_fetch(const struct ublk_queue *ubq,
                                 struct ublk_io *io, struct io_uring_cmd *cmd,
-                                const struct ublksrv_io_cmd *ub_cmd)
+                                const struct ublksrv_io_cmd *ub_cmd,
+                                unsigned int issue_flags)
 {
        struct request *req = io->req;
 
@@ -2014,6 +2064,14 @@ static int ublk_commit_and_fetch(const struct ublk_queue *ubq,
                return -EINVAL;
        }
 
+       if (ublk_support_auto_buf_reg(ubq)) {
+               if (io->flags & UBLK_IO_FLAG_AUTO_BUF_REG) {
+                       WARN_ON_ONCE(io_buffer_unregister_bvec(cmd, 0,
+                                               issue_flags));
+                       io->flags &= ~UBLK_IO_FLAG_AUTO_BUF_REG;
+               }
+       }
+
        ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
 
        /* now this cmd slot is owned by ublk driver */
@@ -2110,7 +2168,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
                        goto out;
                break;
        case UBLK_IO_COMMIT_AND_FETCH_REQ:
-               ret = ublk_commit_and_fetch(ubq, io, cmd, ub_cmd);
+               ret = ublk_commit_and_fetch(ubq, io, cmd, ub_cmd, issue_flags);
                if (ret)
                        goto out;
                break;