Skip to contentSkip to conversation

drm/ttm: fix busy memory to fail other user v6

PATCH In review Chunming Zhou 8 participants 12 messages May 7, 2019 – May 8, 2019 6 messages not in the archive

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

PATCH 1/2 drm/ttm: fix busy memory to fail other user v6

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7
---
 drivers/gpu/drm/ttm/ttm_bo.c | 113 ++++++++++++++++++++++++++++++-----
 1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +97 -16
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
  * b. Otherwise, trylock it.
  */
 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
-			struct ttm_operation_ctx *ctx, bool *locked)
+			struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
 {
 	bool ret = false;
 
 	*locked = false;
+	if (busy)
+		*busy = false;
 	if (bo->resv == ctx->resv) {
 		reservation_object_assert_held(bo->resv);
 		if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
@@ -779,35 +781,45 @@ static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
 	} else {
 		*locked = reservation_object_trylock(bo->resv);
 		ret = *locked;
+		if (!ret && busy)
+			*busy = true;
 	}
 
 	return ret;
 }
 
-static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-			       uint32_t mem_type,
-			       const struct ttm_place *place,
-			       struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+			 struct ttm_mem_type_manager *man,
+			 const struct ttm_place *place,
+			 struct ttm_operation_ctx *ctx,
+			 struct ttm_buffer_object **first_bo,
+			 bool *locked)
 {
-	struct ttm_bo_global *glob = bdev->glob;
-	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
 	struct ttm_buffer_object *bo = NULL;
-	bool locked = false;
-	unsigned i;
-	int ret;
+	int i;
 
-	spin_lock(&glob->lru_lock);
+	if (first_bo)
+		*first_bo = NULL;
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
 		list_for_each_entry(bo, &man->lru[i], lru) {
-			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+			bool busy = false;
+			if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+							    &busy)) {
+				if (first_bo && !(*first_bo) && busy) {
+					ttm_bo_get(bo);
+					*first_bo = bo;
+				}
 				continue;
+			}
 
 			if (place && !bdev->driver->eviction_valuable(bo,
 								      place)) {
-				if (locked)
+				if (*locked)
 					reservation_object_unlock(bo->resv);
 				continue;
 			}
+
 			break;
 		}
 
@@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 		bo = NULL;
 	}
 
+	return bo;
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+			       uint32_t mem_type,
+			       const struct ttm_place *place,
+			       struct ttm_operation_ctx *ctx)
+{
+	struct ttm_bo_global *glob = bdev->glob;
+	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+	struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+	bool locked = false;
+	int ret;
+
+	spin_lock(&glob->lru_lock);
+	bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, &first_bo,
+				      &locked);
 	if (!bo) {
+		struct ttm_operation_ctx busy_ctx;
+
 		spin_unlock(&glob->lru_lock);
-		return -EBUSY;
+		/* check if other user occupy memory too long time */
+		if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+			if (first_bo)
+				ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
+		if (first_bo->resv == ctx->resv) {
+			ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
+		if (ctx->interruptible)
+			ret = ww_mutex_lock_interruptible(&first_bo->resv->lock,
+							  ctx->resv->lock.ctx);
+		else
+			ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+		if (ret) {
+			ttm_bo_put(first_bo);
+			return ret;
+		}
+		spin_lock(&glob->lru_lock);
+		/* previous busy resv lock is held by above, idle now,
+		 * so let them evictable.
+		 */
+		busy_ctx.interruptible = ctx->interruptible;
+		busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+		busy_ctx.resv	       = first_bo->resv;
+		busy_ctx.flags	       = TTM_OPT_FLAG_ALLOW_RES_EVICT;
+
+		bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+					      &locked);
+		if (bo && (bo->resv == first_bo->resv))
+			locked = true;
+		else if (bo)
+			ww_mutex_unlock(&first_bo->resv->lock);
+		if (!bo) {
+			spin_unlock(&glob->lru_lock);
+			ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
 	}
 
 	kref_get(&bo->list_kref);
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 		ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
 					  ctx->no_wait_gpu, locked);
 		kref_put(&bo->list_kref, ttm_bo_release_list);
+		if (first_bo)
+			ttm_bo_put(first_bo);
 		return ret;
 	}
 
 	ttm_bo_del_from_lru(bo);
 	spin_unlock(&glob->lru_lock);
+	if (first_bo)
+		ttm_bo_put(first_bo);
 
 	ret = ttm_bo_evict(bo, ctx);
 	if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
 {
 	struct ttm_bo_device *bdev = bo->bdev;
 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+	struct ttm_operation_ctx native_ctx = {
+		.interruptible = false,
+		.no_wait_gpu = false,
+		.resv = bo->resv,
+		.flags = 0
+	};
+	struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;
 	int ret;
 
 	do {
@@ -907,7 +987,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, ctx);
+		ret = ttm_mem_evict_first(bdev, mem_type, place, evict_ctx);
 		if (unlikely(ret != 0))
 			return ret;
 	} while (1);
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
 	spin_lock(&glob->lru_lock);
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
 		list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-			if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
+			if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+							   NULL)) {
 				ret = 0;
 				break;
 			}
Signature · 6 lines
2.17.1

_______________________________________________
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): [email protected]

#2 lore raw

PATCH 2/2 drm/amd/display: use ttm_eu_reserve_buffers instead of amdgpu_bo_reserve

add ticket for display bo, so that it can preempt busy bo.

Change-Id I9f031cdcc8267de00e819ae303baa0a52df8ebb9
---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 22 ++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +17 -5
index ac22f7351a42..8633d52e3fbe 100644
@@ -4176,6 +4176,9 @@ static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
 	struct amdgpu_device *adev;
 	struct amdgpu_bo *rbo;
 	struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
+	struct list_head list, duplicates;
+	struct ttm_validate_buffer tv;
+	struct ww_acquire_ctx ticket;
 	uint64_t tiling_flags;
 	uint32_t domain;
 	int r;
@@ -4192,9 +4195,18 @@ static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
 	obj = new_state->fb->obj[0];
 	rbo = gem_to_amdgpu_bo(obj);
 	adev = amdgpu_ttm_adev(rbo->tbo.bdev);
-	r = amdgpu_bo_reserve(rbo, false);
-	if (unlikely(r != 0))
+	INIT_LIST_HEAD(&list);
+	INIT_LIST_HEAD(&duplicates);
+
+	tv.bo = &rbo->tbo;
+	tv.num_shared = 1;
+	list_add(&tv.head, &list);
+
+	r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
+	if (r) {
+		dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
 		return r;
+	}
 
 	if (plane->type != DRM_PLANE_TYPE_CURSOR)
 		domain = amdgpu_display_supported_domains(adev);
@@ -4205,21 +4217,21 @@ static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
 	if (unlikely(r != 0)) {
 		if (r != -ERESTARTSYS)
 			DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
-		amdgpu_bo_unreserve(rbo);
+		ttm_eu_backoff_reservation(&ticket, &list);
 		return r;
 	}
 
 	r = amdgpu_ttm_alloc_gart(&rbo->tbo);
 	if (unlikely(r != 0)) {
 		amdgpu_bo_unpin(rbo);
-		amdgpu_bo_unreserve(rbo);
+		ttm_eu_backoff_reservation(&ticket, &list);
 		DRM_ERROR("%p bind failed\n", rbo);
 		return r;
 	}
 
 	amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
 
-	amdgpu_bo_unreserve(rbo);
+	ttm_eu_backoff_reservation(&ticket, &list);
 
 	afb->address = amdgpu_bo_gpu_offset(rbo);
 
Signature · 6 lines
2.17.1

_______________________________________________
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): [email protected]

#4 lore raw

Re: [PATCH 2/2] drm/amd/display: use ttm_eu_reserve_buffers instead of amdgpu_bo_reserve

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 26 lines

add ticket for display bo, so that it can preempt busy bo.

Change-Id I9f031cdcc8267de00e819ae303baa0a52df8ebb9
---
  .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 22 ++++++++++++++-----
  1 file changed, 17 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +3 -0
index ac22f7351a42..8633d52e3fbe 100644
@@ -4176,6 +4176,9 @@ static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
  	struct amdgpu_device *adev;
  	struct amdgpu_bo *rbo;
  	struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
+	struct list_head list, duplicates;
+	struct ttm_validate_buffer tv;
+	struct ww_acquire_ctx ticket;
  	uint64_t tiling_flags;
  	uint32_t domain;
  	int r;
