Skip to contentSkip to conversation

accel/amdxdna: Fix iommu_map_sgtable() return value handling

PATCH In review Lizhi Hou 2 participants 3 messages Apr 13 – Apr 14

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 accel/amdxdna: Fix iommu_map_sgtable() return value handling

iommu_map_sgtable() returns negative error codes on failure, but the result is stored in an unsigned variable. This prevents proper error detection.

Change the variable type to ssize_t so negative error values can be handled correctly.

Fixes ece3e8980907 ("accel/amdxdna: Allow forcing IOVA-based DMA via module parameter")
---
 drivers/accel/amdxdna/amdxdna_iommu.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/amdxdna_iommu.c +8 -1
index 4626434d4180..2676cfcfabee 100644
@@ -40,7 +40,7 @@ int amdxdna_iommu_map_bo(struct amdxdna_dev *xdna, struct amdxdna_gem_obj *abo)
 	struct sg_table *sgt;
 	dma_addr_t dma_addr;
 	struct iova *iova;
-	size_t size;
+	ssize_t size;
 
 	if (abo->type != AMDXDNA_BO_DEV_HEAP && abo->type != AMDXDNA_BO_SHMEM)
 		return 0;
@@ -65,7 +65,14 @@ int amdxdna_iommu_map_bo(struct amdxdna_dev *xdna, struct amdxdna_gem_obj *abo)
 
 	size = iommu_map_sgtable(xdna->domain, dma_addr, sgt,
 				 IOMMU_READ | IOMMU_WRITE);
+	if (size < 0) {
+		XDNA_ERR(xdna, "iommu_map_sgtable failed: %zd", size);
+		__free_iova(&xdna->iovad, iova);
+		return size;
+	}
+
 	if (size < abo->mem.size) {
+		iommu_unmap(xdna->domain, dma_addr, size);
 		__free_iova(&xdna->iovad, iova);
 		return -ENXIO;
 	}
Signature · 1 line
2.34.1
Reviewed-by Mario Limonciello (AMD) on v1 1/1
#1 lore raw
Quoted 4 lines · Lizhi Hou

iommu_map_sgtable() returns negative error codes on failure, but the result is stored in an unsigned variable. This prevents proper error detection.

Quoted 8 lines

Change the variable type to ssize_t so negative error values can be handled correctly.

Fixes ece3e8980907 ("accel/amdxdna: Allow forcing IOVA-based DMA via module parameter")
Quoted 32 lines
---
  drivers/accel/amdxdna/amdxdna_iommu.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/amdxdna_iommu.c +8 -1
index 4626434d4180..2676cfcfabee 100644
@@ -40,7 +40,7 @@ int amdxdna_iommu_map_bo(struct amdxdna_dev *xdna, struct amdxdna_gem_obj *abo)
  	struct sg_table *sgt;
  	dma_addr_t dma_addr;
  	struct iova *iova;
-	size_t size;
+	ssize_t size;
 
  	if (abo->type != AMDXDNA_BO_DEV_HEAP && abo->type != AMDXDNA_BO_SHMEM)
  		return 0;
@@ -65,7 +65,14 @@ int amdxdna_iommu_map_bo(struct amdxdna_dev *xdna, struct amdxdna_gem_obj *abo)
 
  	size = iommu_map_sgtable(xdna->domain, dma_addr, sgt,
  				 IOMMU_READ | IOMMU_WRITE);
+	if (size < 0) {
+		XDNA_ERR(xdna, "iommu_map_sgtable failed: %zd", size);
+		__free_iova(&xdna->iovad, iova);
+		return size;
+	}
+
  	if (size < abo->mem.size) {
+		iommu_unmap(xdna->domain, dma_addr, size);
  		__free_iova(&xdna->iovad, iova);
  		return -ENXIO;
  	}
author maintainer Applied
#2 lore raw

Applied to drm-misc-next

Quoted 49 lines · Mario Limonciello
Quoted 4 lines · Lizhi Hou

iommu_map_sgtable() returns negative error codes on failure, but the result is stored in an unsigned variable. This prevents proper error detection.

Quoted 8 lines

Change the variable type to ssize_t so negative error values can be handled correctly.

Fixes ece3e8980907 ("accel/amdxdna: Allow forcing IOVA-based DMA via module parameter")
Quoted 32 lines

---
  drivers/accel/amdxdna/amdxdna_iommu.c | 9 ++++++++-

  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/amdxdna_iommu.c +8 -1
index 4626434d4180..2676cfcfabee 100644
@@ -40,7 +40,7 @@ int amdxdna_iommu_map_bo(struct amdxdna_dev *xdna, 
 struct amdxdna_gem_obj *abo)
       struct sg_table *sgt;
       dma_addr_t dma_addr;
       struct iova *iova;
-    size_t size;
+    ssize_t size;
         if (abo->type != AMDXDNA_BO_DEV_HEAP && abo->type != AMDXDNA_BO_SHMEM)
           return 0;
@@ -65,7 +65,14 @@ int amdxdna_iommu_map_bo(struct amdxdna_dev *xdna, 
 struct amdxdna_gem_obj *abo)
         size = iommu_map_sgtable(xdna->domain, dma_addr, sgt,
                    IOMMU_READ | IOMMU_WRITE);
+    if (size < 0) {
+        XDNA_ERR(xdna, "iommu_map_sgtable failed: %zd", size);
+        __free_iova(&xdna->iovad, iova);
+        return size;
+    }
+
       if (size < abo->mem.size) {
+        iommu_unmap(xdna->domain, dma_addr, size);
           __free_iova(&xdna->iovad, iova);
           return -ENXIO;
       }