]> www.infradead.org Git - nvme.git/commitdiff
ublk: add segment parameter
authorMing Lei <ming.lei@redhat.com>
Thu, 27 Mar 2025 09:51:15 +0000 (17:51 +0800)
committerKeith Busch <kbusch@kernel.org>
Mon, 31 Mar 2025 15:48:24 +0000 (08:48 -0700)
IO split is usually bad in io_uring world, since -EAGAIN is caused and
IO handling may have to fallback to io-wq, this way does hurt performance.

ublk starts to support zero copy recently, for avoiding unnecessary IO
split, ublk driver's segment limit should be aligned with backend
device's segment limit.

Another reason is that io_buffer_register_bvec() needs to allocate bvecs,
which number is aligned with ublk request segment number, so that big
memory allocation can be avoided by setting reasonable max_segments limit.

So add segment parameter for providing ublk server chance to align
segment limit with backend, and keep it reasonable from implementation
viewpoint.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250327095123.179113-7-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/ublk_drv.c
include/uapi/linux/ublk_cmd.h

index 1e11816d0b90c606c8be8272c5461f45589c325f..a5bcf3aa9d8c96dfef6f89b62f92af7a95ecf946 100644 (file)
@@ -74,7 +74,7 @@
 #define UBLK_PARAM_TYPE_ALL                                \
        (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD | \
         UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
-        UBLK_PARAM_TYPE_DMA_ALIGN)
+        UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
 struct ublk_rq_data {
        struct kref ref;
@@ -580,6 +580,18 @@ static int ublk_validate_params(const struct ublk_device *ub)
                        return -EINVAL;
        }
 
+       if (ub->params.types & UBLK_PARAM_TYPE_SEGMENT) {
+               const struct ublk_param_segment *p = &ub->params.seg;
+
+               if (!is_power_of_2(p->seg_boundary_mask + 1))
+                       return -EINVAL;
+
+               if (p->seg_boundary_mask + 1 < UBLK_MIN_SEGMENT_SIZE)
+                       return -EINVAL;
+               if (p->max_segment_size < UBLK_MIN_SEGMENT_SIZE)
+                       return -EINVAL;
+       }
+
        return 0;
 }
 
@@ -2370,6 +2382,12 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd)
        if (ub->params.types & UBLK_PARAM_TYPE_DMA_ALIGN)
                lim.dma_alignment = ub->params.dma.alignment;
 
+       if (ub->params.types & UBLK_PARAM_TYPE_SEGMENT) {
+               lim.seg_boundary_mask = ub->params.seg.seg_boundary_mask;
+               lim.max_segment_size = ub->params.seg.max_segment_size;
+               lim.max_segments = ub->params.seg.max_segments;
+       }
+
        if (wait_for_completion_interruptible(&ub->completion) != 0)
                return -EINTR;
 
index 7255b36b5cf6336fd925cc8ec05598ca7c37bada..583b86681c93de4b0b0d501b5bee8b8d1ec9ecc9 100644 (file)
@@ -410,6 +410,29 @@ struct ublk_param_dma_align {
        __u8    pad[4];
 };
 
+#define UBLK_MIN_SEGMENT_SIZE   4096
+/*
+ * If any one of the three segment parameter is set as 0, the behavior is
+ * undefined.
+ */
+struct ublk_param_segment {
+       /*
+        * seg_boundary_mask + 1 needs to be power_of_2(), and the sum has
+        * to be >= UBLK_MIN_SEGMENT_SIZE(4096)
+        */
+       __u64   seg_boundary_mask;
+
+       /*
+        * max_segment_size could be override by virt_boundary_mask, so be
+        * careful when setting both.
+        *
+        * max_segment_size has to be >= UBLK_MIN_SEGMENT_SIZE(4096)
+        */
+       __u32   max_segment_size;
+       __u16   max_segments;
+       __u8    pad[2];
+};
+
 struct ublk_params {
        /*
         * Total length of parameters, userspace has to set 'len' for both
@@ -423,6 +446,7 @@ struct ublk_params {
 #define UBLK_PARAM_TYPE_DEVT            (1 << 2)
 #define UBLK_PARAM_TYPE_ZONED           (1 << 3)
 #define UBLK_PARAM_TYPE_DMA_ALIGN       (1 << 4)
+#define UBLK_PARAM_TYPE_SEGMENT         (1 << 5)
        __u32   types;                  /* types of parameter included */
 
        struct ublk_param_basic         basic;
@@ -430,6 +454,7 @@ struct ublk_params {
        struct ublk_param_devt          devt;
        struct ublk_param_zoned zoned;
        struct ublk_param_dma_align     dma;
+       struct ublk_param_segment       seg;
 };
 
 #endif