linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in memory allocation
@ 2022-02-05 17:10 Christophe JAILLET
  2022-02-05 17:11 ` [PATCH 2/2] iommu/arm-smmu-v3: Simplify " Christophe JAILLET
  2022-02-07 10:26 ` [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in " Robin Murphy
  0 siblings, 2 replies; 4+ messages in thread
From: Christophe JAILLET @ 2022-02-05 17:10 UTC (permalink / raw)
  To: Will Deacon, Robin Murphy, Joerg Roedel
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET,
	linux-arm-kernel, iommu

kmalloc_array()/kcalloc() should be used to avoid potential overflow when
a multiplication is needed to compute the size of the requested memory.

So turn a devm_kzalloc()+explicit size computation into an equivalent
devm_kcalloc().

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This is NOT compile tested.
I don't have the needed cross compiling tools.
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index 6dc6d8b6b368..14d06aad0726 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2981,10 +2981,10 @@ static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
 {
 	unsigned int i;
 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
-	size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
 	void *strtab = smmu->strtab_cfg.strtab;
 
-	cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
+	cfg->l1_desc = devm_kcalloc(smmu->dev, cfg->num_l1_ents,
+				    sizeof(*cfg->l1_desc), GFP_KERNEL);
 	if (!cfg->l1_desc)
 		return -ENOMEM;
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] iommu/arm-smmu-v3: Simplify memory allocation
  2022-02-05 17:10 [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in memory allocation Christophe JAILLET
@ 2022-02-05 17:11 ` Christophe JAILLET
  2022-02-07 10:56   ` Robin Murphy
  2022-02-07 10:26 ` [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in " Robin Murphy
  1 sibling, 1 reply; 4+ messages in thread
From: Christophe JAILLET @ 2022-02-05 17:11 UTC (permalink / raw)
  To: Will Deacon, Robin Murphy, Joerg Roedel
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET,
	linux-arm-kernel, iommu

Use devm_bitmap_zalloc() instead of hand writing it.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This is NOT compile tested.
I don't have the needed cross compiling tools.
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index 14d06aad0726..ba0e7f1f7dbf 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2911,12 +2911,6 @@ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
 	return 0;
 }
 
-static void arm_smmu_cmdq_free_bitmap(void *data)
-{
-	unsigned long *bitmap = data;
-	bitmap_free(bitmap);
-}
-
 static int arm_smmu_cmdq_init(struct arm_smmu_device *smmu)
 {
 	int ret = 0;
@@ -2927,13 +2921,13 @@ static int arm_smmu_cmdq_init(struct arm_smmu_device *smmu)
 	atomic_set(&cmdq->owner_prod, 0);
 	atomic_set(&cmdq->lock, 0);
 
-	bitmap = (atomic_long_t *)bitmap_zalloc(nents, GFP_KERNEL);
+	bitmap = (atomic_long_t *)devm_bitmap_zalloc(smmu->dev, nents,
+						     GFP_KERNEL);
 	if (!bitmap) {
 		dev_err(smmu->dev, "failed to allocate cmdq bitmap\n");
 		ret = -ENOMEM;
 	} else {
 		cmdq->valid_map = bitmap;
-		devm_add_action(smmu->dev, arm_smmu_cmdq_free_bitmap, bitmap);
 	}
 
 	return ret;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in memory allocation
  2022-02-05 17:10 [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in memory allocation Christophe JAILLET
  2022-02-05 17:11 ` [PATCH 2/2] iommu/arm-smmu-v3: Simplify " Christophe JAILLET
@ 2022-02-07 10:26 ` Robin Murphy
  1 sibling, 0 replies; 4+ messages in thread
From: Robin Murphy @ 2022-02-07 10:26 UTC (permalink / raw)
  To: Christophe JAILLET, Will Deacon, Joerg Roedel
  Cc: linux-kernel, kernel-janitors, linux-arm-kernel, iommu

On 2022-02-05 17:10, Christophe JAILLET wrote:
> kmalloc_array()/kcalloc() should be used to avoid potential overflow when
> a multiplication is needed to compute the size of the requested memory.
> 
> So turn a devm_kzalloc()+explicit size computation into an equivalent
> devm_kcalloc().
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> This is NOT compile tested.
> I don't have the needed cross compiling tools.

FYI, https://cdn.kernel.org/pub/tools/crosstool/

Either way, the patch looks reasonable, thanks!

Acked-by: Robin Murphy <robin.murphy@arm.com>

> ---
>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> index 6dc6d8b6b368..14d06aad0726 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -2981,10 +2981,10 @@ static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
>   {
>   	unsigned int i;
>   	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
> -	size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
>   	void *strtab = smmu->strtab_cfg.strtab;
>   
> -	cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
> +	cfg->l1_desc = devm_kcalloc(smmu->dev, cfg->num_l1_ents,
> +				    sizeof(*cfg->l1_desc), GFP_KERNEL);
>   	if (!cfg->l1_desc)
>   		return -ENOMEM;
>   

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] iommu/arm-smmu-v3: Simplify memory allocation
  2022-02-05 17:11 ` [PATCH 2/2] iommu/arm-smmu-v3: Simplify " Christophe JAILLET
@ 2022-02-07 10:56   ` Robin Murphy
  0 siblings, 0 replies; 4+ messages in thread
From: Robin Murphy @ 2022-02-07 10:56 UTC (permalink / raw)
  To: Christophe JAILLET, Will Deacon, Joerg Roedel
  Cc: linux-kernel, kernel-janitors, linux-arm-kernel, iommu

On 2022-02-05 17:11, Christophe JAILLET wrote:
> Use devm_bitmap_zalloc() instead of hand writing it.

Heh, that reminds me that I have more or less the same patch sat locally 
somewhere, except IIRC I took it further and removed the unhelpful error 
message and pruned the local variables as well - I think that would 
still be my preference here (or I could dig out my patch and post it if 
you like).

Cheers,
Robin.

> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> This is NOT compile tested.
> I don't have the needed cross compiling tools.
> ---
>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 10 ++--------
>   1 file changed, 2 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> index 14d06aad0726..ba0e7f1f7dbf 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -2911,12 +2911,6 @@ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
>   	return 0;
>   }
>   
> -static void arm_smmu_cmdq_free_bitmap(void *data)
> -{
> -	unsigned long *bitmap = data;
> -	bitmap_free(bitmap);
> -}
> -
>   static int arm_smmu_cmdq_init(struct arm_smmu_device *smmu)
>   {
>   	int ret = 0;
> @@ -2927,13 +2921,13 @@ static int arm_smmu_cmdq_init(struct arm_smmu_device *smmu)
>   	atomic_set(&cmdq->owner_prod, 0);
>   	atomic_set(&cmdq->lock, 0);
>   
> -	bitmap = (atomic_long_t *)bitmap_zalloc(nents, GFP_KERNEL);
> +	bitmap = (atomic_long_t *)devm_bitmap_zalloc(smmu->dev, nents,
> +						     GFP_KERNEL);
>   	if (!bitmap) {
>   		dev_err(smmu->dev, "failed to allocate cmdq bitmap\n");
>   		ret = -ENOMEM;
>   	} else {
>   		cmdq->valid_map = bitmap;
> -		devm_add_action(smmu->dev, arm_smmu_cmdq_free_bitmap, bitmap);
>   	}
>   
>   	return ret;

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-07 11:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-05 17:10 [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in memory allocation Christophe JAILLET
2022-02-05 17:11 ` [PATCH 2/2] iommu/arm-smmu-v3: Simplify " Christophe JAILLET
2022-02-07 10:56   ` Robin Murphy
2022-02-07 10:26 ` [PATCH 1/2] iommu/arm-smmu-v3: Avoid open coded arithmetic in " Robin Murphy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).