Skip to contentSkip to conversation

drm/ttm,amdgpu: Introduce LRU bulk move functionality

PATCH Superseded Huang Rui 5 participants 12 messages Aug 8, 2018 – Aug 10, 2018 4 messages not in the archive

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Huang Rui

author
#0 lore raw

cover drm/ttm,amdgpu: Introduce LRU bulk move functionality

The idea and proposal is originally from Christian, and I continue to work to deliver it.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

Thanks,
Rui

Christian König (2):
drm/ttm: add helper structures for bulk moves on lru list drm/ttm: revise ttm_bo_move_to_lru_tail to support bulk moves

Huang Rui (3):
drm/ttm: add bulk move function on LRU drm/amdgpu: use bulk moves for efficient VM LRU handling drm/amdgpu: move PD/PT bos on LRU again

 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 +++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++
 drivers/gpu/drm/ttm/ttm_bo.c           | 78 +++++++++++++++++++++++++++++++++-
 include/drm/ttm/ttm_bo_api.h           | 16 ++++++-
 include/drm/ttm/ttm_bo_driver.h        | 28 ++++++++++++
 5 files changed, 139 insertions(+), 11 deletions(-)
Signature · 6 lines
2.7.4

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

PATCH 1/5 drm/ttm: add helper structures for bulk moves on lru list

From: Christian König <[email protected]>

Add bulk move pos to store the pointer of first and last buffer object. The list in between will be bulk moved on lru list.

---
 include/drm/ttm/ttm_bo_driver.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
include/drm/ttm/ttm_bo_driver.h +28 -0
index 3234cc3..e4fee8e 100644
@@ -491,6 +491,34 @@ struct ttm_bo_device {
 };
 
 /**
+ * struct ttm_lru_bulk_move_pos
+ *
+ * @first: first BO in the bulk move range
+ * @last: last BO in the bulk move range
+ *
+ * Positions for a lru bulk move.
+ */
+struct ttm_lru_bulk_move_pos {
+	struct ttm_buffer_object *first;
+	struct ttm_buffer_object *last;
+};
+
+/**
+ * struct ttm_lru_bulk_move
+ *
+ * @tt: first/last lru entry for BOs in the TT domain
+ * @vram: first/last lru entry for BOs in the VRAM domain
+ * @swap: first/last lru entry for BOs on the swap list
+ *
+ * Helper structure for bulk moves on the LRU list.
+ */
+struct ttm_lru_bulk_move {
+	struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
+	struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
+	struct ttm_lru_bulk_move_pos swap[TTM_MAX_BO_PRIORITY];
+};
+
+/**
  * ttm_flag_masked
  *
  * @old: Pointer to the result and original value.
Signature · 6 lines
2.7.4

_______________________________________________
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): 1533722377-27020-1-git-send-email-ray.huang-5C7GfCeVMHo@public.gmane.org

author
#3 lore raw

PATCH 2/5 drm/ttm: revise ttm_bo_move_to_lru_tail to support bulk moves

From: Christian König <[email protected]>

When move a BO to the end of LRU, it need remember the BO positions. Make sure all moved bo in between "first" and "last". And they will be bulk moving together.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c |  8 ++++----
 drivers/gpu/drm/ttm/ttm_bo.c           | 26 +++++++++++++++++++++++++-
 include/drm/ttm/ttm_bo_api.h           |  6 +++++-
 3 files changed, 34 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -4
index 015613b..9c84770 100644
@@ -297,9 +297,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 				break;
 
 			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo);
+			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
 			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
+				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
 			spin_unlock(&glob->lru_lock);
 		}
 
@@ -319,9 +319,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		if (!bo->parent)
 			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo);
+		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
 		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
 	}
 	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/ttm/ttm_bo.c +25 -1
index 7c48472..7117b6b 100644
@@ -214,12 +214,36 @@ void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
 }
 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
 
-void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
+static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
+				     struct ttm_buffer_object *bo)
+{
+	if (!pos->first)
+		pos->first = bo;
+	pos->last = bo;
+}
+
+void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
+			     struct ttm_lru_bulk_move *bulk)
 {
 	reservation_object_assert_held(bo->resv);
 
 	ttm_bo_del_from_lru(bo);
 	ttm_bo_add_to_lru(bo);
+
+	if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
+		switch (bo->mem.mem_type) {
+		case TTM_PL_TT:
+			ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
+			break;
+
+		case TTM_PL_VRAM:
+			ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
+			break;
+		}
+		if (bo->ttm && !(bo->ttm->page_flags &
+				 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
+			ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
+	}
 }
 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
 
include/drm/ttm/ttm_bo_api.h +5 -1
index a01ba20..0d4eb81 100644
@@ -51,6 +51,8 @@ struct ttm_placement;
 
 struct ttm_place;
 
+struct ttm_lru_bulk_move;
+
 /**
  * struct ttm_bus_placement
  *
@@ -405,12 +407,14 @@ void ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
  * ttm_bo_move_to_lru_tail
  *
  * @bo: The buffer object.
+ * @bulk: optional bulk move structure to remember BO positions
  *
  * Move this BO to the tail of all lru lists used to lookup and reserve an
  * object. This function must be called with struct ttm_bo_global::lru_lock
  * held, and is used to make a BO less likely to be considered for eviction.
  */
-void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
+void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
+			     struct ttm_lru_bulk_move *bulk);
 
 /**
  * ttm_bo_lock_delayed_workqueue
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
author
#4 lore raw

PATCH 3/5 drm/ttm: add bulk move function on LRU

In reply to a message not in the archive

This function allow us to bulk move a group of BOs to the tail of their LRU. The positions of group of BOs are stored on the (first, last) bulk_move_pos structure.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 52 ++++++++++++++++++++++++++++++++++++++++++++
 include/drm/ttm/ttm_bo_api.h | 10 +++++++++
 2 files changed, 62 insertions(+)
drivers/gpu/drm/ttm/ttm_bo.c +52 -0
index 7117b6b..39d9d55 100644
@@ -247,6 +247,58 @@ void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
 }
 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
 
+static void ttm_bo_bulk_move_helper(struct ttm_lru_bulk_move_pos *pos,
+				    struct list_head *lru, bool is_swap)
+{
+	struct list_head entries, before;
+	struct list_head *list1, *list2;
+
+	list1 = is_swap ? &pos->last->swap : &pos->last->lru;
+	list2 = is_swap ? pos->first->swap.prev : pos->first->lru.prev;
+
+	list_cut_position(&entries, lru, list1);
+	list_cut_position(&before, &entries, list2);
+	list_splice(&before, lru);
+	list_splice_tail(&entries, lru);
+}
+
+void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
+{
+	unsigned i;
+
+	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
+		struct ttm_mem_type_manager *man;
+
+		if (!bulk->tt[i].first)
+			continue;
+
+		man = &bulk->tt[i].first->bdev->man[TTM_PL_TT];
+		ttm_bo_bulk_move_helper(&bulk->tt[i], &man->lru[i], false);
+	}
+
+	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
+		struct ttm_mem_type_manager *man;
+
+		if (!bulk->vram[i].first)
+			continue;
+
+		man = &bulk->vram[i].first->bdev->man[TTM_PL_VRAM];
+		ttm_bo_bulk_move_helper(&bulk->vram[i], &man->lru[i], false);
+	}
+
+	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
+		struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
+		struct list_head *lru;
+
+		if (!pos->first)
+			continue;
+
+		lru = &pos->first->bdev->glob->swap_lru[i];
+		ttm_bo_bulk_move_helper(&bulk->swap[i], lru, true);
+	}
+}
+EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
+
 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
 				  struct ttm_mem_reg *mem, bool evict,
 				  struct ttm_operation_ctx *ctx)
include/drm/ttm/ttm_bo_api.h +10 -0
index 0d4eb81..8c19470 100644
@@ -417,6 +417,16 @@ void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
 			     struct ttm_lru_bulk_move *bulk);
 
 /**
+ * ttm_bo_bulk_move_lru_tail
+ *
+ * @bulk: bulk move structure
+ *
+ * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
+ * BO order never changes. Should be called with ttm_bo_global::lru_lock held.
+ */
+void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
+
+/**
  * ttm_bo_lock_delayed_workqueue
  *
  * Prevent the delayed workqueue from running.
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
author
#5 lore raw

PATCH 4/5 drm/amdgpu: use bulk moves for efficient VM LRU handling

In reply to a message not in the archive

I continue to work for bulk moving that based on the proposal by Christian.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 22 ++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++++
 2 files changed, 18 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +14 -8
index 9c84770..eda0bb9 100644
@@ -286,6 +286,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 {
 	struct ttm_bo_global *glob = adev->mman.bdev.glob;
 	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	bool validated = false;
 	int r = 0;
 
 	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
@@ -295,14 +296,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			r = validate(param, bo);
 			if (r)
 				break;
-
-			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
-			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
-			spin_unlock(&glob->lru_lock);
 		}
 
+		validated = true;
 		if (bo->tbo.type != ttm_bo_type_kernel) {
 			spin_lock(&vm->moved_lock);
 			list_move(&bo_base->vm_status, &vm->moved);
@@ -312,6 +308,15 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		}
 	}
 
+	if (!validated) {
+		spin_lock(&glob->lru_lock);
+		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
+
+	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+
 	spin_lock(&glob->lru_lock);
 	list_for_each_entry(bo_base, &vm->idle, vm_status) {
 		struct amdgpu_bo *bo = bo_base->bo;
@@ -319,9 +324,10 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		if (!bo->parent)
 			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
 		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
+						&vm->lru_bulk_move);
 	}
 	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4 -0
index 67a15d4..92725ac 100644
@@ -29,6 +29,7 @@
 #include <linux/rbtree.h>
 #include <drm/gpu_scheduler.h>
 #include <drm/drm_file.h>
+#include <drm/ttm/ttm_bo_driver.h>
 
 #include "amdgpu_sync.h"
 #include "amdgpu_ring.h"
@@ -226,6 +227,9 @@ struct amdgpu_vm {
 
 	/* Some basic info about the task */
 	struct amdgpu_task_info task_info;
+
+	/* Store positions of group of BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
 };
 
 struct amdgpu_vm_manager {
Signature · 6 lines
2.7.4

_______________________________________________
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): 1533722377-27020-5-git-send-email-ray.huang-5C7GfCeVMHo@public.gmane.org

#7 lore raw

Re: [PATCH 4/5] drm/amdgpu: use bulk moves for efficient VM LRU handling

Am 08.08.2018 um 11:59 schrieb Huang Rui:

Quoted 97 lines

I continue to work for bulk moving that based on the proposal by Christian.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 22 ++++++++++++++--------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++++
  2 files changed, 18 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -6
index 9c84770..eda0bb9 100644
@@ -286,6 +286,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  {
  	struct ttm_bo_global *glob = adev->mman.bdev.glob;
  	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	bool validated = false;
  	int r = 0;
 
  	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
@@ -295,14 +296,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  			r = validate(param, bo);
  			if (r)
  				break;
-
-			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
-			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
-			spin_unlock(&glob->lru_lock);
  		}
 
+		validated = true;
  		if (bo->tbo.type != ttm_bo_type_kernel) {
  			spin_lock(&vm->moved_lock);
  			list_move(&bo_base->vm_status, &vm->moved);
@@ -312,6 +308,15 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		}
  	}
 
+	if (!validated) {
+		spin_lock(&glob->lru_lock);
+		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
+
+	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+
  	spin_lock(&glob->lru_lock);
  	list_for_each_entry(bo_base, &vm->idle, vm_status) {
  		struct amdgpu_bo *bo = bo_base->bo;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 330

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
@@ -319,9 +324,10 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		if (!bo->parent)
  			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
  		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
+						&vm->lru_bulk_move);

Here is still one problem: The BOs currently evicted are not yet in the idle state.

So this must be moved to the end of the state machine, probably best if we put that into a separate function.

Regards,
Christian.

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
  	}
  	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index 67a15d4..92725ac 100644
@@ -29,6 +29,7 @@
  #include <linux/rbtree.h>
  #include <drm/gpu_scheduler.h>
  #include <drm/drm_file.h>
+#include <drm/ttm/ttm_bo_driver.h>
 
  #include "amdgpu_sync.h"
  #include "amdgpu_ring.h"

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm { · line 235

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
@@ -226,6 +227,9 @@ struct amdgpu_vm {
 
  	/* Some basic info about the task */
  	struct amdgpu_task_info task_info;
+
+	/* Store positions of group of BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
  };
 
  struct amdgpu_vm_manager {

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

#8 lore raw

Re: [PATCH 4/5] drm/amdgpu: use bulk moves for efficient VM LRU handling

Quoted 111 lines · Christian König

Am 08.08.2018 um 11:59 schrieb Huang Rui:

Quoted 97 lines

I continue to work for bulk moving that based on the proposal by Christian.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 22 ++++++++++++++--------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++++
  2 files changed, 18 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -6
index 9c84770..eda0bb9 100644
@@ -286,6 +286,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  {
  	struct ttm_bo_global *glob = adev->mman.bdev.glob;
  	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	bool validated = false;
  	int r = 0;
 
  	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
@@ -295,14 +296,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  			r = validate(param, bo);
  			if (r)
  				break;
-
-			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
-			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
-			spin_unlock(&glob->lru_lock);
  		}
 
+		validated = true;
  		if (bo->tbo.type != ttm_bo_type_kernel) {
  			spin_lock(&vm->moved_lock);
  			list_move(&bo_base->vm_status, &vm->moved);
@@ -312,6 +308,15 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		}
  	}
 
+	if (!validated) {
+		spin_lock(&glob->lru_lock);
+		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
+
+	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+
  	spin_lock(&glob->lru_lock);
  	list_for_each_entry(bo_base, &vm->idle, vm_status) {
  		struct amdgpu_bo *bo = bo_base->bo;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 330

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
@@ -319,9 +324,10 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		if (!bo->parent)
  			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
  		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
+						&vm->lru_bulk_move);

Here is still one problem: The BOs currently evicted are not yet in the idle state.

Quoted 3 lines

So this must be moved to the end of the state machine, probably best if we put that into a separate function.

Thanks, so the evicted BOs should be behind the BOs in idle list that in the group between (first, last) of bulk_move_pos, right?

        spin_lock(&glob->lru_lock);
        list_for_each_entry(bo_base, &vm->idle, vm_status) {
                struct amdgpu_bo *bo = bo_base->bo;

                if (!bo->parent)
                        continue;

                ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                if (bo->shadow)
                        ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                &vm->lru_bulk_move);
        }
        list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
                struct amdgpu_bo *bo = bo_base->bo;
                if (!bo)
                        continue;

                ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                if (bo->shadow)
                        ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                &vm->lru_bulk_move);
        }
        spin_unlock(&glob->lru_lock);

Thanks,
Ray

Quoted 20 lines

Regards,
Christian.

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
  	}
  	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index 67a15d4..92725ac 100644
@@ -29,6 +29,7 @@
  #include <linux/rbtree.h>
  #include <drm/gpu_scheduler.h>
  #include <drm/drm_file.h>
+#include <drm/ttm/ttm_bo_driver.h>
 
  #include "amdgpu_sync.h"
  #include "amdgpu_ring.h"
@@ -226,6 +227,9 @@ struct amdgpu_vm {
 
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm { · line 235

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
  	/* Some basic info about the task */
  	struct amdgpu_task_info task_info;
+
+	/* Store positions of group of BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
  };
 
  struct amdgpu_vm_manager {

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

maintainer
#9 lore raw

Re: [PATCH 4/5] drm/amdgpu: use bulk moves for efficient VM LRU handling

Am 09.08.2018 um 08:18 schrieb Huang Rui:

Quoted 111 lines · Christian König
Quoted 110 lines

Am 08.08.2018 um 11:59 schrieb Huang Rui:

Quoted 97 lines

I continue to work for bulk moving that based on the proposal by Christian.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 22 ++++++++++++++--------
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++++
   2 files changed, 18 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -6
index 9c84770..eda0bb9 100644
@@ -286,6 +286,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   {
   	struct ttm_bo_global *glob = adev->mman.bdev.glob;
   	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	bool validated = false;
   	int r = 0;
 
   	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
@@ -295,14 +296,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   			r = validate(param, bo);
   			if (r)
   				break;
-
-			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
-			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
-			spin_unlock(&glob->lru_lock);
   		}
 
+		validated = true;
   		if (bo->tbo.type != ttm_bo_type_kernel) {
   			spin_lock(&vm->moved_lock);
   			list_move(&bo_base->vm_status, &vm->moved);
@@ -312,6 +308,15 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   		}
   	}
 
+	if (!validated) {
+		spin_lock(&glob->lru_lock);
+		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
+
+	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+
   	spin_lock(&glob->lru_lock);
   	list_for_each_entry(bo_base, &vm->idle, vm_status) {
   		struct amdgpu_bo *bo = bo_base->bo;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 330

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
@@ -319,9 +324,10 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   		if (!bo->parent)
   			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
   		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
+						&vm->lru_bulk_move);

Here is still one problem: The BOs currently evicted are not yet in the idle state.

Quoted 5 lines
Quoted 3 lines

So this must be moved to the end of the state machine, probably best if we put that into a separate function.

Thanks, so the evicted BOs should be behind the BOs in idle list that in the group between (first, last) of bulk_move_pos, right?

The order doesn't matter.

Quoted 14 lines
         spin_lock(&glob->lru_lock);
         list_for_each_entry(bo_base, &vm->idle, vm_status) {
                 struct amdgpu_bo *bo = bo_base->bo;

                 if (!bo->parent)
                         continue;

                 ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                 if (bo->shadow)
                         ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                 &vm->lru_bulk_move);
         }
         list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {

That won't work. The BOs get removed from the evicted list when they are validated.

You need to walk both the relocated as well as the moved list here.

Christian.

Quoted 34 lines
                 struct amdgpu_bo *bo = bo_base->bo;
                 if (!bo)
                         continue;

                 ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                 if (bo->shadow)
                         ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                 &vm->lru_bulk_move);
         }
         spin_unlock(&glob->lru_lock);

Thanks,
Ray

Quoted 20 lines

Regards,
Christian.

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
   	}
   	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index 67a15d4..92725ac 100644
@@ -29,6 +29,7 @@
   #include <linux/rbtree.h>
   #include <drm/gpu_scheduler.h>
   #include <drm/drm_file.h>
+#include <drm/ttm/ttm_bo_driver.h>
 
   #include "amdgpu_sync.h"
   #include "amdgpu_ring.h"
@@ -226,6 +227,9 @@ struct amdgpu_vm {
 
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm { · line 235

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
   	/* Some basic info about the task */
   	struct amdgpu_task_info task_info;
+
+	/* Store positions of group of BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
   };
 
   struct amdgpu_vm_manager {

_______________________________________________
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): fa9f6c9e-6ec5-0d54-8c86-5e4271efab51-5C7GfCeVMHo@public.gmane.org

author
#11 lore raw

Re: [PATCH 4/5] drm/amdgpu: use bulk moves for efficient VM LRU handling

Quoted 135 lines · Christian

Am 09.08.2018 um 08:18 schrieb Huang Rui:

Quoted 111 lines · Christian König
Quoted 110 lines

Am 08.08.2018 um 11:59 schrieb Huang Rui:

Quoted 97 lines

I continue to work for bulk moving that based on the proposal by Christian.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 22 ++++++++++++++--------
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++++
   2 files changed, 18 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -6
index 9c84770..eda0bb9 100644
@@ -286,6 +286,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   {
   	struct ttm_bo_global *glob = adev->mman.bdev.glob;
   	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	bool validated = false;
   	int r = 0;
 
   	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
@@ -295,14 +296,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   			r = validate(param, bo);
   			if (r)
   				break;
-
-			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
-			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
-			spin_unlock(&glob->lru_lock);
   		}
 
+		validated = true;
   		if (bo->tbo.type != ttm_bo_type_kernel) {
   			spin_lock(&vm->moved_lock);
   			list_move(&bo_base->vm_status, &vm->moved);
@@ -312,6 +308,15 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   		}
   	}
 
+	if (!validated) {
+		spin_lock(&glob->lru_lock);
+		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
+
+	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+
   	spin_lock(&glob->lru_lock);
   	list_for_each_entry(bo_base, &vm->idle, vm_status) {
   		struct amdgpu_bo *bo = bo_base->bo;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 330

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
@@ -319,9 +324,10 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   		if (!bo->parent)
   			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
   		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
+						&vm->lru_bulk_move);

Here is still one problem: The BOs currently evicted are not yet in the idle state.

Quoted 5 lines
Quoted 3 lines

So this must be moved to the end of the state machine, probably best if we put that into a separate function.

Thanks, so the evicted BOs should be behind the BOs in idle list that in the group between (first, last) of bulk_move_pos, right?

The order doesn't matter.

Quoted 14 lines
         spin_lock(&glob->lru_lock);
         list_for_each_entry(bo_base, &vm->idle, vm_status) {
                 struct amdgpu_bo *bo = bo_base->bo;

                 if (!bo->parent)
                         continue;

                 ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                 if (bo->shadow)
                         ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                 &vm->lru_bulk_move);
         }
         list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
Quoted 5 lines

That won't work. The BOs get removed from the evicted list when they are validated.

You need to walk both the relocated as well as the moved list here.

Is that because, at the start of the function, we already walk the evicted list, and some of BOs are validated and move to moved or relocated list, so here we need also walk both of them to make them together for bulk move?

Thanks,
Ray

Quoted 36 lines

Christian.

Quoted 34 lines
                 struct amdgpu_bo *bo = bo_base->bo;
                 if (!bo)
                         continue;

                 ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                 if (bo->shadow)
                         ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                 &vm->lru_bulk_move);
         }
         spin_unlock(&glob->lru_lock);

Thanks,
Ray

Quoted 20 lines

Regards,
Christian.

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
   	}
   	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index 67a15d4..92725ac 100644
@@ -29,6 +29,7 @@
   #include <linux/rbtree.h>
   #include <drm/gpu_scheduler.h>
   #include <drm/drm_file.h>
+#include <drm/ttm/ttm_bo_driver.h>
 
   #include "amdgpu_sync.h"
   #include "amdgpu_ring.h"
@@ -226,6 +227,9 @@ struct amdgpu_vm {
 
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm { · line 235

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
   	/* Some basic info about the task */
   	struct amdgpu_task_info task_info;
+
+	/* Store positions of group of BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
   };
 
   struct amdgpu_vm_manager {

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

#12 lore raw

Re: [PATCH 4/5] drm/amdgpu: use bulk moves for efficient VM LRU handling

Am 09.08.2018 um 14:25 schrieb Huang Rui:

Quoted 133 lines · Christian
Quoted 132 lines

Am 09.08.2018 um 08:18 schrieb Huang Rui:

Quoted 111 lines · Christian König
Quoted 110 lines

Am 08.08.2018 um 11:59 schrieb Huang Rui:

Quoted 97 lines

I continue to work for bulk moving that based on the proposal by Christian.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 22 ++++++++++++++--------
    drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++++
    2 files changed, 18 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -6
index 9c84770..eda0bb9 100644
@@ -286,6 +286,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    {
    	struct ttm_bo_global *glob = adev->mman.bdev.glob;
    	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	bool validated = false;
    	int r = 0;
 
    	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
@@ -295,14 +296,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    			r = validate(param, bo);
    			if (r)
    				break;
-
-			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
-			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
-			spin_unlock(&glob->lru_lock);
    		}
 
+		validated = true;
    		if (bo->tbo.type != ttm_bo_type_kernel) {
    			spin_lock(&vm->moved_lock);
    			list_move(&bo_base->vm_status, &vm->moved);
@@ -312,6 +308,15 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    		}
    	}
 
+	if (!validated) {
+		spin_lock(&glob->lru_lock);
+		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
+
+	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+
    	spin_lock(&glob->lru_lock);
    	list_for_each_entry(bo_base, &vm->idle, vm_status) {
    		struct amdgpu_bo *bo = bo_base->bo;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 330

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
@@ -319,9 +324,10 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    		if (!bo->parent)
    			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
    		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
+						&vm->lru_bulk_move);

Here is still one problem: The BOs currently evicted are not yet in the idle state.

Quoted 5 lines
Quoted 3 lines

So this must be moved to the end of the state machine, probably best if we put that into a separate function.

Thanks, so the evicted BOs should be behind the BOs in idle list that in the group between (first, last) of bulk_move_pos, right?

The order doesn't matter.

Quoted 13 lines
          spin_lock(&glob->lru_lock);
          list_for_each_entry(bo_base, &vm->idle, vm_status) {
                  struct amdgpu_bo *bo = bo_base->bo;

                  if (!bo->parent)
                          continue;

                  ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                  if (bo->shadow)
                          ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                  &vm->lru_bulk_move);
          }
          list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
Quoted 8 lines
Quoted 5 lines

That won't work. The BOs get removed from the evicted list when they are validated.

You need to walk both the relocated as well as the moved list here.

Is that because, at the start of the function, we already walk the evicted list, and some of BOs are validated and move to moved or relocated list, so here we need also walk both of them to make them together for bulk move?

Yes, exactly.

Alternatively you can add a new function which is called after command submission is done to move everything on the LRU.

At this point all BOs should now be on the idle list again and you only need to walk that one.

Regards,
Christian.

Quoted 40 lines

Thanks,
Ray

Quoted 36 lines

Christian.

Quoted 34 lines
                  struct amdgpu_bo *bo = bo_base->bo;
                  if (!bo)
                          continue;

                  ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                  if (bo->shadow)
                          ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                  &vm->lru_bulk_move);
          }
          spin_unlock(&glob->lru_lock);

Thanks,
Ray

Quoted 20 lines

Regards,
Christian.

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
    	}
    	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index 67a15d4..92725ac 100644
@@ -29,6 +29,7 @@
    #include <linux/rbtree.h>
    #include <drm/gpu_scheduler.h>
    #include <drm/drm_file.h>
+#include <drm/ttm/ttm_bo_driver.h>
 
    #include "amdgpu_sync.h"
    #include "amdgpu_ring.h"
@@ -226,6 +227,9 @@ struct amdgpu_vm {
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm { · line 235

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
    	/* Some basic info about the task */
    	struct amdgpu_task_info task_info;
+
+	/* Store positions of group of BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
    };
 
    struct amdgpu_vm_manager {

_______________________________________________
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): fd0fe9b8-2216-2d47-7a7a-a9bbdd0c5c53-5C7GfCeVMHo@public.gmane.org

author
#14 lore raw

Re: [PATCH 4/5] drm/amdgpu: use bulk moves for efficient VM LRU handling

Quoted 143 lines · Christian

Am 09.08.2018 um 14:25 schrieb Huang Rui:

Quoted 133 lines · Christian
Quoted 132 lines

Am 09.08.2018 um 08:18 schrieb Huang Rui:

Quoted 111 lines · Christian König
Quoted 110 lines

Am 08.08.2018 um 11:59 schrieb Huang Rui:

Quoted 97 lines

I continue to work for bulk moving that based on the proposal by Christian.

Background:
amdgpu driver will move all PD/PT and PerVM BOs into idle list. Then move all of them on the end of LRU list one by one. Thus, that cause so many BOs moved to the end of the LRU, and impact performance seriously.

Then Christian provided a workaround to not move PD/PT BOs on LRU with below patch:
"drm/amdgpu: band aid validating VM PTs"
Commit 0bbf32026cf5ba41e9922b30e26e1bed1ecd38ae

However, the final solution should bulk move all PD/PT and PerVM BOs on the LRU instead of one by one.

Whenever amdgpu_vm_validate_pt_bos() is called and we have BOs which need to be validated we move all BOs together to the end of the LRU without dropping the lock for the LRU.

While doing so we note the beginning and end of this block in the LRU list.

Now when amdgpu_vm_validate_pt_bos() is called and we don't have anything to do, we don't move every BO one by one, but instead cut the LRU list into pieces so that we bulk move everything to the end in just one operation.

Test data:

+--------------+-----------------+-----------+---------------------------------------+
|              |The Talos        |Clpeak(OCL)|BusSpeedReadback(OCL)                  |
|              |Principle(Vulkan)|           |                                       |
+------------------------------------------------------------------------------------+
|              |                 |           |0.319 ms(1k) 0.314 ms(2K) 0.308 ms(4K) |
| Original     |  147.7 FPS      |  76.86 us |0.307 ms(8K) 0.310 ms(16K)             |
+------------------------------------------------------------------------------------+
| Orignial + WA|                 |           |0.254 ms(1K) 0.241 ms(2K)              |
|(don't move   |  162.1 FPS      |  42.15 us |0.230 ms(4K) 0.223 ms(8K) 0.204 ms(16K)|
|PT BOs on LRU)|                 |           |                                       |
+------------------------------------------------------------------------------------+
| Bulk move    |  163.1 FPS      |  40.52 us |0.244 ms(1K) 0.252 ms(2K) 0.213 ms(4K) |
|              |                 |           |0.214 ms(8K) 0.225 ms(16K)             |
+--------------+-----------------+-----------+---------------------------------------+

After test them with above three benchmarks include vulkan and opencl. We can see the visible improvement than original, and even better than original with workaround.

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 22 ++++++++++++++--------
    drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 ++++
    2 files changed, 18 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -6
index 9c84770..eda0bb9 100644
@@ -286,6 +286,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    {
    	struct ttm_bo_global *glob = adev->mman.bdev.glob;
    	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	bool validated = false;
    	int r = 0;
 
    	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
@@ -295,14 +296,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    			r = validate(param, bo);
    			if (r)
    				break;
-
-			spin_lock(&glob->lru_lock);
-			ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
-			if (bo->shadow)
-				ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
-			spin_unlock(&glob->lru_lock);
    		}
 
+		validated = true;
    		if (bo->tbo.type != ttm_bo_type_kernel) {
    			spin_lock(&vm->moved_lock);
    			list_move(&bo_base->vm_status, &vm->moved);
@@ -312,6 +308,15 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    		}
    	}
 
+	if (!validated) {
+		spin_lock(&glob->lru_lock);
+		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		spin_unlock(&glob->lru_lock);
+		return 0;
+	}
+
+	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+
    	spin_lock(&glob->lru_lock);
    	list_for_each_entry(bo_base, &vm->idle, vm_status) {
    		struct amdgpu_bo *bo = bo_base->bo;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 330

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
@@ -319,9 +324,10 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    		if (!bo->parent)
    			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, NULL);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
    		if (bo->shadow)
-			ttm_bo_move_to_lru_tail(&bo->shadow->tbo, NULL);
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
+						&vm->lru_bulk_move);

Here is still one problem: The BOs currently evicted are not yet in the idle state.

Quoted 5 lines
Quoted 3 lines

So this must be moved to the end of the state machine, probably best if we put that into a separate function.

Thanks, so the evicted BOs should be behind the BOs in idle list that in the group between (first, last) of bulk_move_pos, right?

The order doesn't matter.

Quoted 13 lines
          spin_lock(&glob->lru_lock);
          list_for_each_entry(bo_base, &vm->idle, vm_status) {
                  struct amdgpu_bo *bo = bo_base->bo;

                  if (!bo->parent)
                          continue;

                  ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                  if (bo->shadow)
                          ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                  &vm->lru_bulk_move);
          }
          list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
Quoted 8 lines
Quoted 5 lines

That won't work. The BOs get removed from the evicted list when they are validated.

You need to walk both the relocated as well as the moved list here.

Is that because, at the start of the function, we already walk the evicted list, and some of BOs are validated and move to moved or relocated list, so here we need also walk both of them to make them together for bulk move?

Quoted 7 lines

Yes, exactly.

Alternatively you can add a new function which is called after command submission is done to move everything on the LRU.

At this point all BOs should now be on the idle list again and you only need to walk that one.

Do you mean we move all BOs to idle list again in amdgpu_vm_bo_update of amdgpu_cs_ib_vm_chunk subsequently?
But at that time, I see if the BO is evicted, it will be moved to evicted list, not to idle list again. Am I missed something?

Thanks,
Ray

Quoted 44 lines

Regards,
Christian.

Quoted 40 lines

Thanks,
Ray

Quoted 36 lines

Christian.

Quoted 34 lines
                  struct amdgpu_bo *bo = bo_base->bo;
                  if (!bo)
                          continue;

                  ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
                  if (bo->shadow)
                          ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
                                                  &vm->lru_bulk_move);
          }
          spin_unlock(&glob->lru_lock);

Thanks,
Ray

Quoted 20 lines

Regards,
Christian.

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
    	}
    	spin_unlock(&glob->lru_lock);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index 67a15d4..92725ac 100644
@@ -29,6 +29,7 @@
    #include <linux/rbtree.h>
    #include <drm/gpu_scheduler.h>
    #include <drm/drm_file.h>
+#include <drm/ttm/ttm_bo_driver.h>
 
    #include "amdgpu_sync.h"
    #include "amdgpu_ring.h"
@@ -226,6 +227,9 @@ struct amdgpu_vm {
 
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm { · line 235

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
    	/* Some basic info about the task */
    	struct amdgpu_task_info task_info;
+
+	/* Store positions of group of BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
    };
 
    struct amdgpu_vm_manager {

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

#15 lore raw

PATCH 5/5 drm/amdgpu: move PD/PT bos on LRU again

In reply to Huang Rui

The new bulk moving functionality is ready, the overhead of moving PD/PT bos to LRU is fixed. So move them on LRU again.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index eda0bb9..3550166 100644
@@ -1090,7 +1090,7 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 					   struct amdgpu_vm_bo_base,
 					   vm_status);
 		bo_base->moved = false;
-		list_del_init(&bo_base->vm_status);
+		list_move(&bo_base->vm_status, &vm->idle);
 
 		bo = bo_base->bo->parent;
 		if (!bo)
Signature · 6 lines
2.7.4

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