Skip to contentSkip to conversation

drm/amdgpu/userq: extend userq state

PATCH In review Prike Liang 1 participant 10 messages Sep 29, 2025 – Sep 30, 2025

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

PATCH v5 1/8 drm/amdgpu/userq: extend userq state

Extend the userq state for identifying the
userq invalid cases.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
index c027dd916672..ded33fe76e1c 100644
@@ -37,6 +37,7 @@ enum amdgpu_userq_state {
 	AMDGPU_USERQ_STATE_MAPPED,
 	AMDGPU_USERQ_STATE_PREEMPTED,
 	AMDGPU_USERQ_STATE_HUNG,
+	AMDGPU_USERQ_STATE_INVALID_VA,
 };
 
 struct amdgpu_mqd_prop;
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 2/8 drm/amdgpu: add userq object va track helpers

Add the userq object virtual address list_add(),mapped() and list_del() helpers for tracking the userq obj va address usage.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 71 ++++++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  | 12 +++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 13 ++--
 4 files changed, 76 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
index 656b8a931dae..52c2d1731aab 100644
@@ -96,6 +96,7 @@ struct amdgpu_bo_va {
 	 * if non-zero, cannot unmap from GPU because user queues may still access it
 	 */
 	unsigned int			queue_refcount;
+	atomic_t			userq_va_mapped;
 };
 
 struct amdgpu_bo {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +60 -11

115 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +9 -3
index ded33fe76e1c..f19416feb7ef 100644
@@ -48,6 +48,11 @@ struct amdgpu_userq_obj {
 	struct amdgpu_bo *obj;
 };
 
+struct amdgpu_userq_va_cursor {
+	uint64_t		gpu_addr;
+	struct list_head	list;
+};
+
 struct amdgpu_usermode_queue {
 	int			queue_type;
 	enum amdgpu_userq_state state;
@@ -67,6 +72,8 @@ struct amdgpu_usermode_queue {
 	u32			xcp_id;
 	int			priority;
 	struct dentry		*debugfs_queue;
+
+	struct list_head	userq_va_list;
 };
 
 struct amdgpu_userq_funcs {
@@ -137,7 +144,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 						  u32 idx);
 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 						   u32 idx);
-
-int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
-				   u64 expected_size);
+int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
+				   u64 addr, u64 expected_size);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -7
index 2db9b2c63693..9c73a7238fd6 100644
@@ -298,8 +298,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			goto free_mqd;
 		}
 
-		if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd->eop_va,
-		    max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE)))
+		if (r = amdgpu_userq_input_va_validate(queue, compute_mqd->eop_va,
+					max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE)))
 			goto free_mqd;
 
 		userq_props->eop_gpu_addr = compute_mqd->eop_va;
@@ -330,8 +330,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
 
-		if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
-		    shadow_info.shadow_size))
+		if (r = amdgpu_userq_input_va_validate(queue, mqd_gfx_v11->shadow_va,
+					shadow_info.shadow_size))
 			goto free_mqd;
 
 		kfree(mqd_gfx_v11);
@@ -350,9 +350,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			r = -ENOMEM;
 			goto free_mqd;
 		}
-
-		if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11->csa_va,
-		    shadow_info.csa_size))
+		if (r = amdgpu_userq_input_va_validate(queue, mqd_sdma_v11->csa_va,
+					shadow_info.csa_size))
 			goto free_mqd;
 
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v5 3/8 drm/amdgpu: track the userq bo va for its obj management

In reply to Prike Liang

Track the userq obj for its life time, and reference and dereference the buffer flag at its creating and destroying period.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 28 +++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +28 -0
index a9ac2efcd37b..a82b8d30228d 100644
@@ -124,6 +124,32 @@ static void amdgpu_userq_buffer_va_list_del(struct amdgpu_bo_va_mapping *mapping
 	kfree(va_cursor);
 }
 
