Skip to contentSkip to conversation

accel/amdxdna: Fix leak when pinning ubuf pages

PATCH In review Lizhi Hou 2 participants 3 messages Mar 26

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 accel/amdxdna: Fix leak when pinning ubuf pages

From: Max Zhen <[email protected]>

When pin_user_pages_fast() returns fewer pages than requested, the pages that were successfully pinned are not released, leading to a leak.

Fix this by unpinning any partially pinned pages before returning failure.

Fixes bd72d4acda10 ("accel/amdxdna: Support user space allocated buffer")
---
 drivers/accel/amdxdna/amdxdna_ubuf.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/accel/amdxdna/amdxdna_ubuf.c +8 -4
index fb999aa25318..4c0647057759 100644
@@ -196,13 +196,17 @@ struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev,
 		ret = pin_user_pages_fast(va_ent[i].vaddr, npages,
 					  FOLL_WRITE | FOLL_LONGTERM,
 					  &ubuf->pages[start]);
-		if (ret < 0 || ret != npages) {
-			ret = -ENOMEM;
+		if (ret >= 0) {
+			start += ret;
+			if (ret != npages) {
+				XDNA_ERR(xdna, "Partially pinned pages %d/%u", ret, npages);
+				ret = -ENOMEM;
+				goto destroy_pages;
+			}
+		} else {
 			XDNA_ERR(xdna, "Failed to pin pages ret %d", ret);
 			goto destroy_pages;
 		}
-
-		start += ret;
 	}
 
 	exp_info.ops = &amdxdna_ubuf_dmabuf_ops;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Lizhi Hou

From: Max Zhen <[email protected]>

When pin_user_pages_fast() returns fewer pages than requested, the pages that were successfully pinned are not released, leading to a leak.

Fix this by unpinning any partially pinned pages before returning failure.

Fixes bd72d4acda10 ("accel/amdxdna: Support user space allocated buffer")
Quoted 30 lines
---
  drivers/accel/amdxdna/amdxdna_ubuf.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
drivers/accel/amdxdna/amdxdna_ubuf.c +8 -4
index fb999aa25318..4c0647057759 100644
@@ -196,13 +196,17 @@ struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev,
  		ret = pin_user_pages_fast(va_ent[i].vaddr, npages,
  					  FOLL_WRITE | FOLL_LONGTERM,
  					  &ubuf->pages[start]);
-		if (ret < 0 || ret != npages) {
-			ret = -ENOMEM;
+		if (ret >= 0) {
+			start += ret;
+			if (ret != npages) {
+				XDNA_ERR(xdna, "Partially pinned pages %d/%u", ret, npages);
+				ret = -ENOMEM;
+				goto destroy_pages;
+			}
+		} else {
  			XDNA_ERR(xdna, "Failed to pin pages ret %d", ret);
  			goto destroy_pages;
  		}
-
-		start += ret;
  	}
 
  	exp_info.ops = &amdxdna_ubuf_dmabuf_ops;
author maintainer Applied
#2 lore raw

Applied to drm-misc-next

Quoted 46 lines · Mario Limonciello
Quoted 10 lines · Lizhi Hou

From: Max Zhen <[email protected]>

When pin_user_pages_fast() returns fewer pages than requested, the pages that were successfully pinned are not released, leading to a leak.

Fix this by unpinning any partially pinned pages before returning failure.

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

---
  drivers/accel/amdxdna/amdxdna_ubuf.c | 12 ++++++++----

  1 file changed, 8 insertions(+), 4 deletions(-)
drivers/accel/amdxdna/amdxdna_ubuf.c +8 -4
index fb999aa25318..4c0647057759 100644
@@ -196,13 +196,17 @@ struct dma_buf *amdxdna_get_ubuf(struct 
 drm_device *dev,
           ret = pin_user_pages_fast(va_ent[i].vaddr, npages,
                         FOLL_WRITE | FOLL_LONGTERM,
                         &ubuf->pages[start]);
-        if (ret < 0 || ret != npages) {
-            ret = -ENOMEM;
+        if (ret >= 0) {
+            start += ret;
+            if (ret != npages) {
+                XDNA_ERR(xdna, "Partially pinned pages %d/%u", ret, 
 npages);
+                ret = -ENOMEM;
+                goto destroy_pages;
+            }
+        } else {
               XDNA_ERR(xdna, "Failed to pin pages ret %d", ret);
               goto destroy_pages;
           }
-
-        start += ret;
       }

        exp_info.ops = &amdxdna_ubuf_dmabuf_ops;