Skip to contentSkip to conversation

ttm don't allow multihop moves

RFC In review Dave Airlie 2 participants 4 messages Oct 20, 2020 – Oct 21, 2020

Subsystems (5)
lore
Share & tools
lore

Dave Airlie

author maintainer
#0 lore raw

This is an RFC for a hacky idea I had to at least move the converation forward.

The branch with this in it is:
https://github.com/airlied/linux/tree/ttm-bounce

it won't apply to any other tree as it's based on all those patches I posted and some other refactorings.

The basic idea is if the driver gets a move request from the TTM core that requires it to bounce the buffer through another domain, it returns -EMULTIHOP and puts the domain details into the mem_type, the core code then just does the mem space for the new temp placment, and retries the final placement again.

I've tested on nouveau that the code gets executed (a printk prints at least), and it all doesn't burn down, but it's very lightly tested.

It does allow getting rid of a lot of driver code to handle bouncing moves.

I'm sure this could be prettier or done in a very different way more effectively, but hey this was my chance to misuse an errno value.

Dave.

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

author maintainer
#1 lore raw

PATCH 1/1 drm/ttm: avoid multihop moves in drivers.

From: Dave Airlie <[email protected]>

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 128 +++---------------------
 drivers/gpu/drm/nouveau/nouveau_bo.c    | 106 ++------------------
 drivers/gpu/drm/radeon/radeon_ttm.c     | 110 ++------------------
 drivers/gpu/drm/ttm/ttm_bo.c            |  44 +++++++-
 4 files changed, 77 insertions(+), 311 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +15 -113
index 872795affe87..51d55e3fd8c8 100644
@@ -515,109 +515,6 @@ static int amdgpu_move_blit(struct ttm_buffer_object *bo,
 	return r;
 }
 
-static int amdgpu_move_setup_tt(struct ttm_buffer_object *bo,
-				struct ttm_operation_ctx *ctx,
-				struct ttm_resource *tmp_mem)
-{
-	struct ttm_place placements;
-	struct ttm_placement placement;
-	int r;
-
-	placement.num_placement = 1;
-	placement.placement = &placements;
-	placement.num_busy_placement = 1;
-	placement.busy_placement = &placements;
-	placements.fpfn = 0;
-	placements.lpfn = 0;
-	placements.mem_type = TTM_PL_TT;
-	placements.flags = 0;
-	r = ttm_bo_mem_space(bo, &placement, tmp_mem, ctx);
-	if (unlikely(r)) {
-		pr_err("Failed to find GTT space for blit from VRAM\n");
-		return r;
-	}
-
-	r = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
-	if (unlikely(r))
-		goto out_cleanup;
-
-	/* Bind the memory to the GTT space */
-	r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, tmp_mem);
-	if (unlikely(r)) {
-		goto out_cleanup;
-	}
-	return 0;
-out_cleanup:
-	ttm_resource_free(bo, tmp_mem);
-	return r;
-}
-/**
- * amdgpu_move_vram_ram - Copy VRAM buffer to RAM buffer
- *
- * Called by amdgpu_bo_move().
- */
-static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo, bool evict,
-				struct ttm_operation_ctx *ctx,
-				struct ttm_resource *new_mem)
-{
-	struct ttm_resource *old_mem = &bo->mem;
-	struct ttm_resource tmp_mem;
-	int r;
-
-	/* create space/pages for new_mem in GTT space */
-	tmp_mem = *new_mem;
-	tmp_mem.mm_node = NULL;
-
-	r = amdgpu_move_setup_tt(bo, ctx, &tmp_mem);
-	if (unlikely(r))
-		return r;
-
-	/* blit VRAM to GTT */
-	r = amdgpu_move_blit(bo, evict, &tmp_mem, old_mem);
-	if (unlikely(r)) {
-		goto out_cleanup;
-	}
-
-	r = ttm_bo_wait_ctx(bo, ctx);
-	if (unlikely(r))
-		goto out_cleanup;
-
-	amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
-	ttm_resource_free(bo, &bo->mem);
-	ttm_bo_assign_mem(bo, new_mem);
-out_cleanup:
-	ttm_resource_free(bo, &tmp_mem);
-	return r;
-}
-
-/**
- * amdgpu_move_ram_vram - Copy buffer from RAM to VRAM
- *
- * Called by amdgpu_bo_move().
- */
-static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo, bool evict,
-				struct ttm_operation_ctx *ctx,
-				struct ttm_resource *new_mem)
-{
-	struct ttm_resource *old_mem = &bo->mem;
-	struct ttm_resource tmp_mem;
-	int r;
-
-	/* make space in GTT for old_mem buffer */
-	tmp_mem = *new_mem;
-	tmp_mem.mm_node = NULL;
-
-	r = amdgpu_move_setup_tt(bo, ctx, &tmp_mem);
-	if (unlikely(r))
-		return r;
-
-	ttm_bo_assign_mem(bo, &tmp_mem);
-	/* copy to VRAM */
-	r = amdgpu_move_blit(bo, evict, new_mem, old_mem);
-	ttm_resource_free(bo, &tmp_mem);
-	return r;
-}
-
 /**
  * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
  *
@@ -656,6 +553,19 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
 	struct ttm_resource *old_mem = &bo->mem;
 	int r;
 
+	/* don't go from system->vram in one hop,
+	   report this back to the caller tell it
+	   where to bounce this buffer through. */
+
+	if ((old_mem->mem_type == TTM_PL_SYSTEM &&
+	     new_mem->mem_type == TTM_PL_VRAM) ||
+	    (old_mem->mem_type == TTM_PL_VRAM &&
+	     new_mem->mem_type == TTM_PL_SYSTEM)) {
+		new_mem->mem_type = TTM_PL_TT;
+		new_mem->placement = 0;
+		return -EMULTIHOP;
+	}
+
 	if (new_mem->mem_type == TTM_PL_TT) {
 		r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, new_mem);
 		if (r)
