Skip to contentSkip to conversation

drm/amdgpu: validate userq input args

PATCH Superseded Prike Liang 3 participants 34 messages Jun 24, 2025 – Jul 1, 2025

Subsystems (2)

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/11 drm/amdgpu: validate userq input args

This will help on validating the userq input args, and rejecting for the invalid userq request at the IOCTLs first place.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 81 +++++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |  7 --
 2 files changed, 56 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +56 -25
index 295e7186e156..7f9dfeae4322 100644
@@ -359,27 +359,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		(args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>
 		AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
 
-	/* Usermode queues are only supported for GFX IP as of now */
-	if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
-	    args->in.ip_type != AMDGPU_HW_IP_DMA &&
-	    args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
-		drm_file_err(uq_mgr->file, "Usermode queue doesn't support IP type %u\n",
-			     args->in.ip_type);
-		return -EINVAL;
-	}
-
 	r = amdgpu_userq_priority_permit(filp, priority);
 	if (r)
 		return r;
 
-	if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
-	    (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
-	    (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
-	    !amdgpu_is_tmz(adev)) {
-		drm_file_err(uq_mgr->file, "Secure only supported on GFX/Compute queues\n");
-		return -EINVAL;
-	}
-
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
 	if (r < 0) {
 		drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n");
@@ -485,22 +468,45 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	return r;
 }
 
-int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
-		       struct drm_file *filp)
+static int amdgpu_userq_input_args_validate(struct drm_device *dev,
+					union drm_amdgpu_userq *args,
+					struct drm_file *filp)
 {
-	union drm_amdgpu_userq *args = data;
-	int r;
+	struct amdgpu_device *adev = drm_to_adev(dev);
 
 	switch (args->in.op) {
 	case AMDGPU_USERQ_OP_CREATE:
 		if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
 				       AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))
 			return -EINVAL;
-		r = amdgpu_userq_create(filp, args);
-		if (r)
-			drm_file_err(filp, "Failed to create usermode queue\n");
-		break;
+		/* Usermode queues are only supported for GFX IP as of now */
+		if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
+		    args->in.ip_type != AMDGPU_HW_IP_DMA &&
+		    args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
+			drm_file_err(filp, "Usermode queue doesn't support IP type %u\n",
+				     args->in.ip_type);
+			return -EINVAL;
+		}
+
+		if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
+		    (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
+		    (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
+		    !amdgpu_is_tmz(adev)) {
+			drm_file_err(filp, "Secure only supported on GFX/Compute queues\n");
+			return -EINVAL;
+		}
 
+		if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
+		    args->in.queue_va == 0 ||
+		    args->in.queue_size == 0) {
+			drm_file_err(filp, "invalidate userq queue va or size\n");
+			return -EINVAL;
+		}
+		if (!args->in.wptr_va || !args->in.rptr_va) {
+			drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
+			return -EINVAL;
+		}
+		break;
 	case AMDGPU_USERQ_OP_FREE:
 		if (args->in.ip_type ||
 		    args->in.doorbell_handle ||
@@ -514,6 +520,31 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
 		    args->in.mqd ||
 		    args->in.mqd_size)
 			return -EINVAL;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
+		       struct drm_file *filp)
+{
+	union drm_amdgpu_userq *args = data;
+	int r;
+
+	if (amdgpu_userq_input_args_validate(dev, args, filp) < 0)
+		return -EINVAL;
+
+	switch (args->in.op) {
+	case AMDGPU_USERQ_OP_CREATE:
+		r = amdgpu_userq_create(filp, args);
+		if (r)
+			drm_file_err(filp, "Failed to create usermode queue\n");
+		break;
+
+	case AMDGPU_USERQ_OP_FREE:
 		r = amdgpu_userq_destroy(filp, args->in.queue_id);
 		if (r)
 			drm_file_err(filp, "Failed to destroy usermode queue\n");
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -7
index d6f50b13e2ba..1457fb49a794 100644
@@ -215,13 +215,6 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		return -ENOMEM;
 	}
 
-	if (!mqd_user->wptr_va || !mqd_user->rptr_va ||
-	    !mqd_user->queue_va || mqd_user->queue_size == 0) {
-		DRM_ERROR("Invalid MQD parameters for userqueue\n");
-		r = -EINVAL;
-		goto free_props;
-	}
-
 	r = amdgpu_userq_create_object(uq_mgr, &queue->mqd, mqd_hw_default->mqd_size);
 	if (r) {
 		DRM_ERROR("Failed to create MQD object for userqueue\n");
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 2/11 drm/amdgpu: validate unmap status for destroying userq

Before destroying the userq buffer object, it requires validating the userq unmap status and ensuring the userq is unmapped from hardware.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
index 7f9dfeae4322..c7c9f9e597f1 100644
@@ -319,6 +319,11 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 	}
 	amdgpu_bo_unref(&queue->db_obj.obj);
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
+	/*TODO: It requires a reset for unmap error*/
+	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
+		drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n");
+		r = -ETIMEDOUT;
+	}
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v4 02/11] drm/amdgpu: validate unmap status for destroying userq

Quoted 3 lines · Prike Liang

Before destroying the userq buffer object, it requires validating the userq unmap status and ensuring the userq is unmapped from hardware.

Hui, what is that supposed to be doing?

Regards,
Christian.

Quoted 23 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
index 7f9dfeae4322..c7c9f9e597f1 100644
@@ -319,6 +319,11 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 	}
 	amdgpu_bo_unref(&queue->db_obj.obj);
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
+	/*TODO: It requires a reset for unmap error*/
+	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
+		drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n");
+		r = -ETIMEDOUT;
+	}
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
#3 lore raw

RE: [PATCH v4 02/11] drm/amdgpu: validate unmap status for destroying userq

[Public]

Regards,
Prike

Quoted 8 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:59 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 02/11] drm/amdgpu: validate unmap status for destroying userq

Quoted 5 lines
Quoted 2 lines · Prike Liang

Before destroying the userq buffer object, it requires validating the userq unmap status and ensuring the userq is unmapped from hardware.

Hui, what is that supposed to be doing?

This change arms to validate the userq HW unmap result to see whether it is successful; if the user HW unmap failed, then it needs to reset the queue for reusing when the userq reset implementation lands. I will rework the commit log a bit, and as the failure is unlikely to happen, then I will add the unlikely() for the check condition.

Regards,

Quoted 28 lines

Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int

Quoted 13 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 7f9dfeae4322..c7c9f9e597f1 100644
@@ -319,6 +319,11 @@ amdgpu_userq_destroy(struct drm_file *filp, int

queue_id)

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id)

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
    }
    amdgpu_bo_unref(&queue->db_obj.obj);
    r = amdgpu_userq_unmap_helper(uq_mgr, queue);
+   /*TODO: It requires a reset for unmap error*/
+   if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
+           drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW

mapping userq\n");

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+           r = -ETIMEDOUT;
+   }
    amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
    mutex_unlock(&uq_mgr->userq_mutex);
 
#4 lore raw

PATCH v4 3/11 drm/amdgpu: unify the userq doorbell object destroy

In reply to Prike Liang

Unify and refine the userq doorbell object destroy.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 1 -
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 ++++
 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
index c7c9f9e597f1..acaea4416ed2 100644
@@ -317,7 +317,6 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		amdgpu_bo_unpin(queue->db_obj.obj);
 		amdgpu_bo_unreserve(queue->db_obj.obj);
 	}
-	amdgpu_bo_unref(&queue->db_obj.obj);
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
 	/*TODO: It requires a reset for unmap error*/
 	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
index 1457fb49a794..dbacdfcb6f7b 100644
@@ -336,6 +336,10 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr,
 		      struct amdgpu_usermode_queue *queue)
 {
 	amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+	/*Validate the doorbell obj whether has unpinned by amdgpu_userq_destroy()*/
+	if (queue->db_obj.obj->tbo.pin_count)
+		amdgpu_bo_unpin(queue->db_obj.obj);
+	amdgpu_userq_destroy_object(uq_mgr, &queue->db_obj);
 	kfree(queue->userq_prop);
 	amdgpu_userq_destroy_object(uq_mgr, &queue->mqd);
 }
Signature · 1 line
2.34.1
maintainer
#5 lore raw

Re: [PATCH v4 03/11] drm/amdgpu: unify the userq doorbell object destroy

Quoted 26 lines · Prike Liang

Unify and refine the userq doorbell object destroy.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 1 -
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 ++++
 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
index c7c9f9e597f1..acaea4416ed2 100644
@@ -317,7 +317,6 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		amdgpu_bo_unpin(queue->db_obj.obj);
 		amdgpu_bo_unreserve(queue->db_obj.obj);
 	}
-	amdgpu_bo_unref(&queue->db_obj.obj);
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
 	/*TODO: It requires a reset for unmap error*/
 	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 1457fb49a794..dbacdfcb6f7b 100644

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, · line 342

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
@@ -336,6 +336,10 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr,
 		      struct amdgpu_usermode_queue *queue)
 {
 	amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+	/*Validate the doorbell obj whether has unpinned by amdgpu_userq_destroy()*/
+	if (queue->db_obj.obj->tbo.pin_count)
+		amdgpu_bo_unpin(queue->db_obj.obj);
+	amdgpu_userq_destroy_object(uq_mgr, &queue->db_obj);

Absolutely clear NAK! You are breaking tons of things here.

What the heck are you trying to do?

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
 	kfree(queue->userq_prop);
 	amdgpu_userq_destroy_object(uq_mgr, &queue->mqd);
 }
#6 lore raw

RE: [PATCH v4 03/11] drm/amdgpu: unify the userq doorbell object destroy

[Public]

Regards,
Prike

Quoted 42 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:57 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 03/11] drm/amdgpu: unify the userq doorbell object destroy

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr

Quoted 28 lines · Prike Liang

Unify and refine the userq doorbell object destroy.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 1 -
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 ++++
 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
index c7c9f9e597f1..acaea4416ed2 100644
@@ -317,7 +317,6 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
            amdgpu_bo_unpin(queue->db_obj.obj);
            amdgpu_bo_unreserve(queue->db_obj.obj);
    }
