Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ublk: use struct_size() for allocation

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>

authored by

Ming Lei and committed by
Jens Axboe
c28ba6b6 529d4d63

+3 -3
+3 -3
drivers/block/ublk_drv.c
··· 203 203 bool fail_io; /* copy of dev->state == UBLK_S_DEV_FAIL_IO */ 204 204 spinlock_t cancel_lock; 205 205 struct ublk_device *dev; 206 - struct ublk_io ios[]; 206 + struct ublk_io ios[] __counted_by(q_depth); 207 207 }; 208 208 209 209 struct ublk_device { ··· 2700 2700 static int ublk_init_queue(struct ublk_device *ub, int q_id) 2701 2701 { 2702 2702 int depth = ub->dev_info.queue_depth; 2703 - int ubq_size = sizeof(struct ublk_queue) + depth * sizeof(struct ublk_io); 2704 2703 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO; 2705 2704 struct ublk_queue *ubq; 2706 2705 struct page *page; ··· 2710 2711 numa_node = ublk_get_queue_numa_node(ub, q_id); 2711 2712 2712 2713 /* Allocate queue structure on local NUMA node */ 2713 - ubq = kvzalloc_node(ubq_size, GFP_KERNEL, numa_node); 2714 + ubq = kvzalloc_node(struct_size(ubq, ios, depth), GFP_KERNEL, 2715 + numa_node); 2714 2716 if (!ubq) 2715 2717 return -ENOMEM; 2716 2718