Skip to contentSkip to conversation

drm/amdgpu: validate userq input args

PATCH Superseded Prike Liang 4 participants 31 messages May 30, 2025 – Jun 6, 2025

Subsystems (2)

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/9 drm/amdgpu: validate userq input args

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 80 +++++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |  7 --
 2 files changed, 55 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +55 -25
index 295e7186e156..f67969312c39 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,44 @@ 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_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 +519,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 2/9 drm/amdgpu: bail out for userq unmap error

Before destroy the userq buffer object requires validating the userq unmap status.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
index f67969312c39..8eea0e1e1b6a 100644
@@ -319,6 +319,12 @@ 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);
+	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
+		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue for destroying.\n");
+		mutex_unlock(&uq_mgr->userq_mutex);
+		/*TODO: before return may need to a reset*/
+		return r;
+	}
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/9] drm/amdgpu: bail out for userq unmap error

Quoted 14 lines · Prike Liang

Before destroy the userq buffer object requires validating the userq unmap status.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index f67969312c39..8eea0e1e1b6a 100644

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
@@ -319,6 +319,12 @@ 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);
+       if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
+               drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue for destroying.\n");
+               mutex_unlock(&uq_mgr->userq_mutex);
+               /*TODO: before return may need to a reset*/
+               return r;

If we return early here, we'll leak memory. Presumably if the unmap failed, the queue is hung, so it shouldn't cause any problems.

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
+       }
        amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
        mutex_unlock(&uq_mgr->userq_mutex);
 
--

2.34.1

#3 lore raw

RE: [PATCH 2/9] drm/amdgpu: bail out for userq unmap error

[Public]

Quoted 30 lines

From: Alex Deucher <[email protected]>
Sent: Saturday, May 31, 2025 5:34 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 2/9] drm/amdgpu: bail out for userq unmap error

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

Quoted 15 lines · Prike Liang

Before destroy the userq buffer object requires validating the userq unmap status.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index f67969312c39..8eea0e1e1b6a 100644
@@ -319,6 +319,12 @@ 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 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
        }
        amdgpu_bo_unref(&queue->db_obj.obj);
        r = amdgpu_userq_unmap_helper(uq_mgr, queue);
+       if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
+               drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue
Quoted 7 lines

for destroying.\n");

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

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
+               mutex_unlock(&uq_mgr->userq_mutex);
+               /*TODO: before return may need to a reset*/
+               return r;

If we return early here, we'll leak memory. Presumably if the unmap failed, the queue is hung, so it shouldn't cause any problems.

[Prike] Yeah, maybe it only requires an aware here and then continue destroying the userq software resources. Do we need to reset the queue when unmap fails during userq destroy?

Quoted 9 lines

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
+       }
        amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
        mutex_unlock(&uq_mgr->userq_mutex);
 
--

2.34.1

#4 lore raw

Re: [PATCH 2/9] drm/amdgpu: bail out for userq unmap error

Quoted 38 lines · Prike

[Public]

Quoted 35 lines

From: Alex Deucher <[email protected]>
Sent: Saturday, May 31, 2025 5:34 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 2/9] drm/amdgpu: bail out for userq unmap error

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

Quoted 15 lines · Prike Liang

Before destroy the userq buffer object requires validating the userq unmap status.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index f67969312c39..8eea0e1e1b6a 100644
@@ -319,6 +319,12 @@ 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 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
        }
        amdgpu_bo_unref(&queue->db_obj.obj);
        r = amdgpu_userq_unmap_helper(uq_mgr, queue);
+       if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
+               drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue

for destroying.\n");

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
+               mutex_unlock(&uq_mgr->userq_mutex);
+               /*TODO: before return may need to a reset*/
+               return r;
Quoted 5 lines
Quoted 2 lines

If we return early here, we'll leak memory. Presumably if the unmap failed, the queue is hung, so it shouldn't cause any problems.

[Prike] Yeah, maybe it only requires an aware here and then continue destroying the userq software resources. Do we need to reset the queue when unmap fails during userq destroy?

Probably. Otherwise I suspect the next time the MES tries to use that queue it will already be hung.

Alex

Quoted 10 lines
Quoted 10 lines

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
+       }
        amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
        mutex_unlock(&uq_mgr->userq_mutex);
 
--

2.34.1

#5 lore raw

PATCH 3/9 drm/amdgpu: wait for the user objects done before destroying

In reply to Prike Liang

The userq buffer should complete its attached work before being destroyed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -1
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr,
 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
 				 struct amdgpu_userq_obj *userq_obj)
 {
+	amdgpu_bo_sync_wait(userq_obj->obj, AMDGPU_FENCE_OWNER_UNDEFINED,
+			false);
 	amdgpu_bo_kunmap(userq_obj->obj);
 	amdgpu_bo_unref(&userq_obj->obj);
 }
@@ -317,7 +319,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);
 	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
 		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue for destroying.\n");
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
index 1457fb49a794..b46e67b179fc 100644
@@ -336,6 +336,9 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr,
 		      struct amdgpu_usermode_queue *queue)
 {
 	amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+	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
#6 lore raw

Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 3 lines · Prike Liang

The userq buffer should complete its attached work before being destroyed.

I don't think that is possible. The user may not have a fence associated with the queue. If the user destroys the queue, we should just unmap it and clean up.

Alex

Quoted 45 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -1
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr,
 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
                                 struct amdgpu_userq_obj *userq_obj)
 {
+       amdgpu_bo_sync_wait(userq_obj->obj, AMDGPU_FENCE_OWNER_UNDEFINED,
+                       false);
        amdgpu_bo_kunmap(userq_obj->obj);
        amdgpu_bo_unref(&userq_obj->obj);
 }
@@ -317,7 +319,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);
        if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
                drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue for destroying.\n");
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -1
index 1457fb49a794..b46e67b179fc 100644
@@ -336,6 +336,9 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr,
                      struct amdgpu_usermode_queue *queue)
 {
        amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+       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);
 }
--

2.34.1

#7 lore raw

RE: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

[Public]

Quoted 9 lines

From: Alex Deucher <[email protected]>
Sent: Saturday, May 31, 2025 5:36 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 7 lines
Quoted 3 lines · Prike Liang

The userq buffer should complete its attached work before being destroyed.

I don't think that is possible. The user may not have a fence associated with the queue. If the user destroys the queue, we should just unmap it and clean up.