@@ -4192,9 +4195,18 @@ static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
  	obj = new_state->fb->obj[0];
  	rbo = gem_to_amdgpu_bo(obj);
  	adev = amdgpu_ttm_adev(rbo->tbo.bdev);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c static int dm_plane_helper_prepare_fb(struct drm_plane *plane, · line 4205

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +8 -2
-	r = amdgpu_bo_reserve(rbo, false);
-	if (unlikely(r != 0))
+	INIT_LIST_HEAD(&list);
+	INIT_LIST_HEAD(&duplicates);
+
+	tv.bo = &rbo->tbo;
+	tv.num_shared = 1;
+	list_add(&tv.head, &list);
+
+	r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);

duplicates are superfluous and can be NULL in this case.

Apart from that the patch is Reviewed-by: Christian König <[email protected]>

Regards,
Christian.

Quoted 22 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +5 -2
+	if (r) {
+		dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
  		return r;
+	}
 
  	if (plane->type != DRM_PLANE_TYPE_CURSOR)
  		domain = amdgpu_display_supported_domains(adev);
@@ -4205,21 +4217,21 @@ static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
  	if (unlikely(r != 0)) {
  		if (r != -ERESTARTSYS)
  			DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
-		amdgpu_bo_unreserve(rbo);
+		ttm_eu_backoff_reservation(&ticket, &list);
  		return r;
  	}
 
  	r = amdgpu_ttm_alloc_gart(&rbo->tbo);
  	if (unlikely(r != 0)) {
  		amdgpu_bo_unpin(rbo);
-		amdgpu_bo_unreserve(rbo);
+		ttm_eu_backoff_reservation(&ticket, &list);
  		DRM_ERROR("%p bind failed\n", rbo);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c static int dm_plane_helper_prepare_fb(struct drm_plane *plane, · line 4237

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +1 -1
  		return r;
  	}
 
  	amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
 
-	amdgpu_bo_unreserve(rbo);
+	ttm_eu_backoff_reservation(&ticket, &list);
 
  	afb->address = amdgpu_bo_gpu_offset(rbo);
 

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

#5 lore raw
In reply to Chunming Zhou

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 72 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7
---
  drivers/gpu/drm/ttm/ttm_bo.c | 113 ++++++++++++++++++++++++++++++-----
  1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +13 -10
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
   * b. Otherwise, trylock it.
   */
  static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
-			struct ttm_operation_ctx *ctx, bool *locked)
+			struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
  {
  	bool ret = false;
 
  	*locked = false;
+	if (busy)
+		*busy = false;
  	if (bo->resv == ctx->resv) {
  		reservation_object_assert_held(bo->resv);
  		if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
@@ -779,35 +781,45 @@ static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
  	} else {
  		*locked = reservation_object_trylock(bo->resv);
  		ret = *locked;
+		if (!ret && busy)
+			*busy = true;
  	}
 
  	return ret;
  }
 
-static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-			       uint32_t mem_type,
-			       const struct ttm_place *place,
-			       struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+			 struct ttm_mem_type_manager *man,
+			 const struct ttm_place *place,
+			 struct ttm_operation_ctx *ctx,
+			 struct ttm_buffer_object **first_bo,
+			 bool *locked)
  {
-	struct ttm_bo_global *glob = bdev->glob;
-	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  	struct ttm_buffer_object *bo = NULL;
-	bool locked = false;
-	unsigned i;
-	int ret;
+	int i;

drivers/gpu/drm/ttm/ttm_bo.c static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo, · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +5 -2
 
-	spin_lock(&glob->lru_lock);
+	if (first_bo)
+		*first_bo = NULL;
  	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  		list_for_each_entry(bo, &man->lru[i], lru) {
-			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+			bool busy = false;
+			if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+							    &busy)) {

A newline between declaration and code please.

Quoted 103 lines
drivers/gpu/drm/ttm/ttm_bo.c +69 -2
+				if (first_bo && !(*first_bo) && busy) {
+					ttm_bo_get(bo);
+					*first_bo = bo;
+				}
  				continue;
+			}
 
  			if (place && !bdev->driver->eviction_valuable(bo,
  								      place)) {
-				if (locked)
+				if (*locked)
  					reservation_object_unlock(bo->resv);
  				continue;
  			}
+
  			break;
  		}
 
@@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  		bo = NULL;
  	}
 
+	return bo;
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+			       uint32_t mem_type,
+			       const struct ttm_place *place,
+			       struct ttm_operation_ctx *ctx)
+{
+	struct ttm_bo_global *glob = bdev->glob;
+	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+	struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+	bool locked = false;
+	int ret;
+
+	spin_lock(&glob->lru_lock);
+	bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, &first_bo,
+				      &locked);
  	if (!bo) {
+		struct ttm_operation_ctx busy_ctx;
+
  		spin_unlock(&glob->lru_lock);
-		return -EBUSY;
+		/* check if other user occupy memory too long time */
+		if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+			if (first_bo)
+				ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
+		if (first_bo->resv == ctx->resv) {
+			ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
+		if (ctx->interruptible)
+			ret = ww_mutex_lock_interruptible(&first_bo->resv->lock,
+							  ctx->resv->lock.ctx);
+		else
+			ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+		if (ret) {
+			ttm_bo_put(first_bo);
+			return ret;
+		}
+		spin_lock(&glob->lru_lock);
+		/* previous busy resv lock is held by above, idle now,
+		 * so let them evictable.
+		 */
+		busy_ctx.interruptible = ctx->interruptible;
+		busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+		busy_ctx.resv	       = first_bo->resv;
+		busy_ctx.flags	       = TTM_OPT_FLAG_ALLOW_RES_EVICT;
+
+		bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+					      &locked);
+		if (bo && (bo->resv == first_bo->resv))
+			locked = true;
+		else if (bo)
+			ww_mutex_unlock(&first_bo->resv->lock);
+		if (!bo) {
+			spin_unlock(&glob->lru_lock);
+			ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
  	}
 
  	kref_get(&bo->list_kref);
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  		ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
  					  ctx->no_wait_gpu, locked);
  		kref_put(&bo->list_kref, ttm_bo_release_list);
+		if (first_bo)
+			ttm_bo_put(first_bo);
  		return ret;
  	}
 
  	ttm_bo_del_from_lru(bo);
  	spin_unlock(&glob->lru_lock);
+	if (first_bo)
+		ttm_bo_put(first_bo);
 
  	ret = ttm_bo_evict(bo, ctx);
  	if (locked) {

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

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -0
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  {
  	struct ttm_bo_device *bdev = bo->bdev;
  	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+	struct ttm_operation_ctx native_ctx = {
+		.interruptible = false,
+		.no_wait_gpu = false,
+		.resv = bo->resv,
+		.flags = 0
+	};
+	struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that I remember that incorrectly.

Christian.

Quoted 13 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
  	int ret;
 
  	do {
@@ -907,7 +987,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, ctx);
+		ret = ttm_mem_evict_first(bdev, mem_type, place, evict_ctx);
  		if (unlikely(ret != 0))
  			return ret;
  	} while (1);

drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx) · line 1871

Quoted 9 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
  	spin_lock(&glob->lru_lock);
  	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  		list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-			if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
+			if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+							   NULL)) {
  				ret = 0;
  				break;
  			}

_______________________________________________
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): f4b1ddf2-b80b-260e-54c9-b0e62ecbe90b-5C7GfCeVMHo@public.gmane.org

#7 lore raw
Quoted 185 lines · Christian

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 72 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7
---
   drivers/gpu/drm/ttm/ttm_bo.c | 113 ++++++++++++++++++++++++++++++-----
   1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +13 -10
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
    * b. Otherwise, trylock it.
    */
   static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
-			struct ttm_operation_ctx *ctx, bool *locked)
+			struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
   {
   	bool ret = false;
 
   	*locked = false;
+	if (busy)
+		*busy = false;
   	if (bo->resv == ctx->resv) {
   		reservation_object_assert_held(bo->resv);
   		if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
@@ -779,35 +781,45 @@ static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
   	} else {
   		*locked = reservation_object_trylock(bo->resv);
   		ret = *locked;
+		if (!ret && busy)
+			*busy = true;
   	}
 
   	return ret;
   }
 
-static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-			       uint32_t mem_type,
-			       const struct ttm_place *place,
-			       struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+			 struct ttm_mem_type_manager *man,
+			 const struct ttm_place *place,
+			 struct ttm_operation_ctx *ctx,
+			 struct ttm_buffer_object **first_bo,
+			 bool *locked)
   {
-	struct ttm_bo_global *glob = bdev->glob;
-	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
   	struct ttm_buffer_object *bo = NULL;
-	bool locked = false;
-	unsigned i;
-	int ret;
+	int i;

drivers/gpu/drm/ttm/ttm_bo.c static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo, · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +5 -2
 
-	spin_lock(&glob->lru_lock);
+	if (first_bo)
+		*first_bo = NULL;
   	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
   		list_for_each_entry(bo, &man->lru[i], lru) {
-			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+			bool busy = false;
+			if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+							    &busy)) {

A newline between declaration and code please.

Quoted 100 lines
drivers/gpu/drm/ttm/ttm_bo.c +69 -2
+				if (first_bo && !(*first_bo) && busy) {
+					ttm_bo_get(bo);
+					*first_bo = bo;
+				}
   				continue;
+			}
 
   			if (place && !bdev->driver->eviction_valuable(bo,
   								      place)) {
-				if (locked)
+				if (*locked)
   					reservation_object_unlock(bo->resv);
   				continue;
   			}
+
   			break;
   		}
 
@@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
   		bo = NULL;
   	}
 
+	return bo;
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+			       uint32_t mem_type,
+			       const struct ttm_place *place,
+			       struct ttm_operation_ctx *ctx)
+{
+	struct ttm_bo_global *glob = bdev->glob;
+	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+	struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+	bool locked = false;
+	int ret;
+
+	spin_lock(&glob->lru_lock);
+	bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, &first_bo,
+				      &locked);
   	if (!bo) {
+		struct ttm_operation_ctx busy_ctx;
+
   		spin_unlock(&glob->lru_lock);
-		return -EBUSY;
+		/* check if other user occupy memory too long time */
+		if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+			if (first_bo)
+				ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
+		if (first_bo->resv == ctx->resv) {
+			ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
+		if (ctx->interruptible)
+			ret = ww_mutex_lock_interruptible(&first_bo->resv->lock,
+							  ctx->resv->lock.ctx);
+		else
+			ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+		if (ret) {
+			ttm_bo_put(first_bo);
+			return ret;
+		}
+		spin_lock(&glob->lru_lock);
+		/* previous busy resv lock is held by above, idle now,
+		 * so let them evictable.
+		 */
+		busy_ctx.interruptible = ctx->interruptible;
+		busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+		busy_ctx.resv	       = first_bo->resv;
+		busy_ctx.flags	       = TTM_OPT_FLAG_ALLOW_RES_EVICT;
+
+		bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+					      &locked);
+		if (bo && (bo->resv == first_bo->resv))
+			locked = true;
+		else if (bo)
+			ww_mutex_unlock(&first_bo->resv->lock);
+		if (!bo) {
+			spin_unlock(&glob->lru_lock);
+			ttm_bo_put(first_bo);
+			return -EBUSY;
+		}
   	}
 
   	kref_get(&bo->list_kref);
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
   		ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
   					  ctx->no_wait_gpu, locked);
   		kref_put(&bo->list_kref, ttm_bo_release_list);
+		if (first_bo)
+			ttm_bo_put(first_bo);
   		return ret;
   	}
 
   	ttm_bo_del_from_lru(bo);
   	spin_unlock(&glob->lru_lock);
+	if (first_bo)
+		ttm_bo_put(first_bo);
 
Quoted 15 lines

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

Quoted 13 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -0
   	ret = ttm_bo_evict(bo, ctx);
   	if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
   {
   	struct ttm_bo_device *bdev = bo->bdev;
   	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+	struct ttm_operation_ctx native_ctx = {
+		.interruptible = false,
+		.no_wait_gpu = false,
+		.resv = bo->resv,
+		.flags = 0
+	};
+	struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that I remember that incorrectly.

Prike said he see ctx->resv is null, in that case, code doesn't run into busy path.
Oh, as you mentioned here, we need add .resv=bo->resv for every ttm_operation_ctx. That's a huge change which will cross all vendor drivers.

Can we just force to evaluate evict_ctx->resv = bo->resv? That means we just add one extra line: evict_ctx->resv = bo->resv. How about that?

-David

Quoted 17 lines

Christian.

Quoted 14 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
   	int ret;
 
   	do {
@@ -907,7 +987,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, ctx);
+		ret = ttm_mem_evict_first(bdev, mem_type, place, evict_ctx);
   		if (unlikely(ret != 0))
   			return ret;
   	} while (1);
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
   	spin_lock(&glob->lru_lock);
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx) · line 1871

Quoted 8 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
   	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
   		list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-			if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
+			if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+							   NULL)) {
   				ret = 0;
   				break;
   			}

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

#8 lore raw

Am 07.05.19 um 13:08 schrieb zhoucm1:

Quoted 211 lines · Christian
Quoted 200 lines

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 81 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7

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

   1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
     * b. Otherwise, trylock it.
     */

   static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
-            struct ttm_operation_ctx *ctx, bool *locked) +            struct ttm_operation_ctx *ctx, bool *locked, bool *busy) {
       bool ret = false;
          *locked = false;
+    if (busy)
+        *busy = false;
       if (bo->resv == ctx->resv) { reservation_object_assert_held(bo->resv);
           if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT

drivers/gpu/drm/ttm/ttm_bo.c +9 -8
@@ -779,35 +781,45 @@ static bool 
 ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
        } else {
            *locked = reservation_object_trylock(bo->resv);
            ret = *locked;
+        if (!ret && busy)
+            *busy = true;
        }
           return ret;
    }
    -static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-                   uint32_t mem_type,
-                   const struct ttm_place *place,
-                   struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+             struct ttm_mem_type_manager *man,
+             const struct ttm_place *place,
+             struct ttm_operation_ctx *ctx,
+             struct ttm_buffer_object **first_bo,
+             bool *locked)
    {
-    struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
        struct ttm_buffer_object *bo = NULL;
-    bool locked = false;
-    unsigned i;
-    int ret;

drivers/gpu/drm/ttm/ttm_bo.c static bool · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -1
+    int i;
    -    spin_lock(&glob->lru_lock);
+    if (first_bo)
+        *first_bo = NULL;
        for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
            list_for_each_entry(bo, &man->lru[i], lru) {
-            if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+            bool busy = false;
+            if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+                                &busy)) {

A newline between declaration and code please.

Quoted 106 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -1
+                if (first_bo && !(*first_bo) && busy) {
+                    ttm_bo_get(bo);
+                    *first_bo = bo;
+                }
                    continue;
+            }
                   if (place && !bdev->driver->eviction_valuable(bo,
                                          place)) {
-                if (locked)
+                if (*locked)
                        reservation_object_unlock(bo->resv);
                    continue;
                }
+
                break;
            }

   @@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
           bo = NULL;
       }
   +    return bo;

drivers/gpu/drm/ttm/ttm_bo.c +16 -0
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+                   uint32_t mem_type,
+                   const struct ttm_place *place,
+                   struct ttm_operation_ctx *ctx)
+{
+    struct ttm_bo_global *glob = bdev->glob;
+    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+    bool locked = false;
+    int ret;
+
+    spin_lock(&glob->lru_lock);
+    bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, &first_bo,
+                      &locked);

       if (!bo) {
+        struct ttm_operation_ctx busy_ctx;
+
           spin_unlock(&glob->lru_lock);
-        return -EBUSY;
+        /* check if other user occupy memory too long time */ +        if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) { +            if (first_bo)
+                ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (first_bo->resv == ctx->resv) {
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (ctx->interruptible)
+            ret = ww_mutex_lock_interruptible(&first_bo->resv->lock, +                              ctx->resv->lock.ctx);
+        else
+            ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+        if (ret) {
+            ttm_bo_put(first_bo);
+            return ret;
+        }
+        spin_lock(&glob->lru_lock);
+        /* previous busy resv lock is held by above, idle now, +         * so let them evictable.
+         */
+        busy_ctx.interruptible = ctx->interruptible;
+        busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+        busy_ctx.resv           = first_bo->resv;
+        busy_ctx.flags           = TTM_OPT_FLAG_ALLOW_RES_EVICT; +
+        bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+                          &locked);
+        if (bo && (bo->resv == first_bo->resv))
+            locked = true;
+        else if (bo)
+            ww_mutex_unlock(&first_bo->resv->lock);
+        if (!bo) {
+            spin_unlock(&glob->lru_lock);
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
       }
          kref_get(&bo->list_kref);

drivers/gpu/drm/ttm/ttm_bo.c +5 -0
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct 
 ttm_bo_device *bdev,
            ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
                          ctx->no_wait_gpu, locked);
            kref_put(&bo->list_kref, ttm_bo_release_list);
