iommu/amd: Fix possible memory leak of 'domain'

Message ID 20230606070742.139575-1-suhui@nfschina.com
State New
Headers
Series iommu/amd: Fix possible memory leak of 'domain' |

Commit Message

Su Hui June 6, 2023, 7:07 a.m. UTC
  free memory of domain before return NULL.

Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
Signed-off-by: Su Hui <suhui@nfschina.com>
---
 drivers/iommu/amd/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Vasant Hegde June 7, 2023, 4:19 a.m. UTC | #1
On 6/6/2023 12:37 PM, Su Hui wrote:
> free memory of domain before return NULL.
> 
> Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
> Signed-off-by: Su Hui <suhui@nfschina.com>

Reviewed-by: Vasant Hegde <vasant.hegde@amd.com>


-Vasant


> ---
>  drivers/iommu/amd/iommu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
> index dc1ec6849775..f08e65629c74 100644
> --- a/drivers/iommu/amd/iommu.c
> +++ b/drivers/iommu/amd/iommu.c
> @@ -2094,7 +2094,7 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
>  	} else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
>  		pgtable = amd_iommu_pgtable;
>  	} else {
> -		return NULL;
> +		goto out_err;
>  	}
>  
>  	switch (pgtable) {
  
Jason Gunthorpe June 7, 2023, 2:22 p.m. UTC | #2
On Tue, Jun 06, 2023 at 03:07:42PM +0800, Su Hui wrote:
> free memory of domain before return NULL.
> 
> Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
> Signed-off-by: Su Hui <suhui@nfschina.com>
> ---
>  drivers/iommu/amd/iommu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
> index dc1ec6849775..f08e65629c74 100644
> --- a/drivers/iommu/amd/iommu.c
> +++ b/drivers/iommu/amd/iommu.c
> @@ -2094,7 +2094,7 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
>  	} else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
>  		pgtable = amd_iommu_pgtable;
>  	} else {
> -		return NULL;
> +		goto out_err;
>  	}

It might be nicer to order to allocation after the type checks, it is
more consistent with the other drivers

This is fine too

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Thanks,
Jason
  
Vasant Hegde June 7, 2023, 2:26 p.m. UTC | #3
Jason,


On 6/7/2023 7:52 PM, Jason Gunthorpe wrote:
> On Tue, Jun 06, 2023 at 03:07:42PM +0800, Su Hui wrote:
>> free memory of domain before return NULL.
>>
>> Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
>> Signed-off-by: Su Hui <suhui@nfschina.com>
>> ---
>>  drivers/iommu/amd/iommu.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
>> index dc1ec6849775..f08e65629c74 100644
>> --- a/drivers/iommu/amd/iommu.c
>> +++ b/drivers/iommu/amd/iommu.c
>> @@ -2094,7 +2094,7 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
>>  	} else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
>>  		pgtable = amd_iommu_pgtable;
>>  	} else {
>> -		return NULL;
>> +		goto out_err;
>>  	}
> 
> It might be nicer to order to allocation after the type checks, it is
> more consistent with the other drivers
> 
> This is fine too

Right. We are working on cleaning up/re-arranging this code path.
Hopefully I will have patches soon.


-Vasant
  

Patch

diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index dc1ec6849775..f08e65629c74 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2094,7 +2094,7 @@  static struct protection_domain *protection_domain_alloc(unsigned int type)
 	} else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
 		pgtable = amd_iommu_pgtable;
 	} else {
-		return NULL;
+		goto out_err;
 	}
 
 	switch (pgtable) {