Skip to contentSkip to conversation

drm/amdgpu: add UAPI for user queue query status

PATCH Superseded Prike Liang 2 participants 16 messages Sep 18, 2025

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/10 drm/amdgpu: add UAPI for user queue query status

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 85b3ca14f81e..7292f7bfcd13 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
#1 lore raw

PATCH v3 2/10 drm/amdgpu/userq: implement support for query status

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 | 35 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  1 +
 2 files changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +34 -1
index 9608fe3b5a9e..83f0ecdaa0b7 100644
@@ -472,6 +472,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;
@@ -553,6 +554,34 @@ 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;
+
+	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)
@@ -633,7 +662,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 c027dd916672..2260b1fb8a22 100644
@@ -66,6 +66,7 @@ struct amdgpu_usermode_queue {
 	u32			xcp_id;
 	int			priority;
 	struct dentry		*debugfs_queue;
+	uint64_t		generation;
 };
 
 struct amdgpu_userq_funcs {
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 3/10 drm/amdgpu/userq: extend queue flags for user queue query status

In reply to Prike Liang

Add the userq flag to identify the invalid userq cases.

---
 include/uapi/drm/amdgpu_drm.h | 2 ++
 1 file changed, 2 insertions(+)
include/uapi/drm/amdgpu_drm.h +2 -0
index 7292f7bfcd13..62520c4e4b19 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
#3 lore raw

PATCH v3 4/10 drm/amdgpu/userq: extend userq state

In reply to Prike Liang

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 2260b1fb8a22..33a582200dda 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
Reviewed-by on v3 4/10
#4 lore raw

Re: [PATCH v3 04/10] drm/amdgpu/userq: extend userq state

Quoted 5 lines · Prike Liang

Extend the userq state for identifying the
userq invalid cases.

Quoted 19 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -1
index 2260b1fb8a22..33a582200dda 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;
--

2.34.1

#5 lore raw

PATCH v3 5/10 drm/amdgpu: add userq invalid VA query

In reply to Prike Liang

Add the userq invalid VA query interface.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
index 83f0ecdaa0b7..3b57352e523a 100644
@@ -577,6 +577,8 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args)
 	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;
Signature · 1 line
2.34.1
Reviewed-by on v3 5/10
#6 lore raw

Re: [PATCH v3 05/10] drm/amdgpu: add userq invalid VA query

Quoted 4 lines · Prike Liang

Add the userq invalid VA query interface.

Quoted 20 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -1
index 83f0ecdaa0b7..3b57352e523a 100644
@@ -577,6 +577,8 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args)
        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;
--

2.34.1

#7 lore raw

Re: [PATCH v3 05/10] drm/amdgpu: add userq invalid VA query

In reply to Prike Liang
Quoted 4 lines · Prike Liang

Add the userq invalid VA query interface.

Move patches 1-3, and 5 to the end of the series so we can land the validation changes before the query status changes.

Alex

Quoted 20 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -1
index 83f0ecdaa0b7..3b57352e523a 100644
@@ -577,6 +577,8 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args)
        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;
--

2.34.1

#8 lore raw

PATCH v3 6/10 drm/amdgpu: add userq object va track helpers

In reply to Prike Liang

Add the userq object virtual address get(),mapped() and put() 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  | 73 ++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  | 15 ++++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 13 ++--
 4 files changed, 88 insertions(+), 14 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 +68 -5

110 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +13 -2
index 33a582200dda..e03bfa38c9ce 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;
@@ -68,6 +73,8 @@ struct amdgpu_usermode_queue {
 	int			priority;
 	struct dentry		*debugfs_queue;
 	uint64_t		generation;
+
+	struct list_head	userq_va_list;
 };
 
 struct amdgpu_userq_funcs {
@@ -138,7 +145,11 @@ 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,
+int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
+				   struct amdgpu_vm *vm, u64 addr,
 				   u64 expected_size);
