Skip to contentSkip to conversation

drm/amd/amdgpu: Add timeout for user queue fence waiting

PATCH In review Jesse.Zhang 2 participants 5 messages Jan 9

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

PATCH 1/2 drm/amd/amdgpu: Add timeout for user queue fence waiting

In certain error scenarios (e.g., malformed commands), a fence may never become signaled, causing the kernel to hang indefinitely when waiting with MAX_SCHEDULE_TIMEOUT. To prevent such hangs and ensure system responsiveness, replace the indefinite timeout with a reasonable 2-second limit.

This ensures that even if a fence never signals, the wait will time out and appropriate error handling can take place, rather than stalling the driver indefinitely.

v2: make timeout per queue type (adev->gfx_timeout vs adev->compute_timeout vs adev->sdma_timeout) to be consistent with kernel queues. (Alex)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +19 -1
index 98110f543307..402307581293 100644
@@ -367,11 +367,29 @@ static int amdgpu_userq_map_helper(struct amdgpu_usermode_queue *queue)
 static int amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
 	struct dma_fence *f = queue->last_fence;
+	signed long timeout;
 	int ret = 0;
 
+	/* Determine timeout based on queue type */
+	switch (queue->queue_type) {
+	case AMDGPU_RING_TYPE_GFX:
+		timeout = adev->gfx_timeout;
+		break;
+	case AMDGPU_RING_TYPE_COMPUTE:
+		timeout = adev->compute_timeout;
+		break;
+	case AMDGPU_RING_TYPE_SDMA:
+		timeout = adev->sdma_timeout;
+		break;
+	default:
+		timeout = adev->gfx_timeout;
+		break;
+	}
+
 	if (f && !dma_fence_is_signaled(f)) {
-		ret = dma_fence_wait_timeout(f, true, MAX_SCHEDULE_TIMEOUT);
+		ret = dma_fence_wait_timeout(f, true, timeout);
 		if (ret <= 0) {
 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
 				     f->context, f->seqno);
Signature · 1 line
2.49.0
#1 lore raw

PATCH 2/2 drm/amdgpu: fix user queue destruction ordering in amdgpu_userq_destroy

The queue is being destroyed (entering the amdgpu_userq_destroy function).
The counter is decremented prematurely.
The concurrent reset detection in the amdgpu_userq_detect_and_reset_queues() function skips this queue type because the counter has already become 0 at this point.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
index 402307581293..83b85dc0b735 100644
@@ -612,7 +612,6 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 	}
 	amdgpu_bo_unref(&queue->wptr_obj.obj);
 
-	atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
 #if defined(CONFIG_DEBUG_FS)
 	debugfs_remove_recursive(queue->debugfs_queue);
 #endif
@@ -623,6 +622,7 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n");
 		queue->state = AMDGPU_USERQ_STATE_HUNG;
 	}
+	atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
 	amdgpu_userq_cleanup(queue, queue_id);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
Signature · 1 line
2.49.0
maintainer
#2 lore raw
In reply to Jesse.Zhang
Quoted 10 lines · Jesse.Zhang

In certain error scenarios (e.g., malformed commands), a fence may never become signaled, causing the kernel to hang indefinitely when waiting with MAX_SCHEDULE_TIMEOUT. To prevent such hangs and ensure system responsiveness, replace the indefinite timeout with a reasonable 2-second limit.

This ensures that even if a fence never signals, the wait will time out and appropriate error handling can take place, rather than stalling the driver indefinitely.

v2: make timeout per queue type (adev->gfx_timeout vs adev->compute_timeout vs adev->sdma_timeout) to be consistent with kernel queues. (Alex)

Once more: Absolutely clear NAK to this here!

The function amdgpu_userq_wait_for_last_fence() *MUST* wait forever for the last fence to signal otherwise we run into massive problems.

Regards,
Christian.

Quoted 39 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +19 -1
index 98110f543307..402307581293 100644
@@ -367,11 +367,29 @@ static int amdgpu_userq_map_helper(struct amdgpu_usermode_queue *queue)
 static int amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
 	struct dma_fence *f = queue->last_fence;
+	signed long timeout;
 	int ret = 0;
 
+	/* Determine timeout based on queue type */
+	switch (queue->queue_type) {
+	case AMDGPU_RING_TYPE_GFX:
+		timeout = adev->gfx_timeout;
+		break;
+	case AMDGPU_RING_TYPE_COMPUTE:
+		timeout = adev->compute_timeout;
+		break;
+	case AMDGPU_RING_TYPE_SDMA:
+		timeout = adev->sdma_timeout;
+		break;
+	default:
+		timeout = adev->gfx_timeout;
+		break;
+	}
+
 	if (f && !dma_fence_is_signaled(f)) {
-		ret = dma_fence_wait_timeout(f, true, MAX_SCHEDULE_TIMEOUT);
+		ret = dma_fence_wait_timeout(f, true, timeout);
 		if (ret <= 0) {
 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
 				     f->context, f->seqno);
#3 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 23 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, January 9, 2026 5:02 PM
To: Zhang, Jesse(Jie) <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/2] drm/amd/amdgpu: Add timeout for user queue fence waiting

Quoted 1 line · Jesse.Zhang

In certain error scenarios (e.g., malformed commands), a fence may never

become signaled, causing the kernel to hang indefinitely when waiting with MAX_SCHEDULE_TIMEOUT.

Quoted 1 line

To prevent such hangs and ensure system responsiveness, replace the indefinite

timeout with a reasonable 2-second limit.

Quoted 3 lines

This ensures that even if a fence never signals, the wait will time out and appropriate error handling can take place, rather than stalling the driver

indefinitely.

Quoted 5 lines

