Skip to contentSkip to conversation

accel/amdxdna: Fix memory leak in amdxdna_ubuf_map

PATCH Applied zishun yi 3 participants 4 messages Jan 29 – Jan 30

Subsystems (2)
lore
Share & tools
lore

zishun yi

author
#0 lore raw

PATCH v2 1/1 accel/amdxdna: Fix memory leak in amdxdna_ubuf_map

From: Zishun Yi <[email protected]>

The amdxdna_ubuf_map() function allocates memory for sg and internal sg table structures, but it fails to free them if subsequent operations (sg_alloc_table_from_pages or dma_map_sgtable) fail.

Fixes bd72d4acda10 ("accel/amdxdna: Support user space allocated buffer")

---
v2: Add 'Fixes' tag

 drivers/accel/amdxdna/amdxdna_ubuf.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/accel/amdxdna/amdxdna_ubuf.c +8 -2
index 077b2261cf2a..9e3b3b055caa 100644
@@ -34,15 +34,21 @@ static struct sg_table *amdxdna_ubuf_map(struct dma_buf_attachment *attach,
 	ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->nr_pages, 0,
 					ubuf->nr_pages << PAGE_SHIFT, GFP_KERNEL);
 	if (ret)
-		return ERR_PTR(ret);
+		goto err_free_sg;
 
 	if (ubuf->flags & AMDXDNA_UBUF_FLAG_MAP_DMA) {
 		ret = dma_map_sgtable(attach->dev, sg, direction, 0);
 		if (ret)
-			return ERR_PTR(ret);
+			goto err_free_table;
 	}
 
 	return sg;
+
+err_free_table:
+	sg_free_table(sg);
+err_free_sg:
+	kfree(sg);
+	return ERR_PTR(ret);
 }
 
 static void amdxdna_ubuf_unmap(struct dma_buf_attachment *attach,
Signature · 1 line
2.51.2
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 42 lines · zishun yi

From: Zishun Yi <[email protected]>

The amdxdna_ubuf_map() function allocates memory for sg and internal sg table structures, but it fails to free them if subsequent operations (sg_alloc_table_from_pages or dma_map_sgtable) fail.

Fixes bd72d4acda10 ("accel/amdxdna: Support user space allocated buffer")

---
v2: Add 'Fixes' tag

  drivers/accel/amdxdna/amdxdna_ubuf.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/accel/amdxdna/amdxdna_ubuf.c +8 -2
index 077b2261cf2a..9e3b3b055caa 100644
@@ -34,15 +34,21 @@ static struct sg_table *amdxdna_ubuf_map(struct dma_buf_attachment *attach,
  	ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->nr_pages, 0,
  					ubuf->nr_pages << PAGE_SHIFT, GFP_KERNEL);
  	if (ret)
-		return ERR_PTR(ret);
+		goto err_free_sg;
 
  	if (ubuf->flags & AMDXDNA_UBUF_FLAG_MAP_DMA) {
  		ret = dma_map_sgtable(attach->dev, sg, direction, 0);
  		if (ret)
-			return ERR_PTR(ret);
+			goto err_free_table;
  	}
 
  	return sg;
+
+err_free_table:
+	sg_free_table(sg);
+err_free_sg:
+	kfree(sg);
+	return ERR_PTR(ret);
  }
 
  static void amdxdna_ubuf_unmap(struct dma_buf_attachment *attach,
maintainer Applied
#3 lore raw
In reply to zishun yi

Applied to drm-misc-next-fixes.

Quoted 42 lines · zishun yi

From: Zishun Yi <[email protected]>

The amdxdna_ubuf_map() function allocates memory for sg and internal sg table structures, but it fails to free them if subsequent operations (sg_alloc_table_from_pages or dma_map_sgtable) fail.

Fixes bd72d4acda10 ("accel/amdxdna: Support user space allocated buffer")

---
v2: Add 'Fixes' tag

  drivers/accel/amdxdna/amdxdna_ubuf.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/accel/amdxdna/amdxdna_ubuf.c +8 -2
index 077b2261cf2a..9e3b3b055caa 100644
@@ -34,15 +34,21 @@ static struct sg_table *amdxdna_ubuf_map(struct dma_buf_attachment *attach,
  	ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->nr_pages, 0,
  					ubuf->nr_pages << PAGE_SHIFT, GFP_KERNEL);
  	if (ret)
-		return ERR_PTR(ret);
+		goto err_free_sg;
 
  	if (ubuf->flags & AMDXDNA_UBUF_FLAG_MAP_DMA) {
  		ret = dma_map_sgtable(attach->dev, sg, direction, 0);
  		if (ret)
-			return ERR_PTR(ret);
+			goto err_free_table;
  	}
 
  	return sg;
+
+err_free_table:
+	sg_free_table(sg);
+err_free_sg:
+	kfree(sg);
+	return ERR_PTR(ret);
  }
 
  static void amdxdna_ubuf_unmap(struct dma_buf_attachment *attach,