Skip to contentSkip to conversation

accel/amdxdna: Fix memory leak in amdxdna_iommu_alloc()

PATCH Applied Felix Gu 2 participants 3 messages Apr 16

Subsystems (2)
lore
Share & tools
lore

Felix Gu

author
#0 lore raw

PATCH 1/1 accel/amdxdna: Fix memory leak in amdxdna_iommu_alloc()

In amdxdna_iommu_alloc(), if iommu_map() fails after successfully allocating both iova and cpu_addr, the code jumps to free_iova which only frees the iova, leaking the allocated pages.

Fixes ece3e8980907 ("accel/amdxdna: Allow forcing IOVA-based DMA via module parameter")
---
 drivers/accel/amdxdna/amdxdna_iommu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/amdxdna_iommu.c +3 -1
index 4626434d4180..a2e8d8ac0901 100644
@@ -110,10 +110,12 @@ void *amdxdna_iommu_alloc(struct amdxdna_dev *xdna, size_t size, dma_addr_t *dma
 			iova_align(&xdna->iovad, size),
 			IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);
 	if (ret)
-		goto free_iova;
+		goto free_cpu_addr;
 
 	return cpu_addr;
 
+free_cpu_addr:
+	free_pages((unsigned long)cpu_addr, get_order(size));
 free_iova:
 	__free_iova(&xdna->iovad, iova);
 	return ERR_PTR(ret);

---
base-commit: 936c21068d7ade00325e40d82bfd2f3f29d9f659
change-id: 20260416-amdxdna-f46d045130aa

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Felix Gu

In amdxdna_iommu_alloc(), if iommu_map() fails after successfully allocating both iova and cpu_addr, the code jumps to free_iova which only frees the iova, leaking the allocated pages.

Fixes ece3e8980907 ("accel/amdxdna: Allow forcing IOVA-based DMA via module parameter")
---
  drivers/accel/amdxdna/amdxdna_iommu.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/amdxdna_iommu.c +0 -0
index 4626434d4180..a2e8d8ac0901 100644

drivers/accel/amdxdna/amdxdna_iommu.c void *amdxdna_iommu_alloc(struct amdxdna_dev *xdna, size_t size, dma_addr_t *dma · line 118

Quoted 10 lines
drivers/accel/amdxdna/amdxdna_iommu.c +3 -1
@@ -110,10 +110,12 @@ void *amdxdna_iommu_alloc(struct amdxdna_dev *xdna, size_t size, dma_addr_t *dma
                         iova_align(&xdna->iovad, size),
                         IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);
         if (ret)
-               goto free_iova;
+               goto free_cpu_addr;
 
         return cpu_addr;
 
+free_cpu_addr:
+       free_pages((unsigned long)cpu_addr, get_order(size));
Quoted 12 lines
drivers/accel/amdxdna/amdxdna_iommu.c +0 -1
  free_iova:
         __free_iova(&xdna->iovad, iova);
         return ERR_PTR(ret);
 
---

base-commit: 936c21068d7ade00325e40d82bfd2f3f29d9f659
change-id: 20260416-amdxdna-f46d045130aa

Best regards,
--
Felix Gu <[email protected]>

maintainer Applied
#2 lore raw
In reply to Felix Gu

Applied to drm-misc-next

Quoted 39 lines · Felix Gu

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

In amdxdna_iommu_alloc(), if iommu_map() fails after successfully allocating both iova and cpu_addr, the code jumps to free_iova which only frees the iova, leaking the allocated pages.

Fixes ece3e8980907 ("accel/amdxdna: Allow forcing IOVA-based DMA via module parameter")
---
  drivers/accel/amdxdna/amdxdna_iommu.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/amdxdna_iommu.c +3 -2
index 4626434d4180..a2e8d8ac0901 100644
@@ -110,10 +110,12 @@ void *amdxdna_iommu_alloc(struct amdxdna_dev *xdna, size_t size, dma_addr_t *dma
                         iova_align(&xdna->iovad, size),
                         IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);
         if (ret)
-               goto free_iova;
+               goto free_cpu_addr;
 
         return cpu_addr;
 
+free_cpu_addr:
+       free_pages((unsigned long)cpu_addr, get_order(size));
  free_iova:
         __free_iova(&xdna->iovad, iova);
         return ERR_PTR(ret);
 
---

base-commit: 936c21068d7ade00325e40d82bfd2f3f29d9f659
change-id: 20260416-amdxdna-f46d045130aa

Best regards,
--
Felix Gu <[email protected]>