All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] fix the memory leak of iopf
@ 2022-10-21  3:51 ` Longfang Liu
  0 siblings, 0 replies; 16+ messages in thread
From: Longfang Liu @ 2022-10-21  3:51 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu, liulongfang

fix the memory leak of iopf in arm's smmu driver, and complete
the resource recovery operation when the smmu driver probe
fails.

Longfang Liu (2):
  iommu: fix memory leak of iopf
  iommu: fix smmu initialization memory leak problem

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

-- 
2.24.0


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

* [PATCH 0/2] fix the memory leak of iopf
@ 2022-10-21  3:51 ` Longfang Liu
  0 siblings, 0 replies; 16+ messages in thread
From: Longfang Liu @ 2022-10-21  3:51 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu, liulongfang

fix the memory leak of iopf in arm's smmu driver, and complete
the resource recovery operation when the smmu driver probe
fails.

Longfang Liu (2):
  iommu: fix memory leak of iopf
  iommu: fix smmu initialization memory leak problem

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

-- 
2.24.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* [PATCH 1/2] iommu: fix memory leak of iopf
  2022-10-21  3:51 ` Longfang Liu
@ 2022-10-21  3:51   ` Longfang Liu
  -1 siblings, 0 replies; 16+ messages in thread
From: Longfang Liu @ 2022-10-21  3:51 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu, liulongfang

In arm_smmu_init_queues(), if ARM_SMMU_FEAT_SVA and ARM_SMMU_FEAT_STALLS
are enabled, iopf will be allocated a queue memory, and after
arm_smmu_init_one_queue() processing fails, this part of iopf
memory is not released, and there is a memory leak problem.

Signed-off-by: Longfang Liu <liulongfang@huawei.com>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

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 b788a38d8fdf..a1db07bed6a9 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2939,9 +2939,13 @@ static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
 	if (!(smmu->features & ARM_SMMU_FEAT_PRI))
 		return 0;
 
-	return arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
+	ret = arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
 				       ARM_SMMU_PRIQ_PROD, ARM_SMMU_PRIQ_CONS,
 				       PRIQ_ENT_DWORDS, "priq");
+	if (ret)
+		iopf_queue_free(smmu->evtq.iopf);
+
+	return ret;
 }
 
 static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
-- 
2.24.0


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

* [PATCH 1/2] iommu: fix memory leak of iopf
@ 2022-10-21  3:51   ` Longfang Liu
  0 siblings, 0 replies; 16+ messages in thread
From: Longfang Liu @ 2022-10-21  3:51 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu, liulongfang

In arm_smmu_init_queues(), if ARM_SMMU_FEAT_SVA and ARM_SMMU_FEAT_STALLS
are enabled, iopf will be allocated a queue memory, and after
arm_smmu_init_one_queue() processing fails, this part of iopf
memory is not released, and there is a memory leak problem.

Signed-off-by: Longfang Liu <liulongfang@huawei.com>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

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 b788a38d8fdf..a1db07bed6a9 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2939,9 +2939,13 @@ static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
 	if (!(smmu->features & ARM_SMMU_FEAT_PRI))
 		return 0;
 
-	return arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
+	ret = arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
 				       ARM_SMMU_PRIQ_PROD, ARM_SMMU_PRIQ_CONS,
 				       PRIQ_ENT_DWORDS, "priq");
+	if (ret)
+		iopf_queue_free(smmu->evtq.iopf);
+
+	return ret;
 }
 
 static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
-- 
2.24.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* [PATCH 2/2] iommu: fix smmu initialization memory leak problem
  2022-10-21  3:51 ` Longfang Liu