+static int amdgpu_userq_buffer_vas_list_cleanup(struct amdgpu_device *adev,
+						struct amdgpu_usermode_queue *queue)
+{
+	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
+	struct amdgpu_bo_va_mapping *mapping;
+	int r;
+
+	r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+	if (r)
+		return r;
+
+	list_for_each_entry_safe(va_cursor, tmp, &queue->userq_va_list, list) {
+		mapping = amdgpu_vm_bo_lookup_mapping(queue->vm, va_cursor->gpu_addr);
+		if (!mapping) {
+			r = -EINVAL;
+			goto err;
+		}
+		amdgpu_userq_buffer_va_list_del(mapping, va_cursor);
+		dev_dbg(adev->dev, "delete the userq:%p va:%llx\n",
+			queue, va_cursor->gpu_addr);
+	}
+err:
+	amdgpu_bo_unreserve(queue->vm->root.bo);
+	return r;
+}
+
 static int
 amdgpu_userq_preempt_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -229,6 +255,8 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
 	struct amdgpu_device *adev = uq_mgr->adev;
 	const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
 
+	/* Drop the userq reference. */
+	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 	uq_funcs->mqd_destroy(uq_mgr, queue);
 	amdgpu_userq_fence_driver_free(queue);
 	idr_remove(&uq_mgr->userq_idr, queue_id);
Signature · 1 line
2.34.1
#3 lore raw

PATCH v5 4/8 drm/amdgpu: keeping waiting userq fence infinitely

In reply to Prike Liang

Keeping waiting the userq fence infinitely until hang detection, and then suspend the hang queue and set the fence error.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -3
index a82b8d30228d..93d92b6ea86c 100644
@@ -232,7 +232,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr,
 	return r;
 }
 
-static void
+static int
 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
 				 struct amdgpu_usermode_queue *queue)
 {
@@ -240,11 +240,16 @@ amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
 	int ret;
 
 	if (f && !dma_fence_is_signaled(f)) {
-		ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
-		if (ret <= 0)
+		ret = dma_fence_wait_timeout(f, true, MAX_SCHEDULE_TIMEOUT);
+		if (ret <= 0) {
 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
 				     f->context, f->seqno);
+			queue->state = AMDGPU_USERQ_STATE_HUNG;
+			return -ETIME;
+		}
 	}
+
+	return ret;
 }
 
 static void
Signature · 1 line
2.34.1
#4 lore raw

RE: [PATCH v5 4/8] drm/amdgpu: keeping waiting userq fence infinitely

[Public]

Hi Alex,

Apologies for overlooking your earlier review comments. I just see patches 1-4 have already been reviewed. Can we proceed to land the series (patches 1-6) in drm-next?

Regards,
Prike

Quoted 53 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Monday, September 29, 2025 4:10 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH v5 4/8] drm/amdgpu: keeping waiting userq fence infinitely

Keeping waiting the userq fence infinitely until hang detection, and then suspend the hang queue and set the fence error.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
index a82b8d30228d..93d92b6ea86c 100644
@@ -232,7 +232,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr
 *uq_mgr,
      return r;
 }
 
-static void
+static int
 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
                               struct amdgpu_usermode_queue *queue)  { @@ -

240,11 +240,16 @@ amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,

      int ret;

      if (f && !dma_fence_is_signaled(f)) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -2
-             ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
-             if (ret <= 0)
+             ret = dma_fence_wait_timeout(f, true, MAX_SCHEDULE_TIMEOUT);
+             if (ret <= 0) {
                      drm_file_err(uq_mgr->file, "Timed out waiting for

fence=%llu:%llu\n",

                                   f->context, f->seqno);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+                     queue->state = AMDGPU_USERQ_STATE_HUNG;
+                     return -ETIME;
+             }
      }
+
+     return ret;
 }
 
 static void
--

2.34.1

#5 lore raw

RE: [PATCH v5 4/8] drm/amdgpu: keeping waiting userq fence infinitely

[Public]

