Skip to contentSkip to conversation

drm/ttm: Always and only destroy bo->ttm_resv in ttm_bo_release_list

PATCH Superseded Michel Dänzer 3 participants 6 messages Nov 1, 2017 – Nov 2, 2017 3 messages not in the archive

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

PATCH 1/1 drm/ttm: Always and only destroy bo->ttm_resv in ttm_bo_release_list

From: Michel Dänzer <[email protected]>

Fixes a use-after-free due to a race condition in ttm_bo_cleanup_refs_and_unlock, which allows one task to reserve a BO and destroy its ttm_resv while another task is waiting for it to signal in reservation_object_wait_timeout_rcu.

Fixes 0d2bd2ae045d "drm/ttm: fix memory leak while individualizing BOs"
---
 drivers/gpu/drm/ttm/ttm_bo.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +3 -10
index 379ec41d2c69..a19a0ebf32ac 100644
@@ -150,8 +150,7 @@ static void ttm_bo_release_list(struct kref *list_kref)
 	ttm_tt_destroy(bo->ttm);
 	atomic_dec(&bo->glob->bo_count);
 	dma_fence_put(bo->moving);
-	if (bo->resv == &bo->ttm_resv)
-		reservation_object_fini(&bo->ttm_resv);
+	reservation_object_fini(&bo->ttm_resv);
 	mutex_destroy(&bo->wu_mutex);
 	if (bo->destroy)
 		bo->destroy(bo);
@@ -406,10 +405,8 @@ static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
 	BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
 
 	r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
-	if (r) {
+	if (r)
 		reservation_object_unlock(&bo->ttm_resv);
-		reservation_object_fini(&bo->ttm_resv);
-	}
 
 	return r;
 }
@@ -457,10 +454,8 @@ static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
 		if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
 			ttm_bo_del_from_lru(bo);
 			spin_unlock(&glob->lru_lock);
-			if (bo->resv != &bo->ttm_resv) {
+			if (bo->resv != &bo->ttm_resv)
 				reservation_object_unlock(&bo->ttm_resv);
-				reservation_object_fini(&bo->ttm_resv);
-			}
 
 			ttm_bo_cleanup_memtype_use(bo);
 			return;
@@ -560,8 +555,6 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
 	}
 
 	ttm_bo_del_from_lru(bo);
-	if (!list_empty(&bo->ddestroy) && (bo->resv != &bo->ttm_resv))
-		reservation_object_fini(&bo->ttm_resv);
 	list_del_init(&bo->ddestroy);
 	kref_put(&bo->list_kref, ttm_bo_ref_bug);
 
Signature · 6 lines
2.15.0.rc2

_______________________________________________
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): 20171101161529.4844-1-michel-otUistvHUpPR7s880joybQ@public.gmane.org

#2 lore raw

I think so, it should be enough we free it in the last place.

We also find many issues for bo->resv and bo->ttm_resv when debugging PER-VM-BO feature, after ttm_bo_individualize_resv, we should set bo->resv = &bo->ttm_rev before adding to destroy list, and the root resv is preventing eviction and swapout. Roger will give a summary and send those draft patches.

For this patch, Reviewed-by: Chunming Zhou <[email protected]>

Quoted 52 lines · Michel Dänzer

From: Michel Dänzer <[email protected]>

Fixes a use-after-free due to a race condition in ttm_bo_cleanup_refs_and_unlock, which allows one task to reserve a BO and destroy its ttm_resv while another task is waiting for it to signal in reservation_object_wait_timeout_rcu.

Fixes 0d2bd2ae045d "drm/ttm: fix memory leak while individualizing BOs"
---
  drivers/gpu/drm/ttm/ttm_bo.c | 13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +3 -8
index 379ec41d2c69..a19a0ebf32ac 100644
@@ -150,8 +150,7 @@ static void ttm_bo_release_list(struct kref *list_kref)
  	ttm_tt_destroy(bo->ttm);
  	atomic_dec(&bo->glob->bo_count);
  	dma_fence_put(bo->moving);
