Skip to content

Commit ebdf565

Browse files
committed
drm/ttm: add multihop infrastrucutre (v3)
Currently drivers get called to move a buffer, but if they have to move it temporarily through another space (SYSTEM->VRAM via TT) then they can end up with a lot of ttm->driver->ttm call stacks, if the temprorary space moves requires eviction. Instead of letting the driver do all the placement/space for the temporary, allow it to report back (-EMULTIHOP) and a placement (hop) to the move code, which will then do the temporary move, and the correct placement move afterwards. This removes a lot of code from drivers, at the expense of adding some midlayering. I've some further ideas on how to turn it inside out, but I think this is a good solution to the call stack problems. v2: separate out the driver patches, add WARN for getting MULTHOP in paths we shouldn't (Daniel) v3: use memset (Christian) Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Reviewed-by: hristian König <christian.koenig@amd.com> Signed-off-by: Dave Airlie <airlied@redhat.com> Link: https://patchwork.freedesktop.org/patch/msgid/20201109005432.861936-2-airlied@gmail.com
1 parent 2dcab87 commit ebdf565

8 files changed

Lines changed: 83 additions & 16 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@ static bool amdgpu_mem_visible(struct amdgpu_device *adev,
656656
*/
657657
static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
658658
struct ttm_operation_ctx *ctx,
659-
struct ttm_resource *new_mem)
659+
struct ttm_resource *new_mem,
660+
struct ttm_place *hop)
660661
{
661662
struct amdgpu_device *adev;
662663
struct amdgpu_bo *abo;

‎drivers/gpu/drm/drm_gem_vram_helper.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,8 @@ static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
955955
static int bo_driver_move(struct ttm_buffer_object *bo,
956956
bool evict,
957957
struct ttm_operation_ctx *ctx,
958-
struct ttm_resource *new_mem)
958+
struct ttm_resource *new_mem,
959+
struct ttm_place *hop)
959960
{
960961
struct drm_gem_vram_object *gbo;
961962

‎drivers/gpu/drm/nouveau/nouveau_bo.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,8 @@ nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
10231023
static int
10241024
nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
10251025
struct ttm_operation_ctx *ctx,
1026-
struct ttm_resource *new_reg)
1026+
struct ttm_resource *new_reg,
1027+
struct ttm_place *hop)
10271028
{
10281029
struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
10291030
struct nouveau_bo *nvbo = nouveau_bo(bo);

‎drivers/gpu/drm/qxl/qxl_ttm.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
140140

141141
static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
142142
struct ttm_operation_ctx *ctx,
143-
struct ttm_resource *new_mem)
143+
struct ttm_resource *new_mem,
144+
struct ttm_place *hop)
144145
{
145146
struct ttm_resource *old_mem = &bo->mem;
146147
int ret;

‎drivers/gpu/drm/radeon/radeon_ttm.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ static int radeon_move_ram_vram(struct ttm_buffer_object *bo,
303303

304304
static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
305305
struct ttm_operation_ctx *ctx,
306-
struct ttm_resource *new_mem)
306+
struct ttm_resource *new_mem,
307+
struct ttm_place *hop)
307308
{
308309
struct radeon_device *rdev;
309310
struct radeon_bo *rbo;

‎drivers/gpu/drm/ttm/ttm_bo.c‎

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
231231

232232
static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
233233
struct ttm_resource *mem, bool evict,
234-
struct ttm_operation_ctx *ctx)
234+
struct ttm_operation_ctx *ctx,
235+
struct ttm_place *hop)
235236
{
236237
struct ttm_bo_device *bdev = bo->bdev;
237238
struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
@@ -259,9 +260,12 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
259260
}
260261
}
261262

262-
ret = bdev->driver->move(bo, evict, ctx, mem);
263-
if (ret)
263+
ret = bdev->driver->move(bo, evict, ctx, mem, hop);
264+
if (ret) {
265+
if (ret == -EMULTIHOP)
266+
return ret;
264267
goto out_err;
268+
}
265269

266270
ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
267271
return 0;
@@ -566,8 +570,11 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
566570
struct ttm_bo_device *bdev = bo->bdev;
567571
struct ttm_resource evict_mem;
568572
struct ttm_placement placement;
573+
struct ttm_place hop;
569574
int ret = 0;
570575

576+
memset(&hop, 0, sizeof(hop));
577+
571578
dma_resv_assert_held(bo->base.resv);
572579

573580
placement.num_placement = 0;
@@ -596,8 +603,9 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
596603
goto out;
597604
}
598605

599-
ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
606+
ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
600607
if (unlikely(ret)) {
608+
WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
601609
if (ret != -ERESTARTSYS)
602610
pr_err("Buffer eviction failed\n");
603611
ttm_resource_free(bo, &evict_mem);
@@ -936,15 +944,45 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
936944
}
937945
EXPORT_SYMBOL(ttm_bo_mem_space);
938946

