Move BOs which are currently in a lower domain to the new
LRU before allocating backing space while validating.

This makes sure that we always have enough entries on the
LRU to allow for other processes to wait for an operation
to complete.

v2: generalize the test

Signed-off-by: Christian König <christian.koe...@amd.com>
---
 drivers/gpu/drm/ttm/ttm_bo.c | 45 ++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index fd6dbebea430..4c6389d849ed 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -166,17 +166,17 @@ static void ttm_bo_release_list(struct kref *list_kref)
        ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
 }
 
-void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
+static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
+                                 struct ttm_mem_reg *mem)
 {
        struct ttm_bo_device *bdev = bo->bdev;
        struct ttm_mem_type_manager *man;
 
        reservation_object_assert_held(bo->resv);
+       BUG_ON(!list_empty(&bo->lru));
 
-       if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
-               BUG_ON(!list_empty(&bo->lru));
-
-               man = &bdev->man[bo->mem.mem_type];
+       if (!(mem->placement & TTM_PL_FLAG_NO_EVICT)) {
+               man = &bdev->man[mem->mem_type];
                list_add_tail(&bo->lru, &man->lru[bo->priority]);
                kref_get(&bo->list_kref);
 
@@ -188,6 +188,11 @@ void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
                }
        }
 }
+
+void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
+{
+       ttm_bo_add_mem_to_lru(bo, &bo->mem);
+}
 EXPORT_SYMBOL(ttm_bo_add_to_lru);
 
 static void ttm_bo_ref_bug(struct kref *list_kref)
@@ -1000,6 +1005,14 @@ static int ttm_bo_mem_placement(struct ttm_buffer_object 
*bo,
 
        mem->mem_type = mem_type;
        mem->placement = cur_flags;
+
+       if (bo->mem.mem_type < mem_type && !list_empty(&bo->lru)) {
+               spin_lock(&bo->bdev->glob->lru_lock);
+               ttm_bo_del_from_lru(bo);
+               ttm_bo_add_mem_to_lru(bo, mem);
+               spin_unlock(&bo->bdev->glob->lru_lock);
+       }
+
        return 0;
 }
 
@@ -1033,7 +1046,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
                if (ret == -EBUSY)
                        continue;
                if (ret)
-                       return ret;
+                       goto error;
 
                type_found = true;
                mem->mm_node = NULL;
@@ -1043,13 +1056,13 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
                man = &bdev->man[mem->mem_type];
                ret = (*man->func->get_node)(man, bo, place, mem);
                if (unlikely(ret))
-                       return ret;
+                       goto error;
 
                if (mem->mm_node) {
                        ret = ttm_bo_add_move_fence(bo, man, mem);
                        if (unlikely(ret)) {
                                (*man->func->put_node)(man, mem);
-                               return ret;
+                               goto error;
                        }
                        return 0;
                }
@@ -1062,7 +1075,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
                if (ret == -EBUSY)
                        continue;
                if (ret)
-                       return ret;
+                       goto error;
 
                type_found = true;
                mem->mm_node = NULL;
@@ -1074,15 +1087,23 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
                        return 0;
 
                if (ret && ret != -EBUSY)
-                       return ret;
+                       goto error;
        }
 
+       ret = -ENOMEM;
        if (!type_found) {
                pr_err(TTM_PFX "No compatible memory type found\n");
-               return -EINVAL;
+               ret = -EINVAL;
        }
 
-       return -ENOMEM;
+error:
+       if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
+               spin_lock(&bo->bdev->glob->lru_lock);
+               ttm_bo_move_to_lru_tail(bo, NULL);
+               spin_unlock(&bo->bdev->glob->lru_lock);
+       }
+
+       return ret;
 }
 EXPORT_SYMBOL(ttm_bo_mem_space);
 
-- 
2.17.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Reply via email to