[Prike] The user queue buffer object may add the eviction fence and page table update job scheduled fence, and by dumping the queue object reservation, it shows the fences are attached well. In most cases, the queue object fences are signaled at the user queue destroy phase, but somehow there is sometimes an attached fence without being signaled prior to destroying the queue. In this case, do we still not wait or test that the buffer object's fences are signaled before trying to destroy it?

Quoted 50 lines

Alex

Quoted 13 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct
Quoted 1 line

amdgpu_userq_mgr *uq_mgr, void amdgpu_userq_destroy_object(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 3 lines

struct amdgpu_userq_obj *userq_obj)
{
+ amdgpu_bo_sync_wait(userq_obj->obj,

AMDGPU_FENCE_OWNER_UNDEFINED,

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

Quoted 18 lines
+                       false);
        amdgpu_bo_kunmap(userq_obj->obj);
        amdgpu_bo_unref(&userq_obj->obj);  } @@ -317,7 +319,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);
        if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
                drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap

the queue for destroying.\n"); diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 1457fb49a794..b46e67b179fc 100644

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

*uq_mgr,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -1
                      struct amdgpu_usermode_queue *queue)  {
        amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+       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);  }
--

2.34.1

maintainer
#8 lore raw

Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

In reply to Prike Liang
Quoted 2 lines · Prike Liang

The userq buffer should complete its attached work before being destroyed.

That doesn't make sense at all. We should wait for the requested signal fences, but apart from that it is the responsibility for userspace to make sure that queues are only destroyed after they are idle.

If the kernel does this it should *never* wait for anything ongoing, it should only forcefully unmap the queue and destroy it.

Regards,
Christian.

Quoted 42 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -1
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr,
 void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
 				 struct amdgpu_userq_obj *userq_obj)
 {
+	amdgpu_bo_sync_wait(userq_obj->obj, AMDGPU_FENCE_OWNER_UNDEFINED,
+			false);
 	amdgpu_bo_kunmap(userq_obj->obj);
 	amdgpu_bo_unref(&userq_obj->obj);
 }
@@ -317,7 +319,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);
 	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
 		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue for destroying.\n");
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
index 1457fb49a794..b46e67b179fc 100644
@@ -336,6 +336,9 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr,
 		      struct amdgpu_usermode_queue *queue)
 {
 	amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+	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);
 }
#9 lore raw

RE: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

[Public]

Quoted 13 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, June 2, 2025 7:05 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 2 lines · Prike Liang

The userq buffer should complete its attached work before being destroyed.

Quoted 6 lines

That doesn't make sense at all. We should wait for the requested signal fences, but apart from that it is the responsibility for userspace to make sure that queues are only destroyed after they are idle.

If the kernel does this it should *never* wait for anything ongoing, it should only forcefully unmap the queue and destroy it.

[Prike] Yeah, generally, the userq destroy IOCTL request should be performed after Mesa tears the window context down by executing amdgpu_winsys_destroy().
But if there's an improper case where kernel tries to destroy the userq BOs attached fences somehow haven't signaled, then it's safe to force destroy the userq buffer object with ongoing work?

Quoted 49 lines

Regards,
Christian.

Quoted 13 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct
Quoted 1 line

amdgpu_userq_mgr *uq_mgr, void amdgpu_userq_destroy_object(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 2 lines
                             struct amdgpu_userq_obj *userq_obj)  {

+ amdgpu_bo_sync_wait(userq_obj->obj,

AMDGPU_FENCE_OWNER_UNDEFINED,

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

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
+                   false);
    amdgpu_bo_kunmap(userq_obj->obj);
    amdgpu_bo_unref(&userq_obj->obj);
 }
@@ -317,7 +319,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);
    if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
            drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the

queue

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

Quoted 7 lines

for destroying.\n"); diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 1457fb49a794..b46e67b179fc 100644

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

*uq_mgr,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
                  struct amdgpu_usermode_queue *queue)  {
    amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+   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);  }
maintainer
#10 lore raw

Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 15 lines · Prike

[Public]

Quoted 13 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, June 2, 2025 7:05 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 2 lines · Prike Liang

The userq buffer should complete its attached work before being destroyed.

Quoted 8 lines
Quoted 6 lines

That doesn't make sense at all. We should wait for the requested signal fences, but apart from that it is the responsibility for userspace to make sure that queues are only destroyed after they are idle.

If the kernel does this it should *never* wait for anything ongoing, it should only forcefully unmap the queue and destroy it.

[Prike] Yeah, generally, the userq destroy IOCTL request should be performed after Mesa tears the window context down by executing amdgpu_winsys_destroy().
But if there's an improper case where kernel tries to destroy the userq BOs attached fences somehow haven't signaled, then it's safe to force destroy the userq buffer object with ongoing work?

What do you mean with attached fences? We need to wait for the userq fence before destroying the queue, but apart from that it shouldn't matter what fences are attached where.

Regards,
Christian.

Quoted 51 lines
Quoted 50 lines

Regards,
Christian.

Quoted 13 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct
Quoted 1 line

amdgpu_userq_mgr *uq_mgr, void amdgpu_userq_destroy_object(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 2 lines
                             struct amdgpu_userq_obj *userq_obj)  {

+ amdgpu_bo_sync_wait(userq_obj->obj,

AMDGPU_FENCE_OWNER_UNDEFINED,

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

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
+                   false);
    amdgpu_bo_kunmap(userq_obj->obj);
    amdgpu_bo_unref(&userq_obj->obj);
 }
@@ -317,7 +319,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);
    if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
            drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the

queue

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

Quoted 7 lines

for destroying.\n"); diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 1457fb49a794..b46e67b179fc 100644

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

*uq_mgr,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
                  struct amdgpu_usermode_queue *queue)  {
    amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+   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);  }
#11 lore raw

RE: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

[Public]

Quoted 31 lines

From: Koenig, Christian <[email protected]>
Sent: Thursday, June 5, 2025 9:38 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 15 lines · Prike

[Public]

Quoted 13 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, June 2, 2025 7:05 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 2 lines · Prike Liang

The userq buffer should complete its attached work before being destroyed.

Quoted 7 lines
Quoted 6 lines

That doesn't make sense at all. We should wait for the requested signal fences, but apart from that it is the responsibility for userspace to make sure that queues are only destroyed after they are idle.

If the kernel does this it should *never* wait for anything ongoing, it should only forcefully unmap the queue and destroy it.

[Prike] Yeah, generally, the userq destroy IOCTL request should be performed after

Quoted 8 lines

Mesa tears the window context down by executing amdgpu_winsys_destroy().

Quoted 1 line

But if there's an improper case where kernel tries to destroy the userq BOs

attached fences somehow haven't signaled, then it's safe to force destroy the userq buffer object with ongoing work?

What do you mean with attached fences? We need to wait for the userq fence before destroying the queue, but apart from that it shouldn't matter what fences are attached where.

[Prike] The userq destroy function already waits for its last fence using amdgpu_userq_wait_for_last_fence(). I mean, is it necessary to also wait for the reservation fences of the userq's MQD, doorbell, and WPTR buffer objects to be signaled by using amdgpu_bo_sync_wait() before destroying such BOs?

Quoted 54 lines

Regards,
Christian.

Quoted 51 lines
Quoted 50 lines

Regards,
Christian.

Quoted 13 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct
Quoted 1 line

amdgpu_userq_mgr *uq_mgr, void amdgpu_userq_destroy_object(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 2 lines
                             struct amdgpu_userq_obj *userq_obj)  {

+ amdgpu_bo_sync_wait(userq_obj->obj,

AMDGPU_FENCE_OWNER_UNDEFINED,

Quoted 11 lines
+                   false);
    amdgpu_bo_kunmap(userq_obj->obj);
    amdgpu_bo_unref(&userq_obj->obj);  } @@ -317,7 +319,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);
    if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
            drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap
Quoted 1 line

the

queue

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

Quoted 7 lines

for destroying.\n"); diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 1457fb49a794..b46e67b179fc 100644

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

*uq_mgr,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
                  struct amdgpu_usermode_queue *queue)  {
    amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+   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);  }
maintainer
#12 lore raw

Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 38 lines · Prike

[Public]

Quoted 36 lines

From: Koenig, Christian <[email protected]>
Sent: Thursday, June 5, 2025 9:38 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 15 lines · Prike

[Public]

Quoted 13 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, June 2, 2025 7:05 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 3/9] drm/amdgpu: wait for the user objects done before destroying

Quoted 2 lines · Prike Liang

The userq buffer should complete its attached work before being destroyed.

Quoted 7 lines
Quoted 6 lines

That doesn't make sense at all. We should wait for the requested signal fences, but apart from that it is the responsibility for userspace to make sure that queues are only destroyed after they are idle.

If the kernel does this it should *never* wait for anything ongoing, it should only forcefully unmap the queue and destroy it.

[Prike] Yeah, generally, the userq destroy IOCTL request should be performed after

Mesa tears the window context down by executing amdgpu_winsys_destroy().

Quoted 1 line

But if there's an improper case where kernel tries to destroy the userq BOs

attached fences somehow haven't signaled, then it's safe to force destroy the userq buffer object with ongoing work?

Quoted 6 lines
Quoted 3 lines

What do you mean with attached fences? We need to wait for the userq fence before destroying the queue, but apart from that it shouldn't matter what fences are attached where.

[Prike] The userq destroy function already waits for its last fence using amdgpu_userq_wait_for_last_fence(). I mean, is it necessary to also wait for the reservation fences of the userq's MQD, doorbell, and WPTR buffer objects to be signaled by using amdgpu_bo_sync_wait() before destroying such BOs?

No absolutely not.

Regards,
Christian.

Quoted 56 lines
Quoted 55 lines

Regards,
Christian.

Quoted 51 lines
Quoted 50 lines

Regards,
Christian.

Quoted 13 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 3 ++-
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 8eea0e1e1b6a..f45585bd5872 100644
@@ -217,6 +217,8 @@ int amdgpu_userq_create_object(struct
Quoted 1 line

amdgpu_userq_mgr *uq_mgr, void amdgpu_userq_destroy_object(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 2 lines
                             struct amdgpu_userq_obj *userq_obj)  {

+ amdgpu_bo_sync_wait(userq_obj->obj,

AMDGPU_FENCE_OWNER_UNDEFINED,

Quoted 11 lines
+                   false);
    amdgpu_bo_kunmap(userq_obj->obj);
    amdgpu_bo_unref(&userq_obj->obj);  } @@ -317,7 +319,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);
    if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
            drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap
Quoted 1 line

the

queue

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

Quoted 7 lines

for destroying.\n"); diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 1457fb49a794..b46e67b179fc 100644

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

*uq_mgr,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
                  struct amdgpu_usermode_queue *queue)  {
    amdgpu_userq_destroy_object(uq_mgr, &queue->fw_obj);
+   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);  }
#13 lore raw

PATCH 4/9 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  | 56 +++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 +
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 +++++++++-
 3 files changed, 80 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +54 -2
index f45585bd5872..32e88064bdea 100644
@@ -44,6 +44,41 @@ 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 *mapping;
+	u64 user_addr;
+	u64 size;
+	int r;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+	size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+	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 (user_addr != mapping->start ||
+	    (size != 0 && user_addr + size - 1 != mapping->last)) {
+		pr_debug("expected size 0x%llx not equal to mapping addr 0x%llx size 0x%llx\n",
+			expected_size, mapping->start << AMDGPU_GPU_PAGE_SHIFT,
+			(mapping->last - mapping->start + 1) << AMDGPU_GPU_PAGE_SHIFT);
+		goto out_err;
+	}
+	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)
@@ -394,6 +429,17 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		r = -EINVAL;
 		goto unlock;
 	}
+	/* Validate the userq virtual address.
+	 * TODO: Need to firgure out why the queue_size(args->in.queue_size)) doesn't match
+	 * with mesa request size. As a workaround there fix the queue_size to the 0x200000.
+	 */
+	if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, 0x200000) ||
+			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 invalidated\n");
+		r = -EINVAL;
+		goto unlock;
+	}
 
 	queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
 	if (!queue) {
@@ -504,11 +550,17 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,
 		}
 
 		if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
-				args->in.queue_size == 0) {
+				args->in.queue_size == 0 ||
+				!access_ok(u64_to_user_ptr(args->in.queue_va & AMDGPU_GMC_HOLE_MASK),
+					args->in.queue_size)) {
 			drm_file_err(filp, "invalidate userq queue va or size\n");
 			return -EINVAL;
 		}
-		if (!args->in.wptr_va || !args->in.rptr_va) {
+		if (!args->in.wptr_va || !args->in.rptr_va ||
+			!access_ok(u64_to_user_ptr(args->in.wptr_va & AMDGPU_GMC_HOLE_MASK),
+						sizeof(uint64_t)) ||
+			!access_ok(u64_to_user_ptr(args->in.rptr_va & AMDGPU_GMC_HOLE_MASK),
+						sizeof(uint64_t))) {
 			drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
 			return -EINVAL;
 		}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index ec040c2fd6c9..375fba639c94 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 +24 -1
index b46e67b179fc..4ba064e6dda0 100644
@@ -28,6 +28,7 @@
 
 #define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE
 #define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE
+#define MQD_SHADOW_BASE_SIZE      73728
 
 static int
 mes_userq_map_gtt_bo_to_gart(struct amdgpu_bo *bo)
@@ -246,6 +247,19 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			r = -ENOMEM;
 			goto free_mqd;
 		}
+		/*
+		 * EOP VA size = MEC_HPD_SIZE * adev->gfx.num_compute_rings.
+		 * Since the userq support newer than gfx10, so the keep MEC_HPD_SIZE as 2048.
+		 * TODO: It requires figuring out why the EOP VA not match with MEC_HPD_SIZE *
+		 * adev->gfx.num_compute_rings.
+		 */
+		if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd->eop_va,
+						/*ALIGN(2048 * adev->gfx.num_compute_rings, PAGE_SIZE)*/
+						0x200000)) {
+			drm_file_err(uq_mgr->file, "EOP VA is invalidated\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;
@@ -274,6 +288,15 @@ 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;
+
+		/* TODO: Need to check the shadow WA size doesn't match with mesa request.*/
+		if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
+					0x200000)) {
+			drm_file_err(uq_mgr->file, "shadow VA is invalidated\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;
@@ -290,7 +313,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			r = -ENOMEM;
 			goto free_mqd;
 		}
-
+		/* TODO: validate the csa and check the va size as well.*/
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
 	}
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH 4/9] drm/amdgpu: validate userq buffer virtual address and size

Quoted 58 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  | 56 +++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 +
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 +++++++++-
 3 files changed, 80 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +35 -0
index f45585bd5872..32e88064bdea 100644
@@ -44,6 +44,41 @@ 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 *mapping;
+       u64 user_addr;
+       u64 size;
+       int r;
+
+       addr &= AMDGPU_GMC_HOLE_MASK;
+       user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+       size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+       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 (user_addr != mapping->start ||
+           (size != 0 && user_addr + size - 1 != mapping->last)) {
+               pr_debug("expected size 0x%llx not equal to mapping addr 0x%llx size 0x%llx\n",
+                       expected_size, mapping->start << AMDGPU_GPU_PAGE_SHIFT,
+                       (mapping->last - mapping->start + 1) << AMDGPU_GPU_PAGE_SHIFT);
+               goto out_err;
+       }
+       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)

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
@@ -394,6 +429,17 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
                r = -EINVAL;
                goto unlock;
        }
+       /* Validate the userq virtual address.
+        * TODO: Need to firgure out why the queue_size(args->in.queue_size)) doesn't match
+        * with mesa request size. As a workaround there fix the queue_size to the 0x200000.
+        */

We just want to validate that the queue is fully within a valid virtual address range for a single buffer. It doesn't matter if the queue address is at the start. Mesa could allocate a single buffer for all of its user queues and then suballocate ring buffers and other metadata out of the memory.

Alex

Quoted 54 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -2
+       if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, 0x200000) ||
+                       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 invalidated\n");
+               r = -EINVAL;
+               goto unlock;
+       }
 
        queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
        if (!queue) {
@@ -504,11 +550,17 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,
                }
 
                if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
-                               args->in.queue_size == 0) {
+                               args->in.queue_size == 0 ||
+                               !access_ok(u64_to_user_ptr(args->in.queue_va & AMDGPU_GMC_HOLE_MASK),
+                                       args->in.queue_size)) {
                        drm_file_err(filp, "invalidate userq queue va or size\n");
                        return -EINVAL;
                }
-               if (!args->in.wptr_va || !args->in.rptr_va) {
+               if (!args->in.wptr_va || !args->in.rptr_va ||
+                       !access_ok(u64_to_user_ptr(args->in.wptr_va & AMDGPU_GMC_HOLE_MASK),
+                                               sizeof(uint64_t)) ||
+                       !access_ok(u64_to_user_ptr(args->in.rptr_va & AMDGPU_GMC_HOLE_MASK),
+                                               sizeof(uint64_t))) {
                        drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
                        return -EINVAL;
                }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index ec040c2fd6c9..375fba639c94 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 +1 -0
index b46e67b179fc..4ba064e6dda0 100644
@@ -28,6 +28,7 @@
 
 #define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE
 #define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE
+#define MQD_SHADOW_BASE_SIZE      73728
 
 static int
 mes_userq_map_gtt_bo_to_gart(struct amdgpu_bo *bo)

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -0
@@ -246,6 +247,19 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                        r = -ENOMEM;
                        goto free_mqd;
                }
+               /*
+                * EOP VA size = MEC_HPD_SIZE * adev->gfx.num_compute_rings.
+                * Since the userq support newer than gfx10, so the keep MEC_HPD_SIZE as 2048.
+                * TODO: It requires figuring out why the EOP VA not match with MEC_HPD_SIZE *
+                * adev->gfx.num_compute_rings.
+                */

The EOP size is MEC_HPD_SIZE. We only multiply it by adev->gfx.num_compute_rings for kernel queues because we use one allocation for the EOP buffer for all of the kernel queues.

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7 -0
+               if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd->eop_va,
+                                               /*ALIGN(2048 * adev->gfx.num_compute_rings, PAGE_SIZE)*/
+                                               0x200000)) {
+                       drm_file_err(uq_mgr->file, "EOP VA is invalidated\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 amdgpu_userq_mgr *uq_mgr, · line 294

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
@@ -274,6 +288,15 @@ 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;
+
+               /* TODO: Need to check the shadow WA size doesn't match with mesa request.*/
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
+                                       0x200000)) {

The shadow size should >= the shadow size defined in struct amdgpu_gfx_shadow_info and returned by adev->gfx.funcs->get_gfx_shadow_info().

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5 -0
+                       drm_file_err(uq_mgr->file, "shadow VA is invalidated\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;

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
@@ -290,7 +313,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                        r = -ENOMEM;
                        goto free_mqd;
                }
-
+               /* TODO: validate the csa and check the va size as well.*/

See adev->gfx.funcs->get_gfx_shadow_info() here as well.

Alex

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

2.34.1

#15 lore raw

RE: [PATCH 4/9] drm/amdgpu: validate userq buffer virtual address and size

[Public]

Quoted 78 lines

From: Alex Deucher <[email protected]>
Sent: Saturday, May 31, 2025 5:48 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 4/9] drm/amdgpu: validate userq buffer virtual address and size

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