The patches 1-4 introduce userq VA tracking, and patches 5-6 add userq VA validation. Together, patches 1-6 provide full userq VA validation support. If there are no objections, I'll push the full series today to complete the implementation.

Regards,
Prike

Quoted 74 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Liang, Prike
Sent: Tuesday, September 30, 2025 4:20 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: RE: [PATCH v5 4/8] drm/amdgpu: keeping waiting userq fence infinitely

[Public]

Hi Alex,

Apologies for overlooking your earlier review comments. I just see patches 1-4 have already been reviewed. Can we proceed to land the series (patches 1-6) in drm- next?

Regards,
Prike

Quoted 55 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Monday, September 29, 2025 4:10 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH v5 4/8] drm/amdgpu: keeping waiting userq fence infinitely

Keeping waiting the userq fence infinitely until hang detection, and then suspend the hang queue and set the fence error.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
index a82b8d30228d..93d92b6ea86c 100644
@@ -232,7 +232,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr
 *uq_mgr,
      return r;
 }
 
-static void
+static int
 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
                               struct amdgpu_usermode_queue *queue)  {

@@ -
240,11 +240,16 @@ amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,

      int ret;

      if (f && !dma_fence_is_signaled(f)) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -2
-             ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
-             if (ret <= 0)
+             ret = dma_fence_wait_timeout(f, true, MAX_SCHEDULE_TIMEOUT);
+             if (ret <= 0) {
                      drm_file_err(uq_mgr->file, "Timed out waiting

for fence=%llu:%llu\n",

                                   f->context, f->seqno);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+                     queue->state = AMDGPU_USERQ_STATE_HUNG;
+                     return -ETIME;
+             }
      }
+
+     return ret;
 }
 
 static void
--

2.34.1

#6 lore raw

PATCH v5 5/8 drm/amdgpu: validate the queue va for resuming the queue

In reply to Prike Liang

It requires validating the userq VA whether is mapped before trying to resume the queue.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 27 +++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +27 -0
index 93d92b6ea86c..15ab2f4c1952 100644
@@ -116,8 +116,27 @@ static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
 	return r;
 }
 
+static bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue)
+{
+	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
+	int r;
+
+	list_for_each_entry_safe(va_cursor, tmp, &queue->userq_va_list, list) {
+		r += amdgpu_userq_buffer_va_mapped(queue->vm, va_cursor->gpu_addr);
+		dev_dbg(queue->userq_mgr->adev->dev,
+			"validate the userq mapping:%p va:%llx r:%d\n",
+			queue, va_cursor->gpu_addr, r);
+	}
+
+	if (r != 0)
+		return true;
+
+	return false;
+}
+
 static void amdgpu_userq_buffer_va_list_del(struct amdgpu_bo_va_mapping *mapping,
 					   struct amdgpu_userq_va_cursor *va_cursor)
+
 {
 	atomic_set(&mapping->bo_va->userq_va_mapped, 0);
 	list_del(&va_cursor->list);
@@ -778,6 +797,14 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 
 	/* Resume all the queues for this process */
 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
+
+		if (!amdgpu_userq_buffer_vas_mapped(queue)) {
+			drm_file_err(uq_mgr->file,
+				     "trying restore queue without va mapping\n");
+			queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
+			continue;
+		}
+
 		r = amdgpu_userq_restore_helper(uq_mgr, queue);
 		if (r)
 			ret = r;
Signature · 1 line
2.34.1
#7 lore raw

PATCH v5 6/8 drm/amdgpu: validate userq va for GEM unmap

In reply to Prike Liang

When a user unmaps a userq VA, the driver must ensure the queue has no in-flight jobs. If there is pending work, the kernel should wait for the attached eviction (bookkeeping) fence to signal before deleting the mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 31 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  3 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 12 +++++++++
 3 files changed, 46 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +31 -0
index 15ab2f4c1952..d7cda252cae9 100644
@@ -1195,3 +1195,34 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
 }
