Skip to contentSkip to conversation

drm/ttm: fix the documentation of ttm_bo_init

PATCH New Nicolai Hähnle 4 participants 5 messages Feb 16, 2017 – Feb 17, 2017 2 messages not in the archive

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

PATCH v3 1/3 drm/ttm: fix the documentation of ttm_bo_init

From: Nicolai Hähnle <[email protected]>

As the comment says: callers of ttm_bo_init cannot rely on having the only reference to the BO when the function returns successfully.

---
 include/drm/ttm/ttm_bo_api.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/drm/ttm/ttm_bo_api.h +5 -1
index f195899..c0ebbd6 100644
@@ -476,7 +476,11 @@ size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  * As this object may be part of a larger structure, this function,
  * together with the @destroy function,
  * enables driver-specific objects derived from a ttm_buffer_object.
- * On successful return, the object kref and list_kref are set to 1.
+ *
+ * On successful return, the caller owns an object kref to @bo. The kref and
+ * list_kref are usually set to 1, but note that in some situations, other
+ * tasks may already be holding references to @bo as well.
+ *
  * If a failure occurs, the function will call the @destroy function, or
  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
  * illegal and will likely cause memory corruption.
Signature · 6 lines
2.9.3

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20170216113946.22046-1-nhaehnle-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH v3 2/3 drm/ttm: add ttm_bo_init_reserved

From: Nicolai Hähnle <[email protected]>

This variant of ttm_bo_init returns the validated buffer object with the reservation lock held when resv == NULL. This is convenient for callers that want to use the BO immediately, e.g. for initializing its contents.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 59 +++++++++++++++++++++++++++++++++-----------
 include/drm/ttm/ttm_bo_api.h | 54 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+), 15 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +44 -15
index c438b04..989b98b 100644
@@ -1119,18 +1119,18 @@ int ttm_bo_validate(struct ttm_buffer_object *bo,
 }
 EXPORT_SYMBOL(ttm_bo_validate);
 