+int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue *queue,
+			       struct amdgpu_bo_va_mapping *va_map, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
+int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm, u64 addr);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -7
index 3a4fd6de08ce..e10e4b6a10d6 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 (amdgpu_userq_input_va_validate(queue, queue->vm, 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 (amdgpu_userq_input_va_validate(queue, queue->vm, 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 (amdgpu_userq_input_va_validate(queue, queue->vm, 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
#9 lore raw

Re: [PATCH v3 06/10] drm/amdgpu: add userq object va track helpers

Quoted 47 lines · Prike Liang

Add the userq object virtual address get(),mapped() and put() 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  | 73 ++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  | 15 ++++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 13 ++--
 4 files changed, 88 insertions(+), 14 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 +3 -1
index 3b57352e523a..71cac54791c9 100644
@@ -44,7 +44,8 @@ u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
        return userq_ip_mask;
 }
 
-int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
+                                  struct amdgpu_vm *vm, u64 addr,
                                   u64 expected_size)
 {
        struct amdgpu_bo_va_mapping *va_map;
@@ -67,6 +68,7 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
        /* Only validate the userq whether resident in the VM mapping range */
        if (user_addr >= va_map->start  &&
            va_map->last - user_addr + 1 >= size) {
+               amdgpu_userq_buffer_va_list_add(queue, va_map, user_addr);
                amdgpu_bo_unreserve(vm->root.bo);
                return 0;
        }

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr, · line 81

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
@@ -76,6 +78,66 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
        return r;
 }
 
+int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue *queue,

This should be static as the function is not used outside of this file.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -0
+                              struct amdgpu_bo_va_mapping *va_map, u64 addr)
+{
+       struct amdgpu_userq_va_cursor *va_cursor;
+       struct userq_va_list;
+
+       va_cursor = kzalloc(sizeof(*va_cursor), GFP_KERNEL);
+       if (!va_cursor)
+               return -ENOMEM;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr, · line 100

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+       INIT_LIST_HEAD(&va_cursor->list);
+       va_cursor->gpu_addr = addr;
+       atomic_set(&va_map->bo_va->userq_va_mapped, 1);
+       list_add(&va_cursor->list, &queue->userq_va_list);
+
+       return 0;
+
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)

This should be static as it's not used outside of this file.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       bool r;
+
+       if (amdgpu_bo_reserve(vm->root.bo, false))
+               return false;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr, · line 118

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+       if (!IS_ERR_OR_NULL(mapping) && atomic_read(&mapping->bo_va->userq_va_mapped))
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}
+
+int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm,

This should be static as it's not used outside of this file.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+                                   u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       int r;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr, · line 133

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
+       if (!mapping)
+               goto out_err;
+
+       atomic_set(&mapping->bo_va->userq_va_mapped, 0);
+       amdgpu_bo_unreserve(vm->root.bo);

Why not remove from the list here as well? The only place that calls this just calls list_del() after calling this function. Also move the locking (amndgpu_bo_reserve()) into the caller for symmetry with amdgpu_userq_buffer_va_list_add().

Alex

Alex

Quoted 121 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12 -4
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
@@ -141,6 +203,7 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
        uq_funcs->mqd_destroy(uq_mgr, queue);
        amdgpu_userq_fence_driver_free(queue);
        idr_remove(&uq_mgr->userq_idr, queue_id);
+       list_del(&queue->userq_va_list);
        kfree(queue);
 }
 
@@ -461,10 +524,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
                goto unlock;
        }
 
+       INIT_LIST_HEAD(&queue->userq_va_list);
        /* Validate the userq virtual address.*/
-       if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, args->in.queue_size) ||
-           amdgpu_userq_input_va_validate(&fpriv->vm, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE) ||
-           amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) {
+       if (amdgpu_userq_input_va_validate(queue, &fpriv->vm, args->in.queue_va, args->in.queue_size) ||
+           amdgpu_userq_input_va_validate(queue, &fpriv->vm, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE ) ||
+           amdgpu_userq_input_va_validate(queue, &fpriv->vm, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) {
                kfree(queue);
                goto unlock;
        }
@@ -504,7 +568,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
                goto unlock;
        }
 
-
        qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);
        if (qid < 0) {
                drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +13 -2
index 33a582200dda..e03bfa38c9ce 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;
@@ -68,6 +73,8 @@ struct amdgpu_usermode_queue {
        int                     priority;
        struct dentry           *debugfs_queue;
        uint64_t                generation;
+
+       struct list_head        userq_va_list;
 };
 
 struct amdgpu_userq_funcs {
@@ -138,7 +145,11 @@ 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,
+int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
+                                  struct amdgpu_vm *vm, u64 addr,
                                   u64 expected_size);
+int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue *queue,
+                              struct amdgpu_bo_va_mapping *va_map, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
+int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm, u64 addr);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -8
index 3a4fd6de08ce..e10e4b6a10d6 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 (amdgpu_userq_input_va_validate(queue, queue->vm, 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 (amdgpu_userq_input_va_validate(queue, queue->vm, 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 (amdgpu_userq_input_va_validate(queue, queue->vm, mqd_sdma_v11->csa_va,
+                                       shadow_info.csa_size))
                        goto free_mqd;
 
                userq_props->csa_addr = mqd_sdma_v11->csa_va;
--

2.34.1

#10 lore raw

PATCH v3 7/10 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 | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +18 -0
index 71cac54791c9..3c8f05a9e82d 100644
@@ -138,6 +138,22 @@ int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm,
 	return -EINVAL;
 }
 
+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;
+	int r;
+
+	list_for_each_entry_safe(va_cursor, tmp, &queue->userq_va_list, list) {
+		r += amdgpu_userq_buffer_va_list_del(queue->vm, va_cursor->gpu_addr);
+		dev_dbg(adev->dev, "delete the userq:%p va:%llx r:%d\n",
+			queue, va_cursor->gpu_addr, r);
+		list_del(&va_cursor->list);
+		kfree(va_cursor);
+	}
+	return r;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -200,6 +216,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
#11 lore raw

Re: [PATCH v3 07/10] drm/amdgpu: track the userq bo va for its obj management

Quoted 20 lines · 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 | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
index 71cac54791c9..3c8f05a9e82d 100644
@@ -138,6 +138,22 @@ int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm,
        return -EINVAL;
 }
 
+static int amdgpu_userq_buffer_vas_list_cleanup(struct amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm, · line 151

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+                               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_list_del(queue->vm, va_cursor->gpu_addr);
+               dev_dbg(adev->dev, "delete the userq:%p va:%llx r:%d\n",
+                       queue, va_cursor->gpu_addr, r);
+               list_del(&va_cursor->list);

Move list_del() into the amdgpu_userq_buffer_va_list_del() and move the bo_reserve/unreserve() into this function.

Alex

Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -1
+               kfree(va_cursor);
+       }
+       return r;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
@@ -200,6 +216,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);
--

2.34.1

#12 lore raw

PATCH v3 8/10 drm/amdgpu: keeping waiting userq fence infinitely

In reply to Prike Liang

Keeping waiting the userq fence infinitely untill 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 3c8f05a9e82d..5c918faca776 100644
@@ -193,7 +193,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)
 {
@@ -201,11 +201,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
Acked-by on v3 8/10
#13 lore raw

Re: [PATCH v3 08/10] drm/amdgpu: keeping waiting userq fence infinitely

Quoted 6 lines · Prike Liang

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

Quoted 39 lines
---
 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 -4
index 3c8f05a9e82d..5c918faca776 100644
@@ -193,7 +193,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)
 {
@@ -201,11 +201,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
--

2.34.1

#14 lore raw

PATCH v3 9/10 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 | 24 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  1 +
 2 files changed, 25 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +24 -0
index 5c918faca776..6d21d02d7647 100644
@@ -115,6 +115,23 @@ bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
 	return r;
 }
 
+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;
+}
+
 int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm,
 				    u64 addr)
 {
@@ -772,6 +789,13 @@ 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 mappping\n");
+			queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
+			continue;
+		}
+
 		r = amdgpu_userq_map_helper(uq_mgr, queue);
 		if (r)
 			ret = r;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
index e03bfa38c9ce..2cb17d47d30a 100644
@@ -152,4 +152,5 @@ int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue *queue,
 			       struct amdgpu_bo_va_mapping *va_map, u64 addr);
 bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
 int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue);
 #endif
Signature · 1 line
2.34.1
#15 lore raw

PATCH v3 10/10 drm/amdgpu: validate userq va for GEM unmap

In reply to Prike Liang

When an 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 | 29 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  3 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 11 +++++++++
 3 files changed, 43 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +29 -0
index 6d21d02d7647..7fdedbb40f69 100644
@@ -1182,3 +1182,32 @@ 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 2cb17d47d30a..973f8901904a 100644
@@ -153,4 +153,7 @@ int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue *queue,
 bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
 int amdgpu_userq_buffer_va_list_del(struct amdgpu_vm *vm, u64 addr);
 bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue);
+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 +11 -0
index bd12d8ff15a4..b3632f7b72d2 100644
@@ -1941,6 +1941,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;
 
@@ -1961,6 +1962,16 @@ 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, "Here should be an improper unmap request from user space\n");
+	}
+
 	list_del(&mapping->list);
 	amdgpu_vm_it_remove(mapping, &vm->va);
 	mapping->bo_va = NULL;
Signature · 1 line
2.34.1