Skip to contentSkip to conversation

drm/amdgpu: make sure BOs are locked in amdgpu_vm_get_memory

PATCH In review Christian König 4 participants 6 messages Jun 5, 2023 – Jun 15, 2023

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

PATCH 1/2 drm/amdgpu: make sure BOs are locked in amdgpu_vm_get_memory

We need to grab the lock of the BO or otherwise can run into a crash when we try to inspect the current location.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 69 +++++++++++++++-----------
 1 file changed, 39 insertions(+), 30 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +39 -30
index 3c0310576b3b..2c8cafec48a4 100644
@@ -920,42 +920,51 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	return r;
 }
 
+static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,
+				    struct amdgpu_mem_stats *stats)
+{
+	struct amdgpu_vm *vm = bo_va->base.vm;
+	struct amdgpu_bo *bo = bo_va->base.bo;
+
+	if (!bo)
+		return;
+
+	/*
+	 * For now ignore BOs which are currently locked and potentially
+	 * changing their location.
+	 */
+	if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv &&
+	    !dma_resv_trylock(bo->tbo.base.resv))
+		return;
+
+	amdgpu_bo_get_memory(bo, stats);
+	if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv)
+	    dma_resv_unlock(bo->tbo.base.resv);
+}
+
 void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
 			  struct amdgpu_mem_stats *stats)
 {
 	struct amdgpu_bo_va *bo_va, *tmp;
 
 	spin_lock(&vm->status_lock);
-	list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
-		if (!bo_va->base.bo)
-			continue;
-		amdgpu_bo_get_memory(bo_va->base.bo, stats);
-	}
-	list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
-		if (!bo_va->base.bo)
-			continue;
-		amdgpu_bo_get_memory(bo_va->base.bo, stats);
-	}
-	list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
-		if (!bo_va->base.bo)
-			continue;
-		amdgpu_bo_get_memory(bo_va->base.bo, stats);
-	}
-	list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
-		if (!bo_va->base.bo)
-			continue;
-		amdgpu_bo_get_memory(bo_va->base.bo, stats);
-	}
-	list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
-		if (!bo_va->base.bo)
-			continue;
-		amdgpu_bo_get_memory(bo_va->base.bo, stats);
-	}
-	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
-		if (!bo_va->base.bo)
-			continue;
-		amdgpu_bo_get_memory(bo_va->base.bo, stats);
-	}
+	list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status)
+		amdgpu_vm_bo_get_memory(bo_va, stats);
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
+		amdgpu_vm_bo_get_memory(bo_va, stats);
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status)
+		amdgpu_vm_bo_get_memory(bo_va, stats);
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
+		amdgpu_vm_bo_get_memory(bo_va, stats);
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status)
+		amdgpu_vm_bo_get_memory(bo_va, stats);
+
+	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status)
+		amdgpu_vm_bo_get_memory(bo_va, stats);
 	spin_unlock(&vm->status_lock);
 }
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amdgpu: make sure that BOs have a backing store

It's perfectly possible that the BO is about to be destroyed and doesn't have a backing store associated with it.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +5 -1
index 2bd1a54ee866..249385985a4f 100644
@@ -1268,8 +1268,12 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
 void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
 			  struct amdgpu_mem_stats *stats)
 {
-	unsigned int domain;
 	uint64_t size = amdgpu_bo_size(bo);
+	unsigned int domain;
+
+	/* Abort if the BO doesn't currently have a backing store */
+	if (!bo->tbo.resource)
+		return;
 
 	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
 	switch (domain) {
Signature · 1 line
2.34.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: make sure that BOs have a backing store

Quoted 5 lines · Christian König

It's perfectly possible that the BO is about to be destroyed and doesn't have a backing store associated with it.

Series is:

Quoted 25 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +5 -2
index 2bd1a54ee866..249385985a4f 100644
@@ -1268,8 +1268,12 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
 void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                          struct amdgpu_mem_stats *stats)
 {
-       unsigned int domain;
        uint64_t size = amdgpu_bo_size(bo);
+       unsigned int domain;
+
+       /* Abort if the BO doesn't currently have a backing store */
+       if (!bo->tbo.resource)
+               return;
 
        domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
        switch (domain) {
--

2.34.1

Tested-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: make sure that BOs have a backing store

In reply to Christian König
Quoted 4 lines · Christian König

It's perfectly possible that the BO is about to be destroyed and doesn't have a backing store associated with it.

Thanks Christian. I appreciate your brilliant work. "KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]" finally fixed.

Signature · 2 lines
Best Regards,
Mike Gavrilov.
Acked-by on v1 1/2
#4 lore raw
In reply to Christian König

[Public]

A simple question is we don't need to hold the lock if bo locations are not changed?

Regards,
Guchun

Quoted 112 lines

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, June 5, 2023 5:11 PM
To: [email protected]; [email protected]; Chen, Guchun <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: make sure BOs are locked in amdgpu_vm_get_memory

We need to grab the lock of the BO or otherwise can run into a crash when we try to inspect the current location.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 69 +++++++++++++++---------
--
 1 file changed, 39 insertions(+), 30 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +28 -30
index 3c0310576b3b..2c8cafec48a4 100644
@@ -920,42 +920,51 @@ int amdgpu_vm_update_range(struct
 amdgpu_device *adev, struct amdgpu_vm *vm,
      return r;
 }
 
+static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,
+                                 struct amdgpu_mem_stats *stats) {
+     struct amdgpu_vm *vm = bo_va->base.vm;
+     struct amdgpu_bo *bo = bo_va->base.bo;
+
+     if (!bo)
+             return;
+
+     /*
+      * For now ignore BOs which are currently locked and potentially
+      * changing their location.
+      */
+     if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv &&
+         !dma_resv_trylock(bo->tbo.base.resv))
+             return;
+
+     amdgpu_bo_get_memory(bo, stats);
+     if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv)
+         dma_resv_unlock(bo->tbo.base.resv);
+}
+
 void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
                        struct amdgpu_mem_stats *stats)
 {
      struct amdgpu_bo_va *bo_va, *tmp;
 
      spin_lock(&vm->status_lock);
-     list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
 {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->relocated,
 base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
 {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->invalidated,
 base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
+     list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->relocated,

base.vm_status)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -0
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->invalidated,

base.vm_status)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
      spin_unlock(&vm->status_lock);
 }
 
--

2.34.1

#5 lore raw

Am 06.06.23 um 03:11 schrieb Chen, Guchun:

Quoted 5 lines

[Public]

A simple question is we don't need to hold the lock if bo locations are not changed?

Well we take the look to make sure that BO locations don't change.

Otherwise we potentially access freed memory when looking at the resource.

Regards,
Christian.

Quoted 116 lines

Regards,
Guchun

Quoted 112 lines

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, June 5, 2023 5:11 PM
To: [email protected]; [email protected]; Chen, Guchun <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: make sure BOs are locked in amdgpu_vm_get_memory

We need to grab the lock of the BO or otherwise can run into a crash when we try to inspect the current location.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 69 +++++++++++++++---------
--
  1 file changed, 39 insertions(+), 30 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +28 -30
index 3c0310576b3b..2c8cafec48a4 100644
@@ -920,42 +920,51 @@ int amdgpu_vm_update_range(struct
 amdgpu_device *adev, struct amdgpu_vm *vm,
       return r;
  }
 
+static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,
+                                 struct amdgpu_mem_stats *stats) {
+     struct amdgpu_vm *vm = bo_va->base.vm;
+     struct amdgpu_bo *bo = bo_va->base.bo;
+
+     if (!bo)
+             return;
+
+     /*
+      * For now ignore BOs which are currently locked and potentially
+      * changing their location.
+      */
+     if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv &&
+         !dma_resv_trylock(bo->tbo.base.resv))
+             return;
+
+     amdgpu_bo_get_memory(bo, stats);
+     if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv)
+         dma_resv_unlock(bo->tbo.base.resv);
+}
+
  void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
                         struct amdgpu_mem_stats *stats)
  {
       struct amdgpu_bo_va *bo_va, *tmp;
 
       spin_lock(&vm->status_lock);
-     list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
 {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->relocated,
 base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
 {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->invalidated,
 base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
-     list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
-             if (!bo_va->base.bo)
-                     continue;
-             amdgpu_bo_get_memory(bo_va->base.bo, stats);
-     }
+     list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->relocated,

base.vm_status)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -0
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->invalidated,

base.vm_status)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
+             amdgpu_vm_bo_get_memory(bo_va, stats);
+
+     list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status)
+             amdgpu_vm_bo_get_memory(bo_va, stats);
       spin_unlock(&vm->status_lock);
  }
 
--

2.34.1