Skip to contentSkip to conversation

drm/amdgpu: undo the halted activities when the HW reset is skipped

PATCH In review Jesse Zhang 2 participants 5 messages Oct 13

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

PATCH v2 1/4 drm/amdgpu: undo the halted activities when the HW reset is skipped

When the guilty job has already signaled, amdgpu_device_gpu_recover() jumps to skip_hw_reset after amdgpu_device_halt_activities(). The following parts of the halt are only undone in amdgpu_device_reinit_after_reset(), so they stay in effect after this "successful" recovery:

- amdgpu_userq_pre_reset() unmapped all user queues and marked them HUNG; they are never mapped again.
- drm_client_dev_suspend() suspended fbdev; the console stops updating.
- amdgpu_ras_suspend() disabled RAS on all IPs.
- amdgpu_unregister_gpu_instance() dropped the device from mgpu_info.

Seen on navi31 with ring and soft reset disabled and a GFX job that completes right after its timeout:

amdgpu: ring gfx_0.0.0 timeout, signaled seq=3, emitted seq=4
amdgpu: GPU reset begin!. Source: 1
amdgpu: Guilty job already signaled, skipping HW reset

where tracing shows userq_pre_reset, drm_client_dev_suspend, ras_suspend and unregister_gpu_instance without their counterparts.

Undo them on the skip path the same way the reinit path does.

Fixes fdafb3597a2c ("drm/amdgpu: fix MGPU fan boost enablement for XGMI reset")
Fixes f1c1314be429 ("drm/amdgpu: Disable ras features on all IPs before gpu reset")
Fixes 565d19415577 ("drm/amdgpu: add fbdev suspend/resume on gpu reset")
Fixes 290f46cf5726 ("drm/amdgpu: Implement user queue reset functionality")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -0
index e0014611f039..753df841be15 100644
@@ -5565,6 +5565,25 @@ static void amdgpu_device_halt_activities(struct amdgpu_device *adev,
 	}
 }
 
+/*
+ * Undo the parts of amdgpu_device_halt_activities() that are otherwise only
+ * undone by amdgpu_device_reinit_after_reset(), for a recovery that skips
+ * the HW reset.
+ */
+static void amdgpu_device_resume_activities(struct amdgpu_device *adev,
+					    struct list_head *device_list)
+{
+	struct amdgpu_device *tmp_adev = NULL;
+
+	list_for_each_entry(tmp_adev, device_list, reset_list) {
+		amdgpu_register_gpu_instance(tmp_adev);
+		amdgpu_userq_post_reset(tmp_adev, false);
+		drm_client_dev_resume(adev_to_drm(tmp_adev));
+		if (!amdgpu_reset_in_dpc(adev))
+			amdgpu_ras_resume(tmp_adev);
+	}
+}
+
 static int amdgpu_device_asic_reset(struct amdgpu_device *adev,
 			      struct list_head *device_list,
 			      struct amdgpu_reset_context *reset_context)
@@ -5806,6 +5825,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 	if (job && (dma_fence_get_status(&job->hw_fence->base) > 0)) {
 		job_signaled = true;
 		dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
+		amdgpu_device_resume_activities(adev, &device_list);
 		goto skip_hw_reset;
 	}
 
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 2/4 drm/amdgpu: report the BO alignment in bytes in GEM_LIST_HANDLES

The uapi documents drm_amdgpu_gem_list_handles_entry.alignment as "physical start_addr alignment in bytes", and GEM_OP GET_GEM_CREATE_INFO returns the same value in bytes, but GEM_LIST_HANDLES returns tbo.page_alignment, which is in pages. For a BO created with a 64 KiB alignment on navi31:

GET_GEM_CREATE_INFO: alignment=65536
GEM_LIST_HANDLES: alignment=16

so a user such as CRIU that recreates BOs from this list uses the wrong alignment. Convert it to bytes.

Fixes f9db1fc52ceb ("drm/amdgpu: Add ioctl to get all gem handles for a process")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
index e791d066fd47..8dbd7bb77305 100644
@@ -1281,7 +1281,7 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 		bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
 		bo_entry->preferred_domains = bo->preferred_domains;
 		bo_entry->gem_handle = id;
-		bo_entry->alignment = bo->tbo.page_alignment;
+		bo_entry->alignment = (u64)bo->tbo.page_alignment << PAGE_SHIFT;
 
 		if (bo->tbo.base.import_attach)
 			bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
Signature · 1 line
2.50.1
#2 lore raw

PATCH v2 3/4 drm/amdgpu: keep the eviction fence until the last handle is closed

In reply to Jesse Zhang

amdgpu_gem_object_close() detaches the user queue eviction fence from the BO before it checks bo_va->ref_count, so closing any handle of a BO removes the fence, even when the file still has another handle to the BO and the bo_va with its GPU mappings stays. The BO can then be evicted without suspending the user queues of the file, which keep accessing it through mappings that are only marked for update.

