Skip to contentSkip to conversation

drm/amdgpu: fix bo->pin leaking in amdgpu_bo_create_reserved

PATCH In review Zhu Lingshan 3 participants 3 messages Jul 10

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: fix bo->pin leaking in amdgpu_bo_create_reserved

amdgpu_bo_create_reserved() only allocates a new BO when *bo_ptr (struct amdgpu_bo **bo_ptr as input parameter) is NULL, it simply skips creation when *bo_ptr is non-NULL. But it unconditionally reserves, pins, gart allocates and maps the BO afterwards.

When the same non-NULL BO pointer is passed in again,
for example firmware buffers that live in adev and are re-loaded on every resume / cp_resume / start
under AMDGPU_FW_LOAD_DIRECT, amdgpu_bo_pin() just increases pin_count unconditionally, however the matching teardown only unpins once, so pin_count never drops to zero, so TTM is not able to move, swap or evict a BO, causing BO leaks.

This commit fixes this issue by only pinning the bo once at creation, and repeated calls no longer take additional pin references.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +8 -5
index 4dd7c712b8c3..7ac3b8fd963a 100644
@@ -276,10 +276,12 @@ int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
 		goto error_free;
 	}
 
-	r = amdgpu_bo_pin(*bo_ptr, domain);
-	if (r) {
-		dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
-		goto error_unreserve;
+	if (free) {
+		r = amdgpu_bo_pin(*bo_ptr, domain);
+		if (r) {
+			dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
+			goto error_unreserve;
+		}
 	}
 
 	r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo);
@@ -302,7 +304,8 @@ int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
 	return 0;
 
 error_unpin:
-	amdgpu_bo_unpin(*bo_ptr);
+	if (free)
+		amdgpu_bo_unpin(*bo_ptr);
 error_unreserve:
 	amdgpu_bo_unreserve(*bo_ptr);
 
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Zhu Lingshan

amdgpu_bo_create_reserved() only allocates a new BO when *bo_ptr (struct amdgpu_bo **bo_ptr as input parameter) is NULL, it simply skips creation when *bo_ptr is non-NULL. But it unconditionally reserves, pins, gart allocates and maps the BO afterwards.

When the same non-NULL BO pointer is passed in again,
for example firmware buffers that live in adev and are re-loaded on every resume / cp_resume / start
under AMDGPU_FW_LOAD_DIRECT, amdgpu_bo_pin() just increases pin_count unconditionally, however the matching teardown only unpins once, so pin_count never drops to zero, so TTM is not able to move, swap or evict a BO, causing BO leaks.

Quoted 5 lines

This commit fixes this issue by only pinning the bo once at creation, and repeated calls no longer take additional pin references.

Quoted 38 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +8 -6
index 4dd7c712b8c3..7ac3b8fd963a 100644
@@ -276,10 +276,12 @@ int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
                goto error_free;
        }
 
-       r = amdgpu_bo_pin(*bo_ptr, domain);
-       if (r) {
-               dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
-               goto error_unreserve;
+       if (free) {
+               r = amdgpu_bo_pin(*bo_ptr, domain);
+               if (r) {
+                       dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
+                       goto error_unreserve;
+               }
        }
 
        r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo);
@@ -302,7 +304,8 @@ int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
        return 0;
 
 error_unpin:
-       amdgpu_bo_unpin(*bo_ptr);
+       if (free)
+               amdgpu_bo_unpin(*bo_ptr);
 error_unreserve:
        amdgpu_bo_unreserve(*bo_ptr);
 
--

2.55.0

maintainer Reviewed-by
#2 lore raw
Quoted 16 lines · Alex Deucher
Quoted 15 lines · Zhu Lingshan

amdgpu_bo_create_reserved() only allocates a new BO when *bo_ptr (struct amdgpu_bo **bo_ptr as input parameter) is NULL, it simply skips creation when *bo_ptr is non-NULL. But it unconditionally reserves, pins, gart allocates and maps the BO afterwards.

When the same non-NULL BO pointer is passed in again,
for example firmware buffers that live in adev and are re-loaded on every resume / cp_resume / start
under AMDGPU_FW_LOAD_DIRECT, amdgpu_bo_pin() just increases pin_count unconditionally, however the matching teardown only unpins once, so pin_count never drops to zero, so TTM is not able to move, swap or evict a BO, causing BO leaks.

Quoted 7 lines
Quoted 5 lines

This commit fixes this issue by only pinning the bo once at creation, and repeated calls no longer take additional pin references.

Quoted 39 lines
Quoted 39 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +8 -6
index 4dd7c712b8c3..7ac3b8fd963a 100644
@@ -276,10 +276,12 @@ int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
                goto error_free;
        }
 
-       r = amdgpu_bo_pin(*bo_ptr, domain);
-       if (r) {
-               dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
-               goto error_unreserve;
+       if (free) {
+               r = amdgpu_bo_pin(*bo_ptr, domain);
+               if (r) {
+                       dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
+                       goto error_unreserve;
+               }
        }
 
        r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo);
@@ -302,7 +304,8 @@ int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
        return 0;
 
 error_unpin:
-       amdgpu_bo_unpin(*bo_ptr);
+       if (free)
+               amdgpu_bo_unpin(*bo_ptr);
 error_unreserve:
        amdgpu_bo_unreserve(*bo_ptr);
 
--

2.55.0