v2: make timeout per queue type (adev->gfx_timeout vs adev->compute_timeout vs adev->sdma_timeout) to be consistent with kernel queues. (Alex)

Quoted 7 lines
Quoted 2 lines

Once more: Absolutely clear NAK to this here!

The function amdgpu_userq_wait_for_last_fence() *MUST* wait forever for the last fence to signal otherwise we run into massive problems.

[Zhang, Jesse(Jie)]
If data packets are corrupted (e.g., injecting error data packets), it can cause the queue to hang. The last fence will not return, and the process will wait indefinitely. Therefore, we should set an appropriate timeout period.
We can trigger a detection and reset mechanism to recover the hung queue and, if necessary, set the error status of the last fence.

Thanks
Jesse.

Quoted 46 lines

Regards,
Christian.

Quoted 26 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 98110f543307..402307581293 100644
@@ -367,11 +367,29 @@ static int amdgpu_userq_map_helper(struct

amdgpu_usermode_queue *queue) static int

amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue *queue)  {
    struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+   struct amdgpu_device *adev = uq_mgr->adev;
    struct dma_fence *f = queue->last_fence;
+   signed long timeout;
    int ret = 0;
 
+   /* Determine timeout based on queue type */
+   switch (queue->queue_type) {
+   case AMDGPU_RING_TYPE_GFX:
+           timeout = adev->gfx_timeout;
+           break;
+   case AMDGPU_RING_TYPE_COMPUTE:
+           timeout = adev->compute_timeout;
+           break;

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_map_helper(struct amdgpu_usermode_queue *queue)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -1
+   case AMDGPU_RING_TYPE_SDMA:
+           timeout = adev->sdma_timeout;
+           break;
+   default:
+           timeout = adev->gfx_timeout;
+           break;
+   }
+
    if (f && !dma_fence_is_signaled(f)) {
-           ret = dma_fence_wait_timeout(f, true,

MAX_SCHEDULE_TIMEOUT);

Quoted 3 lines
+           ret = dma_fence_wait_timeout(f, true, timeout);
            if (ret <= 0) {
                    drm_file_err(uq_mgr->file, "Timed out waiting for

fence=%llu:%llu\n",

Quoted 1 line
                                 f->context, f->seqno);
maintainer
#4 lore raw
Quoted 28 lines · Jesse(Jie)

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 26 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, January 9, 2026 5:02 PM
To: Zhang, Jesse(Jie) <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/2] drm/amd/amdgpu: Add timeout for user queue fence waiting

Quoted 1 line · Jesse.Zhang

In certain error scenarios (e.g., malformed commands), a fence may never

become signaled, causing the kernel to hang indefinitely when waiting with MAX_SCHEDULE_TIMEOUT.

Quoted 1 line

To prevent such hangs and ensure system responsiveness, replace the indefinite

timeout with a reasonable 2-second limit.

Quoted 3 lines

This ensures that even if a fence never signals, the wait will time out and appropriate error handling can take place, rather than stalling the driver

indefinitely.

Quoted 7 lines

v2: make timeout per queue type (adev->gfx_timeout vs adev->compute_timeout vs adev->sdma_timeout) to be consistent with kernel queues. (Alex)

Quoted 6 lines
Quoted 4 lines

Once more: Absolutely clear NAK to this here!

The function amdgpu_userq_wait_for_last_fence() *MUST* wait forever for the last fence to signal otherwise we run into massive problems.

[Zhang, Jesse(Jie)]
If data packets are corrupted (e.g., injecting error data packets), it can cause the queue to hang. The last fence will not return, and the process will wait indefinitely.

Yes that is expected behavior.

Quoted 1 line

Therefore, we should set an appropriate timeout period.

No, exactly that is absolutely broken.

Quoted 1 line

We can trigger a detection and reset mechanism to recover the hung queue and, if necessary, set the error status of the last fence.

Yes, that is the right thing to do.

The timeout detection must be independent from the fence, e.g. you don't wait for a timeout on the fence but rather have the timeout start as soon as the fence is initialized.

The timeout will then trigger, does the queue reset and signals the fence with an error code.

This will then unblock the waiting processes.

Regards,
Christian.

Quoted 51 lines

Thanks
Jesse.

Quoted 46 lines

Regards,
Christian.

Quoted 26 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 98110f543307..402307581293 100644
@@ -367,11 +367,29 @@ static int amdgpu_userq_map_helper(struct

amdgpu_usermode_queue *queue) static int

amdgpu_userq_wait_for_last_fence(struct amdgpu_usermode_queue *queue)  {
    struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+   struct amdgpu_device *adev = uq_mgr->adev;
    struct dma_fence *f = queue->last_fence;
+   signed long timeout;
    int ret = 0;
 
+   /* Determine timeout based on queue type */
+   switch (queue->queue_type) {
+   case AMDGPU_RING_TYPE_GFX:
+           timeout = adev->gfx_timeout;
+           break;
+   case AMDGPU_RING_TYPE_COMPUTE:
+           timeout = adev->compute_timeout;
+           break;

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_map_helper(struct amdgpu_usermode_queue *queue)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -1
+   case AMDGPU_RING_TYPE_SDMA:
+           timeout = adev->sdma_timeout;
+           break;
+   default:
+           timeout = adev->gfx_timeout;
+           break;
+   }
+
    if (f && !dma_fence_is_signaled(f)) {
-           ret = dma_fence_wait_timeout(f, true,

MAX_SCHEDULE_TIMEOUT);

Quoted 3 lines
+           ret = dma_fence_wait_timeout(f, true, timeout);
            if (ret <= 0) {
                    drm_file_err(uq_mgr->file, "Timed out waiting for

fence=%llu:%llu\n",

Quoted 1 line
                                 f->context, f->seqno);