2023-06-08 02:37:12

by Su Hui

[permalink] [raw]
Subject: [PATCH v2] iommu/amd: Fix possible memory leak of 'domain'

Move allocation code down to avoid memory leak.

Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
Signed-off-by: Su Hui <[email protected]>
---
Changes in v2:
change code order is better and more consistent with other
drivers.
drivers/iommu/amd/iommu.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index dc1ec6849775..e8a2e5984acb 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2078,10 +2078,6 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
int mode = DEFAULT_PGTABLE_LEVEL;
int ret;

- domain = kzalloc(sizeof(*domain), GFP_KERNEL);
- if (!domain)
- return NULL;
-
/*
* Force IOMMU v1 page table when iommu=pt and
* when allocating domain for pass-through devices.
@@ -2097,6 +2093,10 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
return NULL;
}

+ domain = kzalloc(sizeof(*domain), GFP_KERNEL);
+ if (!domain)
+ return NULL;
+
switch (pgtable) {
case AMD_IOMMU_V1:
ret = protection_domain_init_v1(domain, mode);
--
2.30.2



2023-06-08 04:37:18

by Vasant Hegde

[permalink] [raw]
Subject: Re: [PATCH v2] iommu/amd: Fix possible memory leak of 'domain'

On 6/8/2023 7:49 AM, Su Hui wrote:
> Move allocation code down to avoid memory leak.
>
> Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
> Signed-off-by: Su Hui <[email protected]>

Reviewed-by: Vasant Hegde <[email protected]>


-Vasant


> ---
> Changes in v2:
> change code order is better and more consistent with other
> drivers.
> drivers/iommu/amd/iommu.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
> index dc1ec6849775..e8a2e5984acb 100644
> --- a/drivers/iommu/amd/iommu.c
> +++ b/drivers/iommu/amd/iommu.c
> @@ -2078,10 +2078,6 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
> int mode = DEFAULT_PGTABLE_LEVEL;
> int ret;
>
> - domain = kzalloc(sizeof(*domain), GFP_KERNEL);
> - if (!domain)
> - return NULL;
> -
> /*
> * Force IOMMU v1 page table when iommu=pt and
> * when allocating domain for pass-through devices.
> @@ -2097,6 +2093,10 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
> return NULL;
> }
>
> + domain = kzalloc(sizeof(*domain), GFP_KERNEL);
> + if (!domain)
> + return NULL;
> +
> switch (pgtable) {
> case AMD_IOMMU_V1:
> ret = protection_domain_init_v1(domain, mode);


2023-06-12 18:27:07

by Jerry Snitselaar

[permalink] [raw]
Subject: Re: [PATCH v2] iommu/amd: Fix possible memory leak of 'domain'

On Thu, Jun 08, 2023 at 10:19:34AM +0800, Su Hui wrote:
> Move allocation code down to avoid memory leak.
>
> Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
> Signed-off-by: Su Hui <[email protected]>

Reviewed-by: Jerry Snitselaar <[email protected]>

> ---
> Changes in v2:
> change code order is better and more consistent with other
> drivers.
> drivers/iommu/amd/iommu.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
> index dc1ec6849775..e8a2e5984acb 100644
> --- a/drivers/iommu/amd/iommu.c
> +++ b/drivers/iommu/amd/iommu.c
> @@ -2078,10 +2078,6 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
> int mode = DEFAULT_PGTABLE_LEVEL;
> int ret;
>
> - domain = kzalloc(sizeof(*domain), GFP_KERNEL);
> - if (!domain)
> - return NULL;
> -
> /*
> * Force IOMMU v1 page table when iommu=pt and
> * when allocating domain for pass-through devices.
> @@ -2097,6 +2093,10 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
> return NULL;
> }
>
> + domain = kzalloc(sizeof(*domain), GFP_KERNEL);
> + if (!domain)
> + return NULL;
> +
> switch (pgtable) {
> case AMD_IOMMU_V1:
> ret = protection_domain_init_v1(domain, mode);
> --
> 2.30.2
>


2023-06-12 18:36:06

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH v2] iommu/amd: Fix possible memory leak of 'domain'

On Thu, Jun 08, 2023 at 10:19:34AM +0800, Su Hui wrote:
> Move allocation code down to avoid memory leak.
>
> Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks")
> Signed-off-by: Su Hui <[email protected]>
> ---
> Changes in v2:
> change code order is better and more consistent with other
> drivers.
> drivers/iommu/amd/iommu.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)

Reviewed-by: Jason Gunthorpe <[email protected]>

Thanks,
Jason