]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
ublk: use struct_size() for allocation
authorMing Lei <ming.lei@redhat.com>
Sat, 1 Nov 2025 13:31:18 +0000 (21:31 +0800)
committerJens Axboe <axboe@kernel.dk>
Mon, 3 Nov 2025 15:34:59 +0000 (08:34 -0700)
Convert ublk_queue to use struct_size() for allocation.

Changes in this commit:

1. Update ublk_init_queue() to use struct_size(ubq, ios, depth)
   instead of manual size calculation (sizeof(struct ublk_queue) +
   depth * sizeof(struct ublk_io)).

This provides better type safety and makes the code more maintainable
by using standard kernel macro for flexible array handling.

Meantime annotate ublk_queue.ios by __counted_by().

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

index d920f7cf4dad2a52fa6672e55d72971c00de0c2e..96e07763cd28c9929879bcecde86d9541617875f 100644 (file)
@@ -203,7 +203,7 @@ struct ublk_queue {
        bool fail_io; /* copy of dev->state == UBLK_S_DEV_FAIL_IO */
        spinlock_t              cancel_lock;
        struct ublk_device *dev;
-       struct ublk_io ios[];
+       struct ublk_io ios[] __counted_by(q_depth);
 };
 
 struct ublk_device {
@@ -2700,7 +2700,6 @@ static int ublk_get_queue_numa_node(struct ublk_device *ub, int q_id)
 static int ublk_init_queue(struct ublk_device *ub, int q_id)
 {
        int depth = ub->dev_info.queue_depth;
-       int ubq_size = sizeof(struct ublk_queue) + depth * sizeof(struct ublk_io);
        gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
        struct ublk_queue *ubq;
        struct page *page;
@@ -2711,7 +2710,8 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
        numa_node = ublk_get_queue_numa_node(ub, q_id);
 
        /* Allocate queue structure on local NUMA node */
-       ubq = kvzalloc_node(ubq_size, GFP_KERNEL, numa_node);
+       ubq = kvzalloc_node(struct_size(ubq, ios, depth), GFP_KERNEL,
+                           numa_node);
        if (!ubq)
                return -ENOMEM;