-   amdgpu_bo_unref(&queue->db_obj.obj);
    r = amdgpu_userq_unmap_helper(uq_mgr, queue);
    /*TODO: It requires a reset for unmap error*/
    if (r != AMDGPU_USERQ_STATE_UNMAPPED) { diff --git

a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 1457fb49a794..dbacdfcb6f7b 100644

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -336,6 +336,10 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr

*uq_mgr,

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr · line 338

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
                  struct amdgpu_usermode_queue *queue)  {
    amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+   /*Validate the doorbell obj whether has unpinned by
Quoted 8 lines

amdgpu_userq_destroy()*/

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, · line 342

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+   if (queue->db_obj.obj->tbo.pin_count)
+           amdgpu_bo_unpin(queue->db_obj.obj);
+   amdgpu_userq_destroy_object(uq_mgr, &queue->db_obj);

Absolutely clear NAK! You are breaking tons of things here.

What the heck are you trying to do?

This wants to unify and destroy the userq doorbell objects at mes_userq_mqd_destroy(), and this change will also help on unpinning and destroying the userq doorbell objects for amdgpu_userq_mgr_fini() at releasing the drm files.

Quoted 6 lines

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
    kfree(queue->userq_prop);
    amdgpu_userq_destroy_object(uq_mgr, &queue->mqd);  }
#7 lore raw

PATCH v4 4/11 drm/amdgpu: validate userq buffer virtual address and size

In reply to Prike Liang

It needs to validate the userq object virtual address whether it is validated in vm mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 44 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 +
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 ++++++++++++
 3 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +44 -0
index acaea4416ed2..a20f7ccbe98e 100644
@@ -31,6 +31,8 @@
 #include "amdgpu_userq.h"
 #include "amdgpu_userq_fence.h"
 
+#define amdgpu_userq_va_align(va) (va & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT
+
 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
 {
 	int i;
@@ -44,6 +46,36 @@ 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)
+{
+	struct amdgpu_bo_va_mapping *va_map;
+	u64 user_addr;
+	u64 size;
+	int r;
+
+	user_addr = amdgpu_userq_va_align(addr);
+	size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+	r = amdgpu_bo_reserve(vm->root.bo, false);
+	if (r)
+		return r;
+
+	va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+	if (!va_map)
+		goto out_err;
+	/* Only validate the userq whether resident in the VM mapping range */
+	if (user_addr >= va_map->start &&
+	    (size != 0 && user_addr + size - 1 <= va_map->last)) {
+		amdgpu_bo_unreserve(vm->root.bo);
+		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)
@@ -391,6 +423,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		r = -EINVAL;
 		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, PAGE_SIZE) ||
+	    amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va, PAGE_SIZE)) {
+		drm_file_err(uq_mgr->file, "Usermode queue input virt address is invalid\n");
+		r = -EINVAL;
+		goto unlock;
+	}
 
 	queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
 	if (!queue) {
@@ -500,6 +540,10 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,
 			return -EINVAL;
 		}
 
+		/* As the queue_va and wptr_va etc are the useq cpu access address and they
+		 * are also come from the user space IOCTL request, so they may need a access_ok()
+		 * check before trying accessing them.
+		 */
 		if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
 		    args->in.queue_va == 0 ||
 		    args->in.queue_size == 0) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index ec040c2fd6c9..704935ca0c36 100644
@@ -132,4 +132,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 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);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +25 -0
index dbacdfcb6f7b..4615d3fba530 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 	struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
 	struct drm_amdgpu_userq_in *mqd_user = args_in;
 	struct amdgpu_mqd_prop *userq_props;
+	struct amdgpu_gfx_shadow_info shadow_info;
 	int r;
 
 	/* Structure to initialize MQD for userqueue using generic MQD init function */
@@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 	userq_props->doorbell_index = queue->doorbell_index;
 	userq_props->fence_address = queue->fence_drv->gpu_addr;
 
+	if (adev->gfx.funcs->get_gfx_shadow_info)
+		adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
 	if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
 		struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
 
@@ -247,6 +250,13 @@ 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))) {
+			drm_file_err(uq_mgr->file, "EOP VA is invalid\n");
+			r = -EINVAL;
+			goto free_mqd;
+		}
+
 		userq_props->eop_gpu_addr = compute_mqd->eop_va;
 		userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
 		userq_props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
@@ -274,6 +284,14 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->csa_addr = mqd_gfx_v11->csa_va;
 		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)) {
+			drm_file_err(uq_mgr->file, "shadow VA is invalid\n");
+			r = -EINVAL;
+			goto free_mqd;
+		}
+
 		kfree(mqd_gfx_v11);
 	} else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
 		struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;
@@ -291,6 +309,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			goto free_mqd;
 		}
 
+		if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11->csa_va,
+					shadow_info.csa_size)) {
+			drm_file_err(uq_mgr->file, "CSA VA is invalid\n");
+			r = -EINVAL;
+			goto free_mqd;
+		}
+
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
 	}
Signature · 1 line
2.34.1
Reviewed-by on v4 4/11
#8 lore raw

Re: [PATCH v4 04/11] drm/amdgpu: validate userq buffer virtual address and size

Quoted 77 lines · Prike Liang

It needs to validate the userq object virtual address whether it is validated in vm mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 44 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 +
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 ++++++++++++
 3 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +40 -0
index acaea4416ed2..a20f7ccbe98e 100644
@@ -31,6 +31,8 @@
 #include "amdgpu_userq.h"
 #include "amdgpu_userq_fence.h"
 
+#define amdgpu_userq_va_align(va) (va & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT
+
 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
 {
        int i;
@@ -44,6 +46,36 @@ 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)
+{
+       struct amdgpu_bo_va_mapping *va_map;
+       u64 user_addr;
+       u64 size;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!va_map)
+               goto out_err;
+       /* Only validate the userq whether resident in the VM mapping range */
+       if (user_addr >= va_map->start &&
+           (size != 0 && user_addr + size - 1 <= va_map->last)) {
+               amdgpu_bo_unreserve(vm->root.bo);
+               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)
@@ -391,6 +423,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
                r = -EINVAL;
                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, PAGE_SIZE) ||
+           amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va, PAGE_SIZE)) {
+               drm_file_err(uq_mgr->file, "Usermode queue input virt address is invalid\n");
+               r = -EINVAL;
+               goto unlock;
+       }
 
        queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
        if (!queue) {

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 546

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
@@ -500,6 +540,10 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,
                        return -EINVAL;
                }
 
+               /* As the queue_va and wptr_va etc are the useq cpu access address and they
+                * are also come from the user space IOCTL request, so they may need a access_ok()
+                * check before trying accessing them.
+                */

This comment is incorrect. These are GPU virtual addresses. With that fixed, the patch is:

Quoted 81 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
                if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
                    args->in.queue_va == 0 ||
                    args->in.queue_size == 0) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index ec040c2fd6c9..704935ca0c36 100644
@@ -132,4 +132,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 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);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +25 -1
index dbacdfcb6f7b..4615d3fba530 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
        struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
        struct drm_amdgpu_userq_in *mqd_user = args_in;
        struct amdgpu_mqd_prop *userq_props;
+       struct amdgpu_gfx_shadow_info shadow_info;
        int r;
 
        /* Structure to initialize MQD for userqueue using generic MQD init function */
@@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
        userq_props->doorbell_index = queue->doorbell_index;
        userq_props->fence_address = queue->fence_drv->gpu_addr;
 
+       if (adev->gfx.funcs->get_gfx_shadow_info)
+               adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
        if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
                struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
 
@@ -247,6 +250,13 @@ 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))) {
+                       drm_file_err(uq_mgr->file, "EOP VA is invalid\n");
+                       r = -EINVAL;
+                       goto free_mqd;
+               }
+
                userq_props->eop_gpu_addr = compute_mqd->eop_va;
                userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
                userq_props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
@@ -274,6 +284,14 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                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)) {
+                       drm_file_err(uq_mgr->file, "shadow VA is invalid\n");
+                       r = -EINVAL;
+                       goto free_mqd;
+               }
+
                kfree(mqd_gfx_v11);
        } else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
                struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;
@@ -291,6 +309,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                        goto free_mqd;
                }
 
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11->csa_va,
+                                       shadow_info.csa_size)) {
+                       drm_file_err(uq_mgr->file, "CSA VA is invalid\n");
+                       r = -EINVAL;
+                       goto free_mqd;
+               }
+
                userq_props->csa_addr = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }
--

2.34.1

maintainer
#9 lore raw

Re: [PATCH v4 04/11] drm/amdgpu: validate userq buffer virtual address and size

In reply to Prike Liang
Quoted 15 lines · Prike Liang

It needs to validate the userq object virtual address whether it is validated in vm mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 44 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 +
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 ++++++++++++
 3 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index acaea4416ed2..a20f7ccbe98e 100644

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
@@ -31,6 +31,8 @@
 #include "amdgpu_userq.h"
 #include "amdgpu_userq_fence.h"
 
+#define amdgpu_userq_va_align(va) (va & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT

That doesn't seem to make sense to me. Why is that needed?

Christian

Quoted 142 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +43 -0
+
 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
 {
 	int i;
@@ -44,6 +46,36 @@ 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)
+{
+	struct amdgpu_bo_va_mapping *va_map;
+	u64 user_addr;
+	u64 size;
+	int r;
+
+	user_addr = amdgpu_userq_va_align(addr);
+	size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+	r = amdgpu_bo_reserve(vm->root.bo, false);
+	if (r)
+		return r;
+
+	va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+	if (!va_map)
+		goto out_err;
+	/* Only validate the userq whether resident in the VM mapping range */
+	if (user_addr >= va_map->start &&
+	    (size != 0 && user_addr + size - 1 <= va_map->last)) {
+		amdgpu_bo_unreserve(vm->root.bo);
+		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)
@@ -391,6 +423,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		r = -EINVAL;
 		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, PAGE_SIZE) ||
+	    amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va, PAGE_SIZE)) {
+		drm_file_err(uq_mgr->file, "Usermode queue input virt address is invalid\n");
+		r = -EINVAL;
+		goto unlock;
+	}
 
 	queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
 	if (!queue) {
@@ -500,6 +540,10 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,
 			return -EINVAL;
 		}
 
+		/* As the queue_va and wptr_va etc are the useq cpu access address and they
+		 * are also come from the user space IOCTL request, so they may need a access_ok()
+		 * check before trying accessing them.
+		 */
 		if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
 		    args->in.queue_va == 0 ||
 		    args->in.queue_size == 0) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index ec040c2fd6c9..704935ca0c36 100644
@@ -132,4 +132,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 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);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +25 -0
index dbacdfcb6f7b..4615d3fba530 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 	struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
 	struct drm_amdgpu_userq_in *mqd_user = args_in;
 	struct amdgpu_mqd_prop *userq_props;
+	struct amdgpu_gfx_shadow_info shadow_info;
 	int r;
 
 	/* Structure to initialize MQD for userqueue using generic MQD init function */
@@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 	userq_props->doorbell_index = queue->doorbell_index;
 	userq_props->fence_address = queue->fence_drv->gpu_addr;
 
+	if (adev->gfx.funcs->get_gfx_shadow_info)
+		adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
 	if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
 		struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
 