947+
static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
948+
struct ttm_resource *mem,
949+
struct ttm_operation_ctx *ctx,
950+
struct ttm_place *hop)
951+
{
952+
struct ttm_placement hop_placement;
953+
int ret;
954+
struct ttm_resource hop_mem = *mem;
955+
956+
hop_mem.mm_node = NULL;
957+
hop_mem.mem_type = TTM_PL_SYSTEM;
958+
hop_mem.placement = 0;
959+
960+
hop_placement.num_placement = hop_placement.num_busy_placement = 1;
961+
hop_placement.placement = hop_placement.busy_placement = hop;
962+
963+
/* find space in the bounce domain */
964+
ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
965+
if (ret)
966+
return ret;
967+
/* move to the bounce domain */
968+
ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
969+
if (ret)
970+
return ret;
971+
return 0;
972+
}
973+
939974
static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
940975
struct ttm_placement *placement,
941976
struct ttm_operation_ctx *ctx)
942977
{
943978
int ret = 0;
979+
struct ttm_place hop;
944980
struct ttm_resource mem;
945981

946982
dma_resv_assert_held(bo->base.resv);
947983

984+
memset(&hop, 0, sizeof(hop));
985+
948986
mem.num_pages = bo->num_pages;
949987
mem.size = mem.num_pages << PAGE_SHIFT;
950988
mem.page_alignment = bo->mem.page_alignment;
@@ -954,12 +992,25 @@ static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
954992

955993
/*
956994
* Determine where to move the buffer.
995+
*
996+
* If driver determines move is going to need
997+
* an extra step then it will return -EMULTIHOP
998+
* and the buffer will be moved to the temporary
999+
* stop and the driver will be called to make
1000+
* the second hop.
9571001
*/
1002+
bounce:
9581003
ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
9591004
if (ret)
960-
goto out_unlock;
961-
ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
962-
out_unlock:
1005+
return ret;
1006+
ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
1007+
if (ret == -EMULTIHOP) {
1008+
ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
1009+
if (ret)
1010+
return ret;
1011+
/* try and move to final place now. */
1012+
goto bounce;
1013+
}
9631014
if (ret)
9641015
ttm_resource_free(bo, &mem);
9651016
return ret;
@@ -1432,15 +1483,20 @@ int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
14321483
if (bo->mem.mem_type != TTM_PL_SYSTEM) {
14331484
struct ttm_operation_ctx ctx = { false, false };
14341485
struct ttm_resource evict_mem;
1486+
struct ttm_place hop;
1487+
1488+
memset(&hop, 0, sizeof(hop));
14351489

14361490
evict_mem = bo->mem;
14371491
evict_mem.mm_node = NULL;
14381492
evict_mem.placement = 0;
14391493
evict_mem.mem_type = TTM_PL_SYSTEM;
14401494

1441-
ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1442-
if (unlikely(ret != 0))
1495+
ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1496+
if (unlikely(ret != 0)) {
1497+
WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
14431498
goto out;
1499+
}
14441500
}
14451501

14461502
/**

‎drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ static void vmw_swap_notify(struct ttm_buffer_object *bo)
695695
static int vmw_move(struct ttm_buffer_object *bo,
696696
bool evict,
697697
struct ttm_operation_ctx *ctx,
698-
struct ttm_resource *new_mem)
698+
struct ttm_resource *new_mem,
699+
struct ttm_place *hop)
699700
{
700701
struct ttm_resource_manager *old_man = ttm_manager_type(bo->bdev, bo->mem.mem_type);
701702
struct ttm_resource_manager *new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);

‎include/drm/ttm/ttm_bo_driver.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ struct ttm_bo_driver {
121121
* Return the bo flags for a buffer which is not mapped to the hardware.
122122
* These will be placed in proposed_flags so that when the move is
123123
* finished, they'll end up in bo->mem.flags
124+
* This should not cause multihop evictions, and the core will warn
125+
* if one is proposed.
124126
*/
125127

126128
void (*evict_flags)(struct ttm_buffer_object *bo,
@@ -134,12 +136,15 @@ struct ttm_bo_driver {
134136
* the graphics address space
135137
* @ctx: context for this move with parameters
136138
* @new_mem: the new memory region receiving the buffer
139+
@ @hop: placement for driver directed intermediate hop
137140
*
138141
* Move a buffer between two memory regions.
142+
* Returns errno -EMULTIHOP if driver requests a hop
139143
*/
140144
int (*move)(struct ttm_buffer_object *bo, bool evict,
141145
struct ttm_operation_ctx *ctx,
142-
struct ttm_resource *new_mem);
146+
struct ttm_resource *new_mem,
147+
struct ttm_place *hop);
143148

144149
/**
145150
* struct ttm_bo_driver_member verify_access

0 commit comments

Comments
 (0)