+
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
+				       struct amdgpu_bo_va_mapping *mapping,
+				       uint64_t saddr)
+{
+	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
+	struct amdgpu_bo_va *bo_va = mapping->bo_va;
+	struct dma_resv *resv = bo_va->base.bo->tbo.base.resv;
+	int ret;
+
+	if (!ip_mask)
+		return 0;
+
+	dev_warn_once(adev->dev, "now unmapping a vital queue va:%llx\n", saddr);
+	/**
+	 * The userq VA mapping reservation should include the eviction fence,
+	 * if the eviction fence can't signal successfully during unmapping,
+	 * then driver will warn to flag this improper unmap of the userq VA.
+	 * Note: The eviction fence may be attached to different BOs, and this
+	 * unmap is only for one kind of userq VAs, so at this point suppose
+	 * the eviction fence is always unsignaled.
+	 */
+	if (!dma_resv_test_signaled(resv, DMA_RESV_USAGE_BOOKKEEP)) {
+		ret = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP, true,
+					    MAX_SCHEDULE_TIMEOUT);
+		if (ret <= 0)
+			return -EBUSY;
+	}
+
+	return 0;
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -0
index f19416feb7ef..7bbbb5988fc7 100644
@@ -146,4 +146,7 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 						   u32 idx);
 int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
 				   u64 addr, u64 expected_size);
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
+				       struct amdgpu_bo_va_mapping *mapping,
+				       uint64_t saddr);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -0
index 563cad9c6cbc..a5de6e3980db 100644
@@ -1913,6 +1913,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 	struct amdgpu_bo_va_mapping *mapping;
 	struct amdgpu_vm *vm = bo_va->base.vm;
 	bool valid = true;
+	int r;
 
 	saddr /= AMDGPU_GPU_PAGE_SIZE;
 
@@ -1933,6 +1934,17 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 			return -ENOENT;
 	}
 
+	/* It's unlikely to happen that the mapping userq hasn't been idled
+	 * during user requests GEM unmap IOCTL except for forcing the unmap
+	 * from user space.
+	 */
+	if (unlikely(atomic_read(&bo_va->userq_va_mapped) > 0)) {
+		r = amdgpu_userq_gem_va_unmap_validate(adev, mapping, saddr);
+		if (unlikely(r == -EBUSY))
+			dev_warn_once(adev->dev,
+				      "Attempt to unmap an active userq buffer\n");
+	}
+
 	list_del(&mapping->list);
 	amdgpu_vm_it_remove(mapping, &vm->va);
 	mapping->bo_va = NULL;
Signature · 1 line
2.34.1
#8 lore raw

PATCH v5 7/8 drm/amdgpu: add UAPI for user queue query status

In reply to Prike Liang

From: Alex Deucher <[email protected]>

Add an API to query queue status such as whether the queue is hung or whether vram is lost.

---
 include/uapi/drm/amdgpu_drm.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/uapi/drm/amdgpu_drm.h +14 -0