@@ -247,6 +250,13 @@ 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))) {
+			drm_file_err(uq_mgr->file, "EOP VA is invalid\n");
+			r = -EINVAL;
+			goto free_mqd;
+		}
+
 		userq_props->eop_gpu_addr = compute_mqd->eop_va;
 		userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
 		userq_props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
@@ -274,6 +284,14 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->csa_addr = mqd_gfx_v11->csa_va;
 		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)) {
+			drm_file_err(uq_mgr->file, "shadow VA is invalid\n");
+			r = -EINVAL;
+			goto free_mqd;
+		}
+
 		kfree(mqd_gfx_v11);
 	} else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
 		struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;
@@ -291,6 +309,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			goto free_mqd;
 		}
 
+		if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11->csa_va,
+					shadow_info.csa_size)) {
+			drm_file_err(uq_mgr->file, "CSA VA is invalid\n");
+			r = -EINVAL;
+			goto free_mqd;
+		}
+
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
 	}
#10 lore raw

RE: [PATCH v4 04/11] drm/amdgpu: validate userq buffer virtual address and size

[Public]

Regards,
Prike

Quoted 28 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:56 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 04/11] drm/amdgpu: validate userq buffer virtual address and size

Quoted 19 lines · Prike Liang

It needs to validate the userq object virtual address whether it is validated in vm mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 44
++++++++++++++++++++++  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |
2 +  drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 ++++++++++++
 3 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index acaea4416ed2..a20f7ccbe98e 100644
@@ -31,6 +31,8 @@
 #include "amdgpu_userq.h"
 #include "amdgpu_userq_fence.h"
 
Quoted 4 lines

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+#define amdgpu_userq_va_align(va) (va & AMDGPU_GMC_HOLE_MASK) >>
+AMDGPU_GPU_PAGE_SHIFT

That doesn't seem to make sense to me. Why is that needed?

This will help for aligning the userq object VA and then to be used for comparing with the VA range to validate the userq input userq VA whether it is resident in a valid mapping.

Quoted 168 lines

Christian

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+
 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)  {
    int i;
@@ -44,6 +46,36 @@ u32 amdgpu_userq_get_supported_ip_mask(struct

amdgpu_device *adev)

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +23 -0
    return userq_ip_mask;
 }
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                           u64 expected_size)
+{
+   struct amdgpu_bo_va_mapping *va_map;
+   u64 user_addr;
+   u64 size;
+   int r;
+
+   user_addr = amdgpu_userq_va_align(addr);
+   size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+   r = amdgpu_bo_reserve(vm->root.bo, false);
+   if (r)
+           return r;
+
+   va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+   if (!va_map)
+           goto out_err;
+   /* Only validate the userq whether resident in the VM mapping range */
+   if (user_addr >= va_map->start &&
+       (size != 0 && user_addr + size - 1 <= va_map->last)) {
+           amdgpu_bo_unreserve(vm->root.bo);
+           return 0;

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -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) @@ -391,6

+423,14 @@

Quoted 8 lines

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

            r = -EINVAL;
            goto unlock;
    }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+   /* 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,

PAGE_SIZE) ||

Quoted 1 line

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

PAGE_SIZE)) {

Quoted 1 line

+ drm_file_err(uq_mgr->file, "Usermode queue input virt address is

invalid\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
+           r = -EINVAL;
+           goto unlock;
+   }
 
    queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
    if (!queue) {
@@ -500,6 +540,10 @@ static int amdgpu_userq_input_args_validate(struct

drm_device *dev,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev,

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
                    return -EINVAL;
            }
 
+           /* As the queue_va and wptr_va etc are the useq cpu access address

and they

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct · line 544

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+            * are also come from the user space IOCTL request, so they may

need a access_ok()

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

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+            * check before trying accessing them.
+            */
            if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
                args->in.queue_va == 0 ||
                args->in.queue_size == 0) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
index ec040c2fd6c9..704935ca0c36 100644
@@ -132,4 +132,6 @@ int
 amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,

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 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
                                               u32 idx);
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                   u64 expected_size);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index dbacdfcb6f7b..4615d3fba530 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
    struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
    struct drm_amdgpu_userq_in *mqd_user = args_in;
    struct amdgpu_mqd_prop *userq_props;
+   struct amdgpu_gfx_shadow_info shadow_info;
    int r;
 
    /* Structure to initialize MQD for userqueue using generic MQD init

function */ @@ -231,6 +232,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

Quoted 9 lines
    userq_props->doorbell_index = queue->doorbell_index;
    userq_props->fence_address = queue->fence_drv->gpu_addr;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
+   if (adev->gfx.funcs->get_gfx_shadow_info)
+           adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
    if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
            struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
@@ -247,6 +250,13 @@ 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 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
                    goto free_mqd;
            }
 
+           if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd-
 eop_va,
+                                   max_t(u32, PAGE_SIZE,

AMDGPU_GPU_PAGE_SIZE))) {

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5 -0
+                   drm_file_err(uq_mgr->file, "EOP VA is invalid\n");
+                   r = -EINVAL;
+                   goto free_mqd;
+           }
+
            userq_props->eop_gpu_addr = compute_mqd->eop_va;
            userq_props->hqd_pipe_priority =

AMDGPU_GFX_PIPE_PRIO_NORMAL;

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

Quoted 3 lines
            userq_props->hqd_queue_priority =

AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -274,6 +284,14 @@ 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 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
            userq_props->csa_addr = mqd_gfx_v11->csa_va;
            userq_props->tmz_queue =
                    mqd_user->flags &

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +8 -0
+
+           if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11-
 shadow_va,
+                                   shadow_info.shadow_size)) {
+                   drm_file_err(uq_mgr->file, "shadow VA is invalid\n");
+                   r = -EINVAL;
+                   goto free_mqd;
+           }
+
            kfree(mqd_gfx_v11);
    } else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
Quoted 1 line
            struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11; @@

-291,6

Quoted 1 line

+309,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 14 lines
                    goto free_mqd;
            }

+ if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11-
csa_va,

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -0
+                                   shadow_info.csa_size)) {
+                   drm_file_err(uq_mgr->file, "CSA VA is invalid\n");
+                   r = -EINVAL;
+                   goto free_mqd;
+           }
+
            userq_props->csa_addr = mqd_sdma_v11->csa_va;
            kfree(mqd_sdma_v11);
    }
maintainer
#11 lore raw

Re: [PATCH v4 04/11] drm/amdgpu: validate userq buffer virtual address and size

Quoted 33 lines · Prike

[Public]

Regards,
Prike

Quoted 28 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:56 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 04/11] drm/amdgpu: validate userq buffer virtual address and size

Quoted 19 lines · Prike Liang

It needs to validate the userq object virtual address whether it is validated in vm mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 44
++++++++++++++++++++++  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |
2 +  drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 ++++++++++++
 3 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index acaea4416ed2..a20f7ccbe98e 100644
@@ -31,6 +31,8 @@
 #include "amdgpu_userq.h"
 #include "amdgpu_userq_fence.h"
 
Quoted 5 lines
Quoted 4 lines

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+#define amdgpu_userq_va_align(va) (va & AMDGPU_GMC_HOLE_MASK) >>
+AMDGPU_GPU_PAGE_SHIFT

That doesn't seem to make sense to me. Why is that needed?

This will help for aligning the userq object VA and then to be used for comparing with the VA range to validate the userq input userq VA whether it is resident in a valid mapping.

Yeah and exactly that doesn't seem to make sense. Please completely drop that define.

This code belong into the VM handling.

Regards,
Christian.

Quoted 170 lines
Quoted 169 lines

Christian

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+
 u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)  {
    int i;
@@ -44,6 +46,36 @@ u32 amdgpu_userq_get_supported_ip_mask(struct

amdgpu_device *adev)

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +23 -0
    return userq_ip_mask;
 }
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                           u64 expected_size)
+{
+   struct amdgpu_bo_va_mapping *va_map;
+   u64 user_addr;
+   u64 size;
+   int r;
+
+   user_addr = amdgpu_userq_va_align(addr);
+   size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+   r = amdgpu_bo_reserve(vm->root.bo, false);
+   if (r)
+           return r;
+
+   va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+   if (!va_map)
+           goto out_err;
+   /* Only validate the userq whether resident in the VM mapping range */
+   if (user_addr >= va_map->start &&
+       (size != 0 && user_addr + size - 1 <= va_map->last)) {
+           amdgpu_bo_unreserve(vm->root.bo);
+           return 0;

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -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) @@ -391,6

+423,14 @@

Quoted 8 lines

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

            r = -EINVAL;
            goto unlock;
    }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+   /* 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,

PAGE_SIZE) ||

Quoted 1 line

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

PAGE_SIZE)) {

Quoted 1 line

+ drm_file_err(uq_mgr->file, "Usermode queue input virt address is

invalid\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
+           r = -EINVAL;
+           goto unlock;
+   }
 
    queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
    if (!queue) {
@@ -500,6 +540,10 @@ static int amdgpu_userq_input_args_validate(struct

drm_device *dev,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev,

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
                    return -EINVAL;
            }
 
+           /* As the queue_va and wptr_va etc are the useq cpu access address

and they

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct · line 544

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+            * are also come from the user space IOCTL request, so they may

need a access_ok()

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

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+            * check before trying accessing them.
+            */
            if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
                args->in.queue_va == 0 ||
                args->in.queue_size == 0) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
index ec040c2fd6c9..704935ca0c36 100644
@@ -132,4 +132,6 @@ int
 amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,

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 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
                                               u32 idx);
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                   u64 expected_size);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index dbacdfcb6f7b..4615d3fba530 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
    struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
    struct drm_amdgpu_userq_in *mqd_user = args_in;
    struct amdgpu_mqd_prop *userq_props;
+   struct amdgpu_gfx_shadow_info shadow_info;
    int r;
 
    /* Structure to initialize MQD for userqueue using generic MQD init

function */ @@ -231,6 +232,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

Quoted 9 lines
    userq_props->doorbell_index = queue->doorbell_index;
    userq_props->fence_address = queue->fence_drv->gpu_addr;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
+   if (adev->gfx.funcs->get_gfx_shadow_info)
+           adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
    if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
            struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
@@ -247,6 +250,13 @@ 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 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
                    goto free_mqd;
            }
 
+           if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd-
 eop_va,
+                                   max_t(u32, PAGE_SIZE,

AMDGPU_GPU_PAGE_SIZE))) {

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5 -0
+                   drm_file_err(uq_mgr->file, "EOP VA is invalid\n");
+                   r = -EINVAL;
+                   goto free_mqd;
+           }
+
            userq_props->eop_gpu_addr = compute_mqd->eop_va;
            userq_props->hqd_pipe_priority =

