Skip to contentSkip to conversation

drm/amdgpu: rework VM state machine lock handling v2

PATCH In review Christian König 3 participants 10 messages May 17, 2018 – May 22, 2018 4 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/6 drm/amdgpu: rework VM state machine lock handling v2

Only the moved state needs a separate spin lock protection. All other states are protected by reserving the VM anyway.

v2: fix some more incorrect cases

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 66 +++++++++++-----------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 +--
 2 files changed, 21 insertions(+), 49 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +20 -46
index 1a8f4e0dd023..f0deedcaf1c9 100644
@@ -119,9 +119,7 @@ static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
 	 * is currently evicted. add the bo to the evicted list to make sure it
 	 * is validated on next vm use to avoid fault.
 	 * */
-	spin_lock(&vm->status_lock);
 	list_move_tail(&base->vm_status, &vm->evicted);
-	spin_unlock(&vm->status_lock);
 }
 
 /**
@@ -228,7 +226,6 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	struct ttm_bo_global *glob = adev->mman.bdev.glob;
 	int r;
 
-	spin_lock(&vm->status_lock);
 	while (!list_empty(&vm->evicted)) {
 		struct amdgpu_vm_bo_base *bo_base;
 		struct amdgpu_bo *bo;
@@ -236,10 +233,8 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		bo_base = list_first_entry(&vm->evicted,
 					   struct amdgpu_vm_bo_base,
 					   vm_status);
-		spin_unlock(&vm->status_lock);
 
 		bo = bo_base->bo;
-		BUG_ON(!bo);
 		if (bo->parent) {
 			r = validate(param, bo);
 			if (r)
@@ -259,13 +254,14 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 				return r;
 		}
 
-		spin_lock(&vm->status_lock);
-		if (bo->tbo.type != ttm_bo_type_kernel)
+		if (bo->tbo.type != ttm_bo_type_kernel) {
+			spin_lock(&vm->moved_lock);
 			list_move(&bo_base->vm_status, &vm->moved);
-		else
+			spin_unlock(&vm->moved_lock);
+		} else {
 			list_move(&bo_base->vm_status, &vm->relocated);
+		}
 	}
-	spin_unlock(&vm->status_lock);
 
 	return 0;
 }
@@ -279,13 +275,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  */
 bool amdgpu_vm_ready(struct amdgpu_vm *vm)
 {
-	bool ready;
-
-	spin_lock(&vm->status_lock);
-	ready = list_empty(&vm->evicted);
-	spin_unlock(&vm->status_lock);
-
-	return ready;
+	return list_empty(&vm->evicted);
 }
 
 /**
@@ -477,9 +467,7 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
 			pt->parent = amdgpu_bo_ref(parent->base.bo);
 
 			amdgpu_vm_bo_base_init(&entry->base, vm, pt);
-			spin_lock(&vm->status_lock);
 			list_move(&entry->base.vm_status, &vm->relocated);
-			spin_unlock(&vm->status_lock);
 		}
 
 		if (level < AMDGPU_VM_PTB) {
@@ -926,10 +914,8 @@ static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
 		if (!entry->base.bo)
 			continue;
 
-		spin_lock(&vm->status_lock);
 		if (list_empty(&entry->base.vm_status))
 			list_add(&entry->base.vm_status, &vm->relocated);
-		spin_unlock(&vm->status_lock);
 		amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
 	}
 }
@@ -974,7 +960,6 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 		params.func = amdgpu_vm_do_set_ptes;
 	}
 
-	spin_lock(&vm->status_lock);
 	while (!list_empty(&vm->relocated)) {
 		struct amdgpu_vm_bo_base *bo_base, *parent;
 		struct amdgpu_vm_pt *pt, *entry;
@@ -984,13 +969,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 					   struct amdgpu_vm_bo_base,
 					   vm_status);
 		list_del_init(&bo_base->vm_status);
-		spin_unlock(&vm->status_lock);
 
 		bo = bo_base->bo->parent;
-		if (!bo) {
-			spin_lock(&vm->status_lock);
+		if (!bo)
 			continue;
-		}
 
 		parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
 					  bo_list);
@@ -999,12 +981,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 
 		amdgpu_vm_update_pde(&params, vm, pt, entry);
 
-		spin_lock(&vm->status_lock);
 		if (!vm->use_cpu_for_update &&
 		    (ndw - params.ib->length_dw) < 32)
 			break;
 	}
-	spin_unlock(&vm->status_lock);
 
 	if (vm->use_cpu_for_update) {
 		/* Flush HDP */
@@ -1107,9 +1087,7 @@ static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
 		if (entry->huge) {
 			/* Add the entry to the relocated list to update it. */
 			entry->huge = false;
-			spin_lock(&p->vm->status_lock);
 			list_move(&entry->base.vm_status, &p->vm->relocated);
-			spin_unlock(&p->vm->status_lock);
 		}
 		return;
 	}
@@ -1588,8 +1566,9 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 		amdgpu_asic_flush_hdp(adev, NULL);
 	}
 
-	spin_lock(&vm->status_lock);
+	spin_lock(&vm->moved_lock);
 	list_del_init(&bo_va->base.vm_status);
+	spin_unlock(&vm->moved_lock);
 
 	/* If the BO is not in its preferred location add it back to
 	 * the evicted list so that it gets validated again on the
@@ -1599,7 +1578,6 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 	    !(bo->preferred_domains &
 	    amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)))
 		list_add_tail(&bo_va->base.vm_status, &vm->evicted);
-	spin_unlock(&vm->status_lock);
 
 	list_splice_init(&bo_va->invalids, &bo_va->valids);
 	bo_va->cleared = clear;
@@ -1811,14 +1789,14 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 	bool clear;
 	int r = 0;
 
-	spin_lock(&vm->status_lock);
+	spin_lock(&vm->moved_lock);
 	while (!list_empty(&vm->moved)) {
 		struct amdgpu_bo_va *bo_va;
 		struct reservation_object *resv;
 
 		bo_va = list_first_entry(&vm->moved,
 			struct amdgpu_bo_va, base.vm_status);
-		spin_unlock(&vm->status_lock);
+		spin_unlock(&vm->moved_lock);
 
 		resv = bo_va->base.bo->tbo.resv;
 
@@ -1839,9 +1817,9 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 		if (!clear && resv != vm->root.base.bo->tbo.resv)
 			reservation_object_unlock(resv);
 
-		spin_lock(&vm->status_lock);
+		spin_lock(&vm->moved_lock);
 	}
-	spin_unlock(&vm->status_lock);
+	spin_unlock(&vm->moved_lock);
 
 	return r;
 }
@@ -1903,10 +1881,10 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
 		amdgpu_vm_prt_get(adev);
 
 	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-		spin_lock(&vm->status_lock);
+		spin_lock(&vm->moved_lock);
 		if (list_empty(&bo_va->base.vm_status))
 			list_add(&bo_va->base.vm_status, &vm->moved);
-		spin_unlock(&vm->status_lock);
+		spin_unlock(&vm->moved_lock);
 	}
 	trace_amdgpu_vm_bo_map(bo_va, mapping);
 }
@@ -2216,9 +2194,9 @@ void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
 
 	list_del(&bo_va->base.bo_list);
 
-	spin_lock(&vm->status_lock);
+	spin_lock(&vm->moved_lock);
 	list_del(&bo_va->base.vm_status);
-	spin_unlock(&vm->status_lock);
+	spin_unlock(&vm->moved_lock);
 
 	list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
 		list_del(&mapping->list);
@@ -2261,28 +2239,24 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
 
 		bo_base->moved = true;
 		if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-			spin_lock(&bo_base->vm->status_lock);
 			if (bo->tbo.type == ttm_bo_type_kernel)
 				list_move(&bo_base->vm_status, &vm->evicted);
 			else
 				list_move_tail(&bo_base->vm_status,
 					       &vm->evicted);
-			spin_unlock(&bo_base->vm->status_lock);
 			continue;
 		}
 
 		if (bo->tbo.type == ttm_bo_type_kernel) {
-			spin_lock(&bo_base->vm->status_lock);
 			if (list_empty(&bo_base->vm_status))
 				list_add(&bo_base->vm_status, &vm->relocated);
-			spin_unlock(&bo_base->vm->status_lock);
 			continue;
 		}
 
-		spin_lock(&bo_base->vm->status_lock);
+		spin_lock(&bo_base->vm->moved_lock);
 		if (list_empty(&bo_base->vm_status))
 			list_add(&bo_base->vm_status, &vm->moved);
-		spin_unlock(&bo_base->vm->status_lock);
+		spin_unlock(&bo_base->vm->moved_lock);
 	}
 }
 
@@ -2391,9 +2365,9 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	vm->va = RB_ROOT_CACHED;
 	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
 		vm->reserved_vmid[i] = NULL;
-	spin_lock_init(&vm->status_lock);
 	INIT_LIST_HEAD(&vm->evicted);
 	INIT_LIST_HEAD(&vm->relocated);
+	spin_lock_init(&vm->moved_lock);
 	INIT_LIST_HEAD(&vm->moved);
 	INIT_LIST_HEAD(&vm->freed);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -3
index d6827083572a..0196b9a782f2 100644
@@ -168,9 +168,6 @@ struct amdgpu_vm {
 	/* tree of virtual addresses mapped */
 	struct rb_root_cached	va;
 
-	/* protecting invalidated */
-	spinlock_t		status_lock;
-
 	/* BOs who needs a validation */
 	struct list_head	evicted;
 
@@ -179,6 +176,7 @@ struct amdgpu_vm {
 
 	/* BOs moved, but not yet updated in the PT */
 	struct list_head	moved;
+	spinlock_t		moved_lock;
 
 	/* BO mappings freed, but not yet updated in the PT */
 	struct list_head	freed;
Signature · 6 lines
2.14.1

_______________________________________________
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): 20180517094936.4293-1-christian.koenig-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 2/6 drm/amdgpu: cleanup amdgpu_vm_validate_pt_bos v2

Use list_for_each_entry_safe here.

v2: Drop the optimization, it doesn't work as expected.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -12
index f0deedcaf1c9..3be4d5fc60b3 100644
@@ -224,21 +224,16 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			      void *param)
 {
 	struct ttm_bo_global *glob = adev->mman.bdev.glob;
-	int r;
-
-	while (!list_empty(&vm->evicted)) {
-		struct amdgpu_vm_bo_base *bo_base;
-		struct amdgpu_bo *bo;
+	struct amdgpu_vm_bo_base *bo_base, *tmp;
+	int r = 0;
 
-		bo_base = list_first_entry(&vm->evicted,
-					   struct amdgpu_vm_bo_base,
-					   vm_status);
+	list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
+		struct amdgpu_bo *bo = bo_base->bo;
 
-		bo = bo_base->bo;
 		if (bo->parent) {
 			r = validate(param, bo);
 			if (r)
-				return r;
+				break;
 
 			spin_lock(&glob->lru_lock);
 			ttm_bo_move_to_lru_tail(&bo->tbo);
@@ -251,7 +246,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		    vm->use_cpu_for_update) {
 			r = amdgpu_bo_kmap(bo, NULL);
 			if (r)
-				return r;
+				break;
 		}
 
 		if (bo->tbo.type != ttm_bo_type_kernel) {
@@ -263,7 +258,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		}
 	}
 
-	return 0;
+	return r;
 }
 
 /**
Signature · 6 lines
2.14.1

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

PATCH 3/6 drm/amdgpu: further optimize amdgpu_vm_handle_moved

In reply to a message not in the archive

Splice the moved list to a local one to avoid taking the lock over and over again.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +14 -13
index 3be4d5fc60b3..4d88b060fbde 100644
@@ -1781,19 +1781,18 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 			   struct amdgpu_vm *vm)
 {
+	struct amdgpu_bo_va *bo_va, *tmp;
+	struct list_head moved;
 	bool clear;
-	int r = 0;
+	int r;
 
+	INIT_LIST_HEAD(&moved);
 	spin_lock(&vm->moved_lock);
-	while (!list_empty(&vm->moved)) {
-		struct amdgpu_bo_va *bo_va;
-		struct reservation_object *resv;
-
-		bo_va = list_first_entry(&vm->moved,
-			struct amdgpu_bo_va, base.vm_status);
-		spin_unlock(&vm->moved_lock);
+	list_splice_init(&vm->moved, &moved);
+	spin_unlock(&vm->moved_lock);
 
-		resv = bo_va->base.bo->tbo.resv;
+	list_for_each_entry_safe(bo_va, tmp, &moved, base.vm_status) {
+		struct reservation_object *resv = bo_va->base.bo->tbo.resv;
 
 		/* Per VM BOs never need to bo cleared in the page tables */
 		if (resv == vm->root.base.bo->tbo.resv)
@@ -1806,17 +1805,19 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 			clear = true;
 
 		r = amdgpu_vm_bo_update(adev, bo_va, clear);
-		if (r)
+		if (r) {
+			spin_lock(&vm->moved_lock);
+			list_splice(&moved, &vm->moved);
+			spin_unlock(&vm->moved_lock);
 			return r;
+		}
 
 		if (!clear && resv != vm->root.base.bo->tbo.resv)
 			reservation_object_unlock(resv);
 
-		spin_lock(&vm->moved_lock);
 	}
-	spin_unlock(&vm->moved_lock);
 
-	return r;
+	return 0;
 }
 
 /**
Signature · 6 lines
2.14.1

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

PATCH 4/6 drm/amdgpu: kmap PDs/PTs in amdgpu_vm_update_directories

In reply to a message not in the archive

In theory it is possible that PDs/PTs can move without eviction.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8 -7
index 4d88b060fbde..a31afac8e8e9 100644
@@ -242,13 +242,6 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			spin_unlock(&glob->lru_lock);
 		}
 
-		if (bo->tbo.type == ttm_bo_type_kernel &&
-		    vm->use_cpu_for_update) {
-			r = amdgpu_bo_kmap(bo, NULL);
-			if (r)
-				break;
-		}
-
 		if (bo->tbo.type != ttm_bo_type_kernel) {
 			spin_lock(&vm->moved_lock);
 			list_move(&bo_base->vm_status, &vm->moved);
@@ -940,6 +933,14 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 	params.adev = adev;
 
 	if (vm->use_cpu_for_update) {
+		struct amdgpu_vm_bo_base *bo_base;
+
+		list_for_each_entry(bo_base, &vm->relocated, vm_status) {
+			r = amdgpu_bo_kmap(bo_base->bo, NULL);
+			if (unlikely(r))
+				return r;
+		}
+
 		r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
 		if (unlikely(r))
 			return r;
Signature · 6 lines
2.14.1

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

PATCH 5/6 drm/amdgpu: consistenly use VM moved flag

In reply to a message not in the archive

Instead of sometimes checking if the vm_status is empty use the moved flag and also reset it when the BO leaves the state machine.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +15 -13
index a31afac8e8e9..f5dee4c6757c 100644
@@ -902,8 +902,8 @@ static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
 		if (!entry->base.bo)
 			continue;
 
-		if (list_empty(&entry->base.vm_status))
-			list_add(&entry->base.vm_status, &vm->relocated);
+		if (!entry->base.moved)
+			list_move(&entry->base.vm_status, &vm->relocated);
 		amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
 	}
 }
@@ -964,6 +964,7 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 		bo_base = list_first_entry(&vm->relocated,
 					   struct amdgpu_vm_bo_base,
 					   vm_status);
+		bo_base->moved = false;
 		list_del_init(&bo_base->vm_status);
 
 		bo = bo_base->bo->parent;
@@ -1877,10 +1878,10 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
 	if (mapping->flags & AMDGPU_PTE_PRT)
 		amdgpu_vm_prt_get(adev);
 
-	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
+	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
+	    !bo_va->base.moved) {
 		spin_lock(&vm->moved_lock);
-		if (list_empty(&bo_va->base.vm_status))
-			list_add(&bo_va->base.vm_status, &vm->moved);
+		list_move(&bo_va->base.vm_status, &vm->moved);
 		spin_unlock(&vm->moved_lock);
 	}
 	trace_amdgpu_vm_bo_map(bo_va, mapping);
@@ -2233,6 +2234,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
 
 	list_for_each_entry(bo_base, &bo->va, bo_list) {
 		struct amdgpu_vm *vm = bo_base->vm;
+		bool was_moved = bo_base->moved;
 
 		bo_base->moved = true;
 		if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
@@ -2244,16 +2246,16 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
 			continue;
 		}
 
-		if (bo->tbo.type == ttm_bo_type_kernel) {
-			if (list_empty(&bo_base->vm_status))
-				list_add(&bo_base->vm_status, &vm->relocated);
+		if (was_moved)
 			continue;
-		}
 
-		spin_lock(&bo_base->vm->moved_lock);
-		if (list_empty(&bo_base->vm_status))
-			list_add(&bo_base->vm_status, &vm->moved);
-		spin_unlock(&bo_base->vm->moved_lock);
+		if (bo->tbo.type == ttm_bo_type_kernel) {
+			list_move(&bo_base->vm_status, &vm->relocated);
+		} else {
+			spin_lock(&bo_base->vm->moved_lock);
+			list_move(&bo_base->vm_status, &vm->moved);
+			spin_unlock(&bo_base->vm->moved_lock);
+		}
 	}
 }
 
Signature · 6 lines
2.14.1

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

PATCH 6/6 drm/amdgpu: move VM BOs on LRU again

In reply to a message not in the archive

Move all BOs belonging to a VM on the LRU with every submission.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 28 +++++++++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  3 +++
 2 files changed, 26 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +23 -5
index f5dee4c6757c..ccba88cc8c54 100644
@@ -251,6 +251,19 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		}
 	}
 
+	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);
+		if (bo->shadow)
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
+	}
+	spin_unlock(&glob->lru_lock);
+
 	return r;
 }
 
@@ -965,7 +978,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)
@@ -1571,10 +1584,14 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 	 * the evicted list so that it gets validated again on the
 	 * next command submission.
 	 */
-	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
-	    !(bo->preferred_domains &
-	    amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)))
-		list_add_tail(&bo_va->base.vm_status, &vm->evicted);
+	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
+		uint32_t mem_type = bo->tbo.mem.mem_type;
+
+		if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type)))
+			list_add_tail(&bo_va->base.vm_status, &vm->evicted);
+		else
+			list_add(&bo_va->base.vm_status, &vm->idle);
+	}
 
 	list_splice_init(&bo_va->invalids, &bo_va->valids);
 	bo_va->cleared = clear;
@@ -2368,6 +2385,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	INIT_LIST_HEAD(&vm->relocated);
 	spin_lock_init(&vm->moved_lock);
 	INIT_LIST_HEAD(&vm->moved);
+	INIT_LIST_HEAD(&vm->idle);
 	INIT_LIST_HEAD(&vm->freed);
 
 	/* create scheduler entity for page table updates */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
index 0196b9a782f2..061b99a18cb8 100644
@@ -178,6 +178,9 @@ struct amdgpu_vm {
 	struct list_head	moved;
 	spinlock_t		moved_lock;
 
+	/* All BOs of this VM not currently in the state machine */
+	struct list_head	idle;
+
 	/* BO mappings freed, but not yet updated in the PT */
 	struct list_head	freed;
 
Signature · 6 lines
2.14.1

_______________________________________________
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): 20180517094936.4293-6-christian.koenig-5C7GfCeVMHo@public.gmane.org

#8 lore raw

Re: [PATCH 6/6] drm/amdgpu: move VM BOs on LRU again

CPU overhead is increased a bit, but we can optimize it later, the series is Reviewed-by: Chunming Zhou <[email protected]>

Quoted 73 lines · Christian König

Move all BOs belonging to a VM on the LRU with every submission.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 28 +++++++++++++++++++++++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  3 +++
  2 files changed, 26 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +23 -5
index f5dee4c6757c..ccba88cc8c54 100644
@@ -251,6 +251,19 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		}
  	}
 
+	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);
+		if (bo->shadow)
+			ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
+	}
+	spin_unlock(&glob->lru_lock);
+
  	return r;
  }
 
@@ -965,7 +978,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)
@@ -1571,10 +1584,14 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  	 * the evicted list so that it gets validated again on the
  	 * next command submission.
  	 */
-	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
-	    !(bo->preferred_domains &
-	    amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)))
-		list_add_tail(&bo_va->base.vm_status, &vm->evicted);
+	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
+		uint32_t mem_type = bo->tbo.mem.mem_type;
+
+		if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type)))
+			list_add_tail(&bo_va->base.vm_status, &vm->evicted);
+		else
+			list_add(&bo_va->base.vm_status, &vm->idle);
+	}
 
  	list_splice_init(&bo_va->invalids, &bo_va->valids);
  	bo_va->cleared = clear;
@@ -2368,6 +2385,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	INIT_LIST_HEAD(&vm->relocated);
  	spin_lock_init(&vm->moved_lock);
  	INIT_LIST_HEAD(&vm->moved);
+	INIT_LIST_HEAD(&vm->idle);
  	INIT_LIST_HEAD(&vm->freed);
 
  	/* create scheduler entity for page table updates */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index 0196b9a782f2..061b99a18cb8 100644

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
@@ -178,6 +178,9 @@ struct amdgpu_vm {
  	struct list_head	moved;
  	spinlock_t		moved_lock;
 
+	/* All BOs of this VM not currently in the state machine */
+	struct list_head	idle;
+
  	/* BO mappings freed, but not yet updated in the PT */
  	struct list_head	freed;
 

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

Reviewed-by Junwei Zhang
#9 lore raw
In reply to a message not in the archive

2, 3, 4, 5 are

Patch 1:
could you show the reserving VM?

Patch 6:
I could read that code, but not sure the purpose.

Jerry

Quoted 276 lines · Christian König

Only the moved state needs a separate spin lock protection. All other states are protected by reserving the VM anyway.

v2: fix some more incorrect cases

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 66 +++++++++++-----------------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 +--
  2 files changed, 21 insertions(+), 49 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +20 -46
index 1a8f4e0dd023..f0deedcaf1c9 100644
@@ -119,9 +119,7 @@ static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
  	 * is currently evicted. add the bo to the evicted list to make sure it
  	 * is validated on next vm use to avoid fault.
  	 * */
-	spin_lock(&vm->status_lock);
  	list_move_tail(&base->vm_status, &vm->evicted);
-	spin_unlock(&vm->status_lock);
  }
 
  /**
@@ -228,7 +226,6 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	struct ttm_bo_global *glob = adev->mman.bdev.glob;
  	int r;
 
-	spin_lock(&vm->status_lock);
  	while (!list_empty(&vm->evicted)) {
  		struct amdgpu_vm_bo_base *bo_base;
  		struct amdgpu_bo *bo;
@@ -236,10 +233,8 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		bo_base = list_first_entry(&vm->evicted,
  					   struct amdgpu_vm_bo_base,
  					   vm_status);
-		spin_unlock(&vm->status_lock);
 
  		bo = bo_base->bo;
-		BUG_ON(!bo);
  		if (bo->parent) {
  			r = validate(param, bo);
  			if (r)
@@ -259,13 +254,14 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  				return r;
  		}
 
-		spin_lock(&vm->status_lock);
-		if (bo->tbo.type != ttm_bo_type_kernel)
+		if (bo->tbo.type != ttm_bo_type_kernel) {
+			spin_lock(&vm->moved_lock);
  			list_move(&bo_base->vm_status, &vm->moved);
-		else
+			spin_unlock(&vm->moved_lock);
+		} else {
  			list_move(&bo_base->vm_status, &vm->relocated);
+		}
  	}
-	spin_unlock(&vm->status_lock);
 
  	return 0;
  }
@@ -279,13 +275,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   */
  bool amdgpu_vm_ready(struct amdgpu_vm *vm)
  {
-	bool ready;
-
-	spin_lock(&vm->status_lock);
-	ready = list_empty(&vm->evicted);
-	spin_unlock(&vm->status_lock);
-
-	return ready;
+	return list_empty(&vm->evicted);
  }
 
  /**
@@ -477,9 +467,7 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
  			pt->parent = amdgpu_bo_ref(parent->base.bo);
 
  			amdgpu_vm_bo_base_init(&entry->base, vm, pt);
-			spin_lock(&vm->status_lock);
  			list_move(&entry->base.vm_status, &vm->relocated);
-			spin_unlock(&vm->status_lock);
  		}
 
  		if (level < AMDGPU_VM_PTB) {
@@ -926,10 +914,8 @@ static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
  		if (!entry->base.bo)
  			continue;
 
-		spin_lock(&vm->status_lock);
  		if (list_empty(&entry->base.vm_status))
  			list_add(&entry->base.vm_status, &vm->relocated);
-		spin_unlock(&vm->status_lock);
  		amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
  	}
  }
@@ -974,7 +960,6 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  		params.func = amdgpu_vm_do_set_ptes;
  	}
 
-	spin_lock(&vm->status_lock);
  	while (!list_empty(&vm->relocated)) {
  		struct amdgpu_vm_bo_base *bo_base, *parent;
  		struct amdgpu_vm_pt *pt, *entry;
@@ -984,13 +969,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  					   struct amdgpu_vm_bo_base,
  					   vm_status);
  		list_del_init(&bo_base->vm_status);
-		spin_unlock(&vm->status_lock);
 
  		bo = bo_base->bo->parent;
-		if (!bo) {
-			spin_lock(&vm->status_lock);
+		if (!bo)
  			continue;
-		}
 
  		parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
  					  bo_list);
@@ -999,12 +981,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 
  		amdgpu_vm_update_pde(&params, vm, pt, entry);
 
-		spin_lock(&vm->status_lock);
  		if (!vm->use_cpu_for_update &&
  		    (ndw - params.ib->length_dw) < 32)
  			break;
  	}
-	spin_unlock(&vm->status_lock);
 
  	if (vm->use_cpu_for_update) {
  		/* Flush HDP */
@@ -1107,9 +1087,7 @@ static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
  		if (entry->huge) {
  			/* Add the entry to the relocated list to update it. */
  			entry->huge = false;
-			spin_lock(&p->vm->status_lock);
  			list_move(&entry->base.vm_status, &p->vm->relocated);
-			spin_unlock(&p->vm->status_lock);
  		}
  		return;
  	}
@@ -1588,8 +1566,9 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  		amdgpu_asic_flush_hdp(adev, NULL);
  	}
 
-	spin_lock(&vm->status_lock);
+	spin_lock(&vm->moved_lock);
  	list_del_init(&bo_va->base.vm_status);
+	spin_unlock(&vm->moved_lock);
 
  	/* If the BO is not in its preferred location add it back to
  	 * the evicted list so that it gets validated again on the
@@ -1599,7 +1578,6 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  	    !(bo->preferred_domains &
  	    amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)))
  		list_add_tail(&bo_va->base.vm_status, &vm->evicted);
-	spin_unlock(&vm->status_lock);
 
  	list_splice_init(&bo_va->invalids, &bo_va->valids);
  	bo_va->cleared = clear;
@@ -1811,14 +1789,14 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  	bool clear;
  	int r = 0;
 
-	spin_lock(&vm->status_lock);
+	spin_lock(&vm->moved_lock);
  	while (!list_empty(&vm->moved)) {
  		struct amdgpu_bo_va *bo_va;
  		struct reservation_object *resv;
 
  		bo_va = list_first_entry(&vm->moved,
  			struct amdgpu_bo_va, base.vm_status);
-		spin_unlock(&vm->status_lock);
+		spin_unlock(&vm->moved_lock);
 
  		resv = bo_va->base.bo->tbo.resv;
 
@@ -1839,9 +1817,9 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  		if (!clear && resv != vm->root.base.bo->tbo.resv)
  			reservation_object_unlock(resv);
 
-		spin_lock(&vm->status_lock);
+		spin_lock(&vm->moved_lock);
  	}
-	spin_unlock(&vm->status_lock);
+	spin_unlock(&vm->moved_lock);
 
  	return r;
  }
@@ -1903,10 +1881,10 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
  		amdgpu_vm_prt_get(adev);
 
  	if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-		spin_lock(&vm->status_lock);
+		spin_lock(&vm->moved_lock);
  		if (list_empty(&bo_va->base.vm_status))
  			list_add(&bo_va->base.vm_status, &vm->moved);
-		spin_unlock(&vm->status_lock);
+		spin_unlock(&vm->moved_lock);
  	}
  	trace_amdgpu_vm_bo_map(bo_va, mapping);
  }
@@ -2216,9 +2194,9 @@ void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
 
  	list_del(&bo_va->base.bo_list);
 
-	spin_lock(&vm->status_lock);
+	spin_lock(&vm->moved_lock);
  	list_del(&bo_va->base.vm_status);
-	spin_unlock(&vm->status_lock);
+	spin_unlock(&vm->moved_lock);
 
  	list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  		list_del(&mapping->list);
@@ -2261,28 +2239,24 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
 
  		bo_base->moved = true;
  		if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-			spin_lock(&bo_base->vm->status_lock);
  			if (bo->tbo.type == ttm_bo_type_kernel)
  				list_move(&bo_base->vm_status, &vm->evicted);
  			else
  				list_move_tail(&bo_base->vm_status,
  					       &vm->evicted);
-			spin_unlock(&bo_base->vm->status_lock);
  			continue;
  		}
 
  		if (bo->tbo.type == ttm_bo_type_kernel) {
-			spin_lock(&bo_base->vm->status_lock);
  			if (list_empty(&bo_base->vm_status))
  				list_add(&bo_base->vm_status, &vm->relocated);
-			spin_unlock(&bo_base->vm->status_lock);
  			continue;
  		}
 
-		spin_lock(&bo_base->vm->status_lock);
+		spin_lock(&bo_base->vm->moved_lock);
  		if (list_empty(&bo_base->vm_status))
  			list_add(&bo_base->vm_status, &vm->moved);
-		spin_unlock(&bo_base->vm->status_lock);
+		spin_unlock(&bo_base->vm->moved_lock);
  	}
  }
 
@@ -2391,9 +2365,9 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	vm->va = RB_ROOT_CACHED;
  	for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  		vm->reserved_vmid[i] = NULL;
-	spin_lock_init(&vm->status_lock);
  	INIT_LIST_HEAD(&vm->evicted);
  	INIT_LIST_HEAD(&vm->relocated);
+	spin_lock_init(&vm->moved_lock);
  	INIT_LIST_HEAD(&vm->moved);
  	INIT_LIST_HEAD(&vm->freed);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -3
index d6827083572a..0196b9a782f2 100644
@@ -168,9 +168,6 @@ struct amdgpu_vm {
  	/* tree of virtual addresses mapped */
  	struct rb_root_cached	va;
 
-	/* protecting invalidated */
-	spinlock_t		status_lock;
-
  	/* BOs who needs a validation */
  	struct list_head	evicted;
 

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
@@ -179,6 +176,7 @@ struct amdgpu_vm {
 
  	/* BOs moved, but not yet updated in the PT */
  	struct list_head	moved;
+	spinlock_t		moved_lock;
 
  	/* BO mappings freed, but not yet updated in the PT */
  	struct list_head	freed;

_______________________________________________
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): [email protected]

Reviewed-by Junwei Zhang
#11 lore raw
Quoted 2 lines

Patch 6:
I could read that code, but not sure the purpose.

on 2nd thought, perhaps going to reduce the movement for per vm bo for performance increasing.

However, the move lru tail in amdgpu_vm_validate_pt_bos() looks change the per vm bo order again.

IIRC, it expects relative stable order for per vm bo to get a better performance.

Please confirm that.

Regards,
Jerry(Junwei Zhang)

________________________________________
From: amd-gfx <[email protected]> on behalf of Zhang, Jerry (Junwei) <[email protected]>
Sent: Friday, May 18, 2018 5:55:31 PM
To: Christian König; [email protected]
Subject: Re: [PATCH 1/6] drm/amdgpu: rework VM state machine lock handling v2

2, 3, 4, 5 are

Patch 1:
could you show the reserving VM?

Patch 6:
I could read that code, but not sure the purpose.

Jerry

Quoted 276 lines · Christian König

Only the moved state needs a separate spin lock protection. All other states are protected by reserving the VM anyway.

v2: fix some more incorrect cases

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 66 +++++++++++-----------------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 +--
  2 files changed, 21 insertions(+), 49 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +20 -46
index 1a8f4e0dd023..f0deedcaf1c9 100644
@@ -119,9 +119,7 @@ static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
       * is currently evicted. add the bo to the evicted list to make sure it
       * is validated on next vm use to avoid fault.
       * */
-     spin_lock(&vm->status_lock);
      list_move_tail(&base->vm_status, &vm->evicted);
-     spin_unlock(&vm->status_lock);
  }
 
  /**
@@ -228,7 +226,6 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
      struct ttm_bo_global *glob = adev->mman.bdev.glob;
      int r;
 
-     spin_lock(&vm->status_lock);
      while (!list_empty(&vm->evicted)) {
              struct amdgpu_vm_bo_base *bo_base;
              struct amdgpu_bo *bo;
@@ -236,10 +233,8 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
              bo_base = list_first_entry(&vm->evicted,
                                         struct amdgpu_vm_bo_base,
                                         vm_status);
-             spin_unlock(&vm->status_lock);
 
              bo = bo_base->bo;
-             BUG_ON(!bo);
              if (bo->parent) {
                      r = validate(param, bo);
                      if (r)
@@ -259,13 +254,14 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                              return r;
              }
 
-             spin_lock(&vm->status_lock);
-             if (bo->tbo.type != ttm_bo_type_kernel)
+             if (bo->tbo.type != ttm_bo_type_kernel) {
+                     spin_lock(&vm->moved_lock);
                      list_move(&bo_base->vm_status, &vm->moved);
-             else
+                     spin_unlock(&vm->moved_lock);
+             } else {
                      list_move(&bo_base->vm_status, &vm->relocated);
+             }
      }
-     spin_unlock(&vm->status_lock);
 
      return 0;
  }
@@ -279,13 +275,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
   */
  bool amdgpu_vm_ready(struct amdgpu_vm *vm)
  {
-     bool ready;
-
-     spin_lock(&vm->status_lock);
-     ready = list_empty(&vm->evicted);
-     spin_unlock(&vm->status_lock);
-
-     return ready;
+     return list_empty(&vm->evicted);
  }
 
  /**
@@ -477,9 +467,7 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
                      pt->parent = amdgpu_bo_ref(parent->base.bo);
 
                      amdgpu_vm_bo_base_init(&entry->base, vm, pt);
-                     spin_lock(&vm->status_lock);
                      list_move(&entry->base.vm_status, &vm->relocated);
-                     spin_unlock(&vm->status_lock);
              }
 
              if (level < AMDGPU_VM_PTB) {
@@ -926,10 +914,8 @@ static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
              if (!entry->base.bo)
                      continue;
 
-             spin_lock(&vm->status_lock);
              if (list_empty(&entry->base.vm_status))
                      list_add(&entry->base.vm_status, &vm->relocated);
-             spin_unlock(&vm->status_lock);
              amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
      }
  }
@@ -974,7 +960,6 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
              params.func = amdgpu_vm_do_set_ptes;
      }
 
-     spin_lock(&vm->status_lock);
      while (!list_empty(&vm->relocated)) {
              struct amdgpu_vm_bo_base *bo_base, *parent;
              struct amdgpu_vm_pt *pt, *entry;
@@ -984,13 +969,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
                                         struct amdgpu_vm_bo_base,
                                         vm_status);
              list_del_init(&bo_base->vm_status);
-             spin_unlock(&vm->status_lock);
 
              bo = bo_base->bo->parent;
-             if (!bo) {
-                     spin_lock(&vm->status_lock);
+             if (!bo)
                      continue;
-             }
 
              parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
                                        bo_list);
@@ -999,12 +981,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 
              amdgpu_vm_update_pde(&params, vm, pt, entry);
 
-             spin_lock(&vm->status_lock);
              if (!vm->use_cpu_for_update &&
                  (ndw - params.ib->length_dw) < 32)
                      break;
      }
-     spin_unlock(&vm->status_lock);
 
      if (vm->use_cpu_for_update) {
              /* Flush HDP */
@@ -1107,9 +1087,7 @@ static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
              if (entry->huge) {
                      /* Add the entry to the relocated list to update it. */
                      entry->huge = false;
-                     spin_lock(&p->vm->status_lock);
                      list_move(&entry->base.vm_status, &p->vm->relocated);
-                     spin_unlock(&p->vm->status_lock);
              }
              return;
      }
@@ -1588,8 +1566,9 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
              amdgpu_asic_flush_hdp(adev, NULL);
      }
 
-     spin_lock(&vm->status_lock);
+     spin_lock(&vm->moved_lock);
      list_del_init(&bo_va->base.vm_status);
+     spin_unlock(&vm->moved_lock);
 
      /* If the BO is not in its preferred location add it back to
       * the evicted list so that it gets validated again on the
@@ -1599,7 +1578,6 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
          !(bo->preferred_domains &
          amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)))
              list_add_tail(&bo_va->base.vm_status, &vm->evicted);
-     spin_unlock(&vm->status_lock);
 
      list_splice_init(&bo_va->invalids, &bo_va->valids);
      bo_va->cleared = clear;
@@ -1811,14 +1789,14 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
      bool clear;
      int r = 0;
 
-     spin_lock(&vm->status_lock);
+     spin_lock(&vm->moved_lock);
      while (!list_empty(&vm->moved)) {
              struct amdgpu_bo_va *bo_va;
              struct reservation_object *resv;
 
              bo_va = list_first_entry(&vm->moved,
                      struct amdgpu_bo_va, base.vm_status);
-             spin_unlock(&vm->status_lock);
+             spin_unlock(&vm->moved_lock);
 
              resv = bo_va->base.bo->tbo.resv;
 
@@ -1839,9 +1817,9 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
              if (!clear && resv != vm->root.base.bo->tbo.resv)
                      reservation_object_unlock(resv);
 
-             spin_lock(&vm->status_lock);
+             spin_lock(&vm->moved_lock);
      }
-     spin_unlock(&vm->status_lock);
+     spin_unlock(&vm->moved_lock);
 
      return r;
  }
@@ -1903,10 +1881,10 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
              amdgpu_vm_prt_get(adev);
 
      if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-             spin_lock(&vm->status_lock);
+             spin_lock(&vm->moved_lock);
              if (list_empty(&bo_va->base.vm_status))
                      list_add(&bo_va->base.vm_status, &vm->moved);
-             spin_unlock(&vm->status_lock);
+             spin_unlock(&vm->moved_lock);
      }
      trace_amdgpu_vm_bo_map(bo_va, mapping);
  }
@@ -2216,9 +2194,9 @@ void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
 
      list_del(&bo_va->base.bo_list);
 
-     spin_lock(&vm->status_lock);
+     spin_lock(&vm->moved_lock);
      list_del(&bo_va->base.vm_status);
-     spin_unlock(&vm->status_lock);
+     spin_unlock(&vm->moved_lock);
 
      list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
              list_del(&mapping->list);
@@ -2261,28 +2239,24 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
 
              bo_base->moved = true;
              if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-                     spin_lock(&bo_base->vm->status_lock);
                      if (bo->tbo.type == ttm_bo_type_kernel)
                              list_move(&bo_base->vm_status, &vm->evicted);
                      else
                              list_move_tail(&bo_base->vm_status,
                                             &vm->evicted);
-                     spin_unlock(&bo_base->vm->status_lock);
                      continue;
              }
 
              if (bo->tbo.type == ttm_bo_type_kernel) {
-                     spin_lock(&bo_base->vm->status_lock);
                      if (list_empty(&bo_base->vm_status))
                              list_add(&bo_base->vm_status, &vm->relocated);
-                     spin_unlock(&bo_base->vm->status_lock);
                      continue;
              }
 
-             spin_lock(&bo_base->vm->status_lock);
+             spin_lock(&bo_base->vm->moved_lock);
              if (list_empty(&bo_base->vm_status))
                      list_add(&bo_base->vm_status, &vm->moved);
-             spin_unlock(&bo_base->vm->status_lock);
+             spin_unlock(&bo_base->vm->moved_lock);
      }
  }
 
@@ -2391,9 +2365,9 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
      vm->va = RB_ROOT_CACHED;
      for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
              vm->reserved_vmid[i] = NULL;
-     spin_lock_init(&vm->status_lock);
      INIT_LIST_HEAD(&vm->evicted);
      INIT_LIST_HEAD(&vm->relocated);
+     spin_lock_init(&vm->moved_lock);
      INIT_LIST_HEAD(&vm->moved);
      INIT_LIST_HEAD(&vm->freed);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -3
index d6827083572a..0196b9a782f2 100644
@@ -168,9 +168,6 @@ struct amdgpu_vm {
      /* tree of virtual addresses mapped */
      struct rb_root_cached   va;
 
-     /* protecting invalidated */
-     spinlock_t              status_lock;
-
      /* BOs who needs a validation */
      struct list_head        evicted;
 

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
@@ -179,6 +176,7 @@ struct amdgpu_vm {
 
      /* BOs moved, but not yet updated in the PT */
      struct list_head        moved;
+     spinlock_t              moved_lock;
 
      /* BO mappings freed, but not yet updated in the PT */
      struct list_head        freed;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
_______________________________________________
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): CY4PR12MB1414D3B8AE6ECEF5A9277A9AFF970-rpdhrqHFk04b0eJBKAp/[email protected]

#13 lore raw
Quoted 2 lines

Patch 1:
could you show the reserving VM?

The idea is that only the moved state is accessed without the VM being reserved.

Because of this we can remove the locks when we a) are sure that the BO isn't in the moved state and b) don't want to move it into the moved state.

Quoted 5 lines

on 2nd thought, perhaps going to reduce the movement for per vm bo for performance increasing.

However, the move lru tail in amdgpu_vm_validate_pt_bos() looks change the per vm bo order again.

IIRC, it expects relative stable order for per vm bo to get a better performance.

What can happen is that a BO is not evicted because we need visible VRAM.

But the gross of all BOs should be evicted in the order they are allocated and are then moved back into VRAM in the order they are allocated etc...

That isn't 100% stable but still seems to be good enough.

Regards,
Christian.

Am 19.05.2018 um 10:00 schrieb Zhang, Jerry:

Quoted 311 lines
Quoted 2 lines

Patch 6:
I could read that code, but not sure the purpose.

on 2nd thought, perhaps going to reduce the movement for per vm bo for performance increasing.

However, the move lru tail in amdgpu_vm_validate_pt_bos() looks change the per vm bo order again.

IIRC, it expects relative stable order for per vm bo to get a better performance.

Please confirm that.

Regards,
Jerry(Junwei Zhang)

________________________________________
From: amd-gfx <[email protected]> on behalf of Zhang, Jerry (Junwei) <[email protected]>
Sent: Friday, May 18, 2018 5:55:31 PM
To: Christian König; [email protected]
Subject: Re: [PATCH 1/6] drm/amdgpu: rework VM state machine lock handling v2

2, 3, 4, 5 are

Patch 1:
could you show the reserving VM?

Patch 6:
I could read that code, but not sure the purpose.

Jerry

Quoted 280 lines · Christian König

Only the moved state needs a separate spin lock protection. All other states are protected by reserving the VM anyway.

v2: fix some more incorrect cases

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 66 +++++++++++-----------------------
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 +--
   2 files changed, 21 insertions(+), 49 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +20 -46
index 1a8f4e0dd023..f0deedcaf1c9 100644
@@ -119,9 +119,7 @@ static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
        * is currently evicted. add the bo to the evicted list to make sure it
        * is validated on next vm use to avoid fault.
        * */
-     spin_lock(&vm->status_lock);
       list_move_tail(&base->vm_status, &vm->evicted);
-     spin_unlock(&vm->status_lock);
   }
 
   /**
@@ -228,7 +226,6 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
       struct ttm_bo_global *glob = adev->mman.bdev.glob;
       int r;
 
-     spin_lock(&vm->status_lock);
       while (!list_empty(&vm->evicted)) {
               struct amdgpu_vm_bo_base *bo_base;
               struct amdgpu_bo *bo;
@@ -236,10 +233,8 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
               bo_base = list_first_entry(&vm->evicted,
                                          struct amdgpu_vm_bo_base,
                                          vm_status);
-             spin_unlock(&vm->status_lock);
 
               bo = bo_base->bo;
-             BUG_ON(!bo);
               if (bo->parent) {
                       r = validate(param, bo);
                       if (r)
@@ -259,13 +254,14 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                               return r;
               }
 
-             spin_lock(&vm->status_lock);
-             if (bo->tbo.type != ttm_bo_type_kernel)
+             if (bo->tbo.type != ttm_bo_type_kernel) {
+                     spin_lock(&vm->moved_lock);
                       list_move(&bo_base->vm_status, &vm->moved);
-             else
+                     spin_unlock(&vm->moved_lock);
+             } else {
                       list_move(&bo_base->vm_status, &vm->relocated);
+             }
       }
-     spin_unlock(&vm->status_lock);
 
       return 0;
   }
@@ -279,13 +275,7 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
    */
   bool amdgpu_vm_ready(struct amdgpu_vm *vm)
   {
-     bool ready;
-
-     spin_lock(&vm->status_lock);
-     ready = list_empty(&vm->evicted);
-     spin_unlock(&vm->status_lock);
-
-     return ready;
+     return list_empty(&vm->evicted);
   }
 
   /**
@@ -477,9 +467,7 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
                       pt->parent = amdgpu_bo_ref(parent->base.bo);
 
                       amdgpu_vm_bo_base_init(&entry->base, vm, pt);
-                     spin_lock(&vm->status_lock);
                       list_move(&entry->base.vm_status, &vm->relocated);
-                     spin_unlock(&vm->status_lock);
               }
 
               if (level < AMDGPU_VM_PTB) {
@@ -926,10 +914,8 @@ static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
               if (!entry->base.bo)
                       continue;
 
-             spin_lock(&vm->status_lock);
               if (list_empty(&entry->base.vm_status))
                       list_add(&entry->base.vm_status, &vm->relocated);
-             spin_unlock(&vm->status_lock);
               amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
       }
   }
@@ -974,7 +960,6 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
               params.func = amdgpu_vm_do_set_ptes;
       }
 
-     spin_lock(&vm->status_lock);
       while (!list_empty(&vm->relocated)) {
               struct amdgpu_vm_bo_base *bo_base, *parent;
               struct amdgpu_vm_pt *pt, *entry;
@@ -984,13 +969,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
                                          struct amdgpu_vm_bo_base,
                                          vm_status);
               list_del_init(&bo_base->vm_status);
-             spin_unlock(&vm->status_lock);
 
               bo = bo_base->bo->parent;
-             if (!bo) {
-                     spin_lock(&vm->status_lock);
+             if (!bo)
                       continue;
-             }
 
               parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
                                         bo_list);
@@ -999,12 +981,10 @@ int amdgpu_vm_update_directories(struct amdgpu_device *adev,
 
               amdgpu_vm_update_pde(&params, vm, pt, entry);
 
-             spin_lock(&vm->status_lock);
               if (!vm->use_cpu_for_update &&
                   (ndw - params.ib->length_dw) < 32)
                       break;
       }
-     spin_unlock(&vm->status_lock);
 
       if (vm->use_cpu_for_update) {
               /* Flush HDP */
@@ -1107,9 +1087,7 @@ static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
               if (entry->huge) {
                       /* Add the entry to the relocated list to update it. */
                       entry->huge = false;
-                     spin_lock(&p->vm->status_lock);
                       list_move(&entry->base.vm_status, &p->vm->relocated);
-                     spin_unlock(&p->vm->status_lock);
               }
               return;
       }
@@ -1588,8 +1566,9 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
               amdgpu_asic_flush_hdp(adev, NULL);
       }
 
-     spin_lock(&vm->status_lock);
+     spin_lock(&vm->moved_lock);
       list_del_init(&bo_va->base.vm_status);
+     spin_unlock(&vm->moved_lock);
 
       /* If the BO is not in its preferred location add it back to
        * the evicted list so that it gets validated again on the
@@ -1599,7 +1578,6 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
           !(bo->preferred_domains &
           amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)))
               list_add_tail(&bo_va->base.vm_status, &vm->evicted);
-     spin_unlock(&vm->status_lock);
 
       list_splice_init(&bo_va->invalids, &bo_va->valids);
       bo_va->cleared = clear;
@@ -1811,14 +1789,14 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
       bool clear;
       int r = 0;
 
-     spin_lock(&vm->status_lock);
+     spin_lock(&vm->moved_lock);
       while (!list_empty(&vm->moved)) {
               struct amdgpu_bo_va *bo_va;
               struct reservation_object *resv;
 
               bo_va = list_first_entry(&vm->moved,
                       struct amdgpu_bo_va, base.vm_status);
-             spin_unlock(&vm->status_lock);
+             spin_unlock(&vm->moved_lock);
 
               resv = bo_va->base.bo->tbo.resv;
 
@@ -1839,9 +1817,9 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
               if (!clear && resv != vm->root.base.bo->tbo.resv)
                       reservation_object_unlock(resv);
 
-             spin_lock(&vm->status_lock);
+             spin_lock(&vm->moved_lock);
       }
-     spin_unlock(&vm->status_lock);
+     spin_unlock(&vm->moved_lock);
 
       return r;
   }
@@ -1903,10 +1881,10 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
               amdgpu_vm_prt_get(adev);
 
       if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-             spin_lock(&vm->status_lock);
+             spin_lock(&vm->moved_lock);
               if (list_empty(&bo_va->base.vm_status))
                       list_add(&bo_va->base.vm_status, &vm->moved);
-             spin_unlock(&vm->status_lock);
+             spin_unlock(&vm->moved_lock);
       }
       trace_amdgpu_vm_bo_map(bo_va, mapping);
   }
@@ -2216,9 +2194,9 @@ void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
 
       list_del(&bo_va->base.bo_list);
 
-     spin_lock(&vm->status_lock);
+     spin_lock(&vm->moved_lock);
       list_del(&bo_va->base.vm_status);
-     spin_unlock(&vm->status_lock);
+     spin_unlock(&vm->moved_lock);
 
       list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
               list_del(&mapping->list);
@@ -2261,28 +2239,24 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
 
               bo_base->moved = true;
               if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
-                     spin_lock(&bo_base->vm->status_lock);
                       if (bo->tbo.type == ttm_bo_type_kernel)
                               list_move(&bo_base->vm_status, &vm->evicted);
                       else
                               list_move_tail(&bo_base->vm_status,
                                              &vm->evicted);
-                     spin_unlock(&bo_base->vm->status_lock);
                       continue;
               }
 
               if (bo->tbo.type == ttm_bo_type_kernel) {
-                     spin_lock(&bo_base->vm->status_lock);
                       if (list_empty(&bo_base->vm_status))
                               list_add(&bo_base->vm_status, &vm->relocated);
-                     spin_unlock(&bo_base->vm->status_lock);
                       continue;
               }
 
-             spin_lock(&bo_base->vm->status_lock);
+             spin_lock(&bo_base->vm->moved_lock);
               if (list_empty(&bo_base->vm_status))
                       list_add(&bo_base->vm_status, &vm->moved);
-             spin_unlock(&bo_base->vm->status_lock);
+             spin_unlock(&bo_base->vm->moved_lock);
       }
   }
 
@@ -2391,9 +2365,9 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
       vm->va = RB_ROOT_CACHED;
       for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
               vm->reserved_vmid[i] = NULL;
-     spin_lock_init(&vm->status_lock);
       INIT_LIST_HEAD(&vm->evicted);
       INIT_LIST_HEAD(&vm->relocated);
+     spin_lock_init(&vm->moved_lock);
       INIT_LIST_HEAD(&vm->moved);
       INIT_LIST_HEAD(&vm->freed);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -3
index d6827083572a..0196b9a782f2 100644
@@ -168,9 +168,6 @@ struct amdgpu_vm {
       /* tree of virtual addresses mapped */
       struct rb_root_cached   va;
 
-     /* protecting invalidated */
-     spinlock_t              status_lock;
-
       /* BOs who needs a validation */
       struct list_head        evicted;
 
@@ -179,6 +176,7 @@ struct amdgpu_vm {
 
       /* BOs moved, but not yet updated in the PT */
       struct list_head        moved;
+     spinlock_t              moved_lock;
 
Quoted 7 lines

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

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
       /* BO mappings freed, but not yet updated in the PT */
       struct list_head        freed;

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

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