Quoted 17 lines · On Fri, May 30, 2025 at 3:56 AM 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  | 56
+++++++++++++++++++++-  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |
2 +  drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 +++++++++-
 3 files changed, 80 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index f45585bd5872..32e88064bdea 100644
@@ -44,6 +44,41 @@ u32 amdgpu_userq_get_supported_ip_mask(struct

amdgpu_device *adev)

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

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -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 *mapping;
+       u64 user_addr;
+       u64 size;
+       int r;
+
+       addr &= AMDGPU_GMC_HOLE_MASK;
+       user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT,

0x100);

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

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -0
+       size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+       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 (user_addr != mapping->start ||
+           (size != 0 && user_addr + size - 1 != mapping->last)) {
+               pr_debug("expected size 0x%llx not equal to mapping addr 0x%llx size

0x%llx\n",

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+                       expected_size, mapping->start << AMDGPU_GPU_PAGE_SHIFT,
+                       (mapping->last - mapping->start + 1) <<

AMDGPU_GPU_PAGE_SHIFT);

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+               goto out_err;
+       }
+       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 +6 -1
+
+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) @@
-394,6 +429,17 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

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

Quoted 5 lines
                r = -EINVAL;
                goto unlock;
        }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+       /* Validate the userq virtual address.
+        * TODO: Need to firgure out why the queue_size(args->in.queue_size))

doesn't match

Quoted 8 lines
Quoted 1 line

+ * with mesa request size. As a workaround there fix the queue_size to the

0x200000.

Quoted 1 line

+ */

We just want to validate that the queue is fully within a valid virtual address range for a single buffer. It doesn't matter if the queue address is at the start. Mesa could allocate a single buffer for all of its user queues and then suballocate ring buffers and other metadata out of the memory.

[Prike] Thanks, I will update GEM BOs validation method.

Quoted 74 lines

Alex

Quoted 1 line

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

0x200000) ||

Quoted 1 line

+ 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

invalidated\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) {
@@ -504,11 +550,17 @@ 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 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -1
                }
 
                if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
-                               args->in.queue_size == 0) {
+                               args->in.queue_size == 0 ||
+                               !access_ok(u64_to_user_ptr(args->in.queue_va &

AMDGPU_GMC_HOLE_MASK),

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -1
+                                       args->in.queue_size)) {
                        drm_file_err(filp, "invalidate userq queue va or size\n");
                        return -EINVAL;
                }
-               if (!args->in.wptr_va || !args->in.rptr_va) {
+               if (!args->in.wptr_va || !args->in.rptr_va ||
+                       !access_ok(u64_to_user_ptr(args->in.wptr_va &

AMDGPU_GMC_HOLE_MASK),

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+                                               sizeof(uint64_t)) ||
+                       !access_ok(u64_to_user_ptr(args->in.rptr_va &

AMDGPU_GMC_HOLE_MASK),

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

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
+                                               sizeof(uint64_t))) {
                        drm_file_err(filp, "invalidate userq queue rptr or wptr\n");
                        return -EINVAL;
                }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
index ec040c2fd6c9..375fba639c94 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,

Quoted 22 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 b46e67b179fc..4ba064e6dda0 100644
@@ -28,6 +28,7 @@
 
 #define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE  #define

AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE
+#define MQD_SHADOW_BASE_SIZE 73728

static int
mes_userq_map_gtt_bo_to_gart(struct amdgpu_bo *bo) @@ -246,6 +247,19
@@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,

                        r = -ENOMEM;
                        goto free_mqd;
                }

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 +3 -0
+               /*
+                * EOP VA size = MEC_HPD_SIZE * adev->gfx.num_compute_rings.
+                * Since the userq support newer than gfx10, so the keep

MEC_HPD_SIZE as 2048.

Quoted 8 lines
Quoted 1 line

+ * TODO: It requires figuring out why the EOP VA not match with

MEC_HPD_SIZE *

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
+                * adev->gfx.num_compute_rings.
+                */

The EOP size is MEC_HPD_SIZE. We only multiply it by adev->gfx.num_compute_rings for kernel queues because we use one allocation for the EOP buffer for all of the kernel queues.

[Prike] Yes, EOP and below shadow BOs mapping size just wants to align with the mesa BOs create and map size setting.

Thanks,
Prike

Quoted 56 lines
Quoted 3 lines

+ if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd-
eop_va,
+ /*ALIGN(2048 * adev->gfx.num_compute_rings,

PAGE_SIZE)*/

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5 -0
+                                               0x200000)) {
+                       drm_file_err(uq_mgr->file, "EOP VA is invalidated\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; @@ -274,6 +288,15 @@ static int

mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,

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

Quoted 6 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
+
+               /* TODO: Need to check the shadow WA size doesn't match with mesa

request.*/

Quoted 3 lines

+ if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11-
shadow_va,
+ 0x200000)) {

The shadow size should >= the shadow size defined in struct amdgpu_gfx_shadow_info and returned by adev->gfx.funcs->get_gfx_shadow_info().

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5 -0
+                       drm_file_err(uq_mgr->file, "shadow VA is invalidated\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;
@@ -290,7 +313,7 @@ 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 -1
                        r = -ENOMEM;
                        goto free_mqd;
                }
-
+               /* TODO: validate the csa and check the va size as
+ well.*/

See adev->gfx.funcs->get_gfx_shadow_info() here as well.

Alex

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

2.34.1

#16 lore raw

PATCH 5/9 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 | 120 +++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  11 ++
 2 files changed, 130 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +119 -1
index 32e88064bdea..3854b1696d4d 100644
@@ -79,6 +79,122 @@ 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;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+	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;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+	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->doorbell_handle) ||
+		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))
+		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;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+	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;
+	/*
+	 * As the GEM userq bo will be unmapped by amdgpu_vm_bo_unmap() which is
+	 * invoked before userq is destroyed. So if we want to reference/dereference
+	 * the userq vm bo, then ensure the userq vm bo dereferenced  before
+	 * amdgpu_vm_bo_unmap() or fallback to unmap the userq vm bo at amdgpu_userq_destroy().
+	 */
+	//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;
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue)
+{
+	amdgpu_userq_buffer_va_put(vm, queue->doorbell_handle);
+	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);
+
+	return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -451,6 +567,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;
@@ -481,7 +600,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 +11 -0
index 375fba639c94..65f9bd91ac73 100644
@@ -52,6 +52,10 @@ 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		flags;
 	struct amdgpu_mqd_prop	*userq_prop;
 	struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +138,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