+        if (first_bo)
+            ttm_bo_put(first_bo);
            return ret;
        }
           ttm_bo_del_from_lru(bo);
        spin_unlock(&glob->lru_lock);
+    if (first_bo)
+        ttm_bo_put(first_bo);
           ret = ttm_bo_evict(bo, ctx);
        if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct 
 ttm_buffer_object *bo,
    {
        struct ttm_bo_device *bdev = bo->bdev;
        struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_operation_ctx native_ctx = {
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -0
+        .interruptible = false,
+        .no_wait_gpu = false,
+        .resv = bo->resv,
+        .flags = 0
+    };
+    struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that I remember that incorrectly.

Quoted 8 lines

Prike said he see ctx->resv is null, in that case, code doesn't run into busy path.
Oh, as you mentioned here, we need add .resv=bo->resv for every ttm_operation_ctx. That's a huge change which will cross all vendor drivers.

Can we just force to evaluate evict_ctx->resv = bo->resv? That means we just add one extra line: evict_ctx->resv = bo->resv. How about that?

Well only if ctx->resv is NULL, otherwise we would overwrite some reservation context given by the driver.

Probably better to give the acquir_ctx as separate parameter to ttm_mem_evict_first().

Christian.

Quoted 20 lines

-David

Quoted 18 lines

Christian.

Quoted 15 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
        int ret;
           do {
@@ -907,7 +987,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, ctx);
+        ret = ttm_mem_evict_first(bdev, mem_type, place, evict_ctx);
            if (unlikely(ret != 0))
                return ret;

       } while (1);

drivers/gpu/drm/ttm/ttm_bo.c +0 -0
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global *glob, 
 struct ttm_operation_ctx *ctx)
        spin_lock(&glob->lru_lock);
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
        for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
            list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
+            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+                               NULL)) {
                    ret = 0;
                    break;

               }

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

#9 lore raw
Quoted 217 lines · Christian

Am 07.05.19 um 13:08 schrieb zhoucm1:

Quoted 216 lines · Christian
Quoted 200 lines

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 81 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7
---
    drivers/gpu/drm/ttm/ttm_bo.c | 113
++++++++++++++++++++++++++++++-----
    1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +12 -9
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
     * b. Otherwise, trylock it.
     */
    static bool ttm_bo_evict_swapout_allowable(struct
 ttm_buffer_object *bo,
-            struct ttm_operation_ctx *ctx, bool *locked)
+            struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
    {
        bool ret = false;
           *locked = false;
+    if (busy)
+        *busy = false;
        if (bo->resv == ctx->resv) {
            reservation_object_assert_held(bo->resv);
            if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
@@ -779,35 +781,45 @@ static bool
 ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
        } else {
            *locked = reservation_object_trylock(bo->resv);
            ret = *locked;
+        if (!ret && busy)
+            *busy = true;
        }
           return ret;
    }
    -static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-                   uint32_t mem_type,
-                   const struct ttm_place *place,
-                   struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+             struct ttm_mem_type_manager *man,
+             const struct ttm_place *place,
+             struct ttm_operation_ctx *ctx,
+             struct ttm_buffer_object **first_bo,
+             bool *locked)
    {
-    struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
        struct ttm_buffer_object *bo = NULL;
-    bool locked = false;
-    unsigned i;
-    int ret;

drivers/gpu/drm/ttm/ttm_bo.c static bool · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -1
+    int i;
    -    spin_lock(&glob->lru_lock);
+    if (first_bo)
+        *first_bo = NULL;
        for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
            list_for_each_entry(bo, &man->lru[i], lru) {
-            if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+            bool busy = false;
+            if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+                                &busy)) {

A newline between declaration and code please.

Quoted 106 lines
drivers/gpu/drm/ttm/ttm_bo.c +40 -2
+                if (first_bo && !(*first_bo) && busy) {
+                    ttm_bo_get(bo);
+                    *first_bo = bo;
+                }
                    continue;
+            }
                   if (place && !bdev->driver->eviction_valuable(bo,
                                          place)) {
-                if (locked)
+                if (*locked)
                        reservation_object_unlock(bo->resv);
                    continue;
                }
+
                break;
            }
    @@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct
 ttm_bo_device *bdev,
            bo = NULL;
        }
    +    return bo;
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+                   uint32_t mem_type,
+                   const struct ttm_place *place,
+                   struct ttm_operation_ctx *ctx)
+{
+    struct ttm_bo_global *glob = bdev->glob;
+    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+    bool locked = false;
+    int ret;
+
+    spin_lock(&glob->lru_lock);
+    bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, &first_bo,
+                      &locked);
        if (!bo) {
+        struct ttm_operation_ctx busy_ctx;
+
            spin_unlock(&glob->lru_lock);
-        return -EBUSY;
+        /* check if other user occupy memory too long time */
+        if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+            if (first_bo)
+                ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (first_bo->resv == ctx->resv) {
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (ctx->interruptible)
+            ret = ww_mutex_lock_interruptible(&first_bo->resv->lock,
+                              ctx->resv->lock.ctx);
+        else
+            ret = ww_mutex_lock(&first_bo->resv->lock,

ctx->resv->lock.ctx);
+        if (ret) {
+            ttm_bo_put(first_bo);
+            return ret;
+        }
+        spin_lock(&glob->lru_lock);
+        /* previous busy resv lock is held by above, idle now, +         * so let them evictable.
+         */
+        busy_ctx.interruptible = ctx->interruptible;
+        busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+        busy_ctx.resv           = first_bo->resv;
+        busy_ctx.flags           = TTM_OPT_FLAG_ALLOW_RES_EVICT; +
+        bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+                          &locked);
+        if (bo && (bo->resv == first_bo->resv))
+            locked = true;
+        else if (bo)
+            ww_mutex_unlock(&first_bo->resv->lock);
+        if (!bo) {
+            spin_unlock(&glob->lru_lock);
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
       }
          kref_get(&bo->list_kref);

drivers/gpu/drm/ttm/ttm_bo.c +5 -0
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct
 ttm_bo_device *bdev,
            ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
                          ctx->no_wait_gpu, locked);
            kref_put(&bo->list_kref, ttm_bo_release_list);
+        if (first_bo)
+            ttm_bo_put(first_bo);
            return ret;
        }
           ttm_bo_del_from_lru(bo);
        spin_unlock(&glob->lru_lock);
+    if (first_bo)
+        ttm_bo_put(first_bo);
           ret = ttm_bo_evict(bo, ctx);
        if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct
 ttm_buffer_object *bo,
    {
        struct ttm_bo_device *bdev = bo->bdev;
        struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_operation_ctx native_ctx = {
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -0
+        .interruptible = false,
+        .no_wait_gpu = false,
+        .resv = bo->resv,
+        .flags = 0
+    };
+    struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that I remember that incorrectly.

Prike said he see ctx->resv is null, in that case, code doesn't run into busy path.
Oh, as you mentioned here, we need add .resv=bo->resv for every ttm_operation_ctx. That's a huge change which will cross all vendor drivers.

Quoted 7 lines
Quoted 2 lines

Can we just force to evaluate evict_ctx->resv = bo->resv? That means we just add one extra line: evict_ctx->resv = bo->resv. How about that?

Well only if ctx->resv is NULL, otherwise we would overwrite some reservation context given by the driver.

Probably better to give the acquir_ctx as separate parameter to ttm_mem_evict_first().

still put acquire_ctx into ttm_operation_ctx? Then that's same ctx->resv.
Current problem is we don't pass resv anywhere except ALLOW_EVICT case.
If you have concern for overwritten, we have to do ".resv = bo->resv" in every ttm_operation_ctx definitions.

-David

Quoted 21 lines

Christian.

Quoted 18 lines

-David

Quoted 17 lines

Christian.

Quoted 15 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
        int ret;
           do {
@@ -907,7 +987,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, ctx);
+        ret = ttm_mem_evict_first(bdev, mem_type, place, evict_ctx);
            if (unlikely(ret != 0))
                return ret;

       } while (1);

drivers/gpu/drm/ttm/ttm_bo.c +0 -0
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global *glob,
 struct ttm_operation_ctx *ctx)
        spin_lock(&glob->lru_lock);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
        for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
            list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
+            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+                               NULL)) {
                    ret = 0;
                    break;

               }

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

#10 lore raw