Seen on navi31 with a SDMA user queue: a BO opened twice in the same file (GEM_FLINK + GEM_OPEN) loses its eviction fence when the second handle is closed (traced attach/detach of the fence):

attach bo=ffff8c78a6c84c00
GEM_CLOSE handle=7
detach bo=ffff8c78a6c84c00

while handle 6 and the mapping are still in use.

Only detach the fence when the last reference to the bo_va goes away, matching amdgpu_gem_object_open(), which attaches it only when the bo_va is created. With this the fence stays until the file is closed.

Fixes 31f7efcdca4d ("drm/amdgpu: enable eviction fence")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -3
index 8dbd7bb77305..0552b17d9aa8 100644
@@ -351,11 +351,14 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 			goto out_unlock;
 	}
 
+	bo_va = amdgpu_vm_bo_find(vm, bo);
+	/* the fence is attached once per bo_va, so keep it for other handles */
+	if (bo_va && --bo_va->ref_count)
+		goto out_unlock;
+
 	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
 		amdgpu_evf_mgr_detach_fence(&fpriv->evf_mgr, bo);
-
-	bo_va = amdgpu_vm_bo_find(vm, bo);
-	if (!bo_va || --bo_va->ref_count)
+	if (!bo_va)
 		goto out_unlock;
 
 	amdgpu_vm_bo_del(adev, bo_va);
Signature · 1 line
2.50.1
#3 lore raw

PATCH v2 4/4 drm/amdgpu: reject unknown GEM_METADATA ops

In reply to Jesse Zhang

amdgpu_gem_metadata_ioctl() handles AMDGPU_GEM_METADATA_OP_GET_METADATA and AMDGPU_GEM_METADATA_OP_SET_METADATA and returns r otherwise, which is 0 from the successful amdgpu_bo_reserve(). Any other op therefore succeeds without doing anything.

Convert the if/else chain into a switch and return -EINVAL for unknown ops.

v2: use a switch statement instead of if/else if/else (Christian)

Fixes d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9 -3
index 0552b17d9aa8..fb60a99f43c1 100644
@@ -764,22 +764,28 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
 		goto unreserve;
 	}
 
-	if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
+	switch (args->op) {
+	case AMDGPU_GEM_METADATA_OP_GET_METADATA:
 		amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
 		r = amdgpu_bo_get_metadata(robj, args->data.data,
 					   sizeof(args->data.data),
 					   &args->data.data_size_bytes,
 					   &args->data.flags);
-	} else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
+		break;
+	case AMDGPU_GEM_METADATA_OP_SET_METADATA:
 		if (args->data.data_size_bytes > sizeof(args->data.data)) {
 			r = -EINVAL;
-			goto unreserve;
+			break;
 		}
 		r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
 		if (!r)
 			r = amdgpu_bo_set_metadata(robj, args->data.data,
 						   args->data.data_size_bytes,
 						   args->data.flags);
+		break;
+	default:
+		r = -EINVAL;
+		break;
 	}
 
 unreserve:
Signature · 1 line
2.50.1
maintainer Reviewed-by on v2 4/4
#4 lore raw

Re: [PATCH v2 4/4] drm/amdgpu: reject unknown GEM_METADATA ops

Quoted 5 lines · Jesse Zhang

amdgpu_gem_metadata_ioctl() handles AMDGPU_GEM_METADATA_OP_GET_METADATA and AMDGPU_GEM_METADATA_OP_SET_METADATA and returns r otherwise, which is 0 from the successful amdgpu_bo_reserve(). Any other op therefore succeeds without doing anything.

Quoted 7 lines

Convert the if/else chain into a switch and return -EINVAL for unknown ops.

v2: use a switch statement instead of if/else if/else (Christian)

Fixes d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
Quoted 40 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9 -3
index 0552b17d9aa8..fb60a99f43c1 100644
@@ -764,22 +764,28 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
 		goto unreserve;
 	}
 
-	if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
+	switch (args->op) {
+	case AMDGPU_GEM_METADATA_OP_GET_METADATA:
 		amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
 		r = amdgpu_bo_get_metadata(robj, args->data.data,
 					   sizeof(args->data.data),
 					   &args->data.data_size_bytes,
 					   &args->data.flags);
-	} else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
+		break;
+	case AMDGPU_GEM_METADATA_OP_SET_METADATA:
 		if (args->data.data_size_bytes > sizeof(args->data.data)) {
 			r = -EINVAL;
-			goto unreserve;
+			break;
 		}
 		r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
 		if (!r)
 			r = amdgpu_bo_set_metadata(robj, args->data.data,
 						   args->data.data_size_bytes,
 						   args->data.flags);
+		break;
+	default:
+		r = -EINVAL;
+		break;
 	}
 
 unreserve: