Skip to contentSkip to conversation

drm/amdgpu/userq: extend userq state

PATCH Superseded Prike Liang 3 participants 16 messages Sep 19, 2025 – Sep 22, 2025

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/10 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 v4 2/10 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, 77 insertions(+), 20 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 +61 -10

118 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..673dfbbd95e4 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, 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, 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, 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
maintainer
#2 lore raw

Re: [PATCH v4 02/10] drm/amdgpu: add userq object va track helpers

Quoted 17 lines · Prike Liang

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, 77 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index 656b8a931dae..52c2d1731aab 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo_va { · line 99

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
@@ -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;

For now that works, but I think we should just make that a flag which is set whenever an userqeue needs to buffer and never cleared.

Regards,
Christian.

Quoted 204 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
 };
 
 struct amdgpu_bo {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +61 -10
index af7753bfa27d..99e51a8ff62a 100644
@@ -44,10 +44,30 @@ 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,
-				   u64 expected_size)
+static int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue *queue,
+					   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;
+
+	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;
+
+}
+
+int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
+				   u64 addr, u64 expected_size)
 {
 	struct amdgpu_bo_va_mapping *va_map;
+	struct amdgpu_vm *vm = queue->vm;
 	u64 user_addr;
 	u64 size;
 	int r = 0;
@@ -67,15 +87,43 @@ 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;
 	}
 
+	r = -EINVAL;
 out_err:
 	amdgpu_bo_unreserve(vm->root.bo);
 	return r;
 }
 
+static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
+{
+	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);
+	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;
+}
+
+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);
+	kfree(va_cursor);
+}
+
 static int
 amdgpu_userq_preempt_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -184,6 +232,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);
 }
 
@@ -504,13 +553,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto unlock;
 	}
 
-	/* 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)) {
-		kfree(queue);
-		goto unlock;
-	}
+	INIT_LIST_HEAD(&queue->userq_va_list);
 	queue->doorbell_handle = args->in.doorbell_handle;
 	queue->queue_type = args->in.ip_type;
 	queue->vm = &fpriv->vm;
@@ -521,6 +564,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	db_info.db_obj = &queue->db_obj;
 	db_info.doorbell_offset = args->in.doorbell_offset;
 
+	/* Validate the userq virtual address.*/
+	if (amdgpu_userq_input_va_validate(queue, args->in.queue_va, args->in.queue_size) ||
+	    amdgpu_userq_input_va_validate(queue, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE) ||
+	    amdgpu_userq_input_va_validate(queue, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) {
+		r = -EINVAL;
+		kfree(queue);
+		goto unlock;
+	}
+
 	/* Convert relative doorbell offset into absolute doorbell index */
 	index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
 	if (index == (uint64_t)-EINVAL) {
@@ -546,7 +598,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 +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..673dfbbd95e4 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, 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, 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, mqd_sdma_v11->csa_va,
+					shadow_info.csa_size))
 			goto free_mqd;
 
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
#3 lore raw

RE: [PATCH v4 02/10] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 29 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, September 19, 2025 4:34 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 02/10] drm/amdgpu: add userq object va track helpers