Am 07.05.19 um 13:22 schrieb zhoucm1:

Quoted 224 lines · Christian
Quoted 221 lines

Am 07.05.19 um 13:08 schrieb zhoucm1:

Quoted 220 lines · Christian
Quoted 202 lines

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 82 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7

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

    1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
      * b. Otherwise, trylock it.
      */

    static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
-            struct ttm_operation_ctx *ctx, bool *locked) +            struct ttm_operation_ctx *ctx, bool *locked, bool *busy) {
        bool ret = false;
           *locked = false;
+    if (busy)
+        *busy = false;
        if (bo->resv == ctx->resv) { reservation_object_assert_held(bo->resv);
            if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT

drivers/gpu/drm/ttm/ttm_bo.c +9 -8
@@ -779,35 +781,45 @@ static bool
 ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
         } else {
             *locked = reservation_object_trylock(bo->resv);
             ret = *locked;
+        if (!ret && busy)
+            *busy = true;
         }
            return ret;
     }
     -static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-                   uint32_t mem_type,
-                   const struct ttm_place *place,
-                   struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+             struct ttm_mem_type_manager *man,
+             const struct ttm_place *place,
+             struct ttm_operation_ctx *ctx,
+             struct ttm_buffer_object **first_bo,
+             bool *locked)
     {
-    struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
         struct ttm_buffer_object *bo = NULL;
-    bool locked = false;
-    unsigned i;
-    int ret;

drivers/gpu/drm/ttm/ttm_bo.c static bool · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -1
+    int i;
     -    spin_lock(&glob->lru_lock);
+    if (first_bo)
+        *first_bo = NULL;
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &man->lru[i], lru) {
-            if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+            bool busy = false;
+            if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+                                &busy)) {

A newline between declaration and code please.

Quoted 107 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -1
+                if (first_bo && !(*first_bo) && busy) {
+                    ttm_bo_get(bo);
+                    *first_bo = bo;
+                }
                     continue;
+            }
                    if (place && !bdev->driver->eviction_valuable(bo,
                                           place)) {
-                if (locked)
+                if (*locked)
 reservation_object_unlock(bo->resv);
                     continue;
                 }
+
                 break;
             }

    @@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
            bo = NULL;
        }
    +    return bo;

drivers/gpu/drm/ttm/ttm_bo.c +16 -0
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+                   uint32_t mem_type,
+                   const struct ttm_place *place,
+                   struct ttm_operation_ctx *ctx)
+{
+    struct ttm_bo_global *glob = bdev->glob;
+    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+    bool locked = false;
+    int ret;
+
+    spin_lock(&glob->lru_lock);
+    bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, &first_bo,
+                      &locked);

        if (!bo) {
+        struct ttm_operation_ctx busy_ctx;
+
            spin_unlock(&glob->lru_lock);
-        return -EBUSY;
+        /* check if other user occupy memory too long time */ +        if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+            if (first_bo)
+                ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (first_bo->resv == ctx->resv) {
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (ctx->interruptible)
+            ret = ww_mutex_lock_interruptible(&first_bo->resv->lock, +                              ctx->resv->lock.ctx);
+        else
+            ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+        if (ret) {
+            ttm_bo_put(first_bo);
+            return ret;
+        }
+        spin_lock(&glob->lru_lock);
+        /* previous busy resv lock is held by above, idle now, +         * so let them evictable.
+         */
+        busy_ctx.interruptible = ctx->interruptible;
+        busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+        busy_ctx.resv           = first_bo->resv;
+        busy_ctx.flags           = TTM_OPT_FLAG_ALLOW_RES_EVICT; +
+        bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+                          &locked);
+        if (bo && (bo->resv == first_bo->resv))
+            locked = true;
+        else if (bo)
+ ww_mutex_unlock(&first_bo->resv->lock);
+        if (!bo) {
+            spin_unlock(&glob->lru_lock);
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
        }
           kref_get(&bo->list_kref);

drivers/gpu/drm/ttm/ttm_bo.c +5 -0
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct
 ttm_bo_device *bdev,
             ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
                           ctx->no_wait_gpu, locked);
             kref_put(&bo->list_kref, ttm_bo_release_list);
+        if (first_bo)
+            ttm_bo_put(first_bo);
             return ret;
         }
            ttm_bo_del_from_lru(bo);
         spin_unlock(&glob->lru_lock);
+    if (first_bo)
+        ttm_bo_put(first_bo);
            ret = ttm_bo_evict(bo, ctx);
         if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct
 ttm_buffer_object *bo,
     {
         struct ttm_bo_device *bdev = bo->bdev;
         struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_operation_ctx native_ctx = {
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -0
+        .interruptible = false,
+        .no_wait_gpu = false,
+        .resv = bo->resv,
+        .flags = 0
+    };
+    struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that I remember that incorrectly.

Prike said he see ctx->resv is null, in that case, code doesn't run into busy path.
Oh, as you mentioned here, we need add .resv=bo->resv for every ttm_operation_ctx. That's a huge change which will cross all vendor drivers.

Can we just force to evaluate evict_ctx->resv = bo->resv? That means we just add one extra line: evict_ctx->resv = bo->resv. How about that?

Quoted 8 lines
Quoted 5 lines

Well only if ctx->resv is NULL, otherwise we would overwrite some reservation context given by the driver.

Probably better to give the acquir_ctx as separate parameter to ttm_mem_evict_first().

still put acquire_ctx into ttm_operation_ctx? Then that's same ctx->resv.
Current problem is we don't pass resv anywhere except ALLOW_EVICT case.
If you have concern for overwritten, we have to do ".resv = bo->resv" in every ttm_operation_ctx definitions.

No, what I mean is to add the acquire_ctx as separate parameter to ttm_mem_evict_first().

E.g. we only need it in this function and it is actually not related to the ttm operation context filled in by the driver.

Christian.

Quoted 28 lines

-David

Quoted 26 lines

Christian.

Quoted 23 lines

-David

Quoted 22 lines

Christian.

Quoted 20 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
         int ret;
            do {
@@ -907,7 +987,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, ctx);
+        ret = ttm_mem_evict_first(bdev, mem_type, place, evict_ctx);
             if (unlikely(ret != 0))
                 return ret;

        } while (1);

drivers/gpu/drm/ttm/ttm_bo.c +2 -1
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global *glob,
 struct ttm_operation_ctx *ctx)
         spin_lock(&glob->lru_lock);
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
+            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+                               NULL)) {
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
                     ret = 0;
                     break;

                }

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

_______________________________________________
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): 968487eb-f78e-9922-a073-8ed08111e307-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#12 lore raw
Quoted 232 lines · Christian König

Am 07.05.19 um 13:22 schrieb zhoucm1:

Quoted 231 lines · Christian
Quoted 228 lines

Am 07.05.19 um 13:08 schrieb zhoucm1:

Quoted 214 lines · Christian
Quoted 204 lines

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 83 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7

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

    1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
      * b. Otherwise, trylock it.
      */

    static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
-            struct ttm_operation_ctx *ctx, bool *locked) +            struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
    {
        bool ret = false;
           *locked = false;
+    if (busy)
+        *busy = false;
        if (bo->resv == ctx->resv) { reservation_object_assert_held(bo->resv); if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT

drivers/gpu/drm/ttm/ttm_bo.c +9 -8
@@ -779,35 +781,45 @@ static bool
 ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
         } else {
             *locked = reservation_object_trylock(bo->resv);
             ret = *locked;
+        if (!ret && busy)
+            *busy = true;
         }
            return ret;
     }
     -static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-                   uint32_t mem_type,
-                   const struct ttm_place *place,
-                   struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+             struct ttm_mem_type_manager *man,
+             const struct ttm_place *place,
+             struct ttm_operation_ctx *ctx,
+             struct ttm_buffer_object **first_bo,
+             bool *locked)
     {
-    struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
         struct ttm_buffer_object *bo = NULL;
-    bool locked = false;
-    unsigned i;
-    int ret;

drivers/gpu/drm/ttm/ttm_bo.c static bool · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -1
+    int i;
     -    spin_lock(&glob->lru_lock);
+    if (first_bo)
+        *first_bo = NULL;
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &man->lru[i], lru) {
-            if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+            bool busy = false;
+            if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+                                &busy)) {

A newline between declaration and code please.

Quoted 108 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -1
+                if (first_bo && !(*first_bo) && busy) {
+                    ttm_bo_get(bo);
+                    *first_bo = bo;
+                }
                     continue;
+            }
                    if (place && !bdev->driver->eviction_valuable(bo,
                                           place)) {
-                if (locked)
+                if (*locked)
 reservation_object_unlock(bo->resv);
                     continue;
                 }