-	if (bo->resv == &bo->ttm_resv)
-		reservation_object_fini(&bo->ttm_resv);
+	reservation_object_fini(&bo->ttm_resv);
  	mutex_destroy(&bo->wu_mutex);
  	if (bo->destroy)
  		bo->destroy(bo);
@@ -406,10 +405,8 @@ static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
  	BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
 
  	r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
-	if (r) {
+	if (r)
  		reservation_object_unlock(&bo->ttm_resv);
-		reservation_object_fini(&bo->ttm_resv);
-	}
 
  	return r;
  }
@@ -457,10 +454,8 @@ static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  		if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
  			ttm_bo_del_from_lru(bo);
  			spin_unlock(&glob->lru_lock);
-			if (bo->resv != &bo->ttm_resv) {
+			if (bo->resv != &bo->ttm_resv)
  				reservation_object_unlock(&bo->ttm_resv);
-				reservation_object_fini(&bo->ttm_resv);
-			}
 
  			ttm_bo_cleanup_memtype_use(bo);
  			return;

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, · line 560

Quoted 8 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -2
@@ -560,8 +555,6 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  	}
 
  	ttm_bo_del_from_lru(bo);
-	if (!list_empty(&bo->ddestroy) && (bo->resv != &bo->ttm_resv))
-		reservation_object_fini(&bo->ttm_resv);
  	list_del_init(&bo->ddestroy);
  	kref_put(&bo->list_kref, ttm_bo_ref_bug);
 

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

#3 lore raw
In reply to a message not in the archive

Am 01.11.2017 um 17:15 schrieb Michel Dänzer:

Quoted 9 lines

From: Michel Dänzer <[email protected]>

Fixes a use-after-free due to a race condition in ttm_bo_cleanup_refs_and_unlock, which allows one task to reserve a BO and destroy its ttm_resv while another task is waiting for it to signal in reservation_object_wait_timeout_rcu.

Fixes 0d2bd2ae045d "drm/ttm: fix memory leak while individualizing BOs"

Good idea, but one thing we should probably change.

Quoted 9 lines
---
  drivers/gpu/drm/ttm/ttm_bo.c | 13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 379ec41d2c69..a19a0ebf32ac 100644

drivers/gpu/drm/ttm/ttm_bo.c static void ttm_bo_release_list(struct kref *list_kref) · line 153

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -2
@@ -150,8 +150,7 @@ static void ttm_bo_release_list(struct kref *list_kref)
  	ttm_tt_destroy(bo->ttm);
  	atomic_dec(&bo->glob->bo_count);
  	dma_fence_put(bo->moving);
-	if (bo->resv == &bo->ttm_resv)
-		reservation_object_fini(&bo->ttm_resv);
+	reservation_object_fini(&bo->ttm_resv);

When we always call reservation_object_fini() here we should probably also always call reservation_object_init() in ttm_bo_init_reserved() to make sure the object is always initialized.

This way we can also remove the call to reservation_object_init() in ttm_bo_individualize_resv().

Regards,
Christian.

Quoted 28 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -6
  	mutex_destroy(&bo->wu_mutex);
  	if (bo->destroy)
  		bo->destroy(bo);
@@ -406,10 +405,8 @@ static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
  	BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
 
  	r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
-	if (r) {
+	if (r)
  		reservation_object_unlock(&bo->ttm_resv);
-		reservation_object_fini(&bo->ttm_resv);
-	}
 
  	return r;
  }
@@ -457,10 +454,8 @@ static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  		if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
  			ttm_bo_del_from_lru(bo);
  			spin_unlock(&glob->lru_lock);
-			if (bo->resv != &bo->ttm_resv) {
+			if (bo->resv != &bo->ttm_resv)
  				reservation_object_unlock(&bo->ttm_resv);
-				reservation_object_fini(&bo->ttm_resv);
-			}
 
  			ttm_bo_cleanup_memtype_use(bo);
  			return;

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, · line 560

Quoted 8 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -2
@@ -560,8 +555,6 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  	}
 
  	ttm_bo_del_from_lru(bo);
-	if (!list_empty(&bo->ddestroy) && (bo->resv != &bo->ttm_resv))
-		reservation_object_fini(&bo->ttm_resv);
  	list_del_init(&bo->ddestroy);
  	kref_put(&bo->list_kref, ttm_bo_ref_bug);
 

_______________________________________________
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): da4684ab-2c6d-7543-0c5d-3483ca093bc5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#5 lore raw
Quoted 17 lines · Christian König

Am 01.11.2017 um 17:15 schrieb Michel Dänzer:

Quoted 9 lines

From: Michel Dänzer <[email protected]>

Fixes a use-after-free due to a race condition in ttm_bo_cleanup_refs_and_unlock, which allows one task to reserve a BO and destroy its ttm_resv while another task is waiting for it to signal in reservation_object_wait_timeout_rcu.

Fixes 0d2bd2ae045d "drm/ttm: fix memory leak while individualizing BOs"

Good idea, but one thing we should probably change.

Quoted 4 lines

---
  drivers/gpu/drm/ttm/ttm_bo.c | 13 +++----------

  1 file changed, 3 insertions(+), 10 deletions(-)
Quoted 16 lines

drivers/gpu/drm/ttm/ttm_bo.c static void ttm_bo_release_list(struct kref · line 154

Quoted 12 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -2
index 379ec41d2c69..a19a0ebf32ac 100644
@@ -150,8 +150,7 @@ static void ttm_bo_release_list(struct kref
 *list_kref)
       ttm_tt_destroy(bo->ttm);
       atomic_dec(&bo->glob->bo_count);
       dma_fence_put(bo->moving);
-    if (bo->resv == &bo->ttm_resv)
-        reservation_object_fini(&bo->ttm_resv);
+    reservation_object_fini(&bo->ttm_resv);

When we always call reservation_object_fini() here we should probably also always call reservation_object_init() in ttm_bo_init_reserved() to make sure the object is always initialized.

Fair enough.

Quoted 2 lines

This way we can also remove the call to reservation_object_init() in ttm_bo_individualize_resv().

Both done in v2, thanks for the review and suggestions.

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#6 lore raw

PATCH v2 1/1 drm/ttm: Always and only destroy bo->ttm_resv in ttm_bo_release_list

In reply to a message not in the archive

From: Michel Dänzer <[email protected]>

Fixes a use-after-free due to a race condition in ttm_bo_cleanup_refs_and_unlock, which allows one task to reserve a BO and destroy its ttm_resv while another task is waiting for it to signal in reservation_object_wait_timeout_rcu.

v2:
* Always initialize bo->ttm_resv in ttm_bo_init_reserved (Christian König)

Fixes 0d2bd2ae045d "drm/ttm: fix memory leak while individualizing BOs"
---
 drivers/gpu/drm/ttm/ttm_bo.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +4 -12
index 379ec41d2c69..c088703777e2 100644
@@ -150,8 +150,7 @@ static void ttm_bo_release_list(struct kref *list_kref)
 	ttm_tt_destroy(bo->ttm);
 	atomic_dec(&bo->glob->bo_count);
 	dma_fence_put(bo->moving);
-	if (bo->resv == &bo->ttm_resv)
-		reservation_object_fini(&bo->ttm_resv);
+	reservation_object_fini(&bo->ttm_resv);
 	mutex_destroy(&bo->wu_mutex);
 	if (bo->destroy)
 		bo->destroy(bo);
@@ -402,14 +401,11 @@ static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
 	if (bo->resv == &bo->ttm_resv)
 		return 0;
 
-	reservation_object_init(&bo->ttm_resv);
 	BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
 
 	r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
-	if (r) {
+	if (r)
 		reservation_object_unlock(&bo->ttm_resv);
-		reservation_object_fini(&bo->ttm_resv);
-	}
 
 	return r;
 }
@@ -457,10 +453,8 @@ static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
 		if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
 			ttm_bo_del_from_lru(bo);
 			spin_unlock(&glob->lru_lock);
-			if (bo->resv != &bo->ttm_resv) {
+			if (bo->resv != &bo->ttm_resv)
 				reservation_object_unlock(&bo->ttm_resv);
-				reservation_object_fini(&bo->ttm_resv);
-			}
 
 			ttm_bo_cleanup_memtype_use(bo);
 			return;
@@ -560,8 +554,6 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
 	}
 
 	ttm_bo_del_from_lru(bo);
