Skip to contentSkip to conversation

drm/amdgpu/userq: add reset helper and identify guilty user queue

PATCH Superseded Jesse Zhang 2 participants 16 messages Jun 11 – Jun 17

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/7 drm/amdgpu/userq: add reset helper and identify guilty user queue

From: Alex Deucher <[email protected]>

If we get an interrupt for a bad user queue (bad opcode, etc.), add a helper to handle the reset for user queues.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40 +++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 12 +++++++
 2 files changed, 49 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +37 -3
index de6848b4fd67..231ffb29fe5e 100644
@@ -140,9 +140,14 @@ static void amdgpu_userq_hang_detect_work(struct work_struct *work)
 						 AMDGPU_RESET_TYPE_PER_QUEUE)) {
 		int r;
 
-		if (queue->queue_type == AMDGPU_HW_IP_COMPUTE)
-			r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL, NULL, NULL, NULL);
-		else
+		if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
+			struct amdgpu_usermode_queue *guilty_uq;
+
+			/* IRQ-side WRITE_ONCE(guilty,true) hand-off via xchg */
+			guilty_uq = xchg(&queue->guilty, false) ? queue : NULL;
+			r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL,
+							 guilty_uq, NULL, NULL);
+		} else
 			r = userq_funcs->reset(queue);
 		if (r)
 			gpu_reset = true;
@@ -671,6 +676,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	}
 
 	queue->doorbell_index = index;
+	queue->doorbell_offset = (u32)args->in.doorbell_offset;
 	r = uq_funcs->mqd_create(queue, &args->in);
 	if (r) {
 		drm_file_err(uq_mgr->file, "Failed to create Queue\n");
@@ -1111,6 +1117,34 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 	dma_fence_put(ev_fence);
 }
 
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+				    u32 pasid, u32 doorbell_offset)
+{
+	struct xarray *xa = &adev->userq_doorbell_xa;
+	struct amdgpu_usermode_queue *queue;
+	unsigned long flags, idx;
+
+	/*
+	 * CP priv-fault payload is (pasid, src_data[0] & 0x3ff) — the same
+	 * per-process doorbell encoding KFD extracts via
+	 * KFD_CTXID0_DOORBELL_ID_MASK. Find the offending queue by the
+	 * (vm->pasid, doorbell_offset) pair, mark it guilty and fire
+	 * hang_detect immediately (queue_delayed_work() would no-op if the
+	 * work is already armed at submit time).
+	 */
+	xa_lock_irqsave(xa, flags);
+	xa_for_each(xa, idx, queue) {
+		if (queue->vm && queue->vm->pasid == pasid &&
+		    queue->doorbell_offset == doorbell_offset) {
+			WRITE_ONCE(queue->guilty, true);
+			mod_delayed_work(adev->reset_domain->wq,
+					 &queue->hang_detect_work, 0);
+			break;
+		}
+	}
+	xa_unlock_irqrestore(xa, flags);
+}
+
 static int
 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
 {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +12 -0
index 9df1b78407f5..a55d57e2c169 100644
@@ -53,6 +53,7 @@ struct amdgpu_usermode_queue {
 	enum amdgpu_userq_state state;
 	uint64_t		doorbell_handle;
 	uint64_t		doorbell_index;
+	u32			doorbell_offset;
 	uint64_t		flags;
 	struct amdgpu_mqd_prop	*userq_prop;
 	struct amdgpu_userq_mgr *userq_mgr;
@@ -86,6 +87,7 @@ struct amdgpu_usermode_queue {
 	 * Delayed work which runs when userq_fences time out.
 	 */
 	struct delayed_work	hang_detect_work;
+	bool			guilty;
 	struct kref		refcount;
 
 	union {
@@ -176,6 +178,16 @@ int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost);
 void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue);
 void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);
 
+/*
+ * CP packs the per-process doorbell_id of the queue in
+ * CTXID0[9:0] on priv-fault (same encoding KFD uses via
+ * KFD_CTXID0_DOORBELL_ID_MASK)
+ */
+#define AMDGPU_CTXID0_DOORBELL_ID_MASK	0x3ff
+
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+				    u32 pasid, u32 doorbell_offset);
+
 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
 				   struct amdgpu_usermode_queue *queue,
 				   u64 addr, u64 expected_size, u64 *va_out);
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 2/7 drm/amdgpu/gfx11: handle error interrupts for userqs

From: Alex Deucher <[email protected]>

Call the new userq reset helper, and dispatch KQs first by ring_id before falling back to the user-queue lookup.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 32 ++++++++++++++++++--------
 1 file changed, 23 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +23 -9
index 0bd9d8a21f5e..c49a31a3789c 100644
@@ -6681,22 +6681,29 @@ static int gfx_v11_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
 static void gfx_v11_0_handle_priv_fault(struct amdgpu_device *adev,
 					struct amdgpu_iv_entry *entry)
 {
-	u8 me_id, pipe_id, queue_id;
-	struct amdgpu_ring *ring;
-	int i;
-
-	me_id = (entry->ring_id & 0x0c) >> 2;
-	pipe_id = (entry->ring_id & 0x03) >> 0;
-	queue_id = (entry->ring_id & 0x70) >> 4;
+	u32 doorbell_offset = entry->src_data[0] & AMDGPU_CTXID0_DOORBELL_ID_MASK;
 
+	/*
+	 * Try KQ first by ring_id (HW slot is authoritative). The
+	 * KMD compute_hqd_mask contract guarantees KCQ and user queues
+	 * never share a HW slot.
+	 */
 	if (!adev->gfx.disable_kq) {
+		u8 me_id = (entry->ring_id & 0x0c) >> 2;
+		u8 pipe_id = (entry->ring_id & 0x03) >> 0;
+		u8 queue_id = (entry->ring_id & 0x70) >> 4;
+		struct amdgpu_ring *ring;
+		int i;
+
 		switch (me_id) {
 		case 0:
 			for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
 				ring = &adev->gfx.gfx_ring[i];
 				if (ring->me == me_id && ring->pipe == pipe_id &&
-				    ring->queue == queue_id)
+				    ring->queue == queue_id) {
 					drm_sched_fault(&ring->sched);
+					return;
+				}
 			}
 			break;
 		case 1:
@@ -6704,8 +6711,10 @@ static void gfx_v11_0_handle_priv_fault(struct amdgpu_device *adev,
 			for (i = 0; i < adev->gfx.num_compute_rings; i++) {
 				ring = &adev->gfx.compute_ring[i];
 				if (ring->me == me_id && ring->pipe == pipe_id &&
-				    ring->queue == queue_id)
+				    ring->queue == queue_id) {
 					drm_sched_fault(&ring->sched);
+					return;
+				}
 			}
 			break;
 		default:
@@ -6713,6 +6722,11 @@ static void gfx_v11_0_handle_priv_fault(struct amdgpu_device *adev,
 			break;
 		}
 	}