AMDGPU_GFX_PIPE_PRIO_NORMAL;

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

Quoted 3 lines
            userq_props->hqd_queue_priority =

AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -274,6 +284,14 @@ 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 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
            userq_props->csa_addr = mqd_gfx_v11->csa_va;
            userq_props->tmz_queue =
                    mqd_user->flags &

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +8 -0
+
+           if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11-
 shadow_va,
+                                   shadow_info.shadow_size)) {
+                   drm_file_err(uq_mgr->file, "shadow VA is invalid\n");
+                   r = -EINVAL;
+                   goto free_mqd;
+           }
+
            kfree(mqd_gfx_v11);
    } else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
Quoted 1 line
            struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11; @@

-291,6

Quoted 1 line

+309,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 14 lines
                    goto free_mqd;
            }

+ if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11-
csa_va,

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -0
+                                   shadow_info.csa_size)) {
+                   drm_file_err(uq_mgr->file, "CSA VA is invalid\n");
+                   r = -EINVAL;
+                   goto free_mqd;
+           }
+
            userq_props->csa_addr = mqd_sdma_v11->csa_va;
            kfree(mqd_sdma_v11);
    }
#12 lore raw

PATCH v4 5/11 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_userq.c  | 132 ++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  14 +++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 149 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +131 -1
index a20f7ccbe98e..cbbd1860a69a 100644
@@ -76,6 +76,134 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
 	return -EINVAL;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr)
