diff mbox

[2/3] drm/ttm: add new function to check if bo is allowable to evict or swapout

Message ID 1514182052-20798-2-git-send-email-Hongbo.He@amd.com (mailing list archive)
State New, archived
Headers show

Commit Message

He, Hongbo Dec. 25, 2017, 6:07 a.m. UTC
extract a function as ttm_bo_evict_swapout_allowable since eviction and
swapout can share same logic.

v2: modify commit message and add description in the code

Change-Id: I80a475a93fceed8d66d74a1832c815a0756341ac
Signed-off-by: Roger He <Hongbo.He@amd.com>
---
 drivers/gpu/drm/ttm/ttm_bo.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

Comments

Christian König Dec. 25, 2017, 8:21 a.m. UTC | #1
Am 25.12.2017 um 07:07 schrieb Roger He:
> extract a function as ttm_bo_evict_swapout_allowable since eviction and
> swapout can share same logic.
>
> v2: modify commit message and add description in the code
>
> Change-Id: I80a475a93fceed8d66d74a1832c815a0756341ac
> Signed-off-by: Roger He <Hongbo.He@amd.com>

Reviewed-by: Christian König <christian.koenig@amd.com> for now.

We might want a better name for the function, but I want to rework the 
locking around eviction/swapout anyway so that can wait.

Christian.

> ---
>   drivers/gpu/drm/ttm/ttm_bo.c | 40 ++++++++++++++++++++++++++++++----------
>   1 file changed, 30 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
> index e7595b4..5e64091 100644
> --- a/drivers/gpu/drm/ttm/ttm_bo.c
> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
> @@ -706,6 +706,34 @@ bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>   }
>   EXPORT_SYMBOL(ttm_bo_eviction_valuable);
>   
> +/**
> + * Check the target bo is allowable to be evicted or swapout, including cases:
> + *
> + * a. if share same reservation object with ctx->resv, have assumption
> + * reservation objects should already be locked, so not lock again and
> + * return true directly when either the opreation allow_reserved_eviction
> + * or the target bo already is in delayed free list;
> + *
> + * b. Otherwise, trylock it.
> + */
> +static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
> +			struct ttm_operation_ctx *ctx, bool *locked)
> +{
> +	bool ret = false;
> +
> +	*locked = false;
> +	if (bo->resv == ctx->resv) {
> +		reservation_object_assert_held(bo->resv);
> +		if (ctx->allow_reserved_eviction || !list_empty(&bo->ddestroy))
> +			ret = true;
> +	} else {
> +		*locked = reservation_object_trylock(bo->resv);
> +		ret = *locked;
> +	}
> +
> +	return ret;
> +}
> +
>   static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
>   			       uint32_t mem_type,
>   			       const struct ttm_place *place,
> @@ -721,21 +749,13 @@ static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
>   	spin_lock(&glob->lru_lock);
>   	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
>   		list_for_each_entry(bo, &man->lru[i], lru) {
> -			if (bo->resv == ctx->resv) {
> -				if (!ctx->allow_reserved_eviction &&
> -				    list_empty(&bo->ddestroy))
> -					continue;
> -			} else {
> -				locked = reservation_object_trylock(bo->resv);
> -				if (!locked)
> -					continue;
> -			}
> +			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
> +				continue;
>   
>   			if (place && !bdev->driver->eviction_valuable(bo,
>   								      place)) {
>   				if (locked)
>   					reservation_object_unlock(bo->resv);
> -				locked = false;
>   				continue;
>   			}
>   			break;
diff mbox

Patch

diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index e7595b4..5e64091 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -706,6 +706,34 @@  bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
 }
 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
 
+/**
+ * Check the target bo is allowable to be evicted or swapout, including cases:
+ *
+ * a. if share same reservation object with ctx->resv, have assumption
+ * reservation objects should already be locked, so not lock again and
+ * return true directly when either the opreation allow_reserved_eviction
+ * or the target bo already is in delayed free list;
+ *
+ * b. Otherwise, trylock it.
+ */
+static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
+			struct ttm_operation_ctx *ctx, bool *locked)
+{
+	bool ret = false;
+
+	*locked = false;
+	if (bo->resv == ctx->resv) {
+		reservation_object_assert_held(bo->resv);
+		if (ctx->allow_reserved_eviction || !list_empty(&bo->ddestroy))
+			ret = true;
+	} else {
+		*locked = reservation_object_trylock(bo->resv);
+		ret = *locked;
+	}
+
+	return ret;
+}
+
 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 			       uint32_t mem_type,
 			       const struct ttm_place *place,
@@ -721,21 +749,13 @@  static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
 	spin_lock(&glob->lru_lock);
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
 		list_for_each_entry(bo, &man->lru[i], lru) {
-			if (bo->resv == ctx->resv) {
-				if (!ctx->allow_reserved_eviction &&
-				    list_empty(&bo->ddestroy))
-					continue;
-			} else {
-				locked = reservation_object_trylock(bo->resv);
-				if (!locked)
-					continue;
-			}
+			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
+				continue;
 
 			if (place && !bdev->driver->eviction_valuable(bo,
 								      place)) {
 				if (locked)
 					reservation_object_unlock(bo->resv);
-				locked = false;
 				continue;
 			}
 			break;