+
+	/* No KQ matched: HW slot is a MES-scheduled user queue. */
+	if (adev->enable_mes && doorbell_offset)
+		amdgpu_userq_process_reset_irq(adev, entry->pasid,
+					       doorbell_offset);
 }
 
 static int gfx_v11_0_priv_reg_irq(struct amdgpu_device *adev,
Signature · 1 line
2.49.0
Reviewed-by on v2 2/7
#2 lore raw

Re: [PATCH v2 2/7] drm/amdgpu/gfx11: handle error interrupts for userqs

Quoted 2 lines · Jesse Zhang

From: Alex Deucher <[email protected]>

Once again, feel free to take ownership of these. You did most of the work.

Quoted 82 lines

Call the new userq reset helper, and dispatch KQs first by ring_id before falling back to the user-queue lookup.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 32 ++++++++++++++++++--------
 1 file changed, 23 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +23 -10
index 0bd9d8a21f5e..c49a31a3789c 100644
@@ -6681,22 +6681,29 @@ static int gfx_v11_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
 static void gfx_v11_0_handle_priv_fault(struct amdgpu_device *adev,
                                        struct amdgpu_iv_entry *entry)
 {
-       u8 me_id, pipe_id, queue_id;
-       struct amdgpu_ring *ring;
-       int i;
-
-       me_id = (entry->ring_id & 0x0c) >> 2;
-       pipe_id = (entry->ring_id & 0x03) >> 0;
-       queue_id = (entry->ring_id & 0x70) >> 4;
+       u32 doorbell_offset = entry->src_data[0] & AMDGPU_CTXID0_DOORBELL_ID_MASK;
 
+       /*
+        * Try KQ first by ring_id (HW slot is authoritative). The
+        * KMD compute_hqd_mask contract guarantees KCQ and user queues
+        * never share a HW slot.
+        */
        if (!adev->gfx.disable_kq) {
+               u8 me_id = (entry->ring_id & 0x0c) >> 2;
+               u8 pipe_id = (entry->ring_id & 0x03) >> 0;
+               u8 queue_id = (entry->ring_id & 0x70) >> 4;
+               struct amdgpu_ring *ring;
+               int i;
+
                switch (me_id) {
                case 0:
                        for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
                                ring = &adev->gfx.gfx_ring[i];
                                if (ring->me == me_id && ring->pipe == pipe_id &&
-                                   ring->queue == queue_id)
+                                   ring->queue == queue_id) {
                                        drm_sched_fault(&ring->sched);
+                                       return;
+                               }
                        }
                        break;
                case 1:
@@ -6704,8 +6711,10 @@ static void gfx_v11_0_handle_priv_fault(struct amdgpu_device *adev,
                        for (i = 0; i < adev->gfx.num_compute_rings; i++) {
                                ring = &adev->gfx.compute_ring[i];
                                if (ring->me == me_id && ring->pipe == pipe_id &&
-                                   ring->queue == queue_id)
+                                   ring->queue == queue_id) {
                                        drm_sched_fault(&ring->sched);
+                                       return;
+                               }
                        }
                        break;
                default:
@@ -6713,6 +6722,11 @@ static void gfx_v11_0_handle_priv_fault(struct amdgpu_device *adev,
                        break;
                }
        }
+
+       /* No KQ matched: HW slot is a MES-scheduled user queue. */
+       if (adev->enable_mes && doorbell_offset)
+               amdgpu_userq_process_reset_irq(adev, entry->pasid,
+                                              doorbell_offset);
 }
 
 static int gfx_v11_0_priv_reg_irq(struct amdgpu_device *adev,
--

2.49.0

#3 lore raw

PATCH v2 3/7 drm/amdgpu/gfx12: handle error interrupts for userqs

In reply to Jesse Zhang

From: Alex Deucher <[email protected]>

Call the new userq reset helper, and dispatch KQs first by ring_id before falling back to the user-queue lookup.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 33 +++++++++++++++++++-------
 1 file changed, 24 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +24 -9
index 380ba062134e..cb4d84c5f52b 100644
@@ -5009,22 +5009,30 @@ static int gfx_v12_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
 static void gfx_v12_0_handle_priv_fault(struct amdgpu_device *adev,
 					struct amdgpu_iv_entry *entry)
 {
-	u8 me_id, pipe_id, queue_id;
-	struct amdgpu_ring *ring;
-	int i;
-
-	me_id = (entry->ring_id & 0x0c) >> 2;
-	pipe_id = (entry->ring_id & 0x03) >> 0;
-	queue_id = (entry->ring_id & 0x70) >> 4;
+	u32 doorbell_offset = entry->src_data[0] & AMDGPU_CTXID0_DOORBELL_ID_MASK;
 
+	/*
+	 * Try KQ first by ring_id; UQ as fallback. KCQ and UQ never share
+	 * a HW slot (compute_hqd_mask contract).
+	 */
 	if (!adev->gfx.disable_kq) {
+		u8 me_id, pipe_id, queue_id;
+		struct amdgpu_ring *ring;
+		int i;
+
+		me_id = (entry->ring_id & 0x0c) >> 2;
+		pipe_id = (entry->ring_id & 0x03) >> 0;
+		queue_id = (entry->ring_id & 0x70) >> 4;
+
 		switch (me_id) {
 		case 0:
 			for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
 				ring = &adev->gfx.gfx_ring[i];
 				if (ring->me == me_id && ring->pipe == pipe_id &&
-				    ring->queue == queue_id)
+				    ring->queue == queue_id) {
 					drm_sched_fault(&ring->sched);
+					return;
+				}
 			}
 			break;
 		case 1:
@@ -5032,8 +5040,10 @@ static void gfx_v12_0_handle_priv_fault(struct amdgpu_device *adev,
 			for (i = 0; i < adev->gfx.num_compute_rings; i++) {
 				ring = &adev->gfx.compute_ring[i];
 				if (ring->me == me_id && ring->pipe == pipe_id &&
-				    ring->queue == queue_id)
+				    ring->queue == queue_id) {
 					drm_sched_fault(&ring->sched);
+					return;
+				}
 			}
 			break;
 		default:
@@ -5041,6 +5051,11 @@ static void gfx_v12_0_handle_priv_fault(struct amdgpu_device *adev,
 			break;
 		}
 	}
+
+	/* No KQ matched: HW slot is a MES-scheduled user queue. */
+	if (adev->enable_mes && doorbell_offset)
+		amdgpu_userq_process_reset_irq(adev, entry->pasid,
+					       doorbell_offset);
 }
 
 static int gfx_v12_0_priv_reg_irq(struct amdgpu_device *adev,
Signature · 1 line
2.49.0
Reviewed-by on v2 3/7
#4 lore raw

Re: [PATCH v2 3/7] drm/amdgpu/gfx12: handle error interrupts for userqs

Quoted 2 lines · Jesse Zhang

From: Alex Deucher <[email protected]>

Once again, feel free to take ownership of these. You did most of the work.

Quoted 83 lines

Call the new userq reset helper, and dispatch KQs first by ring_id before falling back to the user-queue lookup.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 33 +++++++++++++++++++-------
 1 file changed, 24 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +24 -10
index 380ba062134e..cb4d84c5f52b 100644
@@ -5009,22 +5009,30 @@ static int gfx_v12_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
 static void gfx_v12_0_handle_priv_fault(struct amdgpu_device *adev,
                                        struct amdgpu_iv_entry *entry)
 {
-       u8 me_id, pipe_id, queue_id;
-       struct amdgpu_ring *ring;
-       int i;
-
-       me_id = (entry->ring_id & 0x0c) >> 2;
-       pipe_id = (entry->ring_id & 0x03) >> 0;
-       queue_id = (entry->ring_id & 0x70) >> 4;
+       u32 doorbell_offset = entry->src_data[0] & AMDGPU_CTXID0_DOORBELL_ID_MASK;
 
+       /*
+        * Try KQ first by ring_id; UQ as fallback. KCQ and UQ never share
+        * a HW slot (compute_hqd_mask contract).
+        */
        if (!adev->gfx.disable_kq) {
+               u8 me_id, pipe_id, queue_id;
+               struct amdgpu_ring *ring;
+               int i;
+
+               me_id = (entry->ring_id & 0x0c) >> 2;
+               pipe_id = (entry->ring_id & 0x03) >> 0;
+               queue_id = (entry->ring_id & 0x70) >> 4;
+
                switch (me_id) {
                case 0:
                        for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
                                ring = &adev->gfx.gfx_ring[i];
                                if (ring->me == me_id && ring->pipe == pipe_id &&
-                                   ring->queue == queue_id)
+                                   ring->queue == queue_id) {
                                        drm_sched_fault(&ring->sched);
+                                       return;
+                               }
                        }
                        break;
                case 1:
@@ -5032,8 +5040,10 @@ static void gfx_v12_0_handle_priv_fault(struct amdgpu_device *adev,
                        for (i = 0; i < adev->gfx.num_compute_rings; i++) {
                                ring = &adev->gfx.compute_ring[i];
                                if (ring->me == me_id && ring->pipe == pipe_id &&
-                                   ring->queue == queue_id)
+                                   ring->queue == queue_id) {
                                        drm_sched_fault(&ring->sched);
+                                       return;
+                               }
                        }
                        break;
                default:
@@ -5041,6 +5051,11 @@ static void gfx_v12_0_handle_priv_fault(struct amdgpu_device *adev,
                        break;
                }
        }
+
+       /* No KQ matched: HW slot is a MES-scheduled user queue. */
+       if (adev->enable_mes && doorbell_offset)
+               amdgpu_userq_process_reset_irq(adev, entry->pasid,
+                                              doorbell_offset);
 }
 
 static int gfx_v12_0_priv_reg_irq(struct amdgpu_device *adev,
--

2.49.0

#5 lore raw

PATCH v2 4/7 drm/amdgpu/gfx11: strict (me, pipe, queue) match for gfx userq EOP

In reply to Jesse Zhang

Require a strict (me,pipe,queue) match in the gfx case, then userq gfx EOPs fall through to amdgpu_userq_process_fence_irq().

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +11 -4
index c49a31a3789c..208f9ffed60a 100644
@@ -6530,10 +6530,17 @@ static int gfx_v11_0_eop_irq(struct amdgpu_device *adev,
 
 		switch (me_id) {
 		case 0:
-			if (pipe_id == 0)
-				amdgpu_fence_process(&adev->gfx.gfx_ring[0]);
-			else
-				amdgpu_fence_process(&adev->gfx.gfx_ring[1]);
+			/*
+			 * MES splits gfx HQDs per (me,pipe): KGQ owns queue=0,
+			 * userq gfx owns queue>=1 (see amdgpu_mes_get_hqd_mask).
+			 */
+			for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
+				ring = &adev->gfx.gfx_ring[i];
+				if ((ring->me == me_id) &&
+				    (ring->pipe == pipe_id) &&
+				    (ring->queue == queue_id))
+					amdgpu_fence_process(ring);
+			}
 			break;
 		case 1:
 		case 2:
Signature · 1 line
2.49.0
#6 lore raw

PATCH v2 5/7 drm/amdgpu/gfx12: strict (me, pipe, queue) match for gfx userq EOP

In reply to Jesse Zhang

Require a strict (me,pipe,queue) match in the gfx case.
The EOP IRQ case me_id=0 (gfx) consumed the interrupt for the kernel gfx_ring[] based on pipe_id alone. MES splits gfx HQDs per (me,pipe):
KGQ owns queue=0, userq gfx owns queue>=1 (see amdgpu_mes_get_hqd_mask).

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +11 -4
index cb4d84c5f52b..d9b1fc29e9d8 100644
@@ -4858,10 +4858,17 @@ static int gfx_v12_0_eop_irq(struct amdgpu_device *adev,
 
 		switch (me_id) {
 		case 0:
-			if (pipe_id == 0)
-				amdgpu_fence_process(&adev->gfx.gfx_ring[0]);
-			else
-				amdgpu_fence_process(&adev->gfx.gfx_ring[1]);
+			/*
+			 * MES splits gfx HQDs per (me,pipe): KGQ owns queue=0,
+			 * userq gfx owns queue>=1 (see amdgpu_mes_get_hqd_mask).
+			 */
+			for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
+				ring = &adev->gfx.gfx_ring[i];
+				if ((ring->me == me_id) &&
+				    (ring->pipe == pipe_id) &&
+				    (ring->queue == queue_id))
+					amdgpu_fence_process(ring);
+			}
 			break;
 		case 1:
 		case 2:
Signature · 1 line
2.49.0
#7 lore raw

PATCH v2 6/7 drm/amdgpu/gfx11: try KQ first in EOP IRQ

In reply to Jesse Zhang

KQ EOPs were misrouted into the userq fence path when enable_mes is true. Try KQ by ring_id first (KCQ and UQ never share a HW slot); fall back to amdgpu_userq_process_fence_irq() on miss.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 30 ++++++++++++++++----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +19 -11
index 208f9ffed60a..a3294cf96992 100644
@@ -6515,31 +6515,32 @@ static int gfx_v11_0_eop_irq(struct amdgpu_device *adev,
 			     struct amdgpu_iv_entry *entry)
 {
 	u32 doorbell_offset = entry->src_data[0];
-	u8 me_id, pipe_id, queue_id;
-	struct amdgpu_ring *ring;
-	int i;
 
 	DRM_DEBUG("IH: CP EOP\n");
 
-	if (adev->enable_mes && doorbell_offset) {
-		amdgpu_userq_process_fence_irq(adev, doorbell_offset);
-	} else {
-		me_id = (entry->ring_id & 0x0c) >> 2;
-		pipe_id = (entry->ring_id & 0x03) >> 0;
-		queue_id = (entry->ring_id & 0x70) >> 4;
+	if (!adev->gfx.disable_kq) {
+		u8 me_id = (entry->ring_id & 0x0c) >> 2;
+		u8 pipe_id = (entry->ring_id & 0x03) >> 0;
+		u8 queue_id = (entry->ring_id & 0x70) >> 4;
+		struct amdgpu_ring *ring;
+		int i;
 
 		switch (me_id) {
 		case 0:
 			/*
 			 * MES splits gfx HQDs per (me,pipe): KGQ owns queue=0,
 			 * userq gfx owns queue>=1 (see amdgpu_mes_get_hqd_mask).
+			 * Require a strict (me,pipe,queue) match so userq gfx
+			 * EOPs fall through to amdgpu_userq_process_fence_irq().
 			 */
 			for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
 				ring = &adev->gfx.gfx_ring[i];
 				if ((ring->me == me_id) &&
 				    (ring->pipe == pipe_id) &&
-				    (ring->queue == queue_id))
+				    (ring->queue == queue_id)) {
 					amdgpu_fence_process(ring);
+					return 0;
+				}
 			}
 			break;
 		case 1:
@@ -6552,13 +6553,20 @@ static int gfx_v11_0_eop_irq(struct amdgpu_device *adev,
 				 */
 				if ((ring->me == me_id) &&
 				    (ring->pipe == pipe_id) &&
-				    (ring->queue == queue_id))
+				    (ring->queue == queue_id)) {
 					amdgpu_fence_process(ring);
+					return 0;
+				}
 			}
 			break;
+		default:
+			break;
 		}
 	}
 
+	if (adev->enable_mes && doorbell_offset)
+		amdgpu_userq_process_fence_irq(adev, doorbell_offset);
+
 	return 0;
 }
 
Signature · 1 line
2.49.0
Reviewed-by on v2 6/7
#8 lore raw

Re: [PATCH v2 6/7] drm/amdgpu/gfx11: try KQ first in EOP IRQ

Quoted 7 lines · Jesse Zhang

KQ EOPs were misrouted into the userq fence path when enable_mes is true. Try KQ by ring_id first (KCQ and UQ never share a HW slot); fall back to amdgpu_userq_process_fence_irq() on miss.

I think you can squash 4 and 6 into one patch. With that fixed:

Alex

Quoted 76 lines
---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 30 ++++++++++++++++----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +19 -12
index 208f9ffed60a..a3294cf96992 100644
@@ -6515,31 +6515,32 @@ static int gfx_v11_0_eop_irq(struct amdgpu_device *adev,
                             struct amdgpu_iv_entry *entry)
 {
        u32 doorbell_offset = entry->src_data[0];
-       u8 me_id, pipe_id, queue_id;
-       struct amdgpu_ring *ring;
-       int i;
 
        DRM_DEBUG("IH: CP EOP\n");
 
-       if (adev->enable_mes && doorbell_offset) {
-               amdgpu_userq_process_fence_irq(adev, doorbell_offset);
-       } else {
-               me_id = (entry->ring_id & 0x0c) >> 2;
-               pipe_id = (entry->ring_id & 0x03) >> 0;
-               queue_id = (entry->ring_id & 0x70) >> 4;
+       if (!adev->gfx.disable_kq) {
+               u8 me_id = (entry->ring_id & 0x0c) >> 2;
+               u8 pipe_id = (entry->ring_id & 0x03) >> 0;
+               u8 queue_id = (entry->ring_id & 0x70) >> 4;
+               struct amdgpu_ring *ring;
+               int i;
 
                switch (me_id) {
                case 0:
                        /*
                         * MES splits gfx HQDs per (me,pipe): KGQ owns queue=0,
                         * userq gfx owns queue>=1 (see amdgpu_mes_get_hqd_mask).
+                        * Require a strict (me,pipe,queue) match so userq gfx
+                        * EOPs fall through to amdgpu_userq_process_fence_irq().
                         */
                        for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
                                ring = &adev->gfx.gfx_ring[i];
                                if ((ring->me == me_id) &&
                                    (ring->pipe == pipe_id) &&
-                                   (ring->queue == queue_id))
+                                   (ring->queue == queue_id)) {
                                        amdgpu_fence_process(ring);
+                                       return 0;
+                               }
                        }
                        break;
                case 1:
@@ -6552,13 +6553,20 @@ static int gfx_v11_0_eop_irq(struct amdgpu_device *adev,
                                 */
                                if ((ring->me == me_id) &&
                                    (ring->pipe == pipe_id) &&
-                                   (ring->queue == queue_id))
+                                   (ring->queue == queue_id)) {
                                        amdgpu_fence_process(ring);
+                                       return 0;
+                               }
                        }
                        break;
+               default:
+                       break;
                }
        }
 
+       if (adev->enable_mes && doorbell_offset)
+               amdgpu_userq_process_fence_irq(adev, doorbell_offset);
+
        return 0;
 }
 
--

2.49.0

#9 lore raw

PATCH v2 7/7 drm/amdgpu/gfx12: try KQ first in EOP IRQ

In reply to Jesse Zhang

KCQ EOPs were misrouted into the userq fence path when enable_mes is true. Try KQ by ring_id first (KCQ and UQ never share a HW slot); fall back to amdgpu_userq_process_fence_irq() on miss.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 30 ++++++++++++++++----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +19 -11
index d9b1fc29e9d8..c5fbfc6d8338 100644
@@ -4843,31 +4843,32 @@ static int gfx_v12_0_eop_irq(struct amdgpu_device *adev,
 			     struct amdgpu_iv_entry *entry)
 {
 	u32 doorbell_offset = entry->src_data[0];
-	u8 me_id, pipe_id, queue_id;
-	struct amdgpu_ring *ring;
-	int i;
 
 	DRM_DEBUG("IH: CP EOP\n");
 
-	if (adev->enable_mes && doorbell_offset) {
-		amdgpu_userq_process_fence_irq(adev, doorbell_offset);
-	} else {
-		me_id = (entry->ring_id & 0x0c) >> 2;
-		pipe_id = (entry->ring_id & 0x03) >> 0;
-		queue_id = (entry->ring_id & 0x70) >> 4;
+	if (!adev->gfx.disable_kq) {
+		u8 me_id = (entry->ring_id & 0x0c) >> 2;
+		u8 pipe_id = (entry->ring_id & 0x03) >> 0;
+		u8 queue_id = (entry->ring_id & 0x70) >> 4;
+		struct amdgpu_ring *ring;
+		int i;
 
 		switch (me_id) {
 		case 0:
 			/*
 			 * MES splits gfx HQDs per (me,pipe): KGQ owns queue=0,
 			 * userq gfx owns queue>=1 (see amdgpu_mes_get_hqd_mask).
+			 * Require a strict (me,pipe,queue) match so userq gfx
+			 * EOPs fall through to amdgpu_userq_process_fence_irq().
 			 */
 			for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
 				ring = &adev->gfx.gfx_ring[i];
 				if ((ring->me == me_id) &&
 				    (ring->pipe == pipe_id) &&
-				    (ring->queue == queue_id))
+				    (ring->queue == queue_id)) {
 					amdgpu_fence_process(ring);
+					return 0;
+				}
 			}
 			break;
 		case 1:
@@ -4880,13 +4881,20 @@ static int gfx_v12_0_eop_irq(struct amdgpu_device *adev,
 				 */
 				if ((ring->me == me_id) &&
 				    (ring->pipe == pipe_id) &&
-				    (ring->queue == queue_id))
+				    (ring->queue == queue_id)) {
 					amdgpu_fence_process(ring);
+					return 0;
+				}
 			}
 			break;
+		default:
+			break;
 		}
 	}
 