+{
+	struct amdgpu_bo_va_mapping *mapping;
+	u64 user_addr;
+	int r;
+
+	user_addr = amdgpu_userq_va_align(addr);
+	r = amdgpu_bo_reserve(vm->root.bo, false);
+	if (r)
+		return r;
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+	if (!mapping)
+		goto out_err;
+
+	/*
+	 * Need to unify the following userq va reference.
+	 *  mqd  bo
+	 *  rptr bo
+	 *  wptr bo
+	 *  eop  bo
+	 *  doorbell bo
+	 */
+	/*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+	mapping->bo_va->queue_refcount++;
+
+	amdgpu_bo_unreserve(vm->root.bo);
+	return 0;
+
+out_err:
+	amdgpu_bo_unreserve(vm->root.bo);
+	return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
+{
+	struct amdgpu_bo_va_mapping *mapping;
+	u64 user_addr;
+	bool r;
+
+	user_addr = amdgpu_userq_va_align(addr);
+
+	if (amdgpu_bo_reserve(vm->root.bo, false))
+		return false;
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+	if (!IS_ERR_OR_NULL(mapping) && mapping->bo_va->queue_refcount > 0)
+		r = true;
+	else
+		r = false;
+	amdgpu_bo_unreserve(vm->root.bo);
+
+	return r;
+}
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue)
+{
+
+	if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+	    amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+	    amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+	    amdgpu_userq_buffer_va_mapped(vm, queue->eop_va) ||
+	    amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+	    amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+		return true;
+	else
+		return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr)
+{
+	struct amdgpu_bo_va_mapping *mapping;
+	u64 user_addr;
+	int r;
+
+	user_addr = amdgpu_userq_va_align(addr);
+	r = amdgpu_bo_reserve(vm->root.bo, false);
+	if (r)
+		return r;
+
+	mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+	if (!mapping)
+		goto out_err;
+	/*
+	 * If here refer to the userq VM BOs and keep the usage count by
+	 * amdgpu_bo_ref(mapping->bo_va->base.bo) at creating the userq IOCTL,
+	 * this reference and usage counter will be kept until amdgpu_userq_destroy(),
+	 * while the userq VA is unmapped at amdgpu_vm_bo_unmap(), which is ahead of
+	 * amdgpu_userq_destroy(). So, when amdgpu_vm_bo_unmap() tries to unmap the
+	 * userq VA mapping, it will result in an unmap error caused by the BO's reference.
+	 */
+	/*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+
+	mapping->bo_va->queue_refcount--;
+
+	amdgpu_bo_unreserve(vm->root.bo);
+	return 0;
+
+out_err:
+	amdgpu_bo_unreserve(vm->root.bo);
+	return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue)
+{
+	amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+	amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+	amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+	amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+	amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+	amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue)
+{
+	amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+	amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+	amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+	amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+	amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+	amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+
+	return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -442,6 +570,9 @@ 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->queue_va = args->in.queue_va;
+	queue->rptr_va = args->in.rptr_va;
+	queue->wptr_va = args->in.wptr_va;
 
 	db_info.queue_type = queue->queue_type;
 	db_info.doorbell_handle = queue->doorbell_handle;
@@ -472,7 +603,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 +14 -0
index 704935ca0c36..194ec7a6b3b2 100644
@@ -52,6 +52,13 @@ struct amdgpu_usermode_queue {
 	enum amdgpu_userq_state state;
 	uint64_t		doorbell_handle;
 	uint64_t		doorbell_index;
+	uint64_t		queue_va;
+	uint64_t		rptr_va;
+	uint64_t		wptr_va;
+	uint64_t		eop_va;
+	uint64_t		shadow_va;
+	uint64_t		csa_va;
+
 	uint64_t		flags;
 	struct amdgpu_mqd_prop	*userq_prop;
 	struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +141,11 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 
 int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
 			u64 expected_size);
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr);
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
index 4615d3fba530..c9cde14064d1 100644
@@ -263,6 +263,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->hqd_active = false;
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+		queue->eop_va = compute_mqd->eop_va;
 		kfree(compute_mqd);
 	} else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
 		struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11;
@@ -284,6 +285,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->csa_addr = mqd_gfx_v11->csa_va;
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+		queue->shadow_va = mqd_gfx_v11->shadow_va;
+		queue->csa_va = mqd_gfx_v11->csa_va;
 
 		if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
 					shadow_info.shadow_size)) {
@@ -317,6 +320,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		}
 
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
+		queue->csa_va = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
 	}
 
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH v4 05/11] drm/amdgpu: add userq object va track helpers

Quoted 74 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_userq.c  | 132 ++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  14 +++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 149 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +55 -0
index a20f7ccbe98e..cbbd1860a69a 100644
@@ -76,6 +76,134 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
        return -EINVAL;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)
+               goto out_err;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  doorbell bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+
+       if (amdgpu_bo_reserve(vm->root.bo, false))
+               return false;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!IS_ERR_OR_NULL(mapping) && mapping->bo_va->queue_refcount > 0)
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}
+

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+
+       if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->eop_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))

Only some of these are relevant for each queue type. Rather than checking all of the critical buffers in all of your helper functions, it might be cleaner to add new userq callbacks that only check/update the relevant buffers for the queue type.

Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +18 -0
+               return true;
+       else
+               return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+               goto out_err;
+       /*
+        * If here refer to the userq VM BOs and keep the usage count by
+        * amdgpu_bo_ref(mapping->bo_va->base.bo) at creating the userq IOCTL,
+        * this reference and usage counter will be kept until amdgpu_userq_destroy(),
+        * while the userq VA is unmapped at amdgpu_vm_bo_unmap(), which is ahead of
+        * amdgpu_userq_destroy(). So, when amdgpu_vm_bo_unmap() tries to unmap the
+        * userq VA mapping, it will result in an unmap error caused by the BO's reference.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/

I still don't follow this. Why can't we get a reference in userq create and put the reference in userq destroy. If the app is freeing the buffers before the queue, that's an app bug.

Alex

Quoted 118 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +38 -1
+
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
@@ -442,6 +570,9 @@ 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->queue_va = args->in.queue_va;
+       queue->rptr_va = args->in.rptr_va;
+       queue->wptr_va = args->in.wptr_va;
 
        db_info.queue_type = queue->queue_type;
        db_info.doorbell_handle = queue->doorbell_handle;
@@ -472,7 +603,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 +14 -0
index 704935ca0c36..194ec7a6b3b2 100644
@@ -52,6 +52,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +141,11 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 
 int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
                        u64 expected_size);
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr);
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -1
index 4615d3fba530..c9cde14064d1 100644
@@ -263,6 +263,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11;
@@ -284,6 +285,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->shadow_va = mqd_gfx_v11->shadow_va;
+               queue->csa_va = mqd_gfx_v11->csa_va;
 
                if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
                                        shadow_info.shadow_size)) {
@@ -317,6 +320,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                }
 
                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }
 
--

2.34.1

maintainer
#14 lore raw

Re: [PATCH v4 05/11] drm/amdgpu: add userq object va track helpers

Quoted 115 lines · Alex Deucher
Quoted 74 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_userq.c  | 132 ++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  14 +++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 149 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +55 -0
index a20f7ccbe98e..cbbd1860a69a 100644
@@ -76,6 +76,134 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
        return -EINVAL;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)
+               goto out_err;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  doorbell bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+
+       if (amdgpu_bo_reserve(vm->root.bo, false))
+               return false;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!IS_ERR_OR_NULL(mapping) && mapping->bo_va->queue_refcount > 0)
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}
+

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+
+       if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->eop_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))

Only some of these are relevant for each queue type. Rather than checking all of the critical buffers in all of your helper functions, it might be cleaner to add new userq callbacks that only check/update the relevant buffers for the queue type.

Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +24 -0
+               return true;
+       else
+               return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)
+               goto out_err;
+       /*
+        * If here refer to the userq VM BOs and keep the usage count by
+        * amdgpu_bo_ref(mapping->bo_va->base.bo) at creating the userq IOCTL,
+        * this reference and usage counter will be kept until amdgpu_userq_destroy(),
+        * while the userq VA is unmapped at amdgpu_vm_bo_unmap(), which is ahead of
Quoted 8 lines

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+        * amdgpu_userq_destroy(). So, when amdgpu_vm_bo_unmap() tries to unmap the
+        * userq VA mapping, it will result in an unmap error caused by the BO's reference.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/

I still don't follow this. Why can't we get a reference in userq create and put the reference in userq destroy. If the app is freeing the buffers before the queue, that's an app bug.

We should probably just signal the eviction fence when the application tries to unmap a BO which is vital for an user queue.

On restoring the evicted queue we then check again if all the VAs needed for this queue are valid and if they aren't we deny resuming the queue.

Christian.

Quoted 121 lines

Alex

Quoted 118 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +38 -1
+
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
@@ -442,6 +570,9 @@ 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->queue_va = args->in.queue_va;
+       queue->rptr_va = args->in.rptr_va;
+       queue->wptr_va = args->in.wptr_va;
 
        db_info.queue_type = queue->queue_type;
        db_info.doorbell_handle = queue->doorbell_handle;
@@ -472,7 +603,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 +14 -0
index 704935ca0c36..194ec7a6b3b2 100644
@@ -52,6 +52,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +141,11 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 
 int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
                        u64 expected_size);
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr);
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -1
index 4615d3fba530..c9cde14064d1 100644
@@ -263,6 +263,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11;
@@ -284,6 +285,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->shadow_va = mqd_gfx_v11->shadow_va;
+               queue->csa_va = mqd_gfx_v11->csa_va;
 
                if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
                                        shadow_info.shadow_size)) {
@@ -317,6 +320,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                }
 
                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }
 
--

2.34.1

#15 lore raw

RE: [PATCH v4 05/11] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 71 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 4:02 PM
To: Alex Deucher <[email protected]>; Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 05/11] drm/amdgpu: add userq object va track helpers

Quoted 9 lines · Alex Deucher
Quoted 8 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_userq.c  | 132

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

14 +++

Quoted 9 lines

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

Quoted 9 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 149 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index a20f7ccbe98e..cbbd1860a69a 100644
@@ -76,6 +76,134 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm

*vm, u64 addr,

Quoted 41 lines
Quoted 41 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +38 -0
        return -EINVAL;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)
+               goto out_err;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  doorbell bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
Quoted 30 lines
Quoted 7 lines

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+       user_addr = amdgpu_userq_va_align(addr);
+
+       if (amdgpu_bo_reserve(vm->root.bo, false))
+               return false;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!IS_ERR_OR_NULL(mapping) && mapping->bo_va->queue_refcount >

0)

Quoted 22 lines
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+       if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->eop_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))

Only some of these are relevant for each queue type. Rather than checking all of the critical buffers in all of your helper functions, it might be cleaner to add new userq callbacks that only check/update the relevant buffers for the queue type.

Yes, validating on a per-queue basis will be clearer for each userq type. But that will be more complicated and require more checkpoints. In this current proposal, the irrelevant userq virtual address will be invalid and return early place at amdgpu_vm_bo_lookup_mapping(), and the current implement seems to be more uniform and simpler. Though?

Quoted 174 lines
Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +14 -0
+               return true;
+       else
+               return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
Quoted 8 lines

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 +8 -0
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)
+               goto out_err;
+       /*
+        * If here refer to the userq VM BOs and keep the usage count by
+        * amdgpu_bo_ref(mapping->bo_va->base.bo) at creating the userq IOCTL,
+        * this reference and usage counter will be kept until

amdgpu_userq_destroy(),

Quoted 1 line

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+        * while the userq VA is unmapped at amdgpu_vm_bo_unmap(), which is

ahead of

Quoted 1 line

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+        * amdgpu_userq_destroy(). So, when amdgpu_vm_bo_unmap() tries to

unmap the

Quoted 1 line

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+        * userq VA mapping, it will result in an unmap error caused by the BO's

reference.

Quoted 6 lines

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/

I still don't follow this. Why can't we get a reference in userq create and put the reference in userq destroy. If the app is freeing the buffers before the queue, that's an app bug.

We should probably just signal the eviction fence when the application tries to unmap a BO which is vital for an user queue.

On restoring the evicted queue we then check again if all the VAs needed for this queue are valid and if they aren't we deny resuming the queue.

Christian.

Quoted 31 lines

Alex

Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +28 -0
+
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_get(vm, queue->csa_va); }
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_put(vm, queue->csa_va);
Quoted 8 lines

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 +4 -1
+
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue) @@
-442,6 +570,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 10 lines

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

Quoted 10 lines
        queue->queue_type = args->in.ip_type;
        queue->vm = &fpriv->vm;
        queue->priority = priority;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       queue->queue_va = args->in.queue_va;
+       queue->rptr_va = args->in.rptr_va;
+       queue->wptr_va = args->in.wptr_va;
 
        db_info.queue_type = queue->queue_type;
        db_info.doorbell_handle = queue->doorbell_handle; @@ -472,7
+603,6 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 5 lines
Quoted 5 lines
                goto unlock;
        }

-

        qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,

AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);

Quoted 24 lines
Quoted 24 lines
        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 704935ca0c36..194ec7a6b3b2 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
@@ -52,6 +52,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -134,4 +141,11 @@ int

amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,

Quoted 16 lines

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

Quoted 16 lines

int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,

                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr); bool
+amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr); bool
+amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue); int
+amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr); int
+amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 4615d3fba530..c9cde14064d1 100644
@@ -263,6 +263,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags &
 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11; @@

-284,6 +285,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 11 lines

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

Quoted 8 lines
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags &

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
+               queue->shadow_va = mqd_gfx_v11->shadow_va;
+               queue->csa_va = mqd_gfx_v11->csa_va;
 
                if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11-

shadow_va,

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

Quoted 2 lines
                                        shadow_info.shadow_size)) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -317,6 +320,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
                }
 
                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }
 
--

2.34.1

#16 lore raw

RE: [PATCH v4 05/11] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 118 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Liang, Prike
Sent: Tuesday, July 1, 2025 9:39 PM
To: Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: RE: [PATCH v4 05/11] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 74 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 4:02 PM
To: Alex Deucher <[email protected]>; Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 05/11] drm/amdgpu: add userq object va track helpers

Quoted 9 lines · Alex Deucher
Quoted 8 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_userq.c  | 132

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

14 +++

Quoted 10 lines
Quoted 10 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 149 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index a20f7ccbe98e..cbbd1860a69a 100644
@@ -76,6 +76,134 @@ int amdgpu_userq_input_va_validate(struct

amdgpu_vm

*vm, u64 addr,

Quoted 42 lines
Quoted 42 lines
        return -EINVAL;

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +39 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)
+               goto out_err;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  doorbell bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = amdgpu_userq_va_align(addr);
Quoted 30 lines
Quoted 7 lines

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+
+       if (amdgpu_bo_reserve(vm->root.bo, false))
+               return false;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!IS_ERR_OR_NULL(mapping) &&
+ mapping->bo_va->queue_refcount >

0)

Quoted 22 lines
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+       if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->eop_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+           amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))

Only some of these are relevant for each queue type. Rather than checking all of the critical buffers in all of your helper functions, it might be cleaner to add new userq callbacks that only check/update the relevant buffers for the queue type.

Quoted 5 lines

Yes, validating on a per-queue basis will be clearer for each userq type. But that will be more complicated and require more checkpoints. In this current proposal, the irrelevant userq virtual address will be invalid and return early place at amdgpu_vm_bo_lookup_mapping(), and the current implement seems to be more uniform and simpler. Thought?

Idea? If someone still prefer to do the per queue validation, then I will do that.

Quoted 181 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -0
+               return true;
+       else
+               return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = amdgpu_userq_va_align(addr);
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
Quoted 8 lines
Quoted 8 lines

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 +8 -0
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!mapping)
+               goto out_err;
+       /*
+        * If here refer to the userq VM BOs and keep the usage count by
+        * amdgpu_bo_ref(mapping->bo_va->base.bo) at creating the userq

IOCTL,

Quoted 159 lines
Quoted 1 line
Quoted 1 line

+ * this reference and usage counter will be kept until

amdgpu_userq_destroy(),

Quoted 2 lines
Quoted 2 lines

+ * while the userq VA is unmapped at amdgpu_vm_bo_unmap(),
+ which is

ahead of

Quoted 2 lines
Quoted 2 lines

+ * amdgpu_userq_destroy(). So, when amdgpu_vm_bo_unmap()
+ tries to

unmap the

Quoted 2 lines
Quoted 2 lines

+ * userq VA mapping, it will result in an unmap error
+ caused by the BO's

reference.

Quoted 6 lines

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/

I still don't follow this. Why can't we get a reference in userq create and put the reference in userq destroy. If the app is freeing the buffers before the queue, that's an app bug.

We should probably just signal the eviction fence when the application tries to unmap a BO which is vital for an user queue.

On restoring the evicted queue we then check again if all the VAs needed for this queue are valid and if they aren't we deny resuming the queue.

Christian.

Quoted 31 lines

Alex

Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +28 -0
+
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_get(vm, queue->csa_va); }
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+       amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+       amdgpu_userq_buffer_va_put(vm, queue->csa_va);
Quoted 8 lines

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 +4 -1
+
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue) @@
-442,6 +570,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 10 lines

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

Quoted 10 lines
        queue->queue_type = args->in.ip_type;
        queue->vm = &fpriv->vm;
        queue->priority = priority;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       queue->queue_va = args->in.queue_va;
+       queue->rptr_va = args->in.rptr_va;
+       queue->wptr_va = args->in.wptr_va;
 
        db_info.queue_type = queue->queue_type;
        db_info.doorbell_handle = queue->doorbell_handle; @@ -472,7
+603,6 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 5 lines
Quoted 5 lines
                goto unlock;
        }

-

        qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,

AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);

Quoted 25 lines
Quoted 25 lines
        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 704935ca0c36..194ec7a6b3b2 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
@@ -52,6 +52,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -134,4 +141,11 @@

int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,

Quoted 16 lines

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

Quoted 16 lines

int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,

                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64
+addr); bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue); int
+amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr); int
+amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 4615d3fba530..c9cde14064d1 100644
@@ -263,6 +263,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags &
 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11; @@

-284,6 +285,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 7 lines
Quoted 7 lines
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags &

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
+               queue->shadow_va = mqd_gfx_v11->shadow_va;
+               queue->csa_va = mqd_gfx_v11->csa_va;
Quoted 5 lines
Quoted 2 lines
                if (amdgpu_userq_input_va_validate(queue->vm,

mqd_gfx_v11-

shadow_va,

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

Quoted 2 lines
                                        shadow_info.shadow_size)) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -317,6 +320,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
                }
 
                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }
 
--

2.34.1

#17 lore raw

PATCH v4 6/11 drm/amdgpu: track the userq bo va for its obj management

In reply to Prike Liang

The user queue object destroy requires ensuring its VA has been unmapped prior to the queue being destroyed. Otherwise, the kernel driver should report an invalidated error to the user IOCLT request.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -0
index cbbd1860a69a..79b263c18eb1 100644
@@ -477,12 +477,24 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		amdgpu_bo_unpin(queue->db_obj.obj);
 		amdgpu_bo_unreserve(queue->db_obj.obj);
 	}
+
+	/*
+	 * At this point the userq obj va should be unmapped,
+	 * otherwise will return error to user.
+	 */
+	if (amdgpu_userq_buffer_vas_mapped(&fpriv->vm, queue)) {
+		drm_warn(adev_to_drm(uq_mgr->adev), "the userq obj va shouldn't be mapped during destroying userq\n");
+		r=  -EINVAL;
+	}
+
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
 	/*TODO: It requires a reset for unmap error*/
 	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
 		drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n");
 		r = -ETIMEDOUT;
 	}