@@ -709,16 +619,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
 		goto memcpy;
 	}
 
-	if (old_mem->mem_type == TTM_PL_VRAM &&
-	    new_mem->mem_type == TTM_PL_SYSTEM) {
-		r = amdgpu_move_vram_ram(bo, evict, ctx, new_mem);
-	} else if (old_mem->mem_type == TTM_PL_SYSTEM &&
-		   new_mem->mem_type == TTM_PL_VRAM) {
-		r = amdgpu_move_ram_vram(bo, evict, ctx, new_mem);
-	} else {
-		r = amdgpu_move_blit(bo, evict,
-				     new_mem, old_mem);
-	}
+	r = amdgpu_move_blit(bo, evict,
+			     new_mem, old_mem);
 
 	if (r) {
 memcpy:
drivers/gpu/drm/nouveau/nouveau_bo.c +11 -95

124 lines not on this page · raw message

drivers/gpu/drm/radeon/radeon_ttm.c +11 -99

128 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_bo.c +40 -4

69 lines not on this page · raw message

Signature · 6 lines
2.27.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
maintainer
#2 lore raw

Re: [PATCH] [RFC/HACK] drm/ttm: avoid multihop moves in drivers.

Yes, please! That approach looks even better than what I had in mind.

A few comments below:

Am 20.10.20 um 06:16 schrieb Dave Airlie:

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, · line 564

Quoted 12 lines

From: Dave Airlie <[email protected]>

[SNIP]

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
+	/* don't go from system->vram in one hop,
+	   report this back to the caller tell it
+	   where to bounce this buffer through. */
+
+	if ((old_mem->mem_type == TTM_PL_SYSTEM &&
+	     new_mem->mem_type == TTM_PL_VRAM) ||
+	    (old_mem->mem_type == TTM_PL_VRAM &&
+	     new_mem->mem_type == TTM_PL_SYSTEM)) {
+		new_mem->mem_type = TTM_PL_TT;

Not sure if that is such a good idea, new_mem can be some allocated memory in the VRAM domain at this moment.

Maybe instead give the callback a separate bounce argument instead.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		new_mem->placement = 0;
+		return -EMULTIHOP;

Using EMULTIHOP here is a really nice idea.

Quoted 14 lines

[SNIP]

drivers/gpu/drm/ttm/ttm_bo.c +12 -0
+static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
+				     struct ttm_resource *mem,
+				     struct ttm_operation_ctx *ctx)
+{
+	struct ttm_place placement_memtype = {
+		.fpfn = 0,
+		.lpfn = 0,
+		.mem_type = mem->mem_type,
+		.flags = mem->placement,
+	};
+	struct ttm_placement bounce_placement;
+	int ret;

drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_mem_space(struct ttm_buffer_object *bo, · line 963

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +10 -0
+
+	bounce_placement.num_placement = bounce_placement.num_busy_placement = 1;
+	bounce_placement.placement = bounce_placement.busy_placement = &placement_memtype;
+
+	/* find space in the bounce domain */
+	ret = ttm_bo_mem_space(bo, &bounce_placement, mem, ctx);
+	if (ret)
+		return ret;
+	/* move to the bounce domain */
+	ret = ttm_bo_handle_move_mem(bo, mem, false, ctx);

Is this a recursion? I don't think it is, but I thought I better double check.

Regards,
Christian.

Quoted 30 lines
drivers/gpu/drm/ttm/ttm_bo.c +14 -2
+	if (ret)
+		return ret;
+	return 0;
+}
+
  static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  			      struct ttm_placement *placement,
  			      struct ttm_operation_ctx *ctx)
@@ -954,11 +984,18 @@ static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  	/*
  	 * Determine where to move the buffer.
  	 */
+bounce:
  	ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
  	if (ret)
-		goto out_unlock;
+		return ret;
  	ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
-out_unlock:
+	if (ret == -EMULTIHOP) {
+		ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx);
+		if (ret)
+			return ret;
+		/* try and move to final place now. */
+		goto bounce;
+	}
  	if (ret)
  		ttm_resource_free(bo, &mem);
  	return ret;