+	if (adev->enable_mes && doorbell_offset)
+		amdgpu_userq_process_fence_irq(adev, doorbell_offset);
+
 	return 0;
 }
 
Signature · 1 line
2.49.0
Reviewed-by on v2 7/7
#10 lore raw

Re: [PATCH v2 7/7] drm/amdgpu/gfx12: try KQ first in EOP IRQ

Quoted 7 lines · Jesse Zhang

KCQ EOPs were misrouted into the userq fence path when enable_mes is true. Try KQ by ring_id first (KCQ and UQ never share a HW slot); fall back to amdgpu_userq_process_fence_irq() on miss.

I think you can squash 5 and 7 into one patch. With that fixed:

Alex

Quoted 76 lines
---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 30 ++++++++++++++++----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +19 -12
index d9b1fc29e9d8..c5fbfc6d8338 100644
@@ -4843,31 +4843,32 @@ static int gfx_v12_0_eop_irq(struct amdgpu_device *adev,
                             struct amdgpu_iv_entry *entry)
 {
        u32 doorbell_offset = entry->src_data[0];
-       u8 me_id, pipe_id, queue_id;
-       struct amdgpu_ring *ring;
-       int i;
 
        DRM_DEBUG("IH: CP EOP\n");
 
-       if (adev->enable_mes && doorbell_offset) {
-               amdgpu_userq_process_fence_irq(adev, doorbell_offset);
-       } else {
-               me_id = (entry->ring_id & 0x0c) >> 2;
-               pipe_id = (entry->ring_id & 0x03) >> 0;
-               queue_id = (entry->ring_id & 0x70) >> 4;
+       if (!adev->gfx.disable_kq) {
+               u8 me_id = (entry->ring_id & 0x0c) >> 2;
+               u8 pipe_id = (entry->ring_id & 0x03) >> 0;
+               u8 queue_id = (entry->ring_id & 0x70) >> 4;
+               struct amdgpu_ring *ring;
+               int i;
 
                switch (me_id) {
                case 0:
                        /*
                         * MES splits gfx HQDs per (me,pipe): KGQ owns queue=0,
                         * userq gfx owns queue>=1 (see amdgpu_mes_get_hqd_mask).
+                        * Require a strict (me,pipe,queue) match so userq gfx
+                        * EOPs fall through to amdgpu_userq_process_fence_irq().
                         */
                        for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
                                ring = &adev->gfx.gfx_ring[i];
                                if ((ring->me == me_id) &&
                                    (ring->pipe == pipe_id) &&
-                                   (ring->queue == queue_id))
+                                   (ring->queue == queue_id)) {
                                        amdgpu_fence_process(ring);
+                                       return 0;
+                               }
                        }
                        break;
                case 1:
@@ -4880,13 +4881,20 @@ static int gfx_v12_0_eop_irq(struct amdgpu_device *adev,
                                 */
                                if ((ring->me == me_id) &&
                                    (ring->pipe == pipe_id) &&
-                                   (ring->queue == queue_id))
+                                   (ring->queue == queue_id)) {
                                        amdgpu_fence_process(ring);
+                                       return 0;
+                               }
                        }
                        break;
+               default:
+                       break;
                }
        }
 
