Skip to contentSkip to conversation

drm/ttm: make unlocking in ttm_bo_cleanup_refs optional v2

PATCH In review Christian König 5 participants 8 messages Nov 13, 2017 – Nov 14, 2017 3 messages not in the archive

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

PATCH 1/2 drm/ttm: make unlocking in ttm_bo_cleanup_refs optional v2

Needed for the next patch.

v2: actually predicate all unlocks

---
 drivers/gpu/drm/ttm/ttm_bo.c | 59 +++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 26 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +33 -26
index db0f670911ec..691646c0f8d3 100644
@@ -486,20 +486,21 @@ static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
 }
 
 /**
- * function ttm_bo_cleanup_refs_and_unlock
+ * function ttm_bo_cleanup_refs
  * If bo idle, remove from delayed- and lru lists, and unref.
  * If not idle, do nothing.
  *
  * Must be called with lru_lock and reservation held, this function
- * will drop both before returning.
+ * will drop the lru lock and optionally the reservation lock before returning.
  *
  * @interruptible         Any sleeps should occur interruptibly.
  * @no_wait_gpu           Never wait for gpu. Return -EBUSY instead.
+ * @unlock_resv           Unlock the reservation lock as well.
  */
 
-static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
-					  bool interruptible,
-					  bool no_wait_gpu)
+static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
+			       bool interruptible, bool no_wait_gpu,
+			       bool unlock_resv)
 {
 	struct ttm_bo_global *glob = bo->glob;
 	struct reservation_object *resv;
@@ -518,7 +519,8 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
 	if (ret && !no_wait_gpu) {
 		long lret;
 
-		reservation_object_unlock(bo->resv);
+		if (unlock_resv)
+			reservation_object_unlock(bo->resv);
 		spin_unlock(&glob->lru_lock);
 
 		lret = reservation_object_wait_timeout_rcu(resv, true,
@@ -531,24 +533,28 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
 			return -EBUSY;
 
 		spin_lock(&glob->lru_lock);
-		ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
-
-		/*
-		 * We raced, and lost, someone else holds the reservation now,
-		 * and is probably busy in ttm_bo_cleanup_memtype_use.
-		 *
-		 * Even if it's not the case, because we finished waiting any
-		 * delayed destruction would succeed, so just return success
-		 * here.
-		 */
-		if (ret) {
-			spin_unlock(&glob->lru_lock);
-			return 0;
+		if (unlock_resv) {
+			ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
+			/*
+			 * We raced, and lost, someone else holds the reservation now,
+			 * and is probably busy in ttm_bo_cleanup_memtype_use.
+			 *
+			 * Even if it's not the case, because we finished waiting any
+			 * delayed destruction would succeed, so just return success
+			 * here.
+			 */
+			if (ret) {
+				spin_unlock(&glob->lru_lock);
+				return 0;
+			}
+		} else {
+			ret = 0;
 		}
 	}
 
 	if (ret || unlikely(list_empty(&bo->ddestroy))) {
-		reservation_object_unlock(bo->resv);
+		if (unlock_resv)
+			reservation_object_unlock(bo->resv);
 		spin_unlock(&glob->lru_lock);
 		return ret;
 	}
@@ -559,7 +565,9 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
 
 	spin_unlock(&glob->lru_lock);
 	ttm_bo_cleanup_memtype_use(bo);
-	reservation_object_unlock(bo->resv);
+
+	if (unlock_resv)
+		reservation_object_unlock(bo->resv);
 
 	return 0;
 }
@@ -600,8 +608,8 @@ static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
 		}
 
 		if (!ret)
-			ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
-							     !remove_all);
+			ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
+						  true);
 		else
 			spin_unlock(&glob->lru_lock);
 
@@ -770,8 +778,7 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 	kref_get(&bo->list_kref);
 
 	if (!list_empty(&bo->ddestroy)) {
-		ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
-						     no_wait_gpu);
+		ret = ttm_bo_cleanup_refs(bo, interruptible, no_wait_gpu, true);
 		kref_put(&bo->list_kref, ttm_bo_release_list);
 		return ret;
 	}