+
+	amdgpu_userq_buffer_vas_put(&fpriv->vm, queue);
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
@@ -603,6 +615,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto unlock;
 	}
 
+	/* refer to the userq objects vm bo*/
+	amdgpu_userq_buffer_vas_get(queue->vm, queue);
+
 	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");
Signature · 1 line
2.34.1
maintainer
#18 lore raw

Re: [PATCH v4 06/11] drm/amdgpu: track the userq bo va for its obj management

Quoted 4 lines · Prike Liang

The user queue object destroy requires ensuring its VA has been unmapped prior to the queue being destroyed. Otherwise, the kernel driver should report an invalidated error to the user IOCLT request.

Well that is completely nonsense.

It must be just the other way around, you need to destroy the queue first and *then* unmap the VAs!

Regards,
Christian.

Quoted 45 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -0
index cbbd1860a69a..79b263c18eb1 100644
@@ -477,12 +477,24 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		amdgpu_bo_unpin(queue->db_obj.obj);
 		amdgpu_bo_unreserve(queue->db_obj.obj);
 	}
+
+	/*
+	 * At this point the userq obj va should be unmapped,
+	 * otherwise will return error to user.
+	 */
+	if (amdgpu_userq_buffer_vas_mapped(&fpriv->vm, queue)) {
+		drm_warn(adev_to_drm(uq_mgr->adev), "the userq obj va shouldn't be mapped during destroying userq\n");
+		r=  -EINVAL;
+	}
+
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
 	/*TODO: It requires a reset for unmap error*/
 	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
 		drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HW mapping userq\n");
 		r = -ETIMEDOUT;
 	}
+
+	amdgpu_userq_buffer_vas_put(&fpriv->vm, queue);
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
@@ -603,6 +615,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto unlock;
 	}
 
+	/* refer to the userq objects vm bo*/
+	amdgpu_userq_buffer_vas_get(queue->vm, queue);
+
 	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");
#19 lore raw

PATCH v4 7/11 drm/amdgpu: add user queue vm identifier

In reply to Prike Liang

Add a user queue vm identifier for each userqueue
kms opt accessing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
index 79b263c18eb1..0208c6b8a8e0 100644
@@ -501,6 +501,8 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
+	fpriv->vm.is_userq_context = false;
+
 	return r;
 }
 
@@ -535,6 +537,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		(args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>
 		AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
 
+	fpriv->vm.is_userq_context = true;
+
 	r = amdgpu_userq_priority_permit(filp, priority);
 	if (r)
 		return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 3911c78f8282..5e075e8f0ca3 100644
@@ -2560,7 +2560,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	ttm_lru_bulk_move_init(&vm->lru_bulk_move);
 
 	vm->is_compute_context = false;
-
+	vm->is_userq_context = false;
 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
 				    AMDGPU_VM_USE_CPU_FOR_GFX);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index f3ad687125ad..1e3c0f85c98e 100644
@@ -422,7 +422,7 @@ struct amdgpu_vm {
 	struct ttm_lru_bulk_move lru_bulk_move;
 	/* Flag to indicate if VM is used for compute */
 	bool			is_compute_context;
-
+	bool			is_userq_context;
 	/* Memory partition number, -1 means any partition */
 	int8_t			mem_id;
 
Signature · 1 line
2.34.1
maintainer
#20 lore raw

Re: [PATCH v4 07/11] drm/amdgpu: add user queue vm identifier

Quoted 15 lines · Prike Liang

Add a user queue vm identifier for each userqueue
kms opt accessing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 79b263c18eb1..0208c6b8a8e0 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id) · line 505

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
@@ -501,6 +501,8 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
+	fpriv->vm.is_userq_context = false;
+

I really don't like that approach. The VM should not have any special handling dependign on if user queues exists or not.

Why do you want that?

Regards,
Christian.

Quoted 38 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
 	return r;
 }
 
@@ -535,6 +537,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		(args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>
 		AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
 
+	fpriv->vm.is_userq_context = true;
+
 	r = amdgpu_userq_priority_permit(filp, priority);
 	if (r)
 		return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 3911c78f8282..5e075e8f0ca3 100644
@@ -2560,7 +2560,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	ttm_lru_bulk_move_init(&vm->lru_bulk_move);
 
 	vm->is_compute_context = false;
-
+	vm->is_userq_context = false;
 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
 				    AMDGPU_VM_USE_CPU_FOR_GFX);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index f3ad687125ad..1e3c0f85c98e 100644
@@ -422,7 +422,7 @@ struct amdgpu_vm {
 	struct ttm_lru_bulk_move lru_bulk_move;
 	/* Flag to indicate if VM is used for compute */
 	bool			is_compute_context;
-
+	bool			is_userq_context;
 	/* Memory partition number, -1 means any partition */
 	int8_t			mem_id;
 
#21 lore raw

RE: [PATCH v4 07/11] drm/amdgpu: add user queue vm identifier

[Public]

Regards,
Prike

Quoted 26 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:52 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 07/11] drm/amdgpu: add user queue vm identifier

Quoted 18 lines · Prike Liang

Add a user queue vm identifier for each userqueue kms opt accessing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 79b263c18eb1..0208c6b8a8e0 100644
@@ -501,6 +501,8 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
    pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
    pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id) · line 505

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+   fpriv->vm.is_userq_context = false;
+

I really don't like that approach. The VM should not have any special handling dependign on if user queues exists or not.

Why do you want that?

This change is using the is_userq_context flag for identifying the user queue task to see whether it has been scheduled, and then further used for handling the sequence of attaching the eviction fence . The eviction fence is only available when the user queue process is scheduled, but in the amdgpu_gem_object_open(), it always attaches the eviction fence to the BOs regardless of whether the eviction fence is valid or not. Based on this flag, we can simplify the eviction fence attaching sequence and only attach a valid eviction fence to the BO.

Quoted 47 lines

Regards,
Christian.

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
    return r;
 }
 
@@ -535,6 +537,8 @@ 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 · line 537

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
            (args->in.flags &

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_init(struct amdgpu_device *adev,

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
            AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
 
+   fpriv->vm.is_userq_context = true;
+
    r = amdgpu_userq_priority_permit(filp, priority);
    if (r)
            return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 3911c78f8282..5e075e8f0ca3 100644
@@ -2560,7 +2560,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev,

struct amdgpu_vm *vm,

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
    ttm_lru_bulk_move_init(&vm->lru_bulk_move);
 
    vm->is_compute_context = false;
-
+   vm->is_userq_context = false;
    vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
                                AMDGPU_VM_USE_CPU_FOR_GFX);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index f3ad687125ad..1e3c0f85c98e 100644
@@ -422,7 +422,7 @@ struct amdgpu_vm {
    struct ttm_lru_bulk_move lru_bulk_move;
    /* Flag to indicate if VM is used for compute */
    bool                    is_compute_context;
-
+   bool                    is_userq_context;
    /* Memory partition number, -1 means any partition */
    int8_t                  mem_id;
 
maintainer
#22 lore raw

Re: [PATCH v4 07/11] drm/amdgpu: add user queue vm identifier

Quoted 37 lines · Prike

[Public]

Regards,
Prike

Quoted 32 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:52 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 07/11] drm/amdgpu: add user queue vm identifier

Quoted 15 lines · Prike Liang

Add a user queue vm identifier for each userqueue kms opt accessing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 79b263c18eb1..0208c6b8a8e0 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id) · line 505

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
@@ -501,6 +501,8 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
    pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
    pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
+   fpriv->vm.is_userq_context = false;
+

I really don't like that approach. The VM should not have any special handling dependign on if user queues exists or not.

Quoted 5 lines
Quoted 1 line

Why do you want that?

This change is using the is_userq_context flag for identifying the user queue task to see whether it has been scheduled, and then further used for handling the sequence of attaching the eviction fence . The eviction fence is only available when the user queue process is scheduled, but in the amdgpu_gem_object_open(), it always attaches the eviction fence to the BOs regardless of whether the eviction fence is valid or not.

That is intentionally done this way.

Quoted 2 lines

Based on this flag, we can simplify the eviction fence attaching sequence and only attach a valid eviction fence to the BO.

That won't work and is over all unecessary. The eviction fence is a no-op as long as no user queues are created, but should always be attached.

What you do here sounds like it is racy as hell. Please completely drop that approach.

Regards,
Christian.

Quoted 49 lines
Quoted 48 lines

Regards,
Christian.

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
    return r;
 }
 
@@ -535,6 +537,8 @@ 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 · line 537

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
            (args->in.flags &

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_init(struct amdgpu_device *adev,

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
            AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
 
+   fpriv->vm.is_userq_context = true;
+
    r = amdgpu_userq_priority_permit(filp, priority);
    if (r)
            return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 3911c78f8282..5e075e8f0ca3 100644
@@ -2560,7 +2560,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev,

struct amdgpu_vm *vm,

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
    ttm_lru_bulk_move_init(&vm->lru_bulk_move);
 
    vm->is_compute_context = false;
-
+   vm->is_userq_context = false;
    vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
                                AMDGPU_VM_USE_CPU_FOR_GFX);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index f3ad687125ad..1e3c0f85c98e 100644
@@ -422,7 +422,7 @@ struct amdgpu_vm {
    struct ttm_lru_bulk_move lru_bulk_move;
    /* Flag to indicate if VM is used for compute */
    bool                    is_compute_context;
-
+   bool                    is_userq_context;
    /* Memory partition number, -1 means any partition */
    int8_t                  mem_id;
 
#23 lore raw

PATCH v4 8/11 drm/amdgpu: add userq va unmap validated helper

In reply to Prike Liang

This helper can validate the userq whether can be
unmapped prior to the userq VA GEM unmap.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 57 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  3 ++
 2 files changed, 60 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +57 -0
index 0208c6b8a8e0..534a9c98c011 100644
@@ -1150,3 +1150,60 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
 }
+
+static bool amdgpu_userq_gem_va_unmap_queue_retrieve(struct amdgpu_usermode_queue *queue,
+							uint64_t va)
+{
+	if (amdgpu_userq_va_align(queue->queue_va) != va &&
+	    amdgpu_userq_va_align(queue->wptr_va) != va &&
+	    amdgpu_userq_va_align(queue->rptr_va) != va &&
+	    amdgpu_userq_va_align(queue->eop_va) != va &&
+	    amdgpu_userq_va_align(queue->shadow_va) != va &&
+	    amdgpu_userq_va_align(queue->csa_va) != va)
+		return false;
+
+	return true;
+}
+
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_vm *vm,
+				uint64_t va)
+{
+	struct amdgpu_fpriv *fpriv = vm_to_fpriv(vm);
+	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+
+	if (vm->is_userq_context && &fpriv->vm == vm) {
+		struct amdgpu_usermode_queue *queue;
+		int queue_id, r = 0;
+
+		if (mutex_trylock(&uq_mgr->userq_mutex)) {
+			/* If here the userq bo is busy and needs to deactivate and prevent reusing it.*/
+			idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
+				struct dma_fence *f = queue->last_fence;
+
+
+				if (!amdgpu_userq_gem_va_unmap_queue_retrieve(queue, va)) {
+					dev_dbg(uq_mgr->adev->dev, "queue(id:%d) not belond to vm:%p\n",
+						queue_id,vm);
+					continue;
+				}
+
+				if (f && !dma_fence_is_signaled(f)) {
+
+					dev_warn(uq_mgr->adev->dev, "try to unmap the busy queue(id:%d):%p under vm:%p\n",
+						queue_id, queue, vm);
+					/* Need to set a resonable state for avoiding reusing this queue*/
+					queue->state = AMDGPU_USERQ_STATE_HUNG;
+					r++;
+				}
+			}
+			mutex_unlock(&uq_mgr->userq_mutex);
+			return r;
+		} else {
+			/* do we need a try lock again before return*/
+			return -EBUSY;
+		}
+
+	}
+
+	return 0;
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -0
index 194ec7a6b3b2..08c49d738ec1 100644
@@ -31,6 +31,7 @@
 #define to_ev_fence(f) container_of(f, struct amdgpu_eviction_fence, base)
 #define uq_mgr_to_fpriv(u) container_of(u, struct amdgpu_fpriv, userq_mgr)
 #define work_to_uq_mgr(w, name) container_of(w, struct amdgpu_userq_mgr, name)
+#define vm_to_fpriv(v)  container_of(v, struct amdgpu_fpriv, vm)
 
 enum amdgpu_userq_state {
 	AMDGPU_USERQ_STATE_UNMAPPED = 0,
@@ -148,4 +149,6 @@ bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
 int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr);
 int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
 			struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_vm *vm,
+				uint64_t va);
 #endif
Signature · 1 line
2.34.1
#24 lore raw

PATCH v4 9/11 drm/amdgpu: validate userq activity status for GEM_VA unmap

In reply to Prike Liang

The userq VA unmap requires validating queue status before unamapping it, if user tries to unmap a busy userq by GEM VA IOCTL then the driver should report an error for this illegal usage.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  8 ++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -3
index 534a9c98c011..5243512c9cec 100644
@@ -243,7 +243,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)
 {
@@ -252,10 +252,14 @@ amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
 
 	if (f && !dma_fence_is_signaled(f)) {
 		ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
-		if (ret <= 0)
+		if (ret <= 0) {
 			drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
 				     f->context, f->seqno);
+			return -ETIMEDOUT;
+		}
 	}
+
+	return 0;
 }
 
 static void
@@ -471,7 +475,13 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		mutex_unlock(&uq_mgr->userq_mutex);
 		return -EINVAL;
 	}
-	amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
+
+	if (amdgpu_userq_wait_for_last_fence(uq_mgr, queue)) {
+		drm_warn(adev_to_drm(uq_mgr->adev), "Don't destroy a busy userq\n");
+		mutex_unlock(&uq_mgr->userq_mutex);
+		return -EINVAL;
+	}
+
 	r = amdgpu_bo_reserve(queue->db_obj.obj, true);
 	if (!r) {
 		amdgpu_bo_unpin(queue->db_obj.obj);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8 -0
index 5e075e8f0ca3..afc493f43256 100644
@@ -1929,6 +1929,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;
 
@@ -1949,6 +1950,13 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 			return -ENOENT;
 	}
 
+	/* It's unlike to happen that the mapping userq hasn't been idled
+	 * during user unmap vm except the vm unmaped forcely from user space.
+	 */
+	r = amdgpu_userq_gem_va_unmap_validate(vm, saddr);
+	if (unlikely(r && r != -EBUSY))
+		dev_warn(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
#25 lore raw

Re: [PATCH v4 09/11] drm/amdgpu: validate userq activity status for GEM_VA unmap

Quoted 41 lines · Prike Liang

The userq VA unmap requires validating queue status before unamapping it, if user tries to unmap a busy userq by GEM VA IOCTL then the driver should report an error for this illegal usage.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  8 ++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -2
index 534a9c98c011..5243512c9cec 100644
@@ -243,7 +243,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)
 {
@@ -252,10 +252,14 @@ amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
 
        if (f && !dma_fence_is_signaled(f)) {
                ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));
-               if (ret <= 0)
+               if (ret <= 0) {
                        drm_file_err(uq_mgr->file, "Timed out waiting for fence=%llu:%llu\n",
                                     f->context, f->seqno);
+                       return -ETIMEDOUT;
+               }
        }
+
+       return 0;
 }
 
 static void

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id) · line 482

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
@@ -471,7 +475,13 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
                mutex_unlock(&uq_mgr->userq_mutex);
                return -EINVAL;
        }
-       amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
+
+       if (amdgpu_userq_wait_for_last_fence(uq_mgr, queue)) {
+               drm_warn(adev_to_drm(uq_mgr->adev), "Don't destroy a busy userq\n");
+               mutex_unlock(&uq_mgr->userq_mutex);
+               return -EINVAL;

This is a memory leak. I think we want to destroy a busy queue if the fence doesn't signal. If it doesn't signal, we should unmap/reset the queue and destroy it.

Alex

Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+       }
+
        r = amdgpu_bo_reserve(queue->db_obj.obj, true);
        if (!r) {
                amdgpu_bo_unpin(queue->db_obj.obj);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8 -1
index 5e075e8f0ca3..afc493f43256 100644
@@ -1929,6 +1929,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;
 
@@ -1949,6 +1950,13 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
                        return -ENOENT;
        }
 