+       if (adev->enable_mes && doorbell_offset)
+               amdgpu_userq_process_fence_irq(adev, doorbell_offset);
+
        return 0;
 }
 
--

2.49.0

#11 lore raw
In reply to Jesse Zhang

AMD General

Ping ...

Quoted 134 lines

-----Original Message-----
From: Jesse Zhang <[email protected]>
Sent: Thursday, June 11, 2026 11:57 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: [PATCH v2 1/7] drm/amdgpu/userq: add reset helper and identify guilty user queue

From: Alex Deucher <[email protected]>

If we get an interrupt for a bad user queue (bad opcode, etc.), add a helper to handle the reset for user queues.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40 +++++++++++++++++++++--
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 12 +++++++
 2 files changed, 49 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -3
index de6848b4fd67..231ffb29fe5e 100644
@@ -140,9 +140,14 @@ static void amdgpu_userq_hang_detect_work(struct
 work_struct *work)
 
 AMDGPU_RESET_TYPE_PER_QUEUE)) {
              int r;
 
-             if (queue->queue_type == AMDGPU_HW_IP_COMPUTE)
-                     r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL,
 NULL, NULL, NULL);
-             else
+             if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
+                     struct amdgpu_usermode_queue *guilty_uq;
+
+                     /* IRQ-side WRITE_ONCE(guilty,true) hand-off via xchg */
+                     guilty_uq = xchg(&queue->guilty, false) ? queue : NULL;
+                     r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL,
+                                                      guilty_uq, NULL, NULL);
+             } else
                      r = userq_funcs->reset(queue);
              if (r)
                      gpu_reset = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
@@ -671,6 +676,7 @@ amdgpu_userq_create(struct drm_file *filp, union
 drm_amdgpu_userq *args)
      }
 
      queue->doorbell_index = index;
+     queue->doorbell_offset = (u32)args->in.doorbell_offset;
      r = uq_funcs->mqd_create(queue, &args->in);
      if (r) {
              drm_file_err(uq_mgr->file, "Failed to create Queue\n"); @@ -1111,6
+1117,34 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
      dma_fence_put(ev_fence);

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +27 -0
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                 u32 pasid, u32 doorbell_offset) {
+     struct xarray *xa = &adev->userq_doorbell_xa;
+     struct amdgpu_usermode_queue *queue;
+     unsigned long flags, idx;
+
+     /*
+      * CP priv-fault payload is (pasid, src_data[0] & 0x3ff) - the same
+      * per-process doorbell encoding KFD extracts via
+      * KFD_CTXID0_DOORBELL_ID_MASK. Find the offending queue by the
+      * (vm->pasid, doorbell_offset) pair, mark it guilty and fire
+      * hang_detect immediately (queue_delayed_work() would no-op if the
+      * work is already armed at submit time).
+      */
+     xa_lock_irqsave(xa, flags);
+     xa_for_each(xa, idx, queue) {
+             if (queue->vm && queue->vm->pasid == pasid &&
+                 queue->doorbell_offset == doorbell_offset) {
+                     WRITE_ONCE(queue->guilty, true);
+                     mod_delayed_work(adev->reset_domain->wq,
+                                      &queue->hang_detect_work, 0);
+                     break;
+             }
+     }
+     xa_unlock_irqrestore(xa, flags);
+}
+
 static int
 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)  { diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index 9df1b78407f5..a55d57e2c169 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
@@ -53,6 +53,7 @@ struct amdgpu_usermode_queue {
      enum amdgpu_userq_state state;
      uint64_t                doorbell_handle;
      uint64_t                doorbell_index;
+     u32                     doorbell_offset;
      uint64_t                flags;
      struct amdgpu_mqd_prop  *userq_prop;
      struct amdgpu_userq_mgr *userq_mgr;
@@ -86,6 +87,7 @@ struct amdgpu_usermode_queue {
       * Delayed work which runs when userq_fences time out.
       */
      struct delayed_work     hang_detect_work;
+     bool                    guilty;
      struct kref             refcount;
 
      union {
@@ -176,6 +178,16 @@ int amdgpu_userq_post_reset(struct amdgpu_device
 *adev, bool vram_lost);  void amdgpu_userq_start_hang_detect_work(struct

amdgpu_usermode_queue *queue); void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +10 -1
+/*
+ * CP packs the per-process doorbell_id of the queue in
+ * CTXID0[9:0] on priv-fault (same encoding KFD uses via
+ * KFD_CTXID0_DOORBELL_ID_MASK)
+ */
+#define AMDGPU_CTXID0_DOORBELL_ID_MASK       0x3ff
+
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                 u32 pasid, u32 doorbell_offset);
+
 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
                                 struct amdgpu_usermode_queue *queue,
                                 u64 addr, u64 expected_size, u64 *va_out);
--

2.49.0

#12 lore raw
In reply to Jesse Zhang
Quoted 2 lines · Jesse Zhang

From: Alex Deucher <[email protected]>

Feel free to take over the authorship of this patch. You've done most of the work.

Quoted 60 lines

If we get an interrupt for a bad user queue (bad opcode, etc.), add a helper to handle the reset for user queues.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40 +++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 12 +++++++
 2 files changed, 49 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +21 -3
index de6848b4fd67..231ffb29fe5e 100644
@@ -140,9 +140,14 @@ static void amdgpu_userq_hang_detect_work(struct work_struct *work)
                                                 AMDGPU_RESET_TYPE_PER_QUEUE)) {
                int r;
 
-               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE)
-                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL, NULL, NULL, NULL);
-               else
+               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
+                       struct amdgpu_usermode_queue *guilty_uq;
+
+                       /* IRQ-side WRITE_ONCE(guilty,true) hand-off via xchg */
+                       guilty_uq = xchg(&queue->guilty, false) ? queue : NULL;
+                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL,
+                                                        guilty_uq, NULL, NULL);
+               } else
                        r = userq_funcs->reset(queue);
                if (r)
                        gpu_reset = true;