+
                 break;
             }

    @@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
            bo = NULL;
        }
    +    return bo;

drivers/gpu/drm/ttm/ttm_bo.c +16 -0
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+                   uint32_t mem_type,
+                   const struct ttm_place *place,
+                   struct ttm_operation_ctx *ctx)
+{
+    struct ttm_bo_global *glob = bdev->glob;
+    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+    bool locked = false;
+    int ret;
+
+    spin_lock(&glob->lru_lock);
+    bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, &first_bo,
+                      &locked);

        if (!bo) {
+        struct ttm_operation_ctx busy_ctx;
+
            spin_unlock(&glob->lru_lock);
-        return -EBUSY;
+        /* check if other user occupy memory too long time */ +        if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+            if (first_bo)
+                ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (first_bo->resv == ctx->resv) {
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (ctx->interruptible)
+            ret = ww_mutex_lock_interruptible(&first_bo->resv->lock,
+ ctx->resv->lock.ctx);
+        else
+            ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+        if (ret) {
+            ttm_bo_put(first_bo);
+            return ret;
+        }
+        spin_lock(&glob->lru_lock);
+        /* previous busy resv lock is held by above, idle now, +         * so let them evictable.
+         */
+        busy_ctx.interruptible = ctx->interruptible;
+        busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+        busy_ctx.resv           = first_bo->resv;
+        busy_ctx.flags           = TTM_OPT_FLAG_ALLOW_RES_EVICT; +
+        bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+                          &locked);
+        if (bo && (bo->resv == first_bo->resv))
+            locked = true;
+        else if (bo)
+ ww_mutex_unlock(&first_bo->resv->lock);
+        if (!bo) {
+            spin_unlock(&glob->lru_lock);
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
        }
           kref_get(&bo->list_kref);

drivers/gpu/drm/ttm/ttm_bo.c +5 -0
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct
 ttm_bo_device *bdev,
             ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
                           ctx->no_wait_gpu, locked);
             kref_put(&bo->list_kref, ttm_bo_release_list);
+        if (first_bo)
+            ttm_bo_put(first_bo);
             return ret;
         }
            ttm_bo_del_from_lru(bo);
         spin_unlock(&glob->lru_lock);
+    if (first_bo)
+        ttm_bo_put(first_bo);
            ret = ttm_bo_evict(bo, ctx);
         if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct
 ttm_buffer_object *bo,
     {
         struct ttm_bo_device *bdev = bo->bdev;
         struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_operation_ctx native_ctx = {
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_mem_force_space(struct · line 982

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -0
+        .interruptible = false,
+        .no_wait_gpu = false,
+        .resv = bo->resv,
+        .flags = 0
+    };
+    struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that I remember that incorrectly.

Quoted 8 lines

Prike said he see ctx->resv is null, in that case, code doesn't run into busy path.
Oh, as you mentioned here, we need add .resv=bo->resv for every ttm_operation_ctx. That's a huge change which will cross all vendor drivers.

Can we just force to evaluate evict_ctx->resv = bo->resv? That means we just add one extra line: evict_ctx->resv = bo->resv. How about that?

Well only if ctx->resv is NULL, otherwise we would overwrite some reservation context given by the driver.

Probably better to give the acquir_ctx as separate parameter to ttm_mem_evict_first().

Quoted 7 lines
Quoted 3 lines

still put acquire_ctx into ttm_operation_ctx? Then that's same ctx->resv.
Current problem is we don't pass resv anywhere except ALLOW_EVICT case.
If you have concern for overwritten, we have to do ".resv = bo->resv" in every ttm_operation_ctx definitions.

No, what I mean is to add the acquire_ctx as separate parameter to ttm_mem_evict_first().

E.g. we only need it in this function and it is actually not related to the ttm operation context filled in by the driver.

FWIW, I think it would be nice at some point to have a reservation context being part of the ttm operation context, so that validate and evict could do sleeping reservations, and have bos remain on the lru even when reserved...

/Thomas

Quoted 41 lines

Christian.

Quoted 29 lines

-David

Quoted 27 lines

Christian.

Quoted 24 lines

-David

Quoted 23 lines

Christian.

Quoted 21 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
         int ret;
            do {
@@ -907,7 +987,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, ctx);
+        ret = ttm_mem_evict_first(bdev, mem_type, place, 
 evict_ctx);
             if (unlikely(ret != 0))

                return ret;
        } while (1);

drivers/gpu/drm/ttm/ttm_bo.c +2 -1
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global *glob,
 struct ttm_operation_ctx *ctx)
         spin_lock(&glob->lru_lock);
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
+            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+                               NULL)) {
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
                     ret = 0;
                     break;

                }

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

Quoted 4 lines

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

_______________________________________________
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): 93fbb994-d305-dfc4-f8e5-502647d7386f-4+hqylr40dJg9hUCZPvPmw@public.gmane.org

#14 lore raw

Am 07.05.19 um 13:37 schrieb Thomas Hellstrom:

Quoted 250 lines

[CAUTION: External Email]

Quoted 247 lines · Christian König

Am 07.05.19 um 13:22 schrieb zhoucm1:

Quoted 235 lines · Christian
Quoted 232 lines

Am 07.05.19 um 13:08 schrieb zhoucm1:

Quoted 225 lines · Christian
Quoted 209 lines

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 85 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the
ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to
evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7

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

    1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
      * b. Otherwise, trylock it.
      */

    static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
-            struct ttm_operation_ctx *ctx, bool *locked) +            struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
    {
        bool ret = false;
           *locked = false;
+    if (busy)
+        *busy = false;
        if (bo->resv == ctx->resv) { reservation_object_assert_held(bo->resv);
            if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT

drivers/gpu/drm/ttm/ttm_bo.c +9 -8
@@ -779,35 +781,45 @@ static bool
 ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
         } else {
             *locked = reservation_object_trylock(bo->resv);
             ret = *locked;
+        if (!ret && busy)
+            *busy = true;
         }
            return ret;
     }
     -static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-                   uint32_t mem_type,
-                   const struct ttm_place *place,
-                   struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+             struct ttm_mem_type_manager *man,
+             const struct ttm_place *place,
+             struct ttm_operation_ctx *ctx,
+             struct ttm_buffer_object **first_bo,
+             bool *locked)
     {
-    struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
         struct ttm_buffer_object *bo = NULL;
-    bool locked = false;
-    unsigned i;
-    int ret;

drivers/gpu/drm/ttm/ttm_bo.c static bool · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -1
+    int i;
     -    spin_lock(&glob->lru_lock);
+    if (first_bo)
+        *first_bo = NULL;
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &man->lru[i], lru) {
-            if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+            bool busy = false;
+            if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+                                &busy)) {

A newline between declaration and code please.

Quoted 111 lines
drivers/gpu/drm/ttm/ttm_bo.c +5 -0
+                if (first_bo && !(*first_bo) && busy) {
+                    ttm_bo_get(bo);
+                    *first_bo = bo;
+                }
                     continue;
+            }

                   if (place && !bdev->driver->eviction_valuable(bo,
                                          place)) { -                if (locked)
+                if (*locked) reservation_object_unlock(bo->resv);
                    continue;
                }
+
                break;
            }
    @@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
            bo = NULL;
        }
    +    return bo;

drivers/gpu/drm/ttm/ttm_bo.c +15 -0
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+                   uint32_t mem_type,
+                   const struct ttm_place *place,
+                   struct ttm_operation_ctx *ctx)
+{
+    struct ttm_bo_global *glob = bdev->glob;
+    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+    bool locked = false;
+    int ret;
+
+    spin_lock(&glob->lru_lock);
+    bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx, 

&first_bo,
+                      &locked);
        if (!bo) {
+        struct ttm_operation_ctx busy_ctx;
+
            spin_unlock(&glob->lru_lock);
-        return -EBUSY;
+        /* check if other user occupy memory too long time */ +        if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+            if (first_bo)
+                ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (first_bo->resv == ctx->resv) {
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (ctx->interruptible)
+            ret = ww_mutex_lock_interruptible(&first_bo->resv->lock,
+ ctx->resv->lock.ctx);
+        else
+            ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+        if (ret) {
+            ttm_bo_put(first_bo);
+            return ret;
+        }
+        spin_lock(&glob->lru_lock);
+        /* previous busy resv lock is held by above, idle now, +         * so let them evictable.
+         */
+        busy_ctx.interruptible = ctx->interruptible;
+        busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+        busy_ctx.resv           = first_bo->resv;
+        busy_ctx.flags           = TTM_OPT_FLAG_ALLOW_RES_EVICT; +
+        bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+                          &locked);
+        if (bo && (bo->resv == first_bo->resv))
+            locked = true;
+        else if (bo)
+ ww_mutex_unlock(&first_bo->resv->lock);
+        if (!bo) {
+            spin_unlock(&glob->lru_lock);
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
        }
           kref_get(&bo->list_kref);

drivers/gpu/drm/ttm/ttm_bo.c +6 -0
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct
 ttm_bo_device *bdev,
             ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
                           ctx->no_wait_gpu, locked);
             kref_put(&bo->list_kref, ttm_bo_release_list);