@ 2022-10-21  3:51   ` Longfang Liu
  -1 siblings, 0 replies; 16+ messages in thread
From: Longfang Liu @ 2022-10-21  3:51 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu, liulongfang

When iommu_device_register() in arm_smmu_device_probe() fails,
in addition to sysfs needs to be deleted, device should also
be disabled, and the memory of iopf needs to be released to
prevent memory leak of iopf.

Signed-off-by: Longfang Liu <liulongfang@huawei.com>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
 1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
 	if (ret) {
 		dev_err(dev, "Failed to register iommu\n");
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ret;
+		goto err_sysfs_remove;
 	}
 
 	return 0;
+
+err_sysfs_remove:
+	iommu_device_sysfs_remove(&smmu->iommu);
+	arm_smmu_device_disable(smmu);
+	iopf_queue_free(smmu->evtq.iopf);
+	return ret;
 }
 
 static int arm_smmu_device_remove(struct platform_device *pdev)
-- 
2.24.0


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

* [PATCH 2/2] iommu: fix smmu initialization memory leak problem
@ 2022-10-21  3:51   ` Longfang Liu
  0 siblings, 0 replies; 16+ messages in thread
From: Longfang Liu @ 2022-10-21  3:51 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu, liulongfang

When iommu_device_register() in arm_smmu_device_probe() fails,
in addition to sysfs needs to be deleted, device should also
be disabled, and the memory of iopf needs to be released to
prevent memory leak of iopf.

Signed-off-by: Longfang Liu <liulongfang@huawei.com>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
 1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
 	if (ret) {
 		dev_err(dev, "Failed to register iommu\n");
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ret;
+		goto err_sysfs_remove;
 	}
 
 	return 0;
+
+err_sysfs_remove:
+	iommu_device_sysfs_remove(&smmu->iommu);
+	arm_smmu_device_disable(smmu);
+	iopf_queue_free(smmu->evtq.iopf);
+	return ret;
 }
 
 static int arm_smmu_device_remove(struct platform_device *pdev)
-- 
2.24.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH 1/2] iommu: fix memory leak of iopf
  2022-10-21  3:51   ` Longfang Liu
@ 2022-11-02  1:36     ` liulongfang
  -1 siblings, 0 replies; 16+ messages in thread
From: liulongfang @ 2022-11-02  1:36 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu

kindly ping!
On 2022/10/21 11:51, Longfang Liu wrote:
> In arm_smmu_init_queues(), if ARM_SMMU_FEAT_SVA and ARM_SMMU_FEAT_STALLS
> are enabled, iopf will be allocated a queue memory, and after
> arm_smmu_init_one_queue() processing fails, this part of iopf
> memory is not released, and there is a memory leak problem.
> 
> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
> ---
>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> 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 b788a38d8fdf..a1db07bed6a9 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -2939,9 +2939,13 @@ static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
>  	if (!(smmu->features & ARM_SMMU_FEAT_PRI))
>  		return 0;
>  
> -	return arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
> +	ret = arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
>  				       ARM_SMMU_PRIQ_PROD, ARM_SMMU_PRIQ_CONS,
>  				       PRIQ_ENT_DWORDS, "priq");
> +	if (ret)
> +		iopf_queue_free(smmu->evtq.iopf);
> +
> +	return ret;
>  }
>  
>  static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
> 

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

* Re: [PATCH 1/2] iommu: fix memory leak of iopf
@ 2022-11-02  1:36     ` liulongfang
  0 siblings, 0 replies; 16+ messages in thread
From: liulongfang @ 2022-11-02  1:36 UTC (permalink / raw)
  To: will, robin.murphy; +Cc: linux-arm-kernel, iommu

kindly ping!
On 2022/10/21 11:51, Longfang Liu wrote:
> In arm_smmu_init_queues(), if ARM_SMMU_FEAT_SVA and ARM_SMMU_FEAT_STALLS
> are enabled, iopf will be allocated a queue memory, and after
> arm_smmu_init_one_queue() processing fails, this part of iopf
> memory is not released, and there is a memory leak problem.
> 
> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
> ---
>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> 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 b788a38d8fdf..a1db07bed6a9 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -2939,9 +2939,13 @@ static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
>  	if (!(smmu->features & ARM_SMMU_FEAT_PRI))
>  		return 0;
>  
> -	return arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
> +	ret = arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
>  				       ARM_SMMU_PRIQ_PROD, ARM_SMMU_PRIQ_CONS,
>  				       PRIQ_ENT_DWORDS, "priq");
> +	if (ret)
> +		iopf_queue_free(smmu->evtq.iopf);
> +
> +	return ret;
>  }
>  
>  static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
  2022-10-21  3:51   ` Longfang Liu