Signature · 1 line
2.34.1
#17 lore raw

RE: [PATCH 5/9] drm/amdgpu: add userq object va track helpers

[AMD Official Use Only - AMD Internal Distribution Only]

+ user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);

This is a PAGE_ALIGN_DOWN() macro can simplify your code logic.

Best Regards,
Kevin

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Prike Liang
Sent: Friday, May 30, 2025 15:55
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH 5/9] drm/amdgpu: add userq object va track helpers

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 | 120 +++++++++++++++++++++-  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  11 ++
 2 files changed, 130 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +111 -0
index 32e88064bdea..3854b1696d4d 100644
@@ -79,6 +79,122 @@ 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;
+
+       addr &= AMDGPU_GMC_HOLE_MASK;
+       user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+       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;
+
+       addr &= AMDGPU_GMC_HOLE_MASK;
+       user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+       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->doorbell_handle) ||
+               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))
+               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;
+
+       addr &= AMDGPU_GMC_HOLE_MASK;
+       user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+       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;
+       /*
+        * As the GEM userq bo will be unmapped by amdgpu_vm_bo_unmap() which is
+        * invoked before userq is destroyed. So if we want to reference/dereference
+        * the userq vm bo, then ensure the userq vm bo dereferenced  before
+        * amdgpu_vm_bo_unmap() or fallback to unmap the userq vm bo at amdgpu_userq_destroy().
+        */
+       //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;
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->doorbell_handle);
+       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);
+
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue) @@ -451,6 +567,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; @@ -481,7 +600,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"); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h

index 375fba639c94..65f9bd91ac73 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +11 -0
@@ -52,6 +52,10 @@ 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                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +138,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

--
2.34.1

maintainer
#18 lore raw

Re: [PATCH 5/9] drm/amdgpu: add userq object va track helpers

In reply to Prike Liang
Quoted 33 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 | 120 +++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  11 ++
 2 files changed, 130 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16 -0
index 32e88064bdea..3854b1696d4d 100644
@@ -79,6 +79,122 @@ 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;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+	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;

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

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

Please don't use // in kernel code.

Regards,
Christian.

Quoted 138 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +93 -1
+	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;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+	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->doorbell_handle) ||
+		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))
+		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;
+
+	addr &= AMDGPU_GMC_HOLE_MASK;
+	user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+	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;
+	/*
+	 * As the GEM userq bo will be unmapped by amdgpu_vm_bo_unmap() which is
+	 * invoked before userq is destroyed. So if we want to reference/dereference
+	 * the userq vm bo, then ensure the userq vm bo dereferenced  before
+	 * amdgpu_vm_bo_unmap() or fallback to unmap the userq vm bo at amdgpu_userq_destroy().
+	 */
+	//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;
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue)
+{
+	amdgpu_userq_buffer_va_put(vm, queue->doorbell_handle);
+	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);
+
+	return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -451,6 +567,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;
@@ -481,7 +600,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 +11 -0
index 375fba639c94..65f9bd91ac73 100644
@@ -52,6 +52,10 @@ 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		flags;
 	struct amdgpu_mqd_prop	*userq_prop;
 	struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +138,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
#19 lore raw

RE: [PATCH 5/9] drm/amdgpu: add userq object va track helpers

[Public]

Quoted 47 lines

From: Koenig, Christian <[email protected]>
Sent: Monday, June 2, 2025 7:06 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 5/9] drm/amdgpu: add userq object va track helpers

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

Quoted 16 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 | 120
+++++++++++++++++++++-  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |
11 ++
 2 files changed, 130 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 32e88064bdea..3854b1696d4d 100644
@@ -79,6 +79,122 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm

*vm, u64 addr,

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +19 -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;
+
+   addr &= AMDGPU_GMC_HOLE_MASK;
+   user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+   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
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+    *  rptr bo
+    *  wptr bo
+    *  eop  bo
+    *  doorbell bo
+    */
+   //amdgpu_bo_ref(mapping->bo_va->base.bo);

Please don't use // in kernel code.

Note.

Quoted 144 lines

Regards,
Christian.

Quoted 50 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +50 -0
+   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;
+
+   addr &= AMDGPU_GMC_HOLE_MASK;
+   user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);
+
+   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->doorbell_handle) ||
+           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))
+           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;
+
+   addr &= AMDGPU_GMC_HOLE_MASK;
+   user_addr = ALIGN_DOWN(addr >> AMDGPU_GPU_PAGE_SHIFT, 0x100);

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+
+   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;
+   /*
+    * As the GEM userq bo will be unmapped by amdgpu_vm_bo_unmap()

which is

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

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
+    * invoked before userq is destroyed. So if we want to reference/dereference
+    * the userq vm bo, then ensure the userq vm bo dereferenced  before
+    * amdgpu_vm_bo_unmap() or fallback to unmap the userq vm bo at

amdgpu_userq_destroy().

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16 -0
+    */
+   //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;
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                   struct amdgpu_usermode_queue *queue) {
+   amdgpu_userq_buffer_va_put(vm, queue->doorbell_handle);

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+   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);
+
+   return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                      struct amdgpu_usermode_queue *queue) @@ -451,6

+567,9 @@

Quoted 11 lines

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;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -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; @@ -481,7 +600,6

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

Quoted 5 lines
            goto unlock;
    }

-

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

AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);

Quoted 32 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 375fba639c94..65f9bd91ac73 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4 -0
@@ -52,6 +52,10 @@ 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                flags;
    struct amdgpu_mqd_prop  *userq_prop;
    struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +138,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);
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
#20 lore raw

PATCH 6/9 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 hasn't been unmapped illegally 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 | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +19 -0
index 3854b1696d4d..6267993deb8c 100644
@@ -470,6 +470,17 @@ 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 mapped,
+	 * 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 already unmaped\n");
+		mutex_unlock(&uq_mgr->userq_mutex);
+		return -EINVAL;
+	}
+
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
 	if (r != AMDGPU_USERQ_STATE_UNMAPPED) {
 		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Can't unmap the queue for destroying.\n");
@@ -477,6 +488,8 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		/*TODO: before return may need to a reset*/
 		return r;
 	}