-int ttm_bo_init(struct ttm_bo_device *bdev,
-		struct ttm_buffer_object *bo,
-		unsigned long size,
-		enum ttm_bo_type type,
-		struct ttm_placement *placement,
-		uint32_t page_alignment,
-		bool interruptible,
-		struct file *persistent_swap_storage,
-		size_t acc_size,
-		struct sg_table *sg,
-		struct reservation_object *resv,
-		void (*destroy) (struct ttm_buffer_object *))
+int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
+			 struct ttm_buffer_object *bo,
+			 unsigned long size,
+			 enum ttm_bo_type type,
+			 struct ttm_placement *placement,
+			 uint32_t page_alignment,
+			 bool interruptible,
+			 struct file *persistent_swap_storage,
+			 size_t acc_size,
+			 struct sg_table *sg,
+			 struct reservation_object *resv,
+			 void (*destroy) (struct ttm_buffer_object *))
 {
 	int ret = 0;
 	unsigned long num_pages;
@@ -1214,10 +1214,10 @@ int ttm_bo_init(struct ttm_bo_device *bdev,
 	if (likely(!ret))
 		ret = ttm_bo_validate(bo, placement, interruptible, false);
 
-	if (!resv)
-		ttm_bo_unreserve(bo);
-
 	if (unlikely(ret)) {
+		if (!resv)
+			ttm_bo_unreserve(bo);
+
 		ttm_bo_unref(&bo);
 		return ret;
 	}
@@ -1230,6 +1230,35 @@ int ttm_bo_init(struct ttm_bo_device *bdev,
 
 	return ret;
 }
+EXPORT_SYMBOL(ttm_bo_init_reserved);
+
+int ttm_bo_init(struct ttm_bo_device *bdev,
+		struct ttm_buffer_object *bo,
+		unsigned long size,
+		enum ttm_bo_type type,
+		struct ttm_placement *placement,
+		uint32_t page_alignment,
+		bool interruptible,
+		struct file *persistent_swap_storage,
+		size_t acc_size,
+		struct sg_table *sg,
+		struct reservation_object *resv,
+		void (*destroy) (struct ttm_buffer_object *))
+{
+	int ret;
+
+	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
+				   page_alignment, interruptible,
+				   persistent_swap_storage, acc_size,
+				   sg, resv, destroy);
+	if (ret)
+		return ret;
+
+	if (!resv)
+		ttm_bo_unreserve(bo);
+
+	return 0;
+}
 EXPORT_SYMBOL(ttm_bo_init);
 
 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
include/drm/ttm/ttm_bo_api.h +54 -0

60 lines not on this page · raw message

Signature · 6 lines
2.9.3

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#3 lore raw

PATCH v3 3/3 drm/amdgpu: simplify reservation handling during buffer creation

In reply to Nicolai Hähnle

From: Nicolai Hähnle <[email protected]>

By using ttm_bo_init_reserved instead of the manual initialization of the reservation object, the reservation lock will be properly unlocked and destroyed when the TTM BO initialization fails.

Actual deadlocks caused by the missing unlock should have been fixed by "drm/ttm: never add BO that failed to validate to the LRU list", superseding the flawed fix in commit 38fc4856ad98 ("drm/amdgpu: fix a potential deadlock in amdgpu_bo_create_restricted()").

This change fixes remaining recursive locking errors that can be seen with lock debugging enabled, and avoids the error of freeing a locked mutex.

As an additional minor bonus, buffers created with resv == NULL and the AMDGPU_GEM_CREATE_VRAM_CLEARED flag are now only added to the global LRU list after the fill commands have been issued.

Fixes 12a852219583 ("drm/amdgpu: improve AMDGPU_GEM_CREATE_VRAM_CLEARED handling (v2)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +4 -13
index c2e57f7..4d0536d 100644
@@ -395,19 +395,10 @@ int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
 	amdgpu_fill_placement_to_bo(bo, placement);
 	/* Kernel allocation are uninterruptible */
 
-	if (!resv) {
-		bool locked;
-
-		reservation_object_init(&bo->tbo.ttm_resv);
-		locked = ww_mutex_trylock(&bo->tbo.ttm_resv.lock);
-		WARN_ON(!locked);
-	}
-
 	initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
-	r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
-			&bo->placement, page_align, !kernel, NULL,
-			acc_size, sg, resv ? resv : &bo->tbo.ttm_resv,
-			&amdgpu_ttm_bo_destroy);
+	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
+				 &bo->placement, page_align, !kernel, NULL,
+				 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
 	amdgpu_cs_report_moved_bytes(adev,
 		atomic64_read(&adev->num_bytes_moved) - initial_bytes_moved);
 
@@ -433,7 +424,7 @@ int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
 		fence_put(fence);
 	}
 	if (!resv)
-		ww_mutex_unlock(&bo->tbo.resv->lock);
+		ttm_bo_unreserve(&bo->tbo);
 	*bo_ptr = bo;
 
 	trace_amdgpu_bo_create(bo);
Signature · 6 lines
2.9.3

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 20170216113946.22046-3-nhaehnle-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#5 lore raw

Re: [PATCH v3 3/3] drm/amdgpu: simplify reservation handling during buffer creation

Am 16.02.2017 um 12:39 schrieb Nicolai Hähnle:

Quoted 53 lines

From: Nicolai Hähnle <[email protected]>

By using ttm_bo_init_reserved instead of the manual initialization of the reservation object, the reservation lock will be properly unlocked and destroyed when the TTM BO initialization fails.

Actual deadlocks caused by the missing unlock should have been fixed by "drm/ttm: never add BO that failed to validate to the LRU list", superseding the flawed fix in commit 38fc4856ad98 ("drm/amdgpu: fix a potential deadlock in amdgpu_bo_create_restricted()").

This change fixes remaining recursive locking errors that can be seen with lock debugging enabled, and avoids the error of freeing a locked mutex.

As an additional minor bonus, buffers created with resv == NULL and the AMDGPU_GEM_CREATE_VRAM_CLEARED flag are now only added to the global LRU list after the fill commands have been issued.

Fixes 12a852219583 ("drm/amdgpu: improve AMDGPU_GEM_CREATE_VRAM_CLEARED handling (v2)")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 17 ++++-------------
  1 file changed, 4 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -12
index c2e57f7..4d0536d 100644
@@ -395,19 +395,10 @@ int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
  	amdgpu_fill_placement_to_bo(bo, placement);
  	/* Kernel allocation are uninterruptible */
 