@ 2022-11-14 18:08     ` Will Deacon
  -1 siblings, 0 replies; 16+ messages in thread
From: Will Deacon @ 2022-11-14 18:08 UTC (permalink / raw)
  To: Longfang Liu; +Cc: robin.murphy, linux-arm-kernel, iommu

On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
> When iommu_device_register() in arm_smmu_device_probe() fails,
> in addition to sysfs needs to be deleted, device should also
> be disabled, and the memory of iopf needs to be released to
> prevent memory leak of iopf.
> 
> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
> ---
>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>  1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>  	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>  	if (ret) {
>  		dev_err(dev, "Failed to register iommu\n");
> -		iommu_device_sysfs_remove(&smmu->iommu);
> -		return ret;
> +		goto err_sysfs_remove;
>  	}
>  
>  	return 0;
> +
> +err_sysfs_remove:
> +	iommu_device_sysfs_remove(&smmu->iommu);
> +	arm_smmu_device_disable(smmu);
> +	iopf_queue_free(smmu->evtq.iopf);
> +	return ret;

Doesn't this miss the cases where iommu_device_sysfs_add() or
arm_smmu_device_reset() fail?

We'd probably be better off using something like devres_alloc() to track
the iopf queue here.

Will

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
@ 2022-11-14 18:08     ` Will Deacon
  0 siblings, 0 replies; 16+ messages in thread
From: Will Deacon @ 2022-11-14 18:08 UTC (permalink / raw)
  To: Longfang Liu; +Cc: robin.murphy, linux-arm-kernel, iommu

On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
> When iommu_device_register() in arm_smmu_device_probe() fails,
> in addition to sysfs needs to be deleted, device should also
> be disabled, and the memory of iopf needs to be released to
> prevent memory leak of iopf.
> 
> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
> ---
>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>  1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>  	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>  	if (ret) {
>  		dev_err(dev, "Failed to register iommu\n");
> -		iommu_device_sysfs_remove(&smmu->iommu);
> -		return ret;
> +		goto err_sysfs_remove;
>  	}
>  
>  	return 0;
> +
> +err_sysfs_remove:
> +	iommu_device_sysfs_remove(&smmu->iommu);
> +	arm_smmu_device_disable(smmu);
> +	iopf_queue_free(smmu->evtq.iopf);
> +	return ret;

Doesn't this miss the cases where iommu_device_sysfs_add() or
arm_smmu_device_reset() fail?

We'd probably be better off using something like devres_alloc() to track
the iopf queue here.

Will

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
  2022-11-14 18:08     ` Will Deacon
@ 2022-11-18  9:28       ` liulongfang
  -1 siblings, 0 replies; 16+ messages in thread
From: liulongfang @ 2022-11-18  9:28 UTC (permalink / raw)
  To: Will Deacon; +Cc: robin.murphy, linux-arm-kernel, iommu

On 2022/11/15 2:08, Will Deacon Wrote:
> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
>> When iommu_device_register() in arm_smmu_device_probe() fails,
>> in addition to sysfs needs to be deleted, device should also
>> be disabled, and the memory of iopf needs to be released to
>> prevent memory leak of iopf.
>>
>> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
>> ---
>>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>>  1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>>  	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>>  	if (ret) {
>>  		dev_err(dev, "Failed to register iommu\n");
>> -		iommu_device_sysfs_remove(&smmu->iommu);
>> -		return ret;
>> +		goto err_sysfs_remove;
>>  	}
>>  
>>  	return 0;
>> +
>> +err_sysfs_remove:
>> +	iommu_device_sysfs_remove(&smmu->iommu);
>> +	arm_smmu_device_disable(smmu);
>> +	iopf_queue_free(smmu->evtq.iopf);
>> +	return ret;
> 
> Doesn't this miss the cases where iommu_device_sysfs_add() or
> arm_smmu_device_reset() fail?
> 
> We'd probably be better off using something like devres_alloc() to track
> the iopf queue here.
> 
This is actually not a problem found by the test, but a problem found
by the code logic analysis. When an error exits, the memory allocated
by the iopf queue is not released during the entire exit process.