+
+	amdgpu_userq_buffer_vas_put(&fpriv->vm, queue);
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
@@ -600,6 +613,12 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto unlock;
 	}
 
+	/* refer to the userq objects vm bo*/
+	amdgpu_userq_buffer_va_get(queue->vm, args->in.doorbell_handle);
+	amdgpu_userq_buffer_va_get(queue->vm, args->in.queue_va);
+	amdgpu_userq_buffer_va_get(queue->vm, args->in.rptr_va);
+	amdgpu_userq_buffer_va_get(queue->vm, args->in.wptr_va);
+
 	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
#21 lore raw

PATCH 7/9 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 6267993deb8c..1c58ddbd654f 100644
@@ -496,6 +496,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;
 }
 
@@ -530,6 +532,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
#22 lore raw

PATCH 8/9 drm/amdgpu: add userq va unmap wait helper

In reply to Prike Liang

This helper can validate the userq whether is ative prior to the userq VA GEM unmap.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 13 +++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  2 ++
 2 files changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -0
index 1c58ddbd654f..e43a61f64755 100644
@@ -1152,3 +1152,16 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
 }
+
+int amdgpu_userq_gem_va_unmap_wait(struct amdgpu_vm *vm)
+{
+	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) {
+		dev_dbg(uq_mgr->adev->dev, "try to unmap an userq by gem va ioctl\n");
+		return amdgpu_userq_wait_for_signal(uq_mgr);
+	}
+
+	return 0;
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index 65f9bd91ac73..ea1b631331c9 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,
@@ -145,4 +146,5 @@ 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_wait(struct amdgpu_vm *vm);
 #endif
Signature · 1 line
2.34.1
#23 lore raw

PATCH 9/9 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 an active 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    |  5 +++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -3
index e43a61f64755..e2275280554d 100644
@@ -234,7 +234,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)
 {
@@ -243,10 +243,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
@@ -464,7 +468,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 +5 -0
index 5e075e8f0ca3..168fc3835aaf 100644
@@ -1930,6 +1930,11 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 	struct amdgpu_vm *vm = bo_va->base.vm;
 	bool valid = true;
 
+	if (amdgpu_userq_gem_va_unmap_wait(vm)) {
+		dev_warn(adev->dev, "shouldn't unmap the VA for an active userq\n");
+		return -EINVAL;
+	}
+
 	saddr /= AMDGPU_GPU_PAGE_SIZE;
 
 	list_for_each_entry(mapping, &bo_va->valids, list) {
Signature · 1 line
2.34.1
#24 lore raw

Re: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

Quoted 60 lines · Prike Liang

The userq VA unmap requires validating queue status before unamapping it, if user tries to unmap an active 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    |  5 +++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -3
index e43a61f64755..e2275280554d 100644
@@ -234,7 +234,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)
 {
@@ -243,10 +243,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
@@ -464,7 +468,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 +0 -0
index 5e075e8f0ca3..168fc3835aaf 100644

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
@@ -1930,6 +1930,11 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
        struct amdgpu_vm *vm = bo_va->base.vm;
        bool valid = true;
 
+       if (amdgpu_userq_gem_va_unmap_wait(vm)) {
+               dev_warn(adev->dev, "shouldn't unmap the VA for an active userq\n");
+               return -EINVAL;
+       }

There's not need to wait for anything. Just return an error if the userq has not been destroyed yet.

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
+
        saddr /= AMDGPU_GPU_PAGE_SIZE;
 
        list_for_each_entry(mapping, &bo_va->valids, list) {
--

2.34.1

#25 lore raw

RE: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

[Public]

Quoted 80 lines

From: Alex Deucher <[email protected]>
Sent: Saturday, May 31, 2025 5:54 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr

Quoted 17 lines · Prike Liang

The userq VA unmap requires validating queue status before unamapping it, if user tries to unmap an active 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    |  5 +++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index e43a61f64755..e2275280554d 100644
@@ -234,7 +234,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
        return r;
 }
 
-static void
+static int
 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
                                 struct amdgpu_usermode_queue *queue)

{ @@ -243,10 +243,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));

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
-               if (ret <= 0)
+               if (ret <= 0) {
                        drm_file_err(uq_mgr->file, "Timed out waiting for

fence=%llu:%llu\n",

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

Quoted 10 lines
                                     f->context, f->seqno);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       return -ETIMEDOUT;
+               }
        }
+
+       return 0;
 }
 
 static void
@@ -464,7 +468,13 @@ 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 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -1
                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");

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+               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 +0 -0
index 5e075e8f0ca3..168fc3835aaf 100644
@@ -1930,6 +1930,11 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device

*adev,

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
        struct amdgpu_vm *vm = bo_va->base.vm;
        bool valid = true;
 
Quoted 7 lines

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
+       if (amdgpu_userq_gem_va_unmap_wait(vm)) {
+               dev_warn(adev->dev, "shouldn't unmap the VA for an active userq\n");
+               return -EINVAL;
+       }

There's not need to wait for anything. Just return an error if the userq has not been destroyed yet.

[Prike] The current userq destroy IOCTL request always comes after amdgpu_vm_bo_unmap(), the original destroy validation idea is that the userq BOs only be able to be unmapped when they are idle, so here do we need to do the BOs busy wait or just test and throw a warning/error message when the driver tries unmap a busy BO?

Quoted 9 lines

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
+
        saddr /= AMDGPU_GPU_PAGE_SIZE;
 
        list_for_each_entry(mapping, &bo_va->valids, list) {
--

2.34.1

maintainer
#26 lore raw

Re: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

Quoted 87 lines · Prike

[Public]

Quoted 85 lines

From: Alex Deucher <[email protected]>
Sent: Saturday, May 31, 2025 5:54 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr

Quoted 17 lines · Prike Liang

The userq VA unmap requires validating queue status before unamapping it, if user tries to unmap an active 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    |  5 +++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index e43a61f64755..e2275280554d 100644
@@ -234,7 +234,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
        return r;
 }
 
-static void
+static int
 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
                                 struct amdgpu_usermode_queue *queue)

{ @@ -243,10 +243,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));

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
-               if (ret <= 0)
+               if (ret <= 0) {
                        drm_file_err(uq_mgr->file, "Timed out waiting for

fence=%llu:%llu\n",

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

Quoted 10 lines
                                     f->context, f->seqno);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       return -ETIMEDOUT;
+               }
        }
+
+       return 0;
 }
 
 static void
