Skip to contentSkip to conversation

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

PATCH In review Su Hui 4 participants 4 messages Jun 8, 2023 – Jun 12, 2023

Subsystems (2)
lore
Share & tools
lore

Su Hui

author
#0 lore raw

PATCH v2 1/1 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")

---
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(-)
drivers/iommu/amd/iommu.c +4 -4
index dc1ec6849775..e8a2e5984acb 100644
@@ -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);
Signature · 1 line
2.30.2
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 lines · Su Hui

Move allocation code down to avoid memory leak.

Fixes 29f54745f245 ("iommu/amd: Add missing domain type checks")

-Vasant

Quoted 33 lines

---
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(-)
drivers/iommu/amd/iommu.c +4 -4
index dc1ec6849775..e8a2e5984acb 100644
@@ -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);
Reviewed-by on v2 1/1
#2 lore raw
In reply to Su Hui
Quoted 4 lines · Su Hui

Move allocation code down to avoid memory leak.

Fixes 29f54745f245 ("iommu/amd: Add missing domain type checks")
Quoted 36 lines

---
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(-)
drivers/iommu/amd/iommu.c +4 -4
index dc1ec6849775..e8a2e5984acb 100644
@@ -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);
Signature · 1 line
2.30.2
Reviewed-by on v2 1/1
#3 lore raw
In reply to Su Hui
Quoted 10 lines · Su Hui

Move allocation code down to avoid memory leak.

Fixes 29f54745f245 ("iommu/amd: Add missing domain type checks")

---
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(-)

Thanks,
Jason