@@ -671,6 +676,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
        }
 
        queue->doorbell_index = index;
+       queue->doorbell_offset = (u32)args->in.doorbell_offset;
        r = uq_funcs->mqd_create(queue, &args->in);
        if (r) {
                drm_file_err(uq_mgr->file, "Failed to create Queue\n");
@@ -1111,6 +1117,34 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
        dma_fence_put(ev_fence);
 }
 
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset)
+{
+       struct xarray *xa = &adev->userq_doorbell_xa;
+       struct amdgpu_usermode_queue *queue;
+       unsigned long flags, idx;
+
+       /*
+        * CP priv-fault payload is (pasid, src_data[0] & 0x3ff) — the same
+        * per-process doorbell encoding KFD extracts via
+        * KFD_CTXID0_DOORBELL_ID_MASK. Find the offending queue by the
+        * (vm->pasid, doorbell_offset) pair, mark it guilty and fire

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static void amdgpu_userq_restore_worker(struct work_struct *work) · line 1141

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+        * hang_detect immediately (queue_delayed_work() would no-op if the
+        * work is already armed at submit time).
+        */
+       xa_lock_irqsave(xa, flags);
+       xa_for_each(xa, idx, queue) {
+               if (queue->vm && queue->vm->pasid == pasid &&
+                   queue->doorbell_offset == doorbell_offset) {
+                       WRITE_ONCE(queue->guilty, true);
+                       mod_delayed_work(adev->reset_domain->wq,
+                                        &queue->hang_detect_work, 0);

Why wouldn't we use queue_delayed_work()? If we get multiple interrupts we want to queue resets for all of them. Additionally why do we need the extra guilty tracking?

Alex

Quoted 49 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+                       break;
+               }
+       }
+       xa_unlock_irqrestore(xa, flags);
+}
+
 static int
 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)
 {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +12 -1
index 9df1b78407f5..a55d57e2c169 100644
@@ -53,6 +53,7 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       u32                     doorbell_offset;
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr;
@@ -86,6 +87,7 @@ struct amdgpu_usermode_queue {
         * Delayed work which runs when userq_fences time out.
         */
        struct delayed_work     hang_detect_work;
+       bool                    guilty;
        struct kref             refcount;
 
        union {
@@ -176,6 +178,16 @@ int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost);
 void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue);
 void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);
 
+/*
+ * CP packs the per-process doorbell_id of the queue in
+ * CTXID0[9:0] on priv-fault (same encoding KFD uses via
+ * KFD_CTXID0_DOORBELL_ID_MASK)
+ */
+#define AMDGPU_CTXID0_DOORBELL_ID_MASK 0x3ff
+
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset);
+
 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
                                   struct amdgpu_usermode_queue *queue,
                                   u64 addr, u64 expected_size, u64 *va_out);
--

2.49.0

#13 lore raw

AMD General

Quoted 85 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Wednesday, June 17, 2026 5:47 AM
To: Zhang, Jesse(Jie) <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 1/7] drm/amdgpu/userq: add reset helper and identify guilty user queue

Quoted 2 lines · Jesse Zhang

From: Alex Deucher <[email protected]>

Feel free to take over the authorship of this patch. You've done most of the work.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static void amdgpu_userq_hang_detect_work(struct

Quoted 21 lines

If we get an interrupt for a bad user queue (bad opcode, etc.), add a helper to handle the reset for user queues.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40
+++++++++++++++++++++--  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |
12 +++++++
 2 files changed, 49 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index de6848b4fd67..231ffb29fe5e 100644
@@ -140,9 +140,14 @@ static void amdgpu_userq_hang_detect_work(struct

work_struct *work)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static void amdgpu_userq_hang_detect_work(struct work_struct *work)

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -2
                                                 AMDGPU_RESET_TYPE_PER_QUEUE)) {
                int r;
 
-               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE)
-                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL, NULL,

NULL, NULL);

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -1
-               else
+               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
+                       struct amdgpu_usermode_queue *guilty_uq;
+
+                       /* IRQ-side WRITE_ONCE(guilty,true) hand-off via xchg */
+                       guilty_uq = xchg(&queue->guilty, false) ? queue : NULL;
+                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL,
+                                                        guilty_uq, NULL, NULL);
+               } else
                        r = userq_funcs->reset(queue);
                if (r)
                        gpu_reset = true; @@ -671,6 +676,7 @@

amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)

Quoted 8 lines
        }

        queue->doorbell_index = index;