@@ -1735,7 +1742,7 @@ static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
 	kref_get(&bo->list_kref);
 
 	if (!list_empty(&bo->ddestroy)) {
-		ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
+		ret = ttm_bo_cleanup_refs(bo, false, false, true);
 		kref_put(&bo->list_kref, ttm_bo_release_list);
 		return ret;
 	}
Signature · 6 lines
2.11.0

_______________________________________________
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): 20171113095409.1904-1-christian.koenig-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 2/2 drm/ttm: optimize ttm_mem_evict_first v4

Deleted BOs with the same reservation object can be reaped even if they can't be reserved.

v2: rebase and we still need to remove/add the BO from/to the LRU.
v3: fix remove/add one more time, cleanup the logic a bit
v4: we should still check if the eviction is valuable

---
 drivers/gpu/drm/ttm/ttm_bo.c | 54 +++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 21 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +33 -21
index 691646c0f8d3..7b1525d39ea8 100644
@@ -738,47 +738,57 @@ bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
 
 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-				uint32_t mem_type,
-				const struct ttm_place *place,
-				bool interruptible,
-				bool no_wait_gpu)
+			       struct reservation_object *resv,
+			       uint32_t mem_type,
+			       const struct ttm_place *place,
+			       bool interruptible,
+			       bool no_wait_gpu)
 {
 	struct ttm_bo_global *glob = bdev->glob;
 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
-	struct ttm_buffer_object *bo;
-	int ret = -EBUSY;
+	struct ttm_buffer_object *bo = NULL;
+	bool locked = false;
 	unsigned i;
+	int ret;
 
 	spin_lock(&glob->lru_lock);
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
 		list_for_each_entry(bo, &man->lru[i], lru) {
-			ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
-			if (ret)
-				continue;
+			if (bo->resv == resv) {
+				if (list_empty(&bo->ddestroy))
+					continue;
+
+			} else {
+				locked = reservation_object_trylock(bo->resv);
+				if (!locked)
+					continue;
+			}
 
 			if (place && !bdev->driver->eviction_valuable(bo,
 								      place)) {
-				reservation_object_unlock(bo->resv);
-				ret = -EBUSY;
+				if (locked)
+					reservation_object_unlock(bo->resv);
 				continue;
 			}
-
 			break;
 		}
 
-		if (!ret)
+		if (&bo->lru != &man->lru[i])
 			break;
+		else
+			bo = NULL;
 	}
 