+       /* It's unlike to happen that the mapping userq hasn't been idled
+        * during user unmap vm except the vm unmaped forcely from user space.
+        */
+       r = amdgpu_userq_gem_va_unmap_validate(vm, saddr);
+       if (unlikely(r && r != -EBUSY))
+               dev_warn(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;
--

2.34.1

#26 lore raw

PATCH v4 10/11 drm/amdgpu: only bound the eviction fence to userq bo

In reply to Prike Liang

It doesn't need to attach the invalid eviction fence to non-userq's BOs.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9 -7
index 2c68118fe9fd..50356aa23a99 100644
@@ -294,12 +294,14 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
 	else
 		++bo_va->ref_count;
 
-	/* attach gfx eviction fence */
-	r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
-	if (r) {
-		DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
-		amdgpu_bo_unreserve(abo);
-		return r;
+	/* attach gfx eviction fence when userq created*/
+	if (vm->is_userq_context) {
+		r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
+		if (r) {
+			DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
+			amdgpu_bo_unreserve(abo);
+			return r;
+		}
 	}
 
 	amdgpu_bo_unreserve(abo);
@@ -365,7 +367,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 			goto out_unlock;
 	}
 
-	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
+	if (!amdgpu_vm_is_bo_always_valid(vm, bo) && vm->is_compute_context)
 		amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
 
 	bo_va = amdgpu_vm_bo_find(vm, bo);
Signature · 1 line
2.34.1
#27 lore raw

Re: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

Quoted 35 lines · Prike Liang

It doesn't need to attach the invalid eviction fence to non-userq's BOs.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +8 -6
index 2c68118fe9fd..50356aa23a99 100644
@@ -294,12 +294,14 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
        else
                ++bo_va->ref_count;
 
-       /* attach gfx eviction fence */
-       r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
-       if (r) {
-               DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
-               amdgpu_bo_unreserve(abo);
-               return r;
+       /* attach gfx eviction fence when userq created*/
+       if (vm->is_userq_context) {
+               r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
+               if (r) {
+                       DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
+                       amdgpu_bo_unreserve(abo);
+                       return r;
+               }
        }
 
        amdgpu_bo_unreserve(abo);

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static void amdgpu_gem_object_close(struct drm_gem_object *obj, · line 370

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
@@ -365,7 +367,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
                        goto out_unlock;
        }
 
-       if (!amdgpu_vm_is_bo_always_valid(vm, bo))
+       if (!amdgpu_vm_is_bo_always_valid(vm, bo) && vm->is_compute_context)

Did you mean vm->is_userq_context here?

Alex

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -1
                amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
 
        bo_va = amdgpu_vm_bo_find(vm, bo);
--

2.34.1

#28 lore raw

RE: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

[Public]

Quoted 47 lines

From: Alex Deucher <[email protected]>
Sent: Wednesday, June 25, 2025 1:06 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static int amdgpu_gem_object_open(struct

Quoted 15 lines · Prike Liang

It doesn't need to attach the invalid eviction fence to non-userq's BOs.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
index 2c68118fe9fd..50356aa23a99 100644
@@ -294,12 +294,14 @@ static int amdgpu_gem_object_open(struct

drm_gem_object *obj,

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static void amdgpu_gem_object_close(struct

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +8 -6
        else
                ++bo_va->ref_count;
 
-       /* attach gfx eviction fence */
-       r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
-       if (r) {
-               DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
-               amdgpu_bo_unreserve(abo);
-               return r;
+       /* attach gfx eviction fence when userq created*/
+       if (vm->is_userq_context) {
+               r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
+               if (r) {
+                       DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
+                       amdgpu_bo_unreserve(abo);
+                       return r;
+               }
        }
 
        amdgpu_bo_unreserve(abo);
@@ -365,7 +367,7 @@ static void amdgpu_gem_object_close(struct

drm_gem_object *obj,

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static void amdgpu_gem_object_close(struct drm_gem_object *obj,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
                        goto out_unlock;
        }
 
-       if (!amdgpu_vm_is_bo_always_valid(vm, bo))
+       if (!amdgpu_vm_is_bo_always_valid(vm, bo) &&
+ vm->is_compute_context)

Did you mean vm->is_userq_context here?

Yes, it's a typo and will correct it in the later patch.

Quoted 9 lines

Alex

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -1
                amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
 
        bo_va = amdgpu_vm_bo_find(vm, bo);
--

2.34.1

maintainer
#29 lore raw

Re: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

In reply to Prike Liang
Quoted 2 lines · Prike Liang

It doesn't need to attach the invalid eviction fence to non-userq's BOs.

Clear NAK to this.

BOs are often created before the userqs are created.

Regards,
Christian.

Quoted 40 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9 -7
index 2c68118fe9fd..50356aa23a99 100644
@@ -294,12 +294,14 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
 	else
 		++bo_va->ref_count;
 
-	/* attach gfx eviction fence */
-	r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
-	if (r) {
-		DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
-		amdgpu_bo_unreserve(abo);
-		return r;
+	/* attach gfx eviction fence when userq created*/
+	if (vm->is_userq_context) {
+		r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
+		if (r) {
+			DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
+			amdgpu_bo_unreserve(abo);
+			return r;
+		}
 	}
 
 	amdgpu_bo_unreserve(abo);
@@ -365,7 +367,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 			goto out_unlock;
 	}
 
-	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
+	if (!amdgpu_vm_is_bo_always_valid(vm, bo) && vm->is_compute_context)
 		amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
 
 	bo_va = amdgpu_vm_bo_find(vm, bo);
#30 lore raw

RE: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

[Public]

Regards,
Prike

Quoted 8 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:50 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

Quoted 7 lines
Quoted 2 lines · Prike Liang

It doesn't need to attach the invalid eviction fence to non-userq's BOs.

Clear NAK to this.

BOs are often created before the userqs are created.

Yes, but the eviction fence will only be available when the userq task is scheduled. Before requesting the userq-create IOCTL, the eviction fence is invalid and hasn't been created. In this case, do we need to attach an invalid eviction fence to the BO?

Quoted 47 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static int amdgpu_gem_object_open(struct

Quoted 12 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
index 2c68118fe9fd..50356aa23a99 100644
@@ -294,12 +294,14 @@ static int amdgpu_gem_object_open(struct

drm_gem_object *obj,

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -1
    else
            ++bo_va->ref_count;
 
-   /* attach gfx eviction fence */

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static int amdgpu_gem_object_open(struct drm_gem_object *obj,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +5 -5
-   r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
-   if (r) {
-           DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
-           amdgpu_bo_unreserve(abo);
-           return r;
+   /* attach gfx eviction fence when userq created*/
+   if (vm->is_userq_context) {
+           r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
+           if (r) {
+                   DRM_DEBUG_DRIVER("Failed to attach eviction fence to

BO\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static void amdgpu_gem_object_close(struct

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
+                   amdgpu_bo_unreserve(abo);
+                   return r;
+           }
    }
 
    amdgpu_bo_unreserve(abo);
@@ -365,7 +367,7 @@ static void amdgpu_gem_object_close(struct

drm_gem_object *obj,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
                    goto out_unlock;
    }
 
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo))
+   if (!amdgpu_vm_is_bo_always_valid(vm, bo) && vm->is_compute_context)
            amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
 
    bo_va = amdgpu_vm_bo_find(vm, bo);
maintainer
#31 lore raw

Re: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

Quoted 17 lines · Prike

[Public]

Regards,
Prike

Quoted 12 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, June 25, 2025 3:50 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v4 10/11] drm/amdgpu: only bound the eviction fence to userq bo

Quoted 2 lines · Prike Liang

It doesn't need to attach the invalid eviction fence to non-userq's BOs.

Quoted 6 lines
Quoted 3 lines

Clear NAK to this.

BOs are often created before the userqs are created.

Yes, but the eviction fence will only be available when the userq task is scheduled. Before requesting the userq-create IOCTL, the eviction fence is invalid and hasn't been created. In this case, do we need to attach an invalid eviction fence to the BO?

Yes, we do. Otherwise the whole approach is racy like hell.

Please don't change that in anyway.

Regards,
Christian.

Quoted 49 lines
Quoted 48 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static int amdgpu_gem_object_open(struct

Quoted 12 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
index 2c68118fe9fd..50356aa23a99 100644
@@ -294,12 +294,14 @@ static int amdgpu_gem_object_open(struct

drm_gem_object *obj,

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -1
    else
            ++bo_va->ref_count;
 
-   /* attach gfx eviction fence */

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static int amdgpu_gem_object_open(struct drm_gem_object *obj,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +5 -5
-   r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
-   if (r) {
-           DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
-           amdgpu_bo_unreserve(abo);
-           return r;
+   /* attach gfx eviction fence when userq created*/
+   if (vm->is_userq_context) {
+           r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
+           if (r) {
+                   DRM_DEBUG_DRIVER("Failed to attach eviction fence to

BO\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static void amdgpu_gem_object_close(struct

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
+                   amdgpu_bo_unreserve(abo);
+                   return r;
+           }
    }
 
    amdgpu_bo_unreserve(abo);
@@ -365,7 +367,7 @@ static void amdgpu_gem_object_close(struct

drm_gem_object *obj,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
                    goto out_unlock;
    }
 
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo))
+   if (!amdgpu_vm_is_bo_always_valid(vm, bo) && vm->is_compute_context)
            amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
 
    bo_va = amdgpu_vm_bo_find(vm, bo);
#32 lore raw

PATCH v4 11/11 drm/amdgpu: clean up the amdgpu_userq_active()

In reply to Prike Liang

This is no invocation for amdgpu_userq_active().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 ----------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  2 --
 2 files changed, 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -16
index 5243512c9cec..d5d95864d0d2 100644
@@ -276,22 +276,6 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
 	kfree(queue);
 }
 
-int
-amdgpu_userq_active(struct amdgpu_userq_mgr *uq_mgr)
-{
-	struct amdgpu_usermode_queue *queue;
-	int queue_id;
-	int ret = 0;
-
-	mutex_lock(&uq_mgr->userq_mutex);
-	/* Resume all the queues for this process */
-	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id)
-		ret += queue->state == AMDGPU_USERQ_STATE_MAPPED;
-
-	mutex_unlock(&uq_mgr->userq_mutex);
-	return ret;
-}
-
 static struct amdgpu_usermode_queue *
 amdgpu_userq_find(struct amdgpu_userq_mgr *uq_mgr, int qid)
 {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -2
index 08c49d738ec1..687583688c6e 100644
@@ -121,8 +121,6 @@ void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
 void amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 			struct amdgpu_eviction_fence *ev_fence);
 
-int amdgpu_userq_active(struct amdgpu_userq_mgr *uq_mgr);
-
 void amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *userq_mgr,
 				  struct amdgpu_eviction_fence_mgr *evf_mgr);
 
Signature · 1 line
2.34.1
Reviewed-by on v4 1/11
#33 lore raw
In reply to Prike Liang
Quoted 6 lines · Prike Liang

This will help on validating the userq input args, and rejecting for the invalid userq request at the IOCTLs first place.

Quoted 144 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 81 +++++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |  7 --
 2 files changed, 56 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +56 -25
index 295e7186e156..7f9dfeae4322 100644
@@ -359,27 +359,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
                (args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>
                AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;
 
-       /* Usermode queues are only supported for GFX IP as of now */
-       if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
-           args->in.ip_type != AMDGPU_HW_IP_DMA &&
-           args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
-               drm_file_err(uq_mgr->file, "Usermode queue doesn't support IP type %u\n",
-                            args->in.ip_type);
-               return -EINVAL;
-       }
-
        r = amdgpu_userq_priority_permit(filp, priority);
        if (r)
                return r;
 
-       if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
-           (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
-           (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
-           !amdgpu_is_tmz(adev)) {
-               drm_file_err(uq_mgr->file, "Secure only supported on GFX/Compute queues\n");
-               return -EINVAL;
-       }
-
        r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
        if (r < 0) {
                drm_file_err(uq_mgr->file, "pm_runtime_get_sync() failed for userqueue create\n");
@@ -485,22 +468,45 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
        return r;
 }
 
-int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
-                      struct drm_file *filp)
+static int amdgpu_userq_input_args_validate(struct drm_device *dev,
+                                       union drm_amdgpu_userq *args,
+                                       struct drm_file *filp)
 {
-       union drm_amdgpu_userq *args = data;
-       int r;
+       struct amdgpu_device *adev = drm_to_adev(dev);
 
        switch (args->in.op) {
        case AMDGPU_USERQ_OP_CREATE:
                if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
                                       AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))
                        return -EINVAL;
-               r = amdgpu_userq_create(filp, args);
-               if (r)
-                       drm_file_err(filp, "Failed to create usermode queue\n");
-               break;
+               /* Usermode queues are only supported for GFX IP as of now */
+               if (args->in.ip_type != AMDGPU_HW_IP_GFX &&
+                   args->in.ip_type != AMDGPU_HW_IP_DMA &&
+                   args->in.ip_type != AMDGPU_HW_IP_COMPUTE) {
+                       drm_file_err(filp, "Usermode queue doesn't support IP type %u\n",
+                                    args->in.ip_type);
+                       return -EINVAL;
+               }
+
+               if ((args->in.flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&
+                   (args->in.ip_type != AMDGPU_HW_IP_GFX) &&
+                   (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) &&
+                   !amdgpu_is_tmz(adev)) {
+                       drm_file_err(filp, "Secure only supported on GFX/Compute queues\n");
+                       return -EINVAL;
+               }
 
+               if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
+                   args->in.queue_va == 0 ||
+                   args->in.queue_size == 0) {
+                       drm_file_err(filp, "invalidate userq queue va or size\n");
+                       return -EINVAL;
+               }
+               if (!args->in.wptr_va || !args->in.rptr_va) {
+                       drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
+                       return -EINVAL;
+               }
+               break;
        case AMDGPU_USERQ_OP_FREE:
                if (args->in.ip_type ||
                    args->in.doorbell_handle ||
@@ -514,6 +520,31 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
                    args->in.mqd ||
                    args->in.mqd_size)
                        return -EINVAL;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+       union drm_amdgpu_userq *args = data;
+       int r;
+
+       if (amdgpu_userq_input_args_validate(dev, args, filp) < 0)
+               return -EINVAL;
+
+       switch (args->in.op) {
+       case AMDGPU_USERQ_OP_CREATE:
+               r = amdgpu_userq_create(filp, args);
+               if (r)
+                       drm_file_err(filp, "Failed to create usermode queue\n");
+               break;
+
+       case AMDGPU_USERQ_OP_FREE:
                r = amdgpu_userq_destroy(filp, args->in.queue_id);
                if (r)
                        drm_file_err(filp, "Failed to destroy usermode queue\n");
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -8
index d6f50b13e2ba..1457fb49a794 100644
@@ -215,13 +215,6 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                return -ENOMEM;
        }
 
-       if (!mqd_user->wptr_va || !mqd_user->rptr_va ||
-           !mqd_user->queue_va || mqd_user->queue_size == 0) {
-               DRM_ERROR("Invalid MQD parameters for userqueue\n");
-               r = -EINVAL;
-               goto free_props;
-       }
-
        r = amdgpu_userq_create_object(uq_mgr, &queue->mqd, mqd_hw_default->mqd_size);
        if (r) {
                DRM_ERROR("Failed to create MQD object for userqueue\n");
--

2.34.1