+       queue->doorbell_offset = (u32)args->in.doorbell_offset;
        r = uq_funcs->mqd_create(queue, &args->in);
        if (r) {
                drm_file_err(uq_mgr->file, "Failed to create

Queue\n"); @@ -1111,6 +1117,34 @@ static void

amdgpu_userq_restore_worker(struct work_struct *work)

Quoted 20 lines
        dma_fence_put(ev_fence);

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +17 -0
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset) {
+       struct xarray *xa = &adev->userq_doorbell_xa;
+       struct amdgpu_usermode_queue *queue;
+       unsigned long flags, idx;
+
+       /*
+        * CP priv-fault payload is (pasid, src_data[0] & 0x3ff) — the same
+        * per-process doorbell encoding KFD extracts via
+        * KFD_CTXID0_DOORBELL_ID_MASK. Find the offending queue by the
+        * (vm->pasid, doorbell_offset) pair, mark it guilty and fire
+        * hang_detect immediately (queue_delayed_work() would no-op if the
+        * work is already armed at submit time).
+        */
+       xa_lock_irqsave(xa, flags);
+       xa_for_each(xa, idx, queue) {
+               if (queue->vm && queue->vm->pasid == pasid &&
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static void amdgpu_userq_restore_worker(struct work_struct *work) · line 1141

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                   queue->doorbell_offset == doorbell_offset) {
+                       WRITE_ONCE(queue->guilty, true);
+                       mod_delayed_work(adev->reset_domain->wq,
+                                        &queue->hang_detect_work, 0);

Why wouldn't we use queue_delayed_work()? If we get multiple interrupts we want to queue resets for all of them. Additionally why do we need the extra guilty tracking?

Thanks Alex, will fix it , and replace mod_delayed_work with amdgpu_userq_start_hang_detect_work which will call queue_delayed_work.

About the guilty tracking:
the reason we pass the queue to mes_userq_reset() directly (rather than relying on MES detect) is that for the bad-opcode case we've seen, the queue is clearly broken but amdgpu_mes_detect_and_reset_hung_queues() does not report it as hung.
So the HW priv-fault IRQ is the only reliable signal for which queue to reset.

Jesse

Quoted 54 lines

Alex

Quoted 51 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+                       break;
+               }
+       }
+       xa_unlock_irqrestore(xa, flags); }
+
 static int
 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)  { diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index 9df1b78407f5..a55d57e2c169 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
@@ -53,6 +53,7 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       u32                     doorbell_offset;
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -86,6 +87,7 @@ struct
amdgpu_usermode_queue {
         * Delayed work which runs when userq_fences time out.
         */
        struct delayed_work     hang_detect_work;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+       bool                    guilty;
        struct kref             refcount;
 
        union {
@@ -176,6 +178,16 @@ int amdgpu_userq_post_reset(struct amdgpu_device
 *adev, bool vram_lost);  void

amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue); void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +10 -0
+/*
+ * CP packs the per-process doorbell_id of the queue in
+ * CTXID0[9:0] on priv-fault (same encoding KFD uses via
+ * KFD_CTXID0_DOORBELL_ID_MASK)
+ */
+#define AMDGPU_CTXID0_DOORBELL_ID_MASK 0x3ff
+
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset);
+
 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
                                   struct amdgpu_usermode_queue *queue,
                                   u64 addr, u64 expected_size, u64

*va_out);
--
2.49.0

#14 lore raw

AMD General

Quoted 114 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Zhang, Jesse(Jie)
Sent: Wednesday, June 17, 2026 1:52 PM
To: Alex Deucher <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: RE: [PATCH v2 1/7] drm/amdgpu/userq: add reset helper and identify guilty user queue

AMD General

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Wednesday, June 17, 2026 5:47 AM
To: Zhang, Jesse(Jie) <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 1/7] drm/amdgpu/userq: add reset helper and identify guilty user queue

On Thu, Jun 11, 2026 at 12:34 AM Jesse Zhang <[email protected]>

Quoted 17 lines · wrote:
Quoted 2 lines

From: Alex Deucher <[email protected]>

Feel free to take over the authorship of this patch. You've done most of the work.

Quoted 12 lines

If we get an interrupt for a bad user queue (bad opcode, etc.), add a helper to handle the reset for user queues.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40

+++++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h

|

Quoted 60 lines
Quoted 10 lines

12 +++++++

 2 files changed, 49 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index de6848b4fd67..231ffb29fe5e 100644
@@ -140,9 +140,14 @@ static void

amdgpu_userq_hang_detect_work(struct

work_struct *work)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c

Quoted 5 lines
                                                 AMDGPU_RESET_TYPE_PER_QUEUE)) {
                int r;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -2
-               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE)
-                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL, NULL,

NULL, NULL);

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -1
-               else
+               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
+                       struct amdgpu_usermode_queue *guilty_uq;
+
+                       /* IRQ-side WRITE_ONCE(guilty,true) hand-off via xchg */
+                       guilty_uq = xchg(&queue->guilty, false) ? queue : NULL;
+                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL,
+                                                        guilty_uq, NULL, NULL);
+               } else
                        r = userq_funcs->reset(queue);
                if (r)
                        gpu_reset = true; @@ -671,6 +676,7 @@

amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)

Quoted 8 lines
        }

        queue->doorbell_index = index;
+       queue->doorbell_offset = (u32)args->in.doorbell_offset;
        r = uq_funcs->mqd_create(queue, &args->in);
        if (r) {
                drm_file_err(uq_mgr->file, "Failed to create

Queue\n"); @@ -1111,6 +1117,34 @@ static void

amdgpu_userq_restore_worker(struct work_struct *work)

Quoted 21 lines
        dma_fence_put(ev_fence);

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +18 -0
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset) {
+       struct xarray *xa = &adev->userq_doorbell_xa;
+       struct amdgpu_usermode_queue *queue;
+       unsigned long flags, idx;
+
+       /*
+        * CP priv-fault payload is (pasid, src_data[0] & 0x3ff) — the same
+        * per-process doorbell encoding KFD extracts via
+        * KFD_CTXID0_DOORBELL_ID_MASK. Find the offending queue by the
+        * (vm->pasid, doorbell_offset) pair, mark it guilty and fire
+        * hang_detect immediately (queue_delayed_work() would no-op if the
+        * work is already armed at submit time).
+        */
+       xa_lock_irqsave(xa, flags);
+       xa_for_each(xa, idx, queue) {
+               if (queue->vm && queue->vm->pasid == pasid &&
+                   queue->doorbell_offset == doorbell_offset) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static void amdgpu_userq_restore_worker(struct work_struct *work)

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       WRITE_ONCE(queue->guilty, true);
+                       mod_delayed_work(adev->reset_domain->wq,
+                                        &queue->hang_detect_work,
+ 0);

Why wouldn't we use queue_delayed_work()? If we get multiple interrupts we want to queue resets for all of them. Additionally why do we need the extra guilty tracking?

Thanks Alex, will fix it , and replace mod_delayed_work with
amdgpu_userq_start_hang_detect_work which will call queue_delayed_work.

Quoted 7 lines

About the guilty tracking:
the reason we pass the queue to mes_userq_reset() directly (rather than relying on MES detect) is that for the bad-opcode case we've seen, the queue is clearly broken but amdgpu_mes_detect_and_reset_hung_queues() does not report it as hung.
So the HW priv-fault IRQ is the only reliable signal for which queue to reset.

Think more about it. Maybe we can drop the per-queue guilty flag and always pass the queue into the reset path, regardless of whether the reset is triggered by a userq fence timeout (the hang-detect work start when the fence is initialized) or by a hung queue reported through an error interrupt.

Quoted 55 lines

Jesse

Quoted 54 lines

Alex

Quoted 51 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+                       break;
+               }
+       }
+       xa_unlock_irqrestore(xa, flags); }
+
 static int
 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)  { diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h

b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index 9df1b78407f5..a55d57e2c169 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
@@ -53,6 +53,7 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       u32                     doorbell_offset;
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -86,6 +87,7 @@ struct
amdgpu_usermode_queue {
         * Delayed work which runs when userq_fences time out.
         */
        struct delayed_work     hang_detect_work;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+       bool                    guilty;
        struct kref             refcount;
 
        union {
@@ -176,6 +178,16 @@ int amdgpu_userq_post_reset(struct

amdgpu_device *adev, bool vram_lost); void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue); void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +10 -0
+/*
+ * CP packs the per-process doorbell_id of the queue in
+ * CTXID0[9:0] on priv-fault (same encoding KFD uses via
+ * KFD_CTXID0_DOORBELL_ID_MASK)
+ */
+#define AMDGPU_CTXID0_DOORBELL_ID_MASK 0x3ff
+
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset);
+
 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
                                   struct amdgpu_usermode_queue *queue,
                                   u64 addr, u64 expected_size, u64

*va_out);
--
2.49.0

#15 lore raw
Quoted 124 lines · Jesse(Jie)

AMD General

Quoted 114 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Zhang, Jesse(Jie)
Sent: Wednesday, June 17, 2026 1:52 PM
To: Alex Deucher <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: RE: [PATCH v2 1/7] drm/amdgpu/userq: add reset helper and identify guilty user queue

AMD General

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Wednesday, June 17, 2026 5:47 AM
To: Zhang, Jesse(Jie) <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 1/7] drm/amdgpu/userq: add reset helper and identify guilty user queue

On Thu, Jun 11, 2026 at 12:34 AM Jesse Zhang <[email protected]>

Quoted 17 lines · wrote:
Quoted 2 lines

From: Alex Deucher <[email protected]>

Feel free to take over the authorship of this patch. You've done most of the work.

Quoted 12 lines

If we get an interrupt for a bad user queue (bad opcode, etc.), add a helper to handle the reset for user queues.

v2: squash in fixes

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40

+++++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h

|

Quoted 60 lines
Quoted 10 lines

12 +++++++

 2 files changed, 49 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index de6848b4fd67..231ffb29fe5e 100644
@@ -140,9 +140,14 @@ static void

amdgpu_userq_hang_detect_work(struct

work_struct *work)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c

Quoted 5 lines
                                                 AMDGPU_RESET_TYPE_PER_QUEUE)) {
                int r;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -2
-               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE)
-                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL, NULL,

NULL, NULL);

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -1
-               else
+               if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
+                       struct amdgpu_usermode_queue *guilty_uq;
+
+                       /* IRQ-side WRITE_ONCE(guilty,true) hand-off via xchg */
+                       guilty_uq = xchg(&queue->guilty, false) ? queue : NULL;
+                       r = amdgpu_gfx_reset_mes_compute(adev, NULL, NULL,
+                                                        guilty_uq, NULL, NULL);
+               } else
                        r = userq_funcs->reset(queue);
                if (r)
                        gpu_reset = true; @@ -671,6 +676,7 @@

amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)

Quoted 8 lines
        }

        queue->doorbell_index = index;
+       queue->doorbell_offset = (u32)args->in.doorbell_offset;
        r = uq_funcs->mqd_create(queue, &args->in);
        if (r) {
                drm_file_err(uq_mgr->file, "Failed to create

Queue\n"); @@ -1111,6 +1117,34 @@ static void

amdgpu_userq_restore_worker(struct work_struct *work)

Quoted 21 lines
        dma_fence_put(ev_fence);

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +18 -0
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset) {
+       struct xarray *xa = &adev->userq_doorbell_xa;
+       struct amdgpu_usermode_queue *queue;
+       unsigned long flags, idx;
+
+       /*
+        * CP priv-fault payload is (pasid, src_data[0] & 0x3ff) — the same
+        * per-process doorbell encoding KFD extracts via
+        * KFD_CTXID0_DOORBELL_ID_MASK. Find the offending queue by the
+        * (vm->pasid, doorbell_offset) pair, mark it guilty and fire
+        * hang_detect immediately (queue_delayed_work() would no-op if the
+        * work is already armed at submit time).
+        */
+       xa_lock_irqsave(xa, flags);
+       xa_for_each(xa, idx, queue) {
+               if (queue->vm && queue->vm->pasid == pasid &&
+                   queue->doorbell_offset == doorbell_offset) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static void amdgpu_userq_restore_worker(struct work_struct *work)

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       WRITE_ONCE(queue->guilty, true);
+                       mod_delayed_work(adev->reset_domain->wq,
+                                        &queue->hang_detect_work,
+ 0);

Why wouldn't we use queue_delayed_work()? If we get multiple interrupts we want to queue resets for all of them. Additionally why do we need the extra guilty tracking?

Thanks Alex, will fix it , and replace mod_delayed_work with
amdgpu_userq_start_hang_detect_work which will call queue_delayed_work.

Quoted 7 lines

About the guilty tracking:
the reason we pass the queue to mes_userq_reset() directly (rather than relying on MES detect) is that for the bad-opcode case we've seen, the queue is clearly broken but amdgpu_mes_detect_and_reset_hung_queues() does not report it as hung.
So the HW priv-fault IRQ is the only reliable signal for which queue to reset.

Quoted 3 lines

Think more about it. Maybe we can drop the per-queue guilty flag and always pass the queue into the reset path, regardless of whether the reset is triggered by a userq fence timeout (the hang-detect work start when the fence is initialized) or by a hung queue reported through an error interrupt.

RIght. We know which queue is guilty from the interrupt vector. The work item is hung off the queue so we can just pass that queue in directly in amdgpu_userq_hang_detect_work().

Alex

Quoted 55 lines
Quoted 55 lines

Jesse

Quoted 54 lines

Alex

Quoted 51 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+                       break;
+               }
+       }
+       xa_unlock_irqrestore(xa, flags); }
+
 static int
 amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr)  { diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h

b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index 9df1b78407f5..a55d57e2c169 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
@@ -53,6 +53,7 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       u32                     doorbell_offset;
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -86,6 +87,7 @@ struct
amdgpu_usermode_queue {
         * Delayed work which runs when userq_fences time out.
         */
        struct delayed_work     hang_detect_work;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+       bool                    guilty;
        struct kref             refcount;
 
        union {
@@ -176,6 +178,16 @@ int amdgpu_userq_post_reset(struct

amdgpu_device *adev, bool vram_lost); void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue); void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +10 -0
+/*
+ * CP packs the per-process doorbell_id of the queue in
+ * CTXID0[9:0] on priv-fault (same encoding KFD uses via
+ * KFD_CTXID0_DOORBELL_ID_MASK)
+ */
+#define AMDGPU_CTXID0_DOORBELL_ID_MASK 0x3ff
+
+void amdgpu_userq_process_reset_irq(struct amdgpu_device *adev,
+                                   u32 pasid, u32 doorbell_offset);
+
 int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
                                   struct amdgpu_usermode_queue *queue,
                                   u64 addr, u64 expected_size, u64

*va_out);
--
2.49.0