Skip to contentSkip to conversation

thunderbolt: Use kcalloc and correct the argument to sizeof

PATCH In review Himangi Saraogi 3 participants 4 messages Jul 8, 2014 – Jul 11, 2014 Thunderbolt driver

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 thunderbolt: Use kcalloc and correct the argument to sizeof

nhi->rx_rings does not have type as struct tb_ring *, as it is a double pointer. So, the elements of the array should have pointer type, not structure type. The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The Coccinelle semantic patch that makes the first change is as follows:

// <smpl>
@disable sizeof_type_expr@
type T;
T **x;
@@

x =
<+...sizeof(
- T
+ *x
)...+>
// </smpl>

---
v2: Use kcalloc
v3: Add maintainer in To

 drivers/thunderbolt/nhi.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/thunderbolt/nhi.c +4 -6
index 2054fbf..c68fe12 100644
@@ -569,12 +569,10 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 			 nhi->hop_count);
 	INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
 
-	nhi->tx_rings = devm_kzalloc(&pdev->dev,
-				     nhi->hop_count * sizeof(struct tb_ring),
-				     GFP_KERNEL);
-	nhi->rx_rings = devm_kzalloc(&pdev->dev,
-				     nhi->hop_count * sizeof(struct tb_ring),
-				     GFP_KERNEL);
+	nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
+				     sizeof(*nhi->tx_rings), GFP_KERNEL);
+	nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
+				     sizeof(*nhi->rx_rings), GFP_KERNEL);
 	if (!nhi->tx_rings || !nhi->rx_rings)
 		return -ENOMEM;
 
Signature · 1 line
1.9.1
maintainer Acked-by on v3 1/1
#1 lore raw
Quoted 51 lines · Himangi Saraogi

nhi->rx_rings does not have type as struct tb_ring *, as it is a double pointer. So, the elements of the array should have pointer type, not structure type. The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The Coccinelle semantic patch that makes the first change is as follows:

// <smpl>
@disable sizeof_type_expr@
type T;
T **x;
@@

x =
<+...sizeof(
- T
+ *x
)...+>
// </smpl>

---
v2: Use kcalloc
v3: Add maintainer in To

 drivers/thunderbolt/nhi.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/thunderbolt/nhi.c +4 -7
index 2054fbf..c68fe12 100644
@@ -569,12 +569,10 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
                         nhi->hop_count);
        INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
 
-       nhi->tx_rings = devm_kzalloc(&pdev->dev,
-                                    nhi->hop_count * sizeof(struct tb_ring),
-                                    GFP_KERNEL);
-       nhi->rx_rings = devm_kzalloc(&pdev->dev,
-                                    nhi->hop_count * sizeof(struct tb_ring),
-                                    GFP_KERNEL);
+       nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
+                                    sizeof(*nhi->tx_rings), GFP_KERNEL);
+       nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
+                                    sizeof(*nhi->rx_rings), GFP_KERNEL);
        if (!nhi->tx_rings || !nhi->rx_rings)
                return -ENOMEM;
 
--
Quoted 2 lines

1.9.1

Thanks for catching this.

maintainer
#2 lore raw

Greg, can you add this to your char-misc tree (instead of the kzalloc patch, or do you need a rebased patch)?

Thanks,
Andreas

Quoted 57 lines · Andreas Noever
Quoted 51 lines · Himangi Saraogi

nhi->rx_rings does not have type as struct tb_ring *, as it is a double pointer. So, the elements of the array should have pointer type, not structure type. The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The Coccinelle semantic patch that makes the first change is as follows:

// <smpl>
@disable sizeof_type_expr@
type T;
T **x;
@@

x =
<+...sizeof(
- T
+ *x
)...+>
// </smpl>

---
v2: Use kcalloc
v3: Add maintainer in To

 drivers/thunderbolt/nhi.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/thunderbolt/nhi.c +4 -7
index 2054fbf..c68fe12 100644
@@ -569,12 +569,10 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
                         nhi->hop_count);
        INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
 
-       nhi->tx_rings = devm_kzalloc(&pdev->dev,
-                                    nhi->hop_count * sizeof(struct tb_ring),
-                                    GFP_KERNEL);
-       nhi->rx_rings = devm_kzalloc(&pdev->dev,
-                                    nhi->hop_count * sizeof(struct tb_ring),
-                                    GFP_KERNEL);
+       nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
+                                    sizeof(*nhi->tx_rings), GFP_KERNEL);
+       nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
+                                    sizeof(*nhi->rx_rings), GFP_KERNEL);
        if (!nhi->tx_rings || !nhi->rx_rings)
                return -ENOMEM;
 
--
Quoted 2 lines

1.9.1

Thanks for catching this.

#3 lore raw
Quoted 2 lines · Andreas Noever

Greg, can you add this to your char-misc tree (instead of the kzalloc patch, or do you need a rebased patch)?

I need a patch on top of my char-misc tree as the kzalloc version is already applied there.

thanks,

greg k-h