From f510198855b6ddac0ffe62684dc407c56d3e0f24 Mon Sep 17 00:00:00 2001 From: Caleb Sander Mateos Date: Mon, 3 Aug 2026 15:14:33 -0600 Subject: [PATCH] ublk: consistently use u16 for queue and tag numbers The u16 nr_hw_queues and queue_depth fields of the ublk UAPI struct ublksrv_ctrl_dev_info constrain the number of queues and queue depth of each ublk device. However, the ublk driver is a bit inconsistent with the type it uses to represent these values, mixing u16 with int and unsigned int. Change all queue number, queue depth, q_id, and tag variables/fields to u16 to save some space. Signed-off-by: Caleb Sander Mateos Reviewed-by: Ming Lei Link: https://patch.msgid.link/20260803211441.2538144-2-csander@purestorage.com Signed-off-by: Jens Axboe --- drivers/block/ublk_drv.c | 80 ++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 8101c2a73efd..0743b5ba241d 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -237,8 +237,8 @@ struct ublk_io { } ____cacheline_aligned_in_smp; struct ublk_queue { - int q_id; - int q_depth; + u16 q_id; + u16 q_depth; unsigned long flags; struct ublksrv_io_desc *io_cmd_buf; @@ -248,7 +248,7 @@ struct ublk_queue { bool fail_io; /* copy of dev->state == UBLK_S_DEV_FAIL_IO */ spinlock_t cancel_lock; struct ublk_device *dev; - u32 nr_io_ready; + u16 nr_io_ready; /* * For supporting UBLK_F_BATCH_IO only. @@ -327,7 +327,7 @@ struct ublk_device { struct ublk_params params; - u32 nr_queue_ready; + u16 nr_queue_ready; bool unprivileged_daemons; struct mutex cancel_mutex; bool canceling; @@ -403,7 +403,7 @@ static inline void ublk_io_evts_deinit(struct ublk_queue *q) } static inline struct ublksrv_io_desc * -ublk_get_iod(const struct ublk_queue *ubq, unsigned tag) +ublk_get_iod(const struct ublk_queue *ubq, u16 tag) { return &ubq->io_cmd_buf[tag]; } @@ -423,8 +423,7 @@ static inline bool ublk_support_shmem_zc(const struct ublk_queue *ubq) return ubq->flags & UBLK_F_SHMEM_ZC; } -static inline bool ublk_iod_is_shmem_zc(const struct ublk_queue *ubq, - unsigned int tag) +static inline bool ublk_iod_is_shmem_zc(const struct ublk_queue *ubq, u16 tag) { return ublk_get_iod(ubq, tag)->op_flags & UBLK_IO_F_SHMEM_ZC; } @@ -864,7 +863,7 @@ static unsigned int unprivileged_ublks_added; /* protected by ublk_ctl_mutex */ static struct miscdevice ublk_misc; -static inline unsigned ublk_pos_to_hwq(loff_t pos) +static inline u16 ublk_pos_to_hwq(loff_t pos) { return ((pos - UBLKSRV_IO_BUF_OFFSET) >> UBLK_QID_OFF) & UBLK_QID_BITS_MASK; @@ -875,7 +874,7 @@ static inline unsigned ublk_pos_to_buf_off(loff_t pos) return (pos - UBLKSRV_IO_BUF_OFFSET) & UBLK_IO_BUF_BITS_MASK; } -static inline unsigned ublk_pos_to_tag(loff_t pos) +static inline u16 ublk_pos_to_tag(loff_t pos) { return ((pos - UBLKSRV_IO_BUF_OFFSET) >> UBLK_TAG_OFF) & UBLK_TAG_BITS_MASK; @@ -1231,18 +1230,18 @@ static noinline void ublk_put_device(struct ublk_device *ub) } static inline struct ublk_queue *ublk_get_queue(struct ublk_device *dev, - int qid) + u16 qid) { return dev->queues[qid]; } static inline struct ublksrv_io_desc * -ublk_queue_cmd_buf(struct ublk_device *ub, int q_id) +ublk_queue_cmd_buf(struct ublk_device *ub, u16 q_id) { return ublk_get_queue(ub, q_id)->io_cmd_buf; } -static inline int __ublk_queue_cmd_buf_size(int depth) +static inline int __ublk_queue_cmd_buf_size(u16 depth) { return round_up(depth * sizeof(struct ublksrv_io_desc), PAGE_SIZE); } @@ -1667,7 +1666,7 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq, } static void -ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, unsigned tag) +ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, u16 tag) { struct ublksrv_io_desc *iod = ublk_get_iod(ubq, tag); @@ -1778,7 +1777,7 @@ static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req, static void ublk_dispatch_req(struct ublk_queue *ubq, struct request *req) { unsigned int issue_flags = IO_URING_CMD_TASK_WORK_ISSUE_FLAGS; - int tag = req->tag; + u16 tag = req->tag; struct ublk_io *io = &ubq->ios[tag]; pr_devel("%s: complete: qid %d tag %d io_flags %x addr %llx\n", @@ -2368,7 +2367,7 @@ static const struct blk_mq_ops ublk_batch_mq_ops = { static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq) { - int i; + u16 i; ubq->nr_io_ready = 0; @@ -2413,7 +2412,7 @@ static int ublk_ch_open(struct inode *inode, struct file *filp) static void ublk_reset_ch_dev(struct ublk_device *ub) { - int i; + u16 i; for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { struct ublk_queue *ubq = ublk_get_queue(ub, i); @@ -2485,7 +2484,7 @@ out: static void ublk_set_canceling(struct ublk_device *ub, bool canceling) __must_hold(&ub->cancel_mutex) { - int i; + u16 i; ub->canceling = canceling; for (i = 0; i < ub->dev_info.nr_hw_queues; i++) @@ -2494,7 +2493,7 @@ static void ublk_set_canceling(struct ublk_device *ub, bool canceling) static bool ublk_check_and_reset_active_ref(struct ublk_device *ub) { - int i, j; + u16 i, j; if (!ublk_dev_need_req_ref(ub)) return false; @@ -2527,7 +2526,7 @@ static void ublk_ch_release_work_fn(struct work_struct *work) struct ublk_device *ub = container_of(work, struct ublk_device, exit_work.work); struct gendisk *disk; - int i; + u16 i; /* * For zero-copy and auto buffer register modes, I/O references @@ -2646,7 +2645,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma) size_t sz = vma->vm_end - vma->vm_start; unsigned max_sz = ublk_max_cmd_buf_size(); unsigned long pfn, end, phys_off = vma->vm_pgoff << PAGE_SHIFT; - int q_id, ret = 0; + int ret = 0; + u16 q_id; spin_lock(&ub->lock); if (!ub->mm) @@ -2719,7 +2719,7 @@ static void ublk_abort_batch_queue(struct ublk_device *ub, */ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq) { - int i; + u16 i; for (i = 0; i < ubq->q_depth; i++) { struct ublk_io *io = &ubq->ios[i]; @@ -2762,7 +2762,7 @@ out: ublk_put_disk(disk); } -static void ublk_cancel_cmd(struct ublk_queue *ubq, unsigned tag, +static void ublk_cancel_cmd(struct ublk_queue *ubq, u16 tag, unsigned int issue_flags) { struct ublk_io *io = &ubq->ios[tag]; @@ -2913,7 +2913,7 @@ static inline bool ublk_dev_ready(const struct ublk_device *ub) static void ublk_cancel_queue(struct ublk_queue *ubq) { - int i; + u16 i; if (ublk_support_batch_io(ubq)) { ublk_batch_cancel_queue(ubq); @@ -2927,7 +2927,7 @@ static void ublk_cancel_queue(struct ublk_queue *ubq) /* Cancel all pending commands, must be called after del_gendisk() returns */ static void ublk_cancel_dev(struct ublk_device *ub) { - int i; + u16 i; for (i = 0; i < ub->dev_info.nr_hw_queues; i++) ublk_cancel_queue(ublk_get_queue(ub, i)); @@ -2961,7 +2961,7 @@ static void ublk_wait_tagset_rqs_idle(struct ublk_device *ub) static void ublk_force_abort_dev(struct ublk_device *ub) { - int i; + u16 i; pr_devel("%s: force abort ub: dev_id %d state %s\n", __func__, ub->dev_info.dev_id, @@ -3158,7 +3158,7 @@ ublk_config_io_buf(const struct ublk_device *ub, struct ublk_io *io, static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, unsigned int issue_flags, - struct ublk_queue *ubq, unsigned int tag) + struct ublk_queue *ubq, u16 tag) { struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd); @@ -3962,8 +3962,8 @@ static int ublk_handle_non_batch_cmd(struct io_uring_cmd *cmd, const struct ublksrv_io_cmd *ub_cmd = io_uring_sqe_cmd(cmd->sqe, struct ublksrv_io_cmd); struct ublk_device *ub = cmd->file->private_data; - unsigned tag = READ_ONCE(ub_cmd->tag); - unsigned q_id = READ_ONCE(ub_cmd->q_id); + u16 tag = READ_ONCE(ub_cmd->tag); + u16 q_id = READ_ONCE(ub_cmd->q_id); unsigned index = READ_ONCE(ub_cmd->addr); struct ublk_queue *ubq; struct ublk_io *io; @@ -4169,7 +4169,8 @@ static const struct file_operations ublk_ch_batch_io_fops = { static void __ublk_deinit_queue(struct ublk_device *ub, struct ublk_queue *ubq) { - int size, i; + int size; + u16 i; size = ublk_queue_cmd_buf_size(ub); @@ -4190,7 +4191,7 @@ static void __ublk_deinit_queue(struct ublk_device *ub, struct ublk_queue *ubq) kvfree(ubq); } -static void ublk_deinit_queue(struct ublk_device *ub, int q_id) +static void ublk_deinit_queue(struct ublk_device *ub, u16 q_id) { struct ublk_queue *ubq = ub->queues[q_id]; @@ -4201,7 +4202,7 @@ static void ublk_deinit_queue(struct ublk_device *ub, int q_id) ub->queues[q_id] = NULL; } -static int ublk_get_queue_numa_node(struct ublk_device *ub, int q_id) +static int ublk_get_queue_numa_node(struct ublk_device *ub, u16 q_id) { unsigned int cpu; @@ -4214,14 +4215,15 @@ static int ublk_get_queue_numa_node(struct ublk_device *ub, int q_id) return NUMA_NO_NODE; } -static int ublk_init_queue(struct ublk_device *ub, int q_id) +static int ublk_init_queue(struct ublk_device *ub, u16 q_id) { - int depth = ub->dev_info.queue_depth; + u16 depth = ub->dev_info.queue_depth; gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO; struct ublk_queue *ubq; struct page *page; int numa_node; - int size, i, ret; + int size, ret; + u16 i; /* Determine NUMA node based on queue's CPU affinity */ numa_node = ublk_get_queue_numa_node(ub, q_id); @@ -4266,7 +4268,7 @@ fail: static void ublk_deinit_queues(struct ublk_device *ub) { - int i; + u16 i; for (i = 0; i < ub->dev_info.nr_hw_queues; i++) ublk_deinit_queue(ub, i); @@ -4274,7 +4276,8 @@ static void ublk_deinit_queues(struct ublk_device *ub) static int ublk_init_queues(struct ublk_device *ub) { - int i, ret; + int ret; + u16 i; for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { ret = ublk_init_queue(ub, i); @@ -5172,7 +5175,7 @@ out: struct count_busy { const struct ublk_queue *ubq; - unsigned int nr_busy; + u16 nr_busy; }; static bool ublk_count_busy_req(struct request *rq, void *data) @@ -5210,8 +5213,7 @@ static int ublk_wait_for_idle_io(struct ublk_device *ub, return 0; while (elapsed < timeout_ms && !signal_pending(current)) { - unsigned int queues_cancelable = 0; - int i; + u16 i, queues_cancelable = 0; for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { struct ublk_queue *ubq = ublk_get_queue(ub, i);