In addition, it can also be seen from arm_smmu_device_remove()
that the missing operation when the probe error exits.

Thanks
Longfang.
> Will
> .
> 

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
@ 2022-11-18  9:28       ` liulongfang
  0 siblings, 0 replies; 16+ messages in thread
From: liulongfang @ 2022-11-18  9:28 UTC (permalink / raw)
  To: Will Deacon; +Cc: robin.murphy, linux-arm-kernel, iommu

On 2022/11/15 2:08, Will Deacon Wrote:
> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
>> When iommu_device_register() in arm_smmu_device_probe() fails,
>> in addition to sysfs needs to be deleted, device should also
>> be disabled, and the memory of iopf needs to be released to
>> prevent memory leak of iopf.
>>
>> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
>> ---
>>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>>  1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>>  	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>>  	if (ret) {
>>  		dev_err(dev, "Failed to register iommu\n");
>> -		iommu_device_sysfs_remove(&smmu->iommu);
>> -		return ret;
>> +		goto err_sysfs_remove;
>>  	}
>>  
>>  	return 0;
>> +
>> +err_sysfs_remove:
>> +	iommu_device_sysfs_remove(&smmu->iommu);
>> +	arm_smmu_device_disable(smmu);
>> +	iopf_queue_free(smmu->evtq.iopf);
>> +	return ret;
> 
> Doesn't this miss the cases where iommu_device_sysfs_add() or
> arm_smmu_device_reset() fail?
> 
> We'd probably be better off using something like devres_alloc() to track
> the iopf queue here.
> 
This is actually not a problem found by the test, but a problem found
by the code logic analysis. When an error exits, the memory allocated
by the iopf queue is not released during the entire exit process.


In addition, it can also be seen from arm_smmu_device_remove()
that the missing operation when the probe error exits.

Thanks
Longfang.
> Will
> .
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
  2022-11-18  9:28       ` liulongfang
@ 2022-11-18 11:54         ` Robin Murphy
  -1 siblings, 0 replies; 16+ messages in thread
From: Robin Murphy @ 2022-11-18 11:54 UTC (permalink / raw)
  To: liulongfang, Will Deacon; +Cc: linux-arm-kernel, iommu

On 2022-11-18 09:28, liulongfang wrote:
> On 2022/11/15 2:08, Will Deacon Wrote:
>> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
>>> When iommu_device_register() in arm_smmu_device_probe() fails,
>>> in addition to sysfs needs to be deleted, device should also
>>> be disabled, and the memory of iopf needs to be released to
>>> prevent memory leak of iopf.
>>>
>>> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
>>> ---
>>>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>>>   1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
>>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>>>   	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>>>   	if (ret) {
>>>   		dev_err(dev, "Failed to register iommu\n");
>>> -		iommu_device_sysfs_remove(&smmu->iommu);
>>> -		return ret;
>>> +		goto err_sysfs_remove;
>>>   	}
>>>   
>>>   	return 0;
>>> +
>>> +err_sysfs_remove:
>>> +	iommu_device_sysfs_remove(&smmu->iommu);
>>> +	arm_smmu_device_disable(smmu);
>>> +	iopf_queue_free(smmu->evtq.iopf);
>>> +	return ret;
>>
>> Doesn't this miss the cases where iommu_device_sysfs_add() or
>> arm_smmu_device_reset() fail?
>>
>> We'd probably be better off using something like devres_alloc() to track
>> the iopf queue here.
>>
> This is actually not a problem found by the test, but a problem found
> by the code logic analysis. When an error exits, the memory allocated
> by the iopf queue is not released during the entire exit process.

Sure, but the point is that there are at least 5 points of failure after 
iopf_queue_alloc() succeeds, which could result in an early exit from 
probe. This patch only affects the last one of those, so the theoretical 
problem it claims to fix still exists just as much as before.

Robin.

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
@ 2022-11-18 11:54         ` Robin Murphy
  0 siblings, 0 replies; 16+ messages in thread