+        if (first_bo)
+            ttm_bo_put(first_bo);
             return ret;
         }
            ttm_bo_del_from_lru(bo);
         spin_unlock(&glob->lru_lock);
+    if (first_bo)
+        ttm_bo_put(first_bo);
            ret = ttm_bo_evict(bo, ctx);
         if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct
 ttm_buffer_object *bo,
     {
         struct ttm_bo_device *bdev = bo->bdev;
         struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_operation_ctx native_ctx = {
+        .interruptible = false,
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_mem_force_space(struct · line 982

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c +5 -0
+        .no_wait_gpu = false,
+        .resv = bo->resv,
+        .flags = 0
+    };
+    struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that
I remember that incorrectly.

Prike said he see ctx->resv is null, in that case, code doesn't run into busy path.
Oh, as you mentioned here, we need add .resv=bo->resv for every ttm_operation_ctx. That's a huge change which will cross all vendor drivers.

Quoted 3 lines

Can we just force to evaluate evict_ctx->resv = bo->resv? That means we just add one extra line: evict_ctx->resv = bo->resv. How about that?

Well only if ctx->resv is NULL, otherwise we would overwrite some reservation context given by the driver.

Quoted 7 lines
Quoted 2 lines

Probably better to give the acquir_ctx as separate parameter to ttm_mem_evict_first().

still put acquire_ctx into ttm_operation_ctx? Then that's same ctx->resv.
Current problem is we don't pass resv anywhere except ALLOW_EVICT case. If you have concern for overwritten, we have to do ".resv = bo->resv" in every ttm_operation_ctx definitions.

No, what I mean is to add the acquire_ctx as separate parameter to ttm_mem_evict_first().

Quoted 8 lines
Quoted 2 lines

E.g. we only need it in this function and it is actually not related to the ttm operation context filled in by the driver.

FWIW, I think it would be nice at some point to have a reservation context being part of the ttm operation context, so that validate and evict could do sleeping reservations, and have bos remain on the lru even when reserved...

Yeah, well that's exactly what the ctx->resv parameter is good for :)

And yes, we do keep the BOs on the LRU even when they are reserved.

Christian.

Quoted 53 lines

/Thomas

Quoted 47 lines

Christian.

Quoted 31 lines

-David

Quoted 29 lines

Christian.

Quoted 26 lines

-David

Quoted 25 lines

Christian.

Quoted 23 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
         int ret;
            do {
@@ -907,7 +987,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, ctx);
+        ret = ttm_mem_evict_first(bdev, mem_type, place,
 evict_ctx);
             if (unlikely(ret != 0))

                return ret;
        } while (1);

drivers/gpu/drm/ttm/ttm_bo.c +2 -1
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global 
 *glob,
 struct ttm_operation_ctx *ctx)
         spin_lock(&glob->lru_lock);
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-            if (ttm_bo_evict_swapout_allowable(bo, ctx, 
 &locked)) {
+            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
+                               NULL)) {
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

                    ret = 0;
                    break;
                }

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

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

_______________________________________________
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): fe4a6a5e-b075-b1cc-a24c-af6c3126145b-5C7GfCeVMHo@public.gmane.org

#16 lore raw
Quoted 248 lines · Christian

Am 07.05.19 um 13:37 schrieb Thomas Hellstrom:

Quoted 247 lines

[CAUTION: External Email]

Quoted 244 lines · Christian König

Am 07.05.19 um 13:22 schrieb zhoucm1:

Quoted 233 lines · Christian
Quoted 231 lines

Am 07.05.19 um 13:08 schrieb zhoucm1:

Quoted 224 lines · Christian
Quoted 209 lines

Am 07.05.19 um 11:36 schrieb Chunming Zhou:

Quoted 85 lines

heavy gpu job could occupy memory long time, which lead other user fail to get memory.

basically pick up Christian idea:

1. Reserve the BO in DC using a ww_mutex ticket (trivial).
2. If we then run into this EBUSY condition in TTM check if the BO we need memory for (or rather the ww_mutex of its reservation object) has a ticket assigned.
3. If we have a ticket we grab a reference to the first BO on the LRU, drop the LRU lock and try to grab the reservation lock with the
ticket.
4. If getting the reservation lock with the ticket succeeded we check if the BO is still the first one on the LRU in question (the BO could have moved).
5. If the BO is still the first one on the LRU in question we try to
evict it as we would evict any other BO.
6. If any of the "If's" above fail we just back off and return -EBUSY.

v2: fix some minor check
v3: address Christian v2 comments.
v4: fix some missing
v5: handle first_bo unlock and bo_get/put
v6: abstract unified iterate function, and handle all possible usecase not only pinned bo.

Change-Id I21423fb922f885465f13833c41df1e134364a8e7
---
     drivers/gpu/drm/ttm/ttm_bo.c | 113
++++++++++++++++++++++++++++++-----
     1 file changed, 97 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +3 -1
index 8502b3ed2d88..bbf1d14d00a7 100644
@@ -766,11 +766,13 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
      * b. Otherwise, trylock it.
      */
     static bool ttm_bo_evict_swapout_allowable(struct
 ttm_buffer_object *bo,
-            struct ttm_operation_ctx *ctx, bool *locked)
+            struct ttm_operation_ctx *ctx, bool *locked, bool
 *busy)
     {
         bool ret = false;
            *locked = false;
+    if (busy)
+        *busy = false;
         if (bo->resv == ctx->resv) {
 reservation_object_assert_held(bo->resv);

            if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT

drivers/gpu/drm/ttm/ttm_bo.c +9 -8
@@ -779,35 +781,45 @@ static bool
 ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
         } else {
             *locked = reservation_object_trylock(bo->resv);
             ret = *locked;
+        if (!ret && busy)
+            *busy = true;
         }
            return ret;
     }
     -static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
-                   uint32_t mem_type,
-                   const struct ttm_place *place,
-                   struct ttm_operation_ctx *ctx)
+static struct ttm_buffer_object*
+ttm_mem_find_evitable_bo(struct ttm_bo_device *bdev,
+             struct ttm_mem_type_manager *man,
+             const struct ttm_place *place,
+             struct ttm_operation_ctx *ctx,
+             struct ttm_buffer_object **first_bo,
+             bool *locked)
     {
-    struct ttm_bo_global *glob = bdev->glob;
-    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
         struct ttm_buffer_object *bo = NULL;
-    bool locked = false;
-    unsigned i;
-    int ret;

drivers/gpu/drm/ttm/ttm_bo.c static bool · line 808

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -1
+    int i;
     -    spin_lock(&glob->lru_lock);
+    if (first_bo)
+        *first_bo = NULL;
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &man->lru[i], lru) {
-            if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+            bool busy = false;
+            if (!ttm_bo_evict_swapout_allowable(bo, ctx, locked,
+                                &busy)) {

A newline between declaration and code please.

Quoted 111 lines
drivers/gpu/drm/ttm/ttm_bo.c +7 -1
+                if (first_bo && !(*first_bo) && busy) {
+                    ttm_bo_get(bo);
+                    *first_bo = bo;
+                }
                     continue;
+            }
                    if (place &&
 !bdev->driver->eviction_valuable(bo,
                                           place)) {
-                if (locked)
+                if (*locked)
 reservation_object_unlock(bo->resv);
                     continue;
                 }
+
                 break;
             }
     @@ -818,9 +830,66 @@ static int ttm_mem_evict_first(struct

ttm_bo_device *bdev,
            bo = NULL;
        }
    +    return bo;