@@ -464,7 +468,13 @@ 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 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -1
                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");

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+               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 +0 -0
index 5e075e8f0ca3..168fc3835aaf 100644
@@ -1930,6 +1930,11 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device

*adev,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
        struct amdgpu_vm *vm = bo_va->base.vm;
        bool valid = true;
 
+       if (amdgpu_userq_gem_va_unmap_wait(vm)) {
+               dev_warn(adev->dev, "shouldn't unmap the VA for an active userq\n");
+               return -EINVAL;
+       }
Quoted 6 lines
Quoted 2 lines

There's not need to wait for anything. Just return an error if the userq has not been destroyed yet.

[Prike] The current userq destroy IOCTL request always comes after amdgpu_vm_bo_unmap(), the original destroy validation idea is that the userq BOs only be able to be unmapped when they are idle, so here do we need to do the BOs busy wait or just test and throw a warning/error message when the driver tries unmap a busy BO?

Yeah that discussion came up previously before.

As far as I can see we can't return an error from unmap because the BO might still be used by a queue.

At least on the gfx side such a behavior could easily lead to crashes because userspace doesn't expect that an unmap fails and tries to re-use the address range.

What we can do is deactivate that queue and prevent it from becomming active again.

Regards,
Christian.

Quoted 10 lines
Quoted 10 lines

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
+
        saddr /= AMDGPU_GPU_PAGE_SIZE;
 
        list_for_each_entry(mapping, &bo_va->valids, list) {
--

2.34.1

maintainer NAK
#27 lore raw

Re: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

In reply to Prike Liang
Quoted 3 lines · Prike Liang

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

NAK, it is perfectly valid to unmap an active queue.

We just need to make sure that we waited for the queued up fences to signal.

Regards,
Christian.

Quoted 66 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  5 +++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -3
index e43a61f64755..e2275280554d 100644
@@ -234,7 +234,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)
 {
@@ -243,10 +243,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
@@ -464,7 +468,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 +5 -0
index 5e075e8f0ca3..168fc3835aaf 100644
@@ -1930,6 +1930,11 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 	struct amdgpu_vm *vm = bo_va->base.vm;
 	bool valid = true;
 
+	if (amdgpu_userq_gem_va_unmap_wait(vm)) {
+		dev_warn(adev->dev, "shouldn't unmap the VA for an active userq\n");
+		return -EINVAL;
+	}
+
 	saddr /= AMDGPU_GPU_PAGE_SIZE;
 
 	list_for_each_entry(mapping, &bo_va->valids, list) {
#28 lore raw

RE: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

[Public]

Quoted 8 lines

From: Koenig, Christian <[email protected]>
Sent: Monday, June 2, 2025 7:08 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 9/9] drm/amdgpu: validate userq activity status for GEM_VA unmap

Quoted 8 lines
Quoted 3 lines · Prike Liang

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

NAK, it is perfectly valid to unmap an active queue.

We just need to make sure that we waited for the queued up fences to signal.

[Prike] Yes, in amdgpu_userq_gem_va_unmap_wait () is just waiting for the userq fence signal before the kernel driver can unmap the userq VM BO, and if the fence wait timeout and then will return an error to the user.

Quoted 76 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr

Quoted 12 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  5 +++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index e43a61f64755..e2275280554d 100644
@@ -234,7 +234,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr

*uq_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
    return r;
 }
 
-static void
+static int
 amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
                             struct amdgpu_usermode_queue *queue)  { @@ -

243,10 +243,14 @@

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
 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));
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
-           if (ret <= 0)
+           if (ret <= 0) {
                    drm_file_err(uq_mgr->file, "Timed out waiting for

fence=%llu:%llu\n",

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

Quoted 10 lines
                                 f->context, f->seqno);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                   return -ETIMEDOUT;
+           }
    }
+
+   return 0;
 }
 
 static void
@@ -464,7 +468,13 @@ 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 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -1
            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");

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+           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 +0 -0
index 5e075e8f0ca3..168fc3835aaf 100644
@@ -1930,6 +1930,11 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device

*adev,

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
    struct amdgpu_vm *vm = bo_va->base.vm;
    bool valid = true;
 
+   if (amdgpu_userq_gem_va_unmap_wait(vm)) {
+           dev_warn(adev->dev, "shouldn't unmap the VA for an active userq\n");
+           return -EINVAL;
+   }
+
    saddr /= AMDGPU_GPU_PAGE_SIZE;
 
    list_for_each_entry(mapping, &bo_va->valids, list) {
#29 lore raw
In reply to Prike Liang
Quoted 133 lines · Prike Liang

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 80 +++++++++++++++-------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |  7 --
 2 files changed, 55 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +55 -25
index 295e7186e156..f67969312c39 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,44 @@ 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_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 +519,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 -0
index d6f50b13e2ba..1457fb49a794 100644

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -6
@@ -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;
-       }

I would keep this check here as this is MES specific at this point.

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -2
-
        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

#30 lore raw

[Public]

Quoted 158 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Saturday, May 31, 2025 5:32 AM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH 1/9] drm/amdgpu: validate userq input args

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

Quoted 17 lines · Prike Liang

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 80
+++++++++++++++-------  drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |
7 --
 2 files changed, 55 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 295e7186e156..f67969312c39 100644
@@ -359,27 +359,10 @@ 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 359

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_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -5
                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",

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -5
-                            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) &&

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -4
-           (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");

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -3
-               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,44 @@

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

Quoted 6 lines
        return r;

}

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

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -2
+                                       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 |

Quoted 1 line

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))

Quoted 5 lines
                        return -EINVAL;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -4
-               r = amdgpu_userq_create(filp, args);
-               if (r)
-                       drm_file_err(filp, "Failed to create usermode queue\n");
-               break;

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+               /* 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) &&

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                   (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");

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

Quoted 53 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12 -0
+                       return -EINVAL;
+               }
 
+               if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
+                               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 +519,31 @@

int amdgpu_userq_ioctl(struct drm_device *dev, void *data,

                    args->in.mqd ||
                    args->in.mqd_size)
                        return -EINVAL;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +24 -0
+               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"); diff --git a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index d6f50b13e2ba..1457fb49a794 100644

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -215,13 +215,6 @@ 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
                return -ENOMEM;
        }
 
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -6
-       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;
-       }

I would keep this check here as this is MES specific at this point.

OK, previously I just want to validate the userq input parameters at the useq IOCTL early time to avoid allocating unnecessary resource.

Quoted 11 lines

Alex

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