-	if (!list_empty(&bo->ddestroy) && (bo->resv != &bo->ttm_resv))
-		reservation_object_fini(&bo->ttm_resv);
 	list_del_init(&bo->ddestroy);
 	kref_put(&bo->list_kref, ttm_bo_ref_bug);
 
@@ -1210,8 +1202,8 @@ int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
 		lockdep_assert_held(&bo->resv->lock.base);
 	} else {
 		bo->resv = &bo->ttm_resv;
-		reservation_object_init(&bo->ttm_resv);
 	}
+	reservation_object_init(&bo->ttm_resv);
 	atomic_inc(&bo->glob->bo_count);
 	drm_vma_node_reset(&bo->vma_node);
 	bo->priority = 0;
Signature · 6 lines
2.15.0.rc2

_______________________________________________
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): 20171102172218.7827-1-michel-otUistvHUpPR7s880joybQ@public.gmane.org

Reviewed-by
#8 lore raw

Am 02.11.2017 um 18:22 schrieb Michel Dänzer:

Quoted 7 lines

From: Michel Dänzer <[email protected]>

Fixes a use-after-free due to a race condition in ttm_bo_cleanup_refs_and_unlock, which allows one task to reserve a BO and destroy its ttm_resv while another task is waiting for it to signal in reservation_object_wait_timeout_rcu.

Quoted 7 lines

v2:
* Always initialize bo->ttm_resv in ttm_bo_init_reserved (Christian König)

Fixes 0d2bd2ae045d "drm/ttm: fix memory leak while individualizing BOs"
Quoted 56 lines
---
  drivers/gpu/drm/ttm/ttm_bo.c | 16 ++++------------
  1 file changed, 4 insertions(+), 12 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +3 -11
index 379ec41d2c69..c088703777e2 100644
@@ -150,8 +150,7 @@ static void ttm_bo_release_list(struct kref *list_kref)
  	ttm_tt_destroy(bo->ttm);
  	atomic_dec(&bo->glob->bo_count);
  	dma_fence_put(bo->moving);
-	if (bo->resv == &bo->ttm_resv)
-		reservation_object_fini(&bo->ttm_resv);
+	reservation_object_fini(&bo->ttm_resv);
  	mutex_destroy(&bo->wu_mutex);
  	if (bo->destroy)
  		bo->destroy(bo);
@@ -402,14 +401,11 @@ static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
  	if (bo->resv == &bo->ttm_resv)
  		return 0;
 
-	reservation_object_init(&bo->ttm_resv);
  	BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
 
  	r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
-	if (r) {
+	if (r)
  		reservation_object_unlock(&bo->ttm_resv);
-		reservation_object_fini(&bo->ttm_resv);
-	}
 
  	return r;
  }
@@ -457,10 +453,8 @@ static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  		if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
  			ttm_bo_del_from_lru(bo);
  			spin_unlock(&glob->lru_lock);
-			if (bo->resv != &bo->ttm_resv) {
+			if (bo->resv != &bo->ttm_resv)
  				reservation_object_unlock(&bo->ttm_resv);
-				reservation_object_fini(&bo->ttm_resv);
-			}
 
  			ttm_bo_cleanup_memtype_use(bo);
  			return;
@@ -560,8 +554,6 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  	}
 
  	ttm_bo_del_from_lru(bo);
-	if (!list_empty(&bo->ddestroy) && (bo->resv != &bo->ttm_resv))
-		reservation_object_fini(&bo->ttm_resv);
  	list_del_init(&bo->ddestroy);
  	kref_put(&bo->list_kref, ttm_bo_ref_bug);
 

drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_init_reserved(struct ttm_bo_device *bdev, · line 1209

Quoted 9 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
@@ -1210,8 +1202,8 @@ int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
  		lockdep_assert_held(&bo->resv->lock.base);
  	} else {
  		bo->resv = &bo->ttm_resv;
-		reservation_object_init(&bo->ttm_resv);
  	}
+	reservation_object_init(&bo->ttm_resv);
  	atomic_inc(&bo->glob->bo_count);
  	drm_vma_node_reset(&bo->vma_node);
  	bo->priority = 0;

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