Skip to contentSkip to conversation

nvmet: prefer flex_array_size and struct_size over open coded arithmetic

PATCH Applied Len Baker 5 participants 6 messages Oct 24, 2021 – Oct 29, 2021

Subsystems (2)
lore
Share & tools
lore

Len Baker

author
#0 lore raw

PATCH 1/1 nvmet: prefer flex_array_size and struct_size over open coded arithmetic

In an effort to avoid open-coded arithmetic in the kernel [1], use the flex_array_size() and struct_size() helpers instead of an open-coded calculation.

[1] https://github.com/KSPP/linux/issues/160

---
 drivers/nvme/host/multipath.c   | 2 +-
 drivers/nvme/target/admin-cmd.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/multipath.c +1 -1
index 954e84df6eb7..7f2071f2460c 100644
@@ -562,7 +562,7 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
 			return -EINVAL;
 
 		nr_nsids = le32_to_cpu(desc->nnsids);
-		nsid_buf_size = nr_nsids * sizeof(__le32);
+		nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
 
 		if (WARN_ON_ONCE(desc->grpid == 0))
 			return -EINVAL;
drivers/nvme/target/admin-cmd.c +1 -1
index 403de678fd06..6fb24746de06 100644
@@ -264,7 +264,7 @@ static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
 	desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
 	desc->state = req->port->ana_state[grpid];
 	memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
-	return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);
+	return struct_size(desc, nsids, count);
 }
 
 static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)

--
2.25.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to Len Baker
Quoted 7 lines · Len Baker

In an effort to avoid open-coded arithmetic in the kernel [1], use the flex_array_size() and struct_size() helpers instead of an open-coded calculation.

[1] https://github.com/KSPP/linux/issues/160

Thanks
--
Gustavo

Quoted 34 lines
---
 drivers/nvme/host/multipath.c   | 2 +-
 drivers/nvme/target/admin-cmd.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/multipath.c +1 -1
index 954e84df6eb7..7f2071f2460c 100644
@@ -562,7 +562,7 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
 			return -EINVAL;
 
 		nr_nsids = le32_to_cpu(desc->nnsids);
-		nsid_buf_size = nr_nsids * sizeof(__le32);
+		nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
 
 		if (WARN_ON_ONCE(desc->grpid == 0))
 			return -EINVAL;
drivers/nvme/target/admin-cmd.c +1 -2
index 403de678fd06..6fb24746de06 100644
@@ -264,7 +264,7 @@ static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
 	desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
 	desc->state = req->port->ana_state[grpid];
 	memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
-	return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);
+	return struct_size(desc, nsids, count);
 }
 
 static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
--

2.25.1

author
#5 lore raw
Quoted 3 lines · Christoph Hellwig

Thanks,

applied to nvme-5.16 with a fixed up subject.

Hi all,

Thanks Sagi, Chaitanya, Gustavo and Christoph.

Regards,
Len