Quoted 17 lines · Prike Liang

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, 77 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index 656b8a931dae..52c2d1731aab 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo_va { · line 96

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
@@ -96,6 +96,7 @@ struct amdgpu_bo_va {
     * if non-zero, cannot unmap from GPU because user queues may still

access it

Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo_va { · line 99

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
     */
    unsigned int                    queue_refcount;
+   atomic_t                        userq_va_mapped;

For now that works, but I think we should just make that a flag which is set whenever an userqeue needs to buffer and never cleared.

I’m not sure I fully follow. Are you suggesting we shouldn’t explicitly clear the flag during userq destruction, and instead rely on it being cleared when the mapped BOs are freed? Also, do we still need to walk the userq VA list to check the flag before restoring the userq?

Thanks,
Prike

Quoted 239 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
 };
 
 struct amdgpu_bo {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index af7753bfa27d..99e51a8ff62a 100644
@@ -44,10 +44,30 @@ u32 amdgpu_userq_get_supported_ip_mask(struct

amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -2
    return userq_ip_mask;
 }
 
-int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
-                              u64 expected_size)
+static int amdgpu_userq_buffer_va_list_add(struct amdgpu_usermode_queue

*queue,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct · line 48

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+                                      struct amdgpu_bo_va_mapping *va_map,

u64 addr) {

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +19 -0
+   struct amdgpu_userq_va_cursor *va_cursor;
+   struct userq_va_list;
+
+   va_cursor = kzalloc(sizeof(*va_cursor), GFP_KERNEL);
+   if (!va_cursor)
+           return -ENOMEM;
+
+   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;
+
+}
+
+int amdgpu_userq_input_va_validate(struct amdgpu_usermode_queue *queue,
+                              u64 addr, u64 expected_size)
 {
    struct amdgpu_bo_va_mapping *va_map;
+   struct amdgpu_vm *vm = queue->vm;
    u64 user_addr;
    u64 size;
    int r = 0;
@@ -67,15 +87,43 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm

*vm, u64 addr,

Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12 -0
    /* 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;
    }
 
+   r = -EINVAL;
 out_err:
    amdgpu_bo_unreserve(vm->root.bo);
    return r;
 }
 
+static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64
+addr) {
+   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);
+   if (!IS_ERR_OR_NULL(mapping) && atomic_read(&mapping->bo_va-

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -0
 userq_va_mapped))
+           r = true;
+   else
+           r = false;
+   amdgpu_bo_unreserve(vm->root.bo);
+
+   return r;
+}
+
+static void amdgpu_userq_buffer_va_list_del(struct amdgpu_bo_va_mapping

*mapping,

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+                                      struct amdgpu_userq_va_cursor *va_cursor)

{

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+   atomic_set(&mapping->bo_va->userq_va_mapped, 0);
+   list_del(&va_cursor->list);
+   kfree(va_cursor);
+}
+
 static int
 amdgpu_userq_preempt_helper(struct amdgpu_userq_mgr *uq_mgr,
                      struct amdgpu_usermode_queue *queue) @@ -184,6

+232,7 @@

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union

Quoted 9 lines

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);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+   list_del(&queue->userq_va_list);
    kfree(queue);
 }
@@ -504,13 +553,7 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -2
            goto unlock;
    }
 
-   /* 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) ||

Quoted 1 line

- amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va,

AMDGPU_GPU_PAGE_SIZE)) {

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -3
-           kfree(queue);
-           goto unlock;
-   }
+   INIT_LIST_HEAD(&queue->userq_va_list);
    queue->doorbell_handle = args->in.doorbell_handle;
    queue->queue_type = args->in.ip_type;
    queue->vm = &fpriv->vm;
@@ -521,6 +564,15 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
    db_info.db_obj = &queue->db_obj;
    db_info.doorbell_offset = args->in.doorbell_offset;
 
+   /* Validate the userq virtual address.*/
+   if (amdgpu_userq_input_va_validate(queue, args->in.queue_va, args-

in.queue_size) ||
+ amdgpu_userq_input_va_validate(queue, args->in.rptr_va,

AMDGPU_GPU_PAGE_SIZE) ||

Quoted 1 line

+ amdgpu_userq_input_va_validate(queue, args->in.wptr_va,

AMDGPU_GPU_PAGE_SIZE)) {

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+           r = -EINVAL;
+           kfree(queue);
+           goto unlock;
+   }
+
    /* Convert relative doorbell offset into absolute doorbell index */
    index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
    if (index == (uint64_t)-EINVAL) {
@@ -546,7 +598,6 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
            goto unlock;
    }
 
-
    qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,

AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h int

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
    if (qid < 0) {
            drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n"); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h

b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index ded33fe76e1c..f19416feb7ef 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
@@ -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,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h int · line 145

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
                                              u32 idx);
 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device

*adev,

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -3
                                               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 +0 -0
index 2db9b2c63693..673dfbbd95e4 100644
@@ -298,8 +298,8 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -2
                    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, compute_mqd->eop_va,
+                                   max_t(u32, PAGE_SIZE,

AMDGPU_GPU_PAGE_SIZE)))

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
                    goto free_mqd;
 
            userq_props->eop_gpu_addr = compute_mqd->eop_va; @@ -330,8

+330,8

Quoted 3 lines

@@ 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;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct

Quoted 11 lines

- if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11-
shadow_va,

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
-               shadow_info.shadow_size))
+           if (amdgpu_userq_input_va_validate(queue, mqd_gfx_v11-

shadow_va,

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
+                                   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,

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -3
                    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, mqd_sdma_v11-
 csa_va,
+                                   shadow_info.csa_size))
                    goto free_mqd;
            userq_props->csa_addr = mqd_sdma_v11->csa_va;
