Skip to content

Commit abb50d6

Browse files
author
Thomas Hellström
committed
drm/ttm, drm/amdgpu: Allow the driver some control over swapping
We are calling the eviction_valuable driver callback at eviction time to determine whether we actually can evict a buffer object. The upcoming i915 TTM backend needs the same functionality for swapout, and that might actually be beneficial to other drivers as well. Add an eviction_valuable call also in the swapout path. Try to keep the current behaviour for all drivers by returning true if the buffer object is already in the TTM_PL_SYSTEM placement. We change behaviour for the case where a buffer object is in a TT backed placement when swapped out, in which case the drivers normal eviction_valuable path is run. Reviewed-by: Maarten Lankhorst <[email protected]> Cc: Christian König <[email protected]> Signed-off-by: Thomas Hellström <[email protected]> Acked-by: Christian König <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent a3be8cd commit abb50d6

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,10 @@ static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
13311331
struct dma_fence *f;
13321332
int i;
13331333

1334+
/* Swapout? */
1335+
if (bo->resource->mem_type == TTM_PL_SYSTEM)
1336+
return true;
1337+
13341338
if (bo->type == ttm_bo_type_kernel &&
13351339
!amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
13361340
return false;

drivers/gpu/drm/ttm/ttm_bo.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
538538
bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
539539
const struct ttm_place *place)
540540
{
541+
dma_resv_assert_held(bo->base.resv);
542+
if (bo->resource->mem_type == TTM_PL_SYSTEM)
543+
return true;
544+
541545
/* Don't evict this BO if it's outside of the
542546
* requested placement range
543547
*/
@@ -560,7 +564,9 @@ EXPORT_SYMBOL(ttm_bo_eviction_valuable);
560564
* b. Otherwise, trylock it.
561565
*/
562566
static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
563-
struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
567+
struct ttm_operation_ctx *ctx,
568+
const struct ttm_place *place,
569+
bool *locked, bool *busy)
564570
{
565571
bool ret = false;
566572

@@ -578,6 +584,14 @@ static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
578584
*busy = !ret;
579585
}
580586

587+
if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
588+
ret = false;
589+
if (*locked) {
590+
dma_resv_unlock(bo->base.resv);
591+
*locked = false;
592+
}
593+
}
594+
581595
return ret;
582596
}
583597

@@ -632,20 +646,14 @@ int ttm_mem_evict_first(struct ttm_device *bdev,
632646
list_for_each_entry(bo, &man->lru[i], lru) {
633647
bool busy;
634648

635-
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
636-
&busy)) {
649+
if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
650+
&locked, &busy)) {
637651
if (busy && !busy_bo && ticket !=
638652
dma_resv_locking_ctx(bo->base.resv))
639653
busy_bo = bo;
640654
continue;
641655
}
642656

643-
if (place && !bdev->funcs->eviction_valuable(bo,
644-
place)) {
645-
if (locked)
646-
dma_resv_unlock(bo->base.resv);
647-
continue;
648-
}
649657
if (!ttm_bo_get_unless_zero(bo)) {
650658
if (locked)
651659
dma_resv_unlock(bo->base.resv);
@@ -1116,10 +1124,19 @@ EXPORT_SYMBOL(ttm_bo_wait);
11161124
int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
11171125
gfp_t gfp_flags)
11181126
{
1127+
struct ttm_place place;
11191128
bool locked;
11201129
int ret;
11211130

1122-
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
1131+
/*
1132+
* While the bo may already reside in SYSTEM placement, set
1133+
* SYSTEM as new placement to cover also the move further below.
1134+
* The driver may use the fact that we're moving from SYSTEM
1135+
* as an indication that we're about to swap out.
1136+
*/
1137+
memset(&place, 0, sizeof(place));
1138+
place.mem_type = TTM_PL_SYSTEM;
1139+
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
11231140
return -EBUSY;
11241141

11251142
if (!ttm_bo_get_unless_zero(bo)) {
@@ -1144,13 +1161,9 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
11441161
if (bo->resource->mem_type != TTM_PL_SYSTEM) {
11451162
struct ttm_operation_ctx ctx = { false, false };
11461163
struct ttm_resource *evict_mem;
1147-
struct ttm_place place, hop;
1164+
struct ttm_place hop;
11481165

1149-
memset(&place, 0, sizeof(place));
11501166
memset(&hop, 0, sizeof(hop));
1151-
1152-
place.mem_type = TTM_PL_SYSTEM;
1153-
11541167
ret = ttm_resource_alloc(bo, &place, &evict_mem);
11551168
if (unlikely(ret))
11561169
goto out;
@@ -1178,7 +1191,8 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
11781191
if (bo->bdev->funcs->swap_notify)
11791192
bo->bdev->funcs->swap_notify(bo);
11801193

1181-
ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1194+
if (ttm_tt_is_populated(bo->ttm))
1195+
ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
11821196
out:
11831197

11841198
/*

0 commit comments

Comments
 (0)