Skip to contentSkip to conversation

ioat/dca: Use struct_size() helper

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Aug 28, 2019 – Sep 4, 2019 dmaengine

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ioat/dca: Use struct_size() helper

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct ioat_dca_priv {
	...
        struct ioat_dca_slot     req_slots[0];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

sizeof(*ioatdca) + (sizeof(struct ioat_dca_slot) * slots)

with:

struct_size(ioatdca, req_slots, slots)

This code was detected with the help of Coccinelle.

---
 drivers/dma/ioat/dca.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/dma/ioat/dca.c +1 -2
index 70fd8454d002..be61c32a876f 100644
@@ -286,8 +286,7 @@ struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase)
 		return NULL;
 
 	dca = alloc_dca_provider(&ioat_dca_ops,
-				 sizeof(*ioatdca)
-				      + (sizeof(struct ioat_dca_slot) * slots));
+				 struct_size(ioatdca, req_slots, slots));
 	if (!dca)
 		return NULL;
 
Signature · 1 line
2.23.0
Acked-by on v1 1/1
#1 lore raw
Quoted 16 lines · Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct ioat_dca_priv {
	...
        struct ioat_dca_slot     req_slots[0];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

sizeof(*ioatdca) + (sizeof(struct ioat_dca_slot) * slots)

Quoted 7 lines

with:

struct_size(ioatdca, req_slots, slots)

This code was detected with the help of Coccinelle.

Quoted 19 lines
---
 drivers/dma/ioat/dca.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/dma/ioat/dca.c +1 -2
index 70fd8454d002..be61c32a876f 100644
@@ -286,8 +286,7 @@ struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase)
 		return NULL;
 
 	dca = alloc_dca_provider(&ioat_dca_ops,
-				 sizeof(*ioatdca)
-				      + (sizeof(struct ioat_dca_slot) * slots));
+				 struct_size(ioatdca, req_slots, slots));
 	if (!dca)
 		return NULL;
 
maintainer
#2 lore raw
In reply to Gustavo A. R. Silva
Quoted 14 lines · Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct ioat_dca_priv {
	...
        struct ioat_dca_slot     req_slots[0];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

Quoted 7 lines

sizeof(*ioatdca) + (sizeof(struct ioat_dca_slot) * slots)

with:

struct_size(ioatdca, req_slots, slots)

This code was detected with the help of Coccinelle.

Please do not invent subsystem tags, git log should tell you the convention to be used!

Applied after fixing tags, thanks

Signature · 1 line
~Vinod