drivers/gpu/drm/ttm/ttm_bo.c void ttm_bo_tt_destroy(struct ttm_buffer_object *bo) · line 1481

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -1
@@ -1478,4 +1515,3 @@ void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
  	ttm_tt_destroy(bo->bdev, bo->ttm);
  	bo->ttm = NULL;
  }
-

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

author maintainer
#3 lore raw

Re: [PATCH] [RFC/HACK] drm/ttm: avoid multihop moves in drivers.

Quoted 19 lines · Christian König

Yes, please! That approach looks even better than what I had in mind.

A few comments below:

Am 20.10.20 um 06:16 schrieb Dave Airlie:

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, · line 564

Quoted 12 lines

From: Dave Airlie <[email protected]>

[SNIP]

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
+     /* don't go from system->vram in one hop,
+        report this back to the caller tell it
+        where to bounce this buffer through. */
+
+     if ((old_mem->mem_type == TTM_PL_SYSTEM &&
+          new_mem->mem_type == TTM_PL_VRAM) ||
+         (old_mem->mem_type == TTM_PL_VRAM &&
+          new_mem->mem_type == TTM_PL_SYSTEM)) {
+             new_mem->mem_type = TTM_PL_TT;
Quoted 4 lines

Not sure if that is such a good idea, new_mem can be some allocated memory in the VRAM domain at this moment.

Maybe instead give the callback a separate bounce argument instead.

I've changed this in the latest posting to take a ttm_place that the driver fills out
if it needs a hop.

Quoted 9 lines

drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_mem_space(struct ttm_buffer_object *bo, · line 963

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +6 -0
+     /* find space in the bounce domain */
+     ret = ttm_bo_mem_space(bo, &bounce_placement, mem, ctx);
+     if (ret)
+             return ret;
+     /* move to the bounce domain */
+     ret = ttm_bo_handle_move_mem(bo, mem, false, ctx);

Is this a recursion? I don't think it is, but I thought I better double check.

No it should never recurse because the driver should only return -EMULTIHOP once,
even if a theoretical driver was to need > 1 hops it should work until it returns something
other than -EMULTIHOP. Of course a broken driver could cause bad things, but that is
always true.

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