From: Robin Murphy @ 2022-11-18 11:54 UTC (permalink / raw)
  To: liulongfang, Will Deacon; +Cc: linux-arm-kernel, iommu

On 2022-11-18 09:28, liulongfang wrote:
> On 2022/11/15 2:08, Will Deacon Wrote:
>> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
>>> When iommu_device_register() in arm_smmu_device_probe() fails,
>>> in addition to sysfs needs to be deleted, device should also
>>> be disabled, and the memory of iopf needs to be released to
>>> prevent memory leak of iopf.
>>>
>>> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
>>> ---
>>>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>>>   1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
>>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>>>   	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>>>   	if (ret) {
>>>   		dev_err(dev, "Failed to register iommu\n");
>>> -		iommu_device_sysfs_remove(&smmu->iommu);
>>> -		return ret;
>>> +		goto err_sysfs_remove;
>>>   	}
>>>   
>>>   	return 0;
>>> +
>>> +err_sysfs_remove:
>>> +	iommu_device_sysfs_remove(&smmu->iommu);
>>> +	arm_smmu_device_disable(smmu);
>>> +	iopf_queue_free(smmu->evtq.iopf);
>>> +	return ret;
>>
>> Doesn't this miss the cases where iommu_device_sysfs_add() or
>> arm_smmu_device_reset() fail?
>>
>> We'd probably be better off using something like devres_alloc() to track
>> the iopf queue here.
>>
> This is actually not a problem found by the test, but a problem found
> by the code logic analysis. When an error exits, the memory allocated
> by the iopf queue is not released during the entire exit process.

Sure, but the point is that there are at least 5 points of failure after 
iopf_queue_alloc() succeeds, which could result in an early exit from 
probe. This patch only affects the last one of those, so the theoretical 
problem it claims to fix still exists just as much as before.

Robin.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
  2022-11-18 11:54         ` Robin Murphy
@ 2022-11-21  2:09           ` liulongfang
  -1 siblings, 0 replies; 16+ messages in thread
From: liulongfang @ 2022-11-21  2:09 UTC (permalink / raw)
  To: Robin Murphy, Will Deacon; +Cc: linux-arm-kernel, iommu

On 2022/11/18 19:54, Robin Murphy wrote:
> On 2022-11-18 09:28, liulongfang wrote:
>> On 2022/11/15 2:08, Will Deacon Wrote:
>>> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
>>>> When iommu_device_register() in arm_smmu_device_probe() fails,
>>>> in addition to sysfs needs to be deleted, device should also
>>>> be disabled, and the memory of iopf needs to be released to
>>>> prevent memory leak of iopf.
>>>>
>>>> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
>>>> ---
>>>>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>>>>   1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
>>>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>>>>       ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>>>>       if (ret) {
>>>>           dev_err(dev, "Failed to register iommu\n");
>>>> -        iommu_device_sysfs_remove(&smmu->iommu);
>>>> -        return ret;
>>>> +        goto err_sysfs_remove;
>>>>       }
>>>>         return 0;
>>>> +
>>>> +err_sysfs_remove:
>>>> +    iommu_device_sysfs_remove(&smmu->iommu);
>>>> +    arm_smmu_device_disable(smmu);
>>>> +    iopf_queue_free(smmu->evtq.iopf);
>>>> +    return ret;
>>>
>>> Doesn't this miss the cases where iommu_device_sysfs_add() or
>>> arm_smmu_device_reset() fail?
>>>
>>> We'd probably be better off using something like devres_alloc() to track
>>> the iopf queue here.
>>>
>> This is actually not a problem found by the test, but a problem found
>> by the code logic analysis. When an error exits, the memory allocated
>> by the iopf queue is not released during the entire exit process.
> 
> Sure, but the point is that there are at least 5 points of failure after iopf_queue_alloc() succeeds, 

