Skip to contentSkip to conversation

iommu/mtk_iommu: Use devm_kcalloc() instead of devm_kzalloc()

PATCH Applied Erick Archer 5 participants 5 messages Feb 11, 2024 – Feb 16, 2024

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

PATCH 1/1 iommu/mtk_iommu: Use devm_kcalloc() instead of devm_kzalloc()

This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1].

Here the multiplication is obviously safe because MTK_PROTECT_PA_ALIGN is defined as a literal value of 256 or 128.

For the "mtk_iommu.c" file: 256
For the "mtk_iommu_v1.c" file: 128

However, using devm_kcalloc() is more appropriate [2] and improves readability. This patch has no effect on runtime behavior.

---
 drivers/iommu/mtk_iommu.c    | 2 +-
 drivers/iommu/mtk_iommu_v1.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/mtk_iommu.c +1 -1
index 7abe9e85a570..9aae6eb604b1 100644
@@ -1264,7 +1264,7 @@ static int mtk_iommu_probe(struct platform_device *pdev)
 	data->plat_data = of_device_get_match_data(dev);
 
 	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN, GFP_KERNEL);
 	if (!protect)
 		return -ENOMEM;
 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
drivers/iommu/mtk_iommu_v1.c +2 -2
index 25b41222abae..45cd845d153f 100644
@@ -621,8 +621,8 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
 	data->dev = dev;
 
 	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
-			GFP_KERNEL | GFP_DMA);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN,
+			       GFP_KERNEL | GFP_DMA);
 	if (!protect)
 		return -ENOMEM;
 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);

--
2.25.1

maintainer Reviewed-by on v1 1/1
#1 lore raw

Il 11/02/24 19:22, Erick Archer ha scritto:

Quoted 9 lines

This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1].

Here the multiplication is obviously safe because MTK_PROTECT_PA_ALIGN is defined as a literal value of 256 or 128.

For the "mtk_iommu.c" file: 256
For the "mtk_iommu_v1.c" file: 128

Quoted 6 lines

However, using devm_kcalloc() is more appropriate [2] and improves readability. This patch has no effect on runtime behavior.

Quoted 36 lines
---
  drivers/iommu/mtk_iommu.c    | 2 +-
  drivers/iommu/mtk_iommu_v1.c | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/mtk_iommu.c +1 -1
index 7abe9e85a570..9aae6eb604b1 100644
@@ -1264,7 +1264,7 @@ static int mtk_iommu_probe(struct platform_device *pdev)
  	data->plat_data = of_device_get_match_data(dev);
 
  	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN, GFP_KERNEL);
  	if (!protect)
  		return -ENOMEM;
  	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
drivers/iommu/mtk_iommu_v1.c +2 -3
index 25b41222abae..45cd845d153f 100644
@@ -621,8 +621,8 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
  	data->dev = dev;
 
  	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
-			GFP_KERNEL | GFP_DMA);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN,
+			       GFP_KERNEL | GFP_DMA);
  	if (!protect)
  		return -ENOMEM;
  	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
--

2.25.1

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Erick Archer
Quoted 9 lines · Erick Archer

This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1].

Here the multiplication is obviously safe because MTK_PROTECT_PA_ALIGN is defined as a literal value of 256 or 128.

For the "mtk_iommu.c" file: 256
For the "mtk_iommu_v1.c" file: 128

Quoted 6 lines

However, using devm_kcalloc() is more appropriate [2] and improves readability. This patch has no effect on runtime behavior.

Quoted 36 lines
---
  drivers/iommu/mtk_iommu.c    | 2 +-
  drivers/iommu/mtk_iommu_v1.c | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/mtk_iommu.c +1 -1
index 7abe9e85a570..9aae6eb604b1 100644
@@ -1264,7 +1264,7 @@ static int mtk_iommu_probe(struct platform_device *pdev)
  	data->plat_data = of_device_get_match_data(dev);
 
  	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN, GFP_KERNEL);
  	if (!protect)
  		return -ENOMEM;
  	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
drivers/iommu/mtk_iommu_v1.c +2 -3
index 25b41222abae..45cd845d153f 100644
@@ -621,8 +621,8 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
  	data->dev = dev;
 
  	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
-			GFP_KERNEL | GFP_DMA);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN,
+			       GFP_KERNEL | GFP_DMA);
  	if (!protect)
  		return -ENOMEM;
  	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
--

2.25.1

Reviewed-by on v1 1/1
#3 lore raw
In reply to Erick Archer
Quoted 9 lines · Erick Archer

This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1].

Here the multiplication is obviously safe because MTK_PROTECT_PA_ALIGN is defined as a literal value of 256 or 128.

For the "mtk_iommu.c" file: 256
For the "mtk_iommu_v1.c" file: 128

Quoted 6 lines

However, using devm_kcalloc() is more appropriate [2] and improves readability. This patch has no effect on runtime behavior.

LGTM:

Thanks!

Signature · 1 line
Gustavo
Quoted 37 lines
---
  drivers/iommu/mtk_iommu.c    | 2 +-
  drivers/iommu/mtk_iommu_v1.c | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/mtk_iommu.c +1 -1
index 7abe9e85a570..9aae6eb604b1 100644
@@ -1264,7 +1264,7 @@ static int mtk_iommu_probe(struct platform_device *pdev)
  	data->plat_data = of_device_get_match_data(dev);
 
  	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN, GFP_KERNEL);
  	if (!protect)
  		return -ENOMEM;
  	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
drivers/iommu/mtk_iommu_v1.c +2 -3
index 25b41222abae..45cd845d153f 100644
@@ -621,8 +621,8 @@ static int mtk_iommu_v1_probe(struct platform_device *pdev)
  	data->dev = dev;
 
  	/* Protect memory. HW will access here while translation fault.*/
-	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
-			GFP_KERNEL | GFP_DMA);
+	protect = devm_kcalloc(dev, 2, MTK_PROTECT_PA_ALIGN,
+			       GFP_KERNEL | GFP_DMA);
  	if (!protect)
  		return -ENOMEM;
  	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
--

2.25.1