-	if (ret) {
+	if (!bo) {
 		spin_unlock(&glob->lru_lock);
-		return ret;
+		return -EBUSY;
 	}
 
 	kref_get(&bo->list_kref);
 
 	if (!list_empty(&bo->ddestroy)) {
-		ret = ttm_bo_cleanup_refs(bo, interruptible, no_wait_gpu, true);
+		ret = ttm_bo_cleanup_refs(bo, interruptible, no_wait_gpu,
+					  locked);
 		kref_put(&bo->list_kref, ttm_bo_release_list);
 		return ret;
 	}
@@ -786,10 +796,11 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 	ttm_bo_del_from_lru(bo);
 	spin_unlock(&glob->lru_lock);
 
-	BUG_ON(ret != 0);
-
 	ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
-	ttm_bo_unreserve(bo);
+	if (locked)
+		ttm_bo_unreserve(bo);
+	else
+		ttm_bo_add_to_lru(bo);
 
 	kref_put(&bo->list_kref, ttm_bo_release_list);
 	return ret;
@@ -853,7 +864,7 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
 			return ret;
 		if (mem->mm_node)
 			break;
-		ret = ttm_mem_evict_first(bdev, mem_type, place,
+		ret = ttm_mem_evict_first(bdev, bo->resv, mem_type, place,
 					  interruptible, no_wait_gpu);
 		if (unlikely(ret != 0))
 			return ret;
@@ -1356,7 +1367,8 @@ static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
 		while (!list_empty(&man->lru[i])) {
 			spin_unlock(&glob->lru_lock);
-			ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
+			ret = ttm_mem_evict_first(bdev, NULL, mem_type, NULL,
+						  false, false);
 			if (ret)
 				return ret;
 			spin_lock(&glob->lru_lock);
Signature · 6 lines
2.11.0

_______________________________________________
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): 20171113095409.1904-2-christian.koenig-5C7GfCeVMHo@public.gmane.org

#4 lore raw

Re: [PATCH 2/2] drm/ttm: optimize ttm_mem_evict_first v4

Quoted 38 lines · Christian König

Deleted BOs with the same reservation object can be reaped even if they can't be reserved.

v2: rebase and we still need to remove/add the BO from/to the LRU.
v3: fix remove/add one more time, cleanup the logic a bit
v4: we should still check if the eviction is valuable

---
  drivers/gpu/drm/ttm/ttm_bo.c | 54 +++++++++++++++++++++++++++-----------------
  1 file changed, 33 insertions(+), 21 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +8 -6
index 691646c0f8d3..7b1525d39ea8 100644
@@ -738,47 +738,57 @@ bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  EXPORT_SYMBOL(ttm_bo_eviction_valuable);
 
  static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-				uint32_t mem_type,
-				const struct ttm_place *place,
-				bool interruptible,
-				bool no_wait_gpu)
+			       struct reservation_object *resv,
+			       uint32_t mem_type,
+			       const struct ttm_place *place,
+			       bool interruptible,
+			       bool no_wait_gpu)
  {
  	struct ttm_bo_global *glob = bdev->glob;
  	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
-	struct ttm_buffer_object *bo;
-	int ret = -EBUSY;
+	struct ttm_buffer_object *bo = NULL;
+	bool locked = false;
  	unsigned i;
+	int ret;

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 759

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -3
 
  	spin_lock(&glob->lru_lock);
  	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  		list_for_each_entry(bo, &man->lru[i], lru) {
-			ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
-			if (ret)
-				continue;
+			if (bo->resv == resv) {
+				if (list_empty(&bo->ddestroy))
+					continue;

Do you have further patches for enabling eviction and swapout for allocation?

Regards,
David Zhou

Quoted 67 lines
drivers/gpu/drm/ttm/ttm_bo.c +20 -11
+
+			} else {
+				locked = reservation_object_trylock(bo->resv);
+				if (!locked)
+					continue;
+			}
 
  			if (place && !bdev->driver->eviction_valuable(bo,
  								      place)) {
-				reservation_object_unlock(bo->resv);
-				ret = -EBUSY;
+				if (locked)
+					reservation_object_unlock(bo->resv);
  				continue;
  			}
-
  			break;
  		}
 
-		if (!ret)
+		if (&bo->lru != &man->lru[i])
  			break;
+		else
+			bo = NULL;
  	}
 
-	if (ret) {
+	if (!bo) {
  		spin_unlock(&glob->lru_lock);
-		return ret;
+		return -EBUSY;
  	}
 
  	kref_get(&bo->list_kref);
 
  	if (!list_empty(&bo->ddestroy)) {
-		ret = ttm_bo_cleanup_refs(bo, interruptible, no_wait_gpu, true);
+		ret = ttm_bo_cleanup_refs(bo, interruptible, no_wait_gpu,
+					  locked);
  		kref_put(&bo->list_kref, ttm_bo_release_list);
  		return ret;
  	}
@@ -786,10 +796,11 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  	ttm_bo_del_from_lru(bo);
  	spin_unlock(&glob->lru_lock);
 
-	BUG_ON(ret != 0);
-
  	ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
-	ttm_bo_unreserve(bo);
+	if (locked)
+		ttm_bo_unreserve(bo);
+	else
+		ttm_bo_add_to_lru(bo);
 
  	kref_put(&bo->list_kref, ttm_bo_release_list);
  	return ret;
@@ -853,7 +864,7 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  			return ret;
  		if (mem->mm_node)
  			break;
-		ret = ttm_mem_evict_first(bdev, mem_type, place,
+		ret = ttm_mem_evict_first(bdev, bo->resv, mem_type, place,
  					  interruptible, no_wait_gpu);
  		if (unlikely(ret != 0))
  			return ret;

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, · line 1374

Quoted 9 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
@@ -1356,7 +1367,8 @@ static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  		while (!list_empty(&man->lru[i])) {
  			spin_unlock(&glob->lru_lock);
-			ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
+			ret = ttm_mem_evict_first(bdev, NULL, mem_type, NULL,
+						  false, false);
  			if (ret)
  				return ret;
  			spin_lock(&glob->lru_lock);

_______________________________________________
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): c9c1c66a-8488-36f2-a564-8147ea8022c2-5C7GfCeVMHo@public.gmane.org

#6 lore raw

Re: [PATCH 2/2] drm/ttm: optimize ttm_mem_evict_first v4

Am 14.11.2017 um 03:40 schrieb Chunming Zhou:

Quoted 44 lines · Christian König
Quoted 41 lines

Deleted BOs with the same reservation object can be reaped even if they can't be reserved.

v2: rebase and we still need to remove/add the BO from/to the LRU.
v3: fix remove/add one more time, cleanup the logic a bit
v4: we should still check if the eviction is valuable

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

  1 file changed, 33 insertions(+), 21 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +8 -6
index 691646c0f8d3..7b1525d39ea8 100644
@@ -738,47 +738,57 @@ bool ttm_bo_eviction_valuable(struct 
 ttm_buffer_object *bo,
   EXPORT_SYMBOL(ttm_bo_eviction_valuable);
     static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-                uint32_t mem_type,
-                const struct ttm_place *place,
-                bool interruptible,
-                bool no_wait_gpu)
+                   struct reservation_object *resv,
+                   uint32_t mem_type,
+                   const struct ttm_place *place,
+                   bool interruptible,
+                   bool no_wait_gpu)
   {
       struct ttm_bo_global *glob = bdev->glob;
       struct ttm_mem_type_manager *man = &bdev->man[mem_type];
-    struct ttm_buffer_object *bo;
-    int ret = -EBUSY;
+    struct ttm_buffer_object *bo = NULL;
+    bool locked = false;
       unsigned i;
+    int ret;
         spin_lock(&glob->lru_lock);
       for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 759

Quoted 7 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -3
           list_for_each_entry(bo, &man->lru[i], lru) {
-            ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
-            if (ret)
-                continue;
+            if (bo->resv == resv) {
+                if (list_empty(&bo->ddestroy))
+                    continue;

Do you have further patches for enabling eviction and swapout for allocation?

Yeah, going to work on this.

Basically we need to add a context here so that TTM knows what to do. I had patches for this already done, but never pushed them because time constrains.

But that will probably a few weeks before I have that ready.

Regards,
Christian.

Quoted 69 lines

Regards,
David Zhou

Quoted 66 lines
drivers/gpu/drm/ttm/ttm_bo.c +20 -8
+
+            } else {
+                locked = reservation_object_trylock(bo->resv);
+                if (!locked)
+                    continue;
+            }
                 if (place && !bdev->driver->eviction_valuable(bo,
                                         place)) {
-                reservation_object_unlock(bo->resv);
-                ret = -EBUSY;
+                if (locked)
+                    reservation_object_unlock(bo->resv);
                   continue;
               }
-
               break;
           }
   -        if (!ret)
+        if (&bo->lru != &man->lru[i])
               break;
+        else
+            bo = NULL;
       }
   -    if (ret) {
+    if (!bo) {
           spin_unlock(&glob->lru_lock);
-        return ret;
+        return -EBUSY;
       }
         kref_get(&bo->list_kref);
         if (!list_empty(&bo->ddestroy)) {
-        ret = ttm_bo_cleanup_refs(bo, interruptible, no_wait_gpu, 
 true);
+        ret = ttm_bo_cleanup_refs(bo, interruptible, no_wait_gpu,
+                      locked);
           kref_put(&bo->list_kref, ttm_bo_release_list);
           return ret;
       }
@@ -786,10 +796,11 @@ static int ttm_mem_evict_first(struct 
 ttm_bo_device *bdev,
       ttm_bo_del_from_lru(bo);
       spin_unlock(&glob->lru_lock);
   -    BUG_ON(ret != 0);
-
       ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
-    ttm_bo_unreserve(bo);
+    if (locked)
+        ttm_bo_unreserve(bo);
+    else
+        ttm_bo_add_to_lru(bo);
         kref_put(&bo->list_kref, ttm_bo_release_list);
       return ret;
@@ -853,7 +864,7 @@ static int ttm_bo_mem_force_space(struct 
 ttm_buffer_object *bo,
               return ret;
           if (mem->mm_node)
               break;
-        ret = ttm_mem_evict_first(bdev, mem_type, place,
+        ret = ttm_mem_evict_first(bdev, bo->resv, mem_type, place,
                         interruptible, no_wait_gpu);
           if (unlikely(ret != 0))

              return ret;

drivers/gpu/drm/ttm/ttm_bo.c +0 -0
@@ -1356,7 +1367,8 @@ static int ttm_bo_force_list_clean(struct 
 ttm_bo_device *bdev,
       for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
           while (!list_empty(&man->lru[i])) {
Quoted 9 lines
Quoted 8 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
               spin_unlock(&glob->lru_lock);
-            ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, 
 false);
+            ret = ttm_mem_evict_first(bdev, NULL, mem_type, NULL,
+                          false, false);
               if (ret)

                  return ret;
              spin_lock(&glob->lru_lock);

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

#7 lore raw

Re: [PATCH 2/2] drm/ttm: optimize ttm_mem_evict_first v4

In reply to Christian König
Quoted 8 lines · Christian König

Deleted BOs with the same reservation object can be reaped even if they can't be reserved.

v2: rebase and we still need to remove/add the BO from/to the LRU.
v3: fix remove/add one more time, cleanup the logic a bit
v4: we should still check if the eviction is valuable

[...]

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 760

Quoted 7 lines
drivers/gpu/drm/ttm/ttm_bo.c +4 -3
-			ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
-			if (ret)
-				continue;
+			if (bo->resv == resv) {
+				if (list_empty(&bo->ddestroy))
+					continue;
+

Superfluous blank line here.

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 770

Quoted 12 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -2
+			} else {
+				locked = reservation_object_trylock(bo->resv);
+				if (!locked)
+					continue;
+			}
 
 			if (place && !bdev->driver->eviction_valuable(bo,
 								      place)) {
-				reservation_object_unlock(bo->resv);
-				ret = -EBUSY;
+				if (locked)
+					reservation_object_unlock(bo->resv);

I think we need to always set bo = NULL before continue, otherwise we can end up trying to evict the very last BO even though we didn't consider it suitable for eviction (and if bdev->driver->eviction_valuable returned false, with locked = true even though we already unlocked bo->resv).

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 776

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
-		if (!ret)
+		if (&bo->lru != &man->lru[i])

I'm not sure what the purpose of this test is, can you explain?

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -0
 			break;
+		else
+			bo = NULL;
 	}
Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#8 lore raw

Re: [PATCH 2/2] drm/ttm: optimize ttm_mem_evict_first v4

Am 14.11.2017 um 11:02 schrieb Michel Dänzer:

Quoted 40 lines · Christian König
Quoted 8 lines

Deleted BOs with the same reservation object can be reaped even if they can't be reserved.

v2: rebase and we still need to remove/add the BO from/to the LRU.
v3: fix remove/add one more time, cleanup the logic a bit
v4: we should still check if the eviction is valuable

[...]

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 760

Quoted 7 lines
drivers/gpu/drm/ttm/ttm_bo.c +4 -3
-			ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
-			if (ret)
-				continue;
+			if (bo->resv == resv) {
+				if (list_empty(&bo->ddestroy))
+					continue;
+

Superfluous blank line here.

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 770

Quoted 12 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -2
+			} else {
+				locked = reservation_object_trylock(bo->resv);
+				if (!locked)
+					continue;
+			}
 
  			if (place && !bdev->driver->eviction_valuable(bo,
  								      place)) {
-				reservation_object_unlock(bo->resv);
-				ret = -EBUSY;
+				if (locked)
+					reservation_object_unlock(bo->resv);

I think we need to always set bo = NULL before continue, otherwise we can end up trying to evict the very last BO even though we didn't consider it suitable for eviction (and if bdev->driver->eviction_valuable returned false, with locked = true even though we already unlocked bo->resv).

Quoted 3 lines

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 776

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
-		if (!ret)
+		if (&bo->lru != &man->lru[i])

I'm not sure what the purpose of this test is, can you explain?

It prevents the case you described above.

bo is the element variable of the loop, so we can't set it to NULL inside the loop.

Instead we check after the loop if we reached the end of the list, if yes we set the bo variable to NULL, otherwise we have found a valid entry and can break out of the for loop.

To make it clearer we could also test "i" after the outer loop instead of bo for being NULL. That would at least be easier to understand I think.

Christian.

Quoted 7 lines

drivers/gpu/drm/ttm/ttm_bo.c bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, · line 780

Quoted 6 lines

break;

drivers/gpu/drm/ttm/ttm_bo.c +2 -0
+		else
+			bo = NULL;
  	}

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

#9 lore raw

Re: [PATCH 2/2] drm/ttm: optimize ttm_mem_evict_first v4

Quoted 21 lines · Christian König

Am 14.11.2017 um 11:02 schrieb Michel Dänzer:

Quoted 20 lines · Christian König
Quoted 12 lines

+            } else {
+                locked = reservation_object_trylock(bo->resv); +                if (!locked)
+                    continue;
+            }
                if (place && !bdev->driver->eviction_valuable(bo, place)) {
-                reservation_object_unlock(bo->resv);
-                ret = -EBUSY;
+                if (locked)
+                    reservation_object_unlock(bo->resv);

I think we need to always set bo = NULL before continue, otherwise we can end up trying to evict the very last BO even though we didn't consider it suitable for eviction (and if bdev->driver->eviction_valuable returned false, with locked = true even though we already unlocked bo->resv).

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

-        if (!ret)
+        if (&bo->lru != &man->lru[i])

I'm not sure what the purpose of this test is, can you explain?

It prevents the case you described above.

bo is the element variable of the loop, so we can't set it to NULL inside the loop.

Yeah, that was a brainfart. :)

Quoted 6 lines

Instead we check after the loop if we reached the end of the list, if yes we set the bo variable to NULL, otherwise we have found a valid entry and can break out of the for loop.

To make it clearer we could also test "i" after the outer loop instead of bo for being NULL. That would at least be easier to understand I think.

That would still require detecting early termination of the inner loop and breaking out of the outer loop, so it doesn't buy anything.

The clearest solution might be:

	for (i = 0; !bo && i < TTM_MAX_BO_PRIORITY; ++i) {
		struct ttm_buffer_object *candidate;

		list_for_each_entry(candidate, &man->lru[i], lru) {
			[if/continue using candidate instead of bo]

			bo = candidate;
			break;
		}
	}

	if (!bo) {
		[...]

Alternatively, this might clarify the test for early termination:

		/* If the inner loop terminated early, we have our candidate */
		if (&bo->lru != &man->lru[i])

break;

		bo = NULL;
	}

(could even move the bo = NULL to the end of the outer loop's for () line)

I'll leave it up to you which you choose, either way

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#10 lore raw
In reply to a message not in the archive
Quoted 5 lines · Christian König

Needed for the next patch.

v2: actually predicate all unlocks

[...]

Quoted 25 lines
drivers/gpu/drm/ttm/ttm_bo.c +8 -13
@@ -531,24 +533,28 @@ static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
 			return -EBUSY;
 
 		spin_lock(&glob->lru_lock);
-		ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
-
-		/*
-		 * We raced, and lost, someone else holds the reservation now,
-		 * and is probably busy in ttm_bo_cleanup_memtype_use.
-		 *
-		 * Even if it's not the case, because we finished waiting any
-		 * delayed destruction would succeed, so just return success
-		 * here.
-		 */
-		if (ret) {
-			spin_unlock(&glob->lru_lock);
-			return 0;
+		if (unlock_resv) {
+			ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
+			/*
+			 * We raced, and lost, someone else holds the reservation now,
+			 * and is probably busy in ttm_bo_cleanup_memtype_use.
+			 *
+			 * Even if it's not the case, because we finished waiting any
+			 * delayed destruction would succeed, so just return success

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

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +8 -0
+			 * here.
+			 */
+			if (ret) {
+				spin_unlock(&glob->lru_lock);
+				return 0;
+			}
+		} else {
+			ret = 0;
 		}
 	}

This could be simplified slightly:

		if (unlock_resv && !reservation_object_trylock(bo->resv)) {
			/*
			 * We raced, and lost, someone else holds the reservation now,
			 * and is probably busy in ttm_bo_cleanup_memtype_use.
			 *
			 * Even if it's not the case, because we finished waiting any
			 * delayed destruction would succeed, so just return success
			 * here.
			 */
			spin_unlock(&glob->lru_lock);
			return 0;
		}

		ret = 0;
	}

Either way, this patch is

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