-	if (!resv) {
-		bool locked;
-
-		reservation_object_init(&bo->tbo.ttm_resv);
-		locked = ww_mutex_trylock(&bo->tbo.ttm_resv.lock);
-		WARN_ON(!locked);
-	}
-
  	initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
-	r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
-			&bo->placement, page_align, !kernel, NULL,
-			acc_size, sg, resv ? resv : &bo->tbo.ttm_resv,
-			&amdgpu_ttm_bo_destroy);
+	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
+				 &bo->placement, page_align, !kernel, NULL,
+				 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
  	amdgpu_cs_report_moved_bytes(adev,
  		atomic64_read(&adev->num_bytes_moved) - initial_bytes_moved);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c int amdgpu_bo_create_restricted(struct amdgpu_device *adev, · line 427

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -1
@@ -433,7 +424,7 @@ int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
  		fence_put(fence);
  	}
  	if (!resv)
-		ww_mutex_unlock(&bo->tbo.resv->lock);
+		ttm_bo_unreserve(&bo->tbo);

You can just use amdgpu_bo_unreserve() here. With that fixed the whole set is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c int amdgpu_bo_create_restricted(struct amdgpu_device *adev, · line 430

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
  	*bo_ptr = bo;
 
  	trace_amdgpu_bo_create(bo);

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by
#6 lore raw

Re: [PATCH v3 3/3] drm/amdgpu: simplify reservation handling during buffer creation

Quoted 22 lines · Christian König

Am 16.02.2017 um 12:39 schrieb Nicolai Hähnle:

Quoted 21 lines

From: Nicolai Hähnle <[email protected]>

By using ttm_bo_init_reserved instead of the manual initialization of the reservation object, the reservation lock will be properly unlocked and destroyed when the TTM BO initialization fails.

Actual deadlocks caused by the missing unlock should have been fixed by "drm/ttm: never add BO that failed to validate to the LRU list", superseding the flawed fix in commit 38fc4856ad98 ("drm/amdgpu: fix a potential deadlock in amdgpu_bo_create_restricted()").

This change fixes remaining recursive locking errors that can be seen with lock debugging enabled, and avoids the error of freeing a locked mutex.

As an additional minor bonus, buffers created with resv == NULL and the AMDGPU_GEM_CREATE_VRAM_CLEARED flag are now only added to the global LRU list after the fill commands have been issued.

Fixes 12a852219583 ("drm/amdgpu: improve AMDGPU_GEM_CREATE_VRAM_CLEARED handling (v2)")
Quoted 43 lines
Quoted 35 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 17 ++++-------------
  1 file changed, 4 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -11
index c2e57f7..4d0536d 100644
@@ -395,19 +395,10 @@ int amdgpu_bo_create_restricted(struct 
 amdgpu_device *adev,
      amdgpu_fill_placement_to_bo(bo, placement);
      /* Kernel allocation are uninterruptible */
  -    if (!resv) {
-        bool locked;
-
-        reservation_object_init(&bo->tbo.ttm_resv);
-        locked = ww_mutex_trylock(&bo->tbo.ttm_resv.lock);
-        WARN_ON(!locked);
-    }
-
      initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
-    r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
-            &bo->placement, page_align, !kernel, NULL,
-            acc_size, sg, resv ? resv : &bo->tbo.ttm_resv,
-            &amdgpu_ttm_bo_destroy);
+    r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
+                 &bo->placement, page_align, !kernel, NULL,
+                 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
      amdgpu_cs_report_moved_bytes(adev,
          atomic64_read(&adev->num_bytes_moved) - initial_bytes_moved);
  @@ -433,7 +424,7 @@ int amdgpu_bo_create_restricted(struct 

amdgpu_device *adev,

          fence_put(fence);
      }
      if (!resv)

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c int amdgpu_bo_create_restricted(struct amdgpu_device *adev, · line 427

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -1
-        ww_mutex_unlock(&bo->tbo.resv->lock);
+        ttm_bo_unreserve(&bo->tbo);

You can just use amdgpu_bo_unreserve() here. With that fixed the whole set is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 8 lines
Quoted 2 lines
      *bo_ptr = bo;
        trace_amdgpu_bo_create(bo);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel