Skip to contentSkip to conversation

drm/amdgpu: fix clearing mappings for BOs that are always valid in VM

PATCH Superseded Samuel Pitoiset 3 participants 4 messages Jun 16, 2023

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

PATCH 1/1 drm/amdgpu: fix clearing mappings for BOs that are always valid in VM

If the BO has been moved the PT should be updated, otherwise the VAs might point to invalid PT.

This fixes random GPU hangs when replacing sparse mappings from the userspace, while OP_MAP/OP_UNMAP works fine because always valid BOs are correctly handled there.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -0
index 143d11afe0e5..eff73c428b12 100644
@@ -1771,18 +1771,30 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 
 	/* Insert partial mapping before the range */
 	if (!list_empty(&before->list)) {
+		struct amdgpu_bo *bo = before->bo_va->base.bo;
+
 		amdgpu_vm_it_insert(before, &vm->va);
 		if (before->flags & AMDGPU_PTE_PRT)
 			amdgpu_vm_prt_get(adev);
+
+		if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+		    !before->bo_va->base.moved)
+			amdgpu_vm_bo_moved(&before->bo_va->base);
 	} else {
 		kfree(before);
 	}
 
 	/* Insert partial mapping after the range */
 	if (!list_empty(&after->list)) {
+		struct amdgpu_bo *bo = after->bo_va->base.bo;
+
 		amdgpu_vm_it_insert(after, &vm->va);
 		if (after->flags & AMDGPU_PTE_PRT)
 			amdgpu_vm_prt_get(adev);
+
+		if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+		    !after->bo_va->base.moved)
+			amdgpu_vm_bo_moved(&after->bo_va->base);
 	} else {
 		kfree(after);
 	}
Signature · 1 line
2.41.0
#1 lore raw

Am 16.06.23 um 08:27 schrieb Samuel Pitoiset:

Quoted 2 lines

If the BO has been moved the PT should be updated, otherwise the VAs might point to invalid PT.

You might want to update this sentence a bit. Something like:

Per VM BOs must be marked as moved or otherwise their ranges are not updated on use which might be necessary when the replace operation splits mappings.

Apart from that really good catch and the patch is Reviewed-by: Christian König <[email protected]>

Regards,
Christian.

Quoted 46 lines

This fixes random GPU hangs when replacing sparse mappings from the userspace, while OP_MAP/OP_UNMAP works fine because always valid BOs are correctly handled there.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -0
index 143d11afe0e5..eff73c428b12 100644
@@ -1771,18 +1771,30 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 
  	/* Insert partial mapping before the range */
  	if (!list_empty(&before->list)) {
+		struct amdgpu_bo *bo = before->bo_va->base.bo;
+
  		amdgpu_vm_it_insert(before, &vm->va);
  		if (before->flags & AMDGPU_PTE_PRT)
  			amdgpu_vm_prt_get(adev);
+
+		if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+		    !before->bo_va->base.moved)
+			amdgpu_vm_bo_moved(&before->bo_va->base);
  	} else {
  		kfree(before);
  	}
 
  	/* Insert partial mapping after the range */
  	if (!list_empty(&after->list)) {
+		struct amdgpu_bo *bo = after->bo_va->base.bo;
+
  		amdgpu_vm_it_insert(after, &vm->va);
  		if (after->flags & AMDGPU_PTE_PRT)
  			amdgpu_vm_prt_get(adev);
+
+		if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+		    !after->bo_va->base.moved)
+			amdgpu_vm_bo_moved(&after->bo_va->base);
  	} else {
  		kfree(after);
  	}
#2 lore raw

PATCH v2 1/1 drm/amdgpu: fix clearing mappings for BOs that are always valid in VM

In reply to Samuel Pitoiset

Per VM BOs must be marked as moved or otherwise their ranges are not updated on use which might be necessary when the replace operation splits mappings.

This fixes random GPU hangs when replacing sparse mappings from the userspace, while OP_MAP/OP_UNMAP works fine because always valid BOs are correctly handled there.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -0
index 143d11afe0e5..eff73c428b12 100644
@@ -1771,18 +1771,30 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 
 	/* Insert partial mapping before the range */
 	if (!list_empty(&before->list)) {
+		struct amdgpu_bo *bo = before->bo_va->base.bo;
+
 		amdgpu_vm_it_insert(before, &vm->va);
 		if (before->flags & AMDGPU_PTE_PRT)
 			amdgpu_vm_prt_get(adev);
+
+		if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+		    !before->bo_va->base.moved)
+			amdgpu_vm_bo_moved(&before->bo_va->base);
 	} else {
 		kfree(before);
 	}
 
 	/* Insert partial mapping after the range */
 	if (!list_empty(&after->list)) {
+		struct amdgpu_bo *bo = after->bo_va->base.bo;
+
 		amdgpu_vm_it_insert(after, &vm->va);
 		if (after->flags & AMDGPU_PTE_PRT)
 			amdgpu_vm_prt_get(adev);
+
+		if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+		    !after->bo_va->base.moved)
+			amdgpu_vm_bo_moved(&after->bo_va->base);
 	} else {
 		kfree(after);
 	}
Signature · 1 line
2.41.0
Applied
#3 lore raw

Applied. Thanks!

Alex

Quoted 54 lines · Samuel Pitoiset

Per VM BOs must be marked as moved or otherwise their ranges are not updated on use which might be necessary when the replace operation splits mappings.

This fixes random GPU hangs when replacing sparse mappings from the userspace, while OP_MAP/OP_UNMAP works fine because always valid BOs are correctly handled there.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -1
index 143d11afe0e5..eff73c428b12 100644
@@ -1771,18 +1771,30 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 
        /* Insert partial mapping before the range */
        if (!list_empty(&before->list)) {
+               struct amdgpu_bo *bo = before->bo_va->base.bo;
+
                amdgpu_vm_it_insert(before, &vm->va);
                if (before->flags & AMDGPU_PTE_PRT)
                        amdgpu_vm_prt_get(adev);
+
+               if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+                   !before->bo_va->base.moved)
+                       amdgpu_vm_bo_moved(&before->bo_va->base);
        } else {
                kfree(before);
        }
 
        /* Insert partial mapping after the range */
        if (!list_empty(&after->list)) {
+               struct amdgpu_bo *bo = after->bo_va->base.bo;
+
                amdgpu_vm_it_insert(after, &vm->va);
                if (after->flags & AMDGPU_PTE_PRT)
                        amdgpu_vm_prt_get(adev);
+
+               if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
+                   !after->bo_va->base.moved)
+                       amdgpu_vm_bo_moved(&after->bo_va->base);
        } else {
                kfree(after);
        }
--

2.41.0