index cd7402e36b6d..871e0f56d61e 100644
@@ -334,6 +334,7 @@ union drm_amdgpu_ctx {
 /* user queue IOCTL operations */
 #define AMDGPU_USERQ_OP_CREATE	1
 #define AMDGPU_USERQ_OP_FREE	2
+#define AMDGPU_USERQ_OP_QUERY_STATUS	3
 
 /* queue priority levels */
 /* low < normal low < normal high < high */
@@ -346,6 +347,12 @@ union drm_amdgpu_ctx {
 /* for queues that need access to protected content */
 #define AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE  (1 << 2)
 
+
+/* the queue is hung */
+#define AMDGPU_USERQ_QUERY_STATUS_FLAGS_HUNG    (1 << 0)
+/* indicate vram lost since queue was created */
+#define AMDGPU_USERQ_QUERY_STATUS_FLAGS_VRAMLOST (1 << 1)
+
 /*
  * This structure is a container to pass input configuration
  * info for all supported userqueue related operations.
@@ -427,9 +434,16 @@ struct drm_amdgpu_userq_out {
 	__u32 _pad;
 };
 
+/* The structure to carry output of userqueue ops */
+struct drm_amdgpu_userq_out_query_state {
+	__u32 flags;
+	__u32 _pad;
+};
+
 union drm_amdgpu_userq {
 	struct drm_amdgpu_userq_in in;
 	struct drm_amdgpu_userq_out out;
+	struct drm_amdgpu_userq_out_query_state out_qs;
 };
 
 /* GFX V11 IP specific MQD parameters */
Signature · 1 line
2.34.1
#9 lore raw

PATCH v5 8/8 drm/amdgpu/userq: implement support for query status

In reply to Prike Liang

From: Alex Deucher <[email protected]>

Query the status of the user queue, currently whether the queue is hung and whether or not VRAM is lost.

v2: Misc cleanups

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 37 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  1 +
 include/uapi/drm/amdgpu_drm.h             |  2 ++
 3 files changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +36 -1
index d7cda252cae9..f477e2270724 100644
@@ -610,6 +610,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	queue->queue_type = args->in.ip_type;
 	queue->vm = &fpriv->vm;
 	queue->priority = priority;
+	queue->generation = amdgpu_vm_generation(adev, &fpriv->vm);
 
 	db_info.queue_type = queue->queue_type;
 	db_info.doorbell_handle = queue->doorbell_handle;
@@ -699,6 +700,36 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	return r;
 }
 
+static int
+amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args)
+{
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_usermode_queue *queue;
+	int queue_id = args->in.queue_id;
+
+	mutex_lock(&uq_mgr->userq_mutex);
+
+	queue = amdgpu_userq_find(uq_mgr, queue_id);
+	if (!queue) {
+		dev_dbg(adev->dev, "Invalid queue id to query\n");
+		mutex_unlock(&uq_mgr->userq_mutex);
+		return -EINVAL;
+	}
+	args->out_qs.flags = 0;
+	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
+		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_HUNG;
+	if (queue->generation != amdgpu_vm_generation(adev, &fpriv->vm))
+		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_VRAMLOST;
+
+	if (queue->state == AMDGPU_USERQ_STATE_INVALID_VA)
+		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_INVALID_VA;
+	mutex_unlock(&uq_mgr->userq_mutex);
+
+	return 0;
+}
+
 static int amdgpu_userq_input_args_validate(struct drm_device *dev,
 					union drm_amdgpu_userq *args,
 					struct drm_file *filp)
@@ -779,7 +810,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
 		if (r)
 			drm_file_err(filp, "Failed to destroy usermode queue\n");
 		break;
-
+	case AMDGPU_USERQ_OP_QUERY_STATUS:
+		r = amdgpu_userq_query_status(filp, args);
+		if (r)
+			drm_file_err(filp, "Failed to query usermode queue status\n");
+		break;
 	default:
 		drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
 		return -EINVAL;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
index 7bbbb5988fc7..e0ca466681f3 100644
@@ -74,6 +74,7 @@ struct amdgpu_usermode_queue {
 	struct dentry		*debugfs_queue;
 
 	struct list_head	userq_va_list;
+	uint64_t		generation;
 };
 
 struct amdgpu_userq_funcs {
include/uapi/drm/amdgpu_drm.h +2 -0
index 871e0f56d61e..100ccee37d5c 100644
@@ -352,6 +352,8 @@ union drm_amdgpu_ctx {
 #define AMDGPU_USERQ_QUERY_STATUS_FLAGS_HUNG    (1 << 0)
 /* indicate vram lost since queue was created */
 #define AMDGPU_USERQ_QUERY_STATUS_FLAGS_VRAMLOST (1 << 1)
+/* the invalid userq VA unmapped */
+#define AMDGPU_USERQ_QUERY_STATUS_FLAGS_INVALID_VA (1 << 2)
 
 /*
  * This structure is a container to pass input configuration
Signature · 1 line
2.34.1