#4 lore raw

PATCH v4 3/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 | 28 +++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +28 -0
index 99e51a8ff62a..790f51d2b8f2 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
#5 lore raw

PATCH v4 4/10 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 790f51d2b8f2..9cdb82c13a34 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
Reviewed-by on v4 4/10
#6 lore raw

Re: [PATCH v4 04/10] drm/amdgpu: keeping waiting userq fence infinitely

Quoted 6 lines · Prike Liang

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

Patches 1-4 are:

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 790f51d2b8f2..9cdb82c13a34 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
--

2.34.1

#7 lore raw

PATCH v4 5/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 | 29 ++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +28 -1
index 9cdb82c13a34..402145d64714 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,7 +797,15 @@ 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) {
-		r = amdgpu_userq_restore_helper(uq_mgr, queue);
+
+		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_map_helper(uq_mgr, queue);
 		if (r)
 			ret = r;
 	}
Signature · 1 line
2.34.1
Reviewed-by on v4 5/10
#8 lore raw

Re: [PATCH v4 05/10] drm/amdgpu: validate the queue va for resuming the queue

Quoted 47 lines · 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 | 29 ++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +19 -0
index 9cdb82c13a34..402145d64714 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,7 +797,15 @@ 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) {

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) · line 808

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -1
-               r = amdgpu_userq_restore_helper(uq_mgr, queue);
+
+               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_map_helper(uq_mgr, queue);

Looks like a rebasing issue here. This should be
amdgpu_userq_restore_helper(). WIth that fixed:

Alex

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
                if (r)
                        ret = r;
        }
--

2.34.1

#9 lore raw

PATCH v4 6/10 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 402145d64714..224d09019997 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 1ccd348bf48b..e12a314d27a1 100644
@@ -1966,6 +1966,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;
 
@@ -1986,6 +1987,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,
+				      "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
Reviewed-by on v4 6/10
#10 lore raw

Re: [PATCH v4 06/10] drm/amdgpu: validate userq va for GEM unmap

Quoted 80 lines · 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 402145d64714..224d09019997 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 +1 -0
index 1ccd348bf48b..e12a314d27a1 100644
@@ -1966,6 +1966,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;
 
@@ -1986,6 +1987,17 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
                        return -ENOENT;
        }

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, · line 1998

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -0
 
+       /* 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");

Rephrase the error here. Maybe something like:

"Attempt to unmap an active userq buffer\n"

With that fixed:

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
+       }
+
        list_del(&mapping->list);
        amdgpu_vm_it_remove(mapping, &vm->va);
        mapping->bo_va = NULL;
--

2.34.1

#11 lore raw

PATCH v4 7/10 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
#12 lore raw

PATCH v4 8/10 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 | 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 224d09019997..8ac7236091fa 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,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)
@@ -779,7 +808,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 {
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH v4 08/10] drm/amdgpu/userq: implement support for query status

Quoted 11 lines · 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

Might as well squash patches 9 and 10 into patch 8.

Alex

Quoted 80 lines
---
 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 224d09019997..8ac7236091fa 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,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)
@@ -779,7 +808,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 -1
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 {
--

2.34.1

#14 lore raw

PATCH v4 9/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 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
#15 lore raw

PATCH v4 10/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 8ac7236091fa..a5e7e279b78e 100644
@@ -723,6 +723,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