drivers/gpu/drm/ttm/ttm_bo.c +15 -0
+}
+
+static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
+                   uint32_t mem_type,
+                   const struct ttm_place *place,
+                   struct ttm_operation_ctx *ctx)
+{
+    struct ttm_bo_global *glob = bdev->glob;
+    struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_buffer_object *bo = NULL, *first_bo = NULL;
+    bool locked = false;
+    int ret;
+
+    spin_lock(&glob->lru_lock);
+    bo = ttm_mem_find_evitable_bo(bdev, man, place, ctx,

&first_bo,
+                      &locked);
        if (!bo) {
+        struct ttm_operation_ctx busy_ctx;
+
            spin_unlock(&glob->lru_lock);
-        return -EBUSY;
+        /* check if other user occupy memory too long time */ +        if (!first_bo || !ctx || !ctx->resv || !ctx->resv->lock.ctx) {
+            if (first_bo)
+                ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (first_bo->resv == ctx->resv) {
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
+        if (ctx->interruptible)
+            ret = ww_mutex_lock_interruptible(&first_bo->resv->lock,
+ ctx->resv->lock.ctx);
+        else
+            ret = ww_mutex_lock(&first_bo->resv->lock, ctx->resv->lock.ctx);
+        if (ret) {
+            ttm_bo_put(first_bo);
+            return ret;
+        }
+        spin_lock(&glob->lru_lock);
+        /* previous busy resv lock is held by above, idle now, +         * so let them evictable.
+         */
+        busy_ctx.interruptible = ctx->interruptible;
+        busy_ctx.no_wait_gpu   = ctx->no_wait_gpu;
+        busy_ctx.resv           = first_bo->resv;
+        busy_ctx.flags           = TTM_OPT_FLAG_ALLOW_RES_EVICT; +
+        bo = ttm_mem_find_evitable_bo(bdev, man, place, &busy_ctx, NULL,
+                          &locked);
+        if (bo && (bo->resv == first_bo->resv))
+            locked = true;
+        else if (bo)
+ ww_mutex_unlock(&first_bo->resv->lock);
+        if (!bo) {
+            spin_unlock(&glob->lru_lock);
+            ttm_bo_put(first_bo);
+            return -EBUSY;
+        }
        }
           kref_get(&bo->list_kref);

drivers/gpu/drm/ttm/ttm_bo.c +6 -0
@@ -829,11 +898,15 @@ static int ttm_mem_evict_first(struct
 ttm_bo_device *bdev,
             ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
                           ctx->no_wait_gpu, locked);
             kref_put(&bo->list_kref, ttm_bo_release_list);
+        if (first_bo)
+            ttm_bo_put(first_bo);
             return ret;
         }
            ttm_bo_del_from_lru(bo);
         spin_unlock(&glob->lru_lock);
+    if (first_bo)
+        ttm_bo_put(first_bo);
            ret = ttm_bo_evict(bo, ctx);
         if (locked) {
@@ -899,6 +972,13 @@ static int ttm_bo_mem_force_space(struct
 ttm_buffer_object *bo,
     {
         struct ttm_bo_device *bdev = bo->bdev;
         struct ttm_mem_type_manager *man = &bdev->man[mem_type];
+    struct ttm_operation_ctx native_ctx = {
+        .interruptible = false,
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_mem_force_space(struct · line 982

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c +5 -0
+        .no_wait_gpu = false,
+        .resv = bo->resv,
+        .flags = 0
+    };
+    struct ttm_operation_ctx *evict_ctx = ctx ? ctx : &native_ctx;

I thought we made the ctx parameter mandatory, didn't we? Could be that
I remember that incorrectly.

Prike said he see ctx->resv is null, in that case, code doesn't run into busy path.
Oh, as you mentioned here, we need add .resv=bo->resv for every ttm_operation_ctx. That's a huge change which will cross all vendor drivers.

Quoted 3 lines

Can we just force to evaluate evict_ctx->resv = bo->resv? That means we just add one extra line: evict_ctx->resv = bo->resv. How about that?

Well only if ctx->resv is NULL, otherwise we would overwrite some reservation context given by the driver.

Quoted 7 lines
Quoted 2 lines

Probably better to give the acquir_ctx as separate parameter to ttm_mem_evict_first().

still put acquire_ctx into ttm_operation_ctx? Then that's same ctx->resv.
Current problem is we don't pass resv anywhere except ALLOW_EVICT case. If you have concern for overwritten, we have to do ".resv = bo->resv" in every ttm_operation_ctx definitions.

No, what I mean is to add the acquire_ctx as separate parameter to ttm_mem_evict_first().

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

E.g. we only need it in this function and it is actually not related to the ttm operation context filled in by the driver.

FWIW, I think it would be nice at some point to have a reservation context being part of the ttm operation context, so that validate and evict could do sleeping reservations, and have bos remain on the lru even when reserved...

Yeah, well that's exactly what the ctx->resv parameter is good for :)

Hmm. I don't quite follow? It looks to me like ctx->resv is there to work around recursive reservations?

What I'm after is being able to do sleeping reservations within validate and evict and open up for returning -EDEADLK. One benefit would be to scan over the LRU lists, reserving exactly those bos we want to evict, and when all are reserved, we evict them. If we hit an -EDEADLK while evicting we need to restart. Then we need an acquire_ctx in the ttm_operation_ctx.

Quoted 2 lines

And yes, we do keep the BOs on the LRU even when they are reserved.

static inline int ttm_bo_reserve(struct ttm_buffer_object *bo, bool interruptible, bool no_wait, struct ww_acquire_ctx *ticket)
{
    int ret;

    WARN_ON(!kref_read(&bo->kref));

    ret = __ttm_bo_reserve(bo, interruptible, no_wait, ticket); if (likely(ret == 0)) ttm_bo_del_sub_from_lru(bo);

    return ret;
}

/Thomas

Quoted 49 lines

Christian.

Quoted 46 lines

/Thomas

Quoted 42 lines

Christian.

Quoted 28 lines

-David

Quoted 27 lines

Christian.

Quoted 25 lines

-David

Quoted 24 lines

Christian.

Quoted 22 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
         int ret;
            do {
@@ -907,7 +987,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, ctx);
+        ret = ttm_mem_evict_first(bdev, mem_type, place,
 evict_ctx);
             if (unlikely(ret != 0))

                return ret;
        } while (1);

drivers/gpu/drm/ttm/ttm_bo.c +1 -1
@@ -1784,7 +1864,8 @@ int ttm_bo_swapout(struct ttm_bo_global
 *glob,
 struct ttm_operation_ctx *ctx)
         spin_lock(&glob->lru_lock);
         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
             list_for_each_entry(bo, &glob->swap_lru[i], swap) {
-            if (ttm_bo_evict_swapout_allowable(bo, ctx,
 &locked)) {
+            if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -0
+                               NULL)) {

                    ret = 0;
                    break;
                }

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

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

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

#17 lore raw

Am 08.05.19 um 10:34 schrieb Thomas Hellstrom:

Quoted 7 lines

[SNIP]

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

No, what I mean is to add the acquire_ctx as separate parameter to ttm_mem_evict_first().

E.g. we only need it in this function and it is actually not related to the ttm operation context filled in by the driver.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

FWIW, I think it would be nice at some point to have a reservation context being part of the ttm operation context, so that validate and evict could do sleeping reservations, and have bos remain on the lru even when reserved...

Yeah, well that's exactly what the ctx->resv parameter is good for :)

Hmm. I don't quite follow? It looks to me like ctx->resv is there to work around recursive reservations?

Well yes and no, this is to allow eviction of BOs which share the same reservation object.

Quoted 8 lines

What I'm after is being able to do sleeping reservations within validate and evict and open up for returning -EDEADLK. One benefit would be to scan over the LRU lists, reserving exactly those bos we want to evict, and when all are reserved, we evict them. If we hit an -EDEADLK while evicting we need to restart. Then we need an acquire_ctx in the ttm_operation_ctx.

The acquire_ctx is available from the BO you try to find space for.

But we already tried this approach and it doesn't work. We have a lot of BOs which now share the same reservation object and so would cause an -EDEADLK.

Quoted 5 lines
Quoted 1 line

And yes, we do keep the BOs on the LRU even when they are reserved.

static inline int ttm_bo_reserve(struct ttm_buffer_object *bo, bool interruptible, bool no_wait, struct ww_acquire_ctx *ticket)

ttm_bo_reserve() is not always used any more outside of TTM. The for DMA-buf as well as amdgpu VMs code the reservation object is locked without calling ttm_bo_reserve now.

Regards,
Christian.

Quoted 2 lines

/Thomas

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