Skip to contentSkip to conversation

powerpc/powernv/npu: Allocate enough memory in pnv_try_setup_npu_table_group()

PATCH Applied Dan Carpenter 6 participants 8 messages Jan 9, 2019 – Jan 14, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/powernv/npu: Allocate enough memory in pnv_try_setup_npu_table_group()

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
---
 arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/powernv/npu-dma.c +1 -1
index d7f742ed48ba..3f58c7dbd581 100644
@@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
 		}
 	} else {
 		/* Create a group for 1 GPU and attached NPUs for POWER8 */
-		pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
+		pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
 		table_group = &pe->npucomp->table_group;
 		table_group->ops = &pnv_npu_peers_ops;
 		iommu_register_group(table_group, hose->global_number,
Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · Dan Carpenter

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
---
 arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, I've applied this to my fixes-test tree.

Alexey can you send me an ack?

cheers

Quoted 15 lines
arch/powerpc/platforms/powernv/npu-dma.c +1 -1
index d7f742ed48ba..3f58c7dbd581 100644
@@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
 		}
 	} else {
 		/* Create a group for 1 GPU and attached NPUs for POWER8 */
-		pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
+		pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
 		table_group = &pe->npucomp->table_group;
 		table_group->ops = &pnv_npu_peers_ops;
 		iommu_register_group(table_group, hose->global_number,
Signature · 1 line
2.17.1
#2 lore raw
Quoted 10 lines · Michael Ellerman
Quoted 8 lines · Dan Carpenter

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
---
 arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 3 lines

Thanks, I've applied this to my fixes-test tree.

Alexey can you send me an ack?

Ouch.

Quoted 18 lines

cheers

Quoted 15 lines
arch/powerpc/platforms/powernv/npu-dma.c +1 -1
index d7f742ed48ba..3f58c7dbd581 100644
@@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
 		}
 	} else {
 		/* Create a group for 1 GPU and attached NPUs for POWER8 */
-		pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
+		pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
 		table_group = &pe->npucomp->table_group;
 		table_group->ops = &pnv_npu_peers_ops;
 		iommu_register_group(table_group, hose->global_number,
Signature · 1 line
2.17.1
Signature · 1 line
Alexey
Acked-by on v1 1/1
#3 lore raw
In reply to Dan Carpenter
Quoted 14 lines · Dan Carpenter

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
---
 arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/powernv/npu-dma.c +0 -0
index d7f742ed48ba..3f58c7dbd581 100644

arch/powerpc/platforms/powernv/npu-dma.c struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe) · line 567

Quoted 5 lines
arch/powerpc/platforms/powernv/npu-dma.c +1 -1
@@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
 		}
 	} else {
 		/* Create a group for 1 GPU and attached NPUs for POWER8 */
-		pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
+		pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);

To avoid these in the future, I wonder if instead of sizeof(pe->npucomp), we insist on sizeof structure

pe->npucomp = kzalloc(sizeof(struct npucomp), GFP_KERNEL);

#4 lore raw
Quoted 24 lines · Balbir Singh
Quoted 14 lines · Dan Carpenter

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
---
 arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/powernv/npu-dma.c +0 -0
index d7f742ed48ba..3f58c7dbd581 100644

arch/powerpc/platforms/powernv/npu-dma.c struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe) · line 567

Quoted 5 lines
arch/powerpc/platforms/powernv/npu-dma.c +1 -1
@@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
 		}
 	} else {
 		/* Create a group for 1 GPU and attached NPUs for POWER8 */
-		pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
+		pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);

To avoid these in the future, I wonder if instead of sizeof(pe->npucomp), we insist on sizeof structure

Quoted 2 lines

pe->npucomp = kzalloc(sizeof(struct npucomp), GFP_KERNEL);

The latest kernel fashion is sizeof(*ptr). It can go wrong either way. I don't have strong feelings about it. These sorts of bugs don't last long because they're caught in testing or with static analysis.

regards,
dan carpenter

#5 lore raw
Quoted 25 lines · Dan Carpenter
Quoted 24 lines · Balbir Singh
Quoted 14 lines · Dan Carpenter

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
---
 arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/powernv/npu-dma.c +0 -0
index d7f742ed48ba..3f58c7dbd581 100644

arch/powerpc/platforms/powernv/npu-dma.c struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe) · line 567

Quoted 5 lines
arch/powerpc/platforms/powernv/npu-dma.c +1 -1
@@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
 		}
 	} else {
 		/* Create a group for 1 GPU and attached NPUs for POWER8 */
-		pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
+		pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);

To avoid these in the future, I wonder if instead of sizeof(pe->npucomp), we insist on sizeof structure

Quoted 6 lines
Quoted 2 lines

pe->npucomp = kzalloc(sizeof(struct npucomp), GFP_KERNEL);

The latest kernel fashion is sizeof(*ptr). It can go wrong either way. I don't have strong feelings about it. These sorts of bugs don't last long because they're caught in testing or with static analysis.

And it is easy to see someone forgot the * in "sizeof *ptr", and with experience it will just automatically look wrong if it is forgotten; but it isn't obvious at all if the wrong struct is used, which cannot happen with the *ptr form, but happens frequently with the "sizeof(struct x)" form.

Segher

#6 lore raw
Quoted 33 lines · Segher Boessenkool
Quoted 25 lines · Dan Carpenter
Quoted 24 lines · Balbir Singh
Quoted 14 lines · Dan Carpenter

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
---
 arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/powernv/npu-dma.c +0 -0
index d7f742ed48ba..3f58c7dbd581 100644

arch/powerpc/platforms/powernv/npu-dma.c struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe) · line 567

Quoted 5 lines
arch/powerpc/platforms/powernv/npu-dma.c +1 -1
@@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
 		}
 	} else {
 		/* Create a group for 1 GPU and attached NPUs for POWER8 */
-		pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
+		pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);

To avoid these in the future, I wonder if instead of sizeof(pe->npucomp), we insist on sizeof structure

Quoted 6 lines
Quoted 2 lines

pe->npucomp = kzalloc(sizeof(struct npucomp), GFP_KERNEL);

The latest kernel fashion is sizeof(*ptr). It can go wrong either way. I don't have strong feelings about it. These sorts of bugs don't last long because they're caught in testing or with static analysis.

Quoted 5 lines

And it is easy to see someone forgot the * in "sizeof *ptr", and with experience it will just automatically look wrong if it is forgotten; but it isn't obvious at all if the wrong struct is used, which cannot happen with the *ptr form, but happens frequently with the "sizeof(struct x)" form.

It doesn't happen very frequently. I look for this with Smatch. The results I see are when the first few members of a struct are a header and the actual size can vary.

	hdr = alloc(sizeof(struct larger_struct));

regards,
dan carpenter

#7 lore raw
In reply to Dan Carpenter
Quoted 7 lines · Dan Carpenter

There is a typo so we accidentally allocate enough memory for a pointer when we wanted to allocate enough for a struct.

Fixes 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")

Applied to powerpc fixes, thanks.

https://git.kernel.org/powerpc/c/d7b6cc199b2dea602b4a2a681cf6d322

cheers