OK! Let me change this patch and modify the abnormal exit part related to iopf_queue_alloc() in probe()

Thanks,
Longfang.

which could result in an early exit from probe. This patch only affects the last one of those, so the theoretical problem it claims to fix still exists just as much as before.
> 
> Robin.
> 
> .
> 

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

* Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem
@ 2022-11-21  2:09           ` liulongfang
  0 siblings, 0 replies; 16+ messages in thread
From: liulongfang @ 2022-11-21  2:09 UTC (permalink / raw)
  To: Robin Murphy, Will Deacon; +Cc: linux-arm-kernel, iommu

On 2022/11/18 19:54, Robin Murphy wrote:
> On 2022-11-18 09:28, liulongfang wrote:
>> On 2022/11/15 2:08, Will Deacon Wrote:
>>> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote:
>>>> When iommu_device_register() in arm_smmu_device_probe() fails,
>>>> in addition to sysfs needs to be deleted, device should also
>>>> be disabled, and the memory of iopf needs to be released to
>>>> prevent memory leak of iopf.
>>>>
>>>> Signed-off-by: Longfang Liu <liulongfang@huawei.com>
>>>> ---
>>>>   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++--
>>>>   1 file changed, 7 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 a1db07bed6a9..c70defb0c866 100644
>>>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
>>>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
>>>>       ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
>>>>       if (ret) {
>>>>           dev_err(dev, "Failed to register iommu\n");
>>>> -        iommu_device_sysfs_remove(&smmu->iommu);
>>>> -        return ret;
>>>> +        goto err_sysfs_remove;
>>>>       }
>>>>         return 0;
>>>> +
>>>> +err_sysfs_remove:
>>>> +    iommu_device_sysfs_remove(&smmu->iommu);
>>>> +    arm_smmu_device_disable(smmu);
>>>> +    iopf_queue_free(smmu->evtq.iopf);
>>>> +    return ret;
>>>
>>> Doesn't this miss the cases where iommu_device_sysfs_add() or
>>> arm_smmu_device_reset() fail?
>>>
>>> We'd probably be better off using something like devres_alloc() to track
>>> the iopf queue here.
>>>
>> This is actually not a problem found by the test, but a problem found
>> by the code logic analysis. When an error exits, the memory allocated
>> by the iopf queue is not released during the entire exit process.
> 
> Sure, but the point is that there are at least 5 points of failure after iopf_queue_alloc() succeeds, 

OK! Let me change this patch and modify the abnormal exit part related to iopf_queue_alloc() in probe()

Thanks,
Longfang.

which could result in an early exit from probe. This patch only affects the last one of those, so the theoretical problem it claims to fix still exists just as much as before.
> 
> Robin.
> 
> .
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

end of thread, other threads:[~2022-11-21  2:10 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-21  3:51 [PATCH 0/2] fix the memory leak of iopf Longfang Liu
2022-10-21  3:51 ` Longfang Liu
2022-10-21  3:51 ` [PATCH 1/2] iommu: fix " Longfang Liu
2022-10-21  3:51   ` Longfang Liu
2022-11-02  1:36   ` liulongfang
2022-11-02  1:36     ` liulongfang
2022-10-21  3:51 ` [PATCH 2/2] iommu: fix smmu initialization memory leak problem Longfang Liu
2022-10-21  3:51   ` Longfang Liu
2022-11-14 18:08   ` Will Deacon
2022-11-14 18:08     ` Will Deacon
2022-11-18  9:28     ` liulongfang
2022-11-18  9:28       ` liulongfang
2022-11-18 11:54       ` Robin Murphy
2022-11-18 11:54         ` Robin Murphy
2022-11-21  2:09         ` liulongfang
2022-11-21  2:09           ` liulongfang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.