Skip to contentSkip to conversation

drm/amdgpu: validate userq input args

PATCH Superseded Prike Liang 2 participants 28 messages Aug 26, 2025 – Sep 8, 2025

Subsystems (3)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/14 drm/amdgpu: validate userq input args

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

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

PATCH v9 2/14 drm/amdgpu: validate userq hw unmap status for destroying userq

Before destroying the userq buffer object, it requires validating the userq HW unmap status and ensuring the userq is unmapped from hardware. If the user HW unmap failed, then it needs to reset the queue for reusing.

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

PATCH v9 3/14 drm/amdgpu: add UAPI for user queue query status

In reply to Prike Liang

From: Alex Deucher <[email protected]>

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

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

PATCH v9 4/14 drm/amdgpu/userq: implement support for query status

In reply to Prike Liang

From: Alex Deucher <[email protected]>

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

v2: Misc cleanups

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 35 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  1 +
 2 files changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +34 -1
index 2b35198608a7..75b7e39ee576 100644
@@ -403,6 +403,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	queue->queue_type = args->in.ip_type;
 	queue->vm = &fpriv->vm;
 	queue->priority = priority;
+	queue->generation = amdgpu_vm_generation(adev, &fpriv->vm);
 
 	db_info.queue_type = queue->queue_type;
 	db_info.doorbell_handle = queue->doorbell_handle;
@@ -473,6 +474,34 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	return r;
 }
 
+static int
+amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args)
+{
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_usermode_queue *queue;
+	int queue_id = args->in.queue_id;
+
+	mutex_lock(&uq_mgr->userq_mutex);
+
+	queue = amdgpu_userq_find(uq_mgr, queue_id);
+	if (!queue) {
+		dev_dbg(adev->dev, "Invalid queue id to query\n");
+		mutex_unlock(&uq_mgr->userq_mutex);
+		return -EINVAL;
+	}
+	args->out_qs.flags = 0;
+	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
+		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_HUNG;
+	if (queue->generation != amdgpu_vm_generation(adev, &fpriv->vm))
+		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_VRAMLOST;
+
+	mutex_unlock(&uq_mgr->userq_mutex);
+
+	return 0;
+}
+
 static int amdgpu_userq_input_args_validate(struct drm_device *dev,
 					union drm_amdgpu_userq *args,
 					struct drm_file *filp)
@@ -554,7 +583,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
 		if (r)
 			drm_file_err(filp, "Failed to destroy usermode queue\n");
 		break;
-
+	case AMDGPU_USERQ_OP_QUERY_STATUS:
+		r = amdgpu_userq_query_status(filp, args);
+		if (r)
+			drm_file_err(filp, "Failed to query usermode queue status\n");
+		break;
 	default:
 		drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
 		return -EINVAL;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
index ec040c2fd6c9..48722936ff70 100644
@@ -65,6 +65,7 @@ struct amdgpu_usermode_queue {
 	struct dma_fence	*last_fence;
 	u32			xcp_id;
 	int			priority;
+	uint64_t		generation;
 };
 
 struct amdgpu_userq_funcs {
Signature · 1 line
2.34.1
#4 lore raw

PATCH v9 5/14 drm/amdgpu/userq: extend queue flags for user queue query status

In reply to Prike Liang

Add the userq flag to identify the invalid userq cases.

---
 include/uapi/drm/amdgpu_drm.h | 4 ++++
 1 file changed, 4 insertions(+)
include/uapi/drm/amdgpu_drm.h +4 -0
index efd0d2356077..3c536c8517cd 100644
@@ -346,6 +346,10 @@ union drm_amdgpu_ctx {
 #define AMDGPU_USERQ_QUERY_STATUS_FLAGS_HUNG    (1 << 0)
 /* indicate vram lost since queue was created */
 #define AMDGPU_USERQ_QUERY_STATUS_FLAGS_VRAMLOST (1 << 1)
+/* the invalid userq input argument */
+#define AMDGPU_USERQ_QUERY_STATUS_FLAGS_INVALID_ARG (1 << 2)
+/* the invalid userq VA unmapped */
+#define AMDGPU_USERQ_QUERY_STATUS_FLAGS_INVALID_VA (1 << 3)
 
 /*
  * This structure is a container to pass input configuration
Signature · 1 line
2.34.1
#5 lore raw

PATCH v9 6/14 drm/amdgpu/userq: extend userq state

In reply to Prike Liang

Extend the userq state for identifying the userq invalid cases, such as the IOCTL invalid input parameter, and unmap the VA before destroying userq.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++
 2 files changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
index 75b7e39ee576..b670ca8111f3 100644
@@ -496,6 +496,10 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args)
 		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_HUNG;
 	if (queue->generation != amdgpu_vm_generation(adev, &fpriv->vm))
 		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_VRAMLOST;
+	if (queue->state == AMDGPU_USERQ_STATE_INVALID_ARG)
+		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_INVALID_ARG;
+	if (queue->state == AMDGPU_USERQ_STATE_INVALID_VA)
+		args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_INVALID_VA;
 
 	mutex_unlock(&uq_mgr->userq_mutex);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index 48722936ff70..694f850d102e 100644
@@ -37,6 +37,8 @@ enum amdgpu_userq_state {
 	AMDGPU_USERQ_STATE_MAPPED,
 	AMDGPU_USERQ_STATE_PREEMPTED,
 	AMDGPU_USERQ_STATE_HUNG,
+	AMDGPU_USERQ_STATE_INVALID_ARG,
+	AMDGPU_USERQ_STATE_INVALID_VA,
 };
 
 struct amdgpu_mqd_prop;
Signature · 1 line
2.34.1
#6 lore raw

PATCH v9 7/14 drm/amdgpu: validate userq buffer virtual address and size

In reply to Prike Liang

It needs to validate the userq object virtual address to determin whether it is residented in a valid vm mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 41 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 ++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++++++++++++
 3 files changed, 65 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +41 -0
index b670ca8111f3..0aeb7a96ccbf 100644
@@ -44,6 +44,38 @@ u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
 	return userq_ip_mask;
 }
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+				u64 expected_size)
+{
+	struct amdgpu_bo_va_mapping *va_map;
+	u64 user_addr;
+	u64 size;
+	int r = 0;
+
+	user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT;
+	size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+	r = amdgpu_bo_reserve(vm->root.bo, false);
+	if (r)
+		return r;
+
+	va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+	if (!va_map) {
+		r = -EINVAL;
+		goto out_err;
+	}
+	/* Only validate the userq whether resident in the VM mapping range */
+	if (user_addr >= va_map->start  &&
+	    va_map->last - user_addr + 1 >= size) {
+		amdgpu_bo_unreserve(vm->root.bo);
+		return 0;
+	}
+
+out_err:
+	amdgpu_bo_unreserve(vm->root.bo);
+	return r;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -399,6 +431,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		r = -ENOMEM;
 		goto unlock;
 	}
+
+	/* Validate the userq virtual address.*/
+	if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, args->in.queue_size) ||
+	    amdgpu_userq_input_va_validate(&fpriv->vm, args->in.rptr_va, PAGE_SIZE) ||
+	    amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va, PAGE_SIZE)) {
+		queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;
+		kfree(queue);
+		goto unlock;
+	}
 	queue->doorbell_handle = args->in.doorbell_handle;
 	queue->queue_type = args->in.ip_type;
 	queue->vm = &fpriv->vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index 694f850d102e..0eb2a9c2e340 100644
@@ -135,4 +135,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 +22 -0
index 1457fb49a794..6e29e85bbf9f 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 	struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
 	struct drm_amdgpu_userq_in *mqd_user = args_in;
 	struct amdgpu_mqd_prop *userq_props;
+	struct amdgpu_gfx_shadow_info shadow_info;
 	int r;
 
 	/* Structure to initialize MQD for userqueue using generic MQD init function */
@@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 	userq_props->doorbell_index = queue->doorbell_index;
 	userq_props->fence_address = queue->fence_drv->gpu_addr;
 
+	if (adev->gfx.funcs->get_gfx_shadow_info)
+		adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
 	if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
 		struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
 
@@ -247,6 +250,12 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			goto free_mqd;
 		}
 
+		if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd->eop_va,
+					max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE))) {
+			queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;
+			goto free_mqd;
+		}
+
 		userq_props->eop_gpu_addr = compute_mqd->eop_va;
 		userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
 		userq_props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;
@@ -274,6 +283,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->csa_addr = mqd_gfx_v11->csa_va;
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+
+		if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
+					shadow_info.shadow_size)) {
+			queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;
+			goto free_mqd;
+		}
+
 		kfree(mqd_gfx_v11);
 	} else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
 		struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;
@@ -291,6 +307,12 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 			goto free_mqd;
 		}
 
+		if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11->csa_va,
+					shadow_info.csa_size)) {
+			queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;
+			goto free_mqd;
+		}
+
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
 	}
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v9 07/14] drm/amdgpu: validate userq buffer virtual address and size

Quoted 3 lines · Prike Liang

It needs to validate the userq object virtual address to determin whether it is residented in a valid vm mapping.

determine

Quoted 53 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 41 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 ++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++++++++++++
 3 files changed, 65 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +32 -0
index b670ca8111f3..0aeb7a96ccbf 100644
@@ -44,6 +44,38 @@ u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
        return userq_ip_mask;
 }
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                               u64 expected_size)
+{
+       struct amdgpu_bo_va_mapping *va_map;
+       u64 user_addr;
+       u64 size;
+       int r = 0;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT;
+       size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!va_map) {
+               r = -EINVAL;
+               goto out_err;
+       }
+       /* Only validate the userq whether resident in the VM mapping range */
+       if (user_addr >= va_map->start  &&
+           va_map->last - user_addr + 1 >= size) {
+               amdgpu_bo_unreserve(vm->root.bo);
+               return 0;
+       }
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return r;
+}
+
 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 438

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
@@ -399,6 +431,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
                r = -ENOMEM;
                goto unlock;
        }
+
+       /* Validate the userq virtual address.*/
+       if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, args->in.queue_size) ||
+           amdgpu_userq_input_va_validate(&fpriv->vm, args->in.rptr_va, PAGE_SIZE) ||
+           amdgpu_userq_input_va_validate(&fpriv->vm, args->in.wptr_va, PAGE_SIZE)) {

I think the sizes here should be AMDGPU_GPU_PAGE_SIZE rather than PAGE_SIZE

Quoted 40 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+               queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;
+               kfree(queue);
+               goto unlock;
+       }
        queue->doorbell_handle = args->in.doorbell_handle;
        queue->queue_type = args->in.ip_type;
        queue->vm = &fpriv->vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index 694f850d102e..0eb2a9c2e340 100644
@@ -135,4 +135,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 +3 -0
index 1457fb49a794..6e29e85bbf9f 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
        struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];
        struct drm_amdgpu_userq_in *mqd_user = args_in;
        struct amdgpu_mqd_prop *userq_props;
+       struct amdgpu_gfx_shadow_info shadow_info;
        int r;
 
        /* Structure to initialize MQD for userqueue using generic MQD init function */
@@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
        userq_props->doorbell_index = queue->doorbell_index;
        userq_props->fence_address = queue->fence_drv->gpu_addr;
 
+       if (adev->gfx.funcs->get_gfx_shadow_info)
+               adev->gfx.funcs->get_gfx_shadow_info(adev, &shadow_info, true);
        if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
                struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
 

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

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
@@ -247,6 +250,12 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                        goto free_mqd;
                }
 
+               if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd->eop_va,
+                                       max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE))) {
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

Rather than setting the queue->state, just return -EINVAL. We shouldn't create the queue in the first place if the addresses are invalid.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+                       goto free_mqd;
+               }
+
                userq_props->eop_gpu_addr = compute_mqd->eop_va;
                userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
                userq_props->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
@@ -274,6 +283,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
+                                       shadow_info.shadow_size)) {
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

Same comment here.

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

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -0
+                       goto free_mqd;
+               }
+
                kfree(mqd_gfx_v11);
        } else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
                struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;
@@ -291,6 +307,12 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                        goto free_mqd;
                }
 
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11->csa_va,
+                                       shadow_info.csa_size)) {
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

and here.

Alex

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

2.34.1

#8 lore raw

RE: [PATCH v9 07/14] drm/amdgpu: validate userq buffer virtual address and size

[Public]

Regards,
Prike

Quoted 80 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:00 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 07/14] drm/amdgpu: validate userq buffer virtual address and size

Quoted 3 lines · Prike Liang

It needs to validate the userq object virtual address to determin whether it is residented in a valid vm mapping.

determine

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

Quoted 15 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 41
++++++++++++++++++++++  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |
2 ++  drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++++++++++++
 3 files changed, 65 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index b670ca8111f3..0aeb7a96ccbf 100644
@@ -44,6 +44,38 @@ 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 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
        return userq_ip_mask;
 }
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                               u64 expected_size) {
+       struct amdgpu_bo_va_mapping *va_map;
+       u64 user_addr;
+       u64 size;
+       int r = 0;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +17 -0
+       size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!va_map) {
+               r = -EINVAL;
+               goto out_err;
+       }
+       /* Only validate the userq whether resident in the VM mapping range */
+       if (user_addr >= va_map->start  &&
+           va_map->last - user_addr + 1 >= size) {
+               amdgpu_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 r;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue) @@
-399,6 +431,15 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 8 lines
                r = -ENOMEM;
                goto unlock;
        }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
+
+       /* Validate the userq virtual address.*/
+       if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, args-

in.queue_size) ||
+ amdgpu_userq_input_va_validate(&fpriv->vm, args->in.rptr_va,

Quoted 6 lines

PAGE_SIZE) ||

Quoted 2 lines

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

I think the sizes here should be AMDGPU_GPU_PAGE_SIZE rather than PAGE_SIZE

Yes, even the two value are equal but that more sense for validating the GPU VA.

Quoted 51 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+               queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;
+               kfree(queue);
+               goto unlock;
+       }
        queue->doorbell_handle = args->in.doorbell_handle;
        queue->queue_type = args->in.ip_type;
        queue->vm = &fpriv->vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
index 694f850d102e..0eb2a9c2e340 100644

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
@@ -135,4 +135,6 @@ int
 amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,

int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,

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

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
                                                   u32 idx);
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                       u64 expected_size);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 1457fb49a794..6e29e85bbf9f 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

init function */ @@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

Quoted 11 lines
        userq_props->doorbell_index = queue->doorbell_index;
        userq_props->fence_address = queue->fence_drv->gpu_addr;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+       if (adev->gfx.funcs->get_gfx_shadow_info)
+               adev->gfx.funcs->get_gfx_shadow_info(adev,
+ &shadow_info, true);
        if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
                struct drm_amdgpu_userq_mqd_compute_gfx11

*compute_mqd;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -247,6 +250,12 @@ 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
                        goto free_mqd;
                }
 
Quoted 8 lines

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

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
+               if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd-
 eop_va,
+                                       max_t(u32, PAGE_SIZE,

AMDGPU_GPU_PAGE_SIZE))) {

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

Rather than setting the queue->state, just return -EINVAL. We shouldn't create the queue in the first place if the addresses are invalid.

Note.

Quoted 51 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+                       goto free_mqd;
+               }
+
                userq_props->eop_gpu_addr = compute_mqd->eop_va;
                userq_props->hqd_pipe_priority =

AMDGPU_GFX_PIPE_PRIO_NORMAL;

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

Quoted 3 lines
                userq_props->hqd_queue_priority =

AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -274,6 +283,13 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags &
 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11-
 shadow_va,
+                                       shadow_info.shadow_size)) {
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

Same comment here.

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+                       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 +0 -0
@@ -291,6 +307,12 @@ 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, · line 312

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
                        goto free_mqd;
                }
 
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11-
 csa_va,
+                                       shadow_info.csa_size)) {
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

and here.

Alex

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

--
2.34.1

#9 lore raw

Re: [PATCH v9 07/14] drm/amdgpu: validate userq buffer virtual address and size

Quoted 86 lines · Prike

[Public]

Regards,
Prike

Quoted 80 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:00 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 07/14] drm/amdgpu: validate userq buffer virtual address and size

Quoted 3 lines · Prike Liang

It needs to validate the userq object virtual address to determin whether it is residented in a valid vm mapping.

determine

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

Quoted 15 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 41
++++++++++++++++++++++  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |
2 ++  drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++++++++++++
 3 files changed, 65 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index b670ca8111f3..0aeb7a96ccbf 100644
@@ -44,6 +44,38 @@ 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 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
        return userq_ip_mask;
 }
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                               u64 expected_size) {
+       struct amdgpu_bo_va_mapping *va_map;
+       u64 user_addr;
+       u64 size;
+       int r = 0;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +17 -0
+       size = expected_size >> AMDGPU_GPU_PAGE_SHIFT;
+
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       va_map = amdgpu_vm_bo_lookup_mapping(vm, user_addr);
+       if (!va_map) {
+               r = -EINVAL;
+               goto out_err;
+       }
+       /* Only validate the userq whether resident in the VM mapping range */
+       if (user_addr >= va_map->start  &&
+           va_map->last - user_addr + 1 >= size) {
+               amdgpu_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 r;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue) @@
-399,6 +431,15 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 8 lines
                r = -ENOMEM;
                goto unlock;
        }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -0
+
+       /* Validate the userq virtual address.*/
+       if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, args-

in.queue_size) ||
+ amdgpu_userq_input_va_validate(&fpriv->vm, args->in.rptr_va,

Quoted 7 lines
Quoted 6 lines

PAGE_SIZE) ||

Quoted 2 lines

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

I think the sizes here should be AMDGPU_GPU_PAGE_SIZE rather than PAGE_SIZE

Yes, even the two value are equal but that more sense for validating the GPU VA.

Well, they don't have to be. E.g., if you compile the kernel with a different page size they won't be equal. Plus some other platforms default to a non-4K page.

Alex

Quoted 113 lines
Quoted 52 lines
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+               queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;
+               kfree(queue);
+               goto unlock;
+       }
        queue->doorbell_handle = args->in.doorbell_handle;
        queue->queue_type = args->in.ip_type;
        queue->vm = &fpriv->vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
index 694f850d102e..0eb2a9c2e340 100644

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -0
@@ -135,4 +135,6 @@ int
 amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,

int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,

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

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
                                                   u32 idx);
 
+int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
+                       u64 expected_size);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 1457fb49a794..6e29e85bbf9f 100644
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

init function */ @@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

Quoted 11 lines
        userq_props->doorbell_index = queue->doorbell_index;
        userq_props->fence_address = queue->fence_drv->gpu_addr;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+       if (adev->gfx.funcs->get_gfx_shadow_info)
+               adev->gfx.funcs->get_gfx_shadow_info(adev,
+ &shadow_info, true);
        if (queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
                struct drm_amdgpu_userq_mqd_compute_gfx11

*compute_mqd;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -247,6 +250,12 @@ 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
                        goto free_mqd;
                }
 
Quoted 8 lines

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

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -0
+               if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd-
 eop_va,
+                                       max_t(u32, PAGE_SIZE,

AMDGPU_GPU_PAGE_SIZE))) {

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

Rather than setting the queue->state, just return -EINVAL. We shouldn't create the queue in the first place if the addresses are invalid.

Note.

Quoted 51 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+                       goto free_mqd;
+               }
+
                userq_props->eop_gpu_addr = compute_mqd->eop_va;
                userq_props->hqd_pipe_priority =

AMDGPU_GFX_PIPE_PRIO_NORMAL;

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

Quoted 3 lines
                userq_props->hqd_queue_priority =

AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -274,6 +283,13 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags &
 AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11-
 shadow_va,
+                                       shadow_info.shadow_size)) {
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

Same comment here.

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
+                       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 +0 -0
@@ -291,6 +307,12 @@ 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, · line 312

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
                        goto free_mqd;
                }
 
+               if (amdgpu_userq_input_va_validate(queue->vm, mqd_sdma_v11-
 csa_va,
+                                       shadow_info.csa_size)) {
+                       queue->state = AMDGPU_USERQ_STATE_INVALID_ARG;

and here.

Alex

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

--
2.34.1

#10 lore raw

PATCH v9 8/14 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  | 172 ++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  14 ++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +171 -1
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
 	return r;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr)
+{
+	struct amdgpu_bo_va_mapping *mapping;
+	u64 user_addr;
+	int r;
+
+	user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> 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;
+
+	/*
+	 * Need to unify the following userq va reference.
+	 *  mqd  bo
+	 *  rptr bo
+	 *  wptr bo
+	 *  eop  bo
+	 *  shadow bo
+	 *  csa bo
+	 */
+	/*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+	mapping->bo_va->queue_refcount++;
+
+	amdgpu_bo_unreserve(vm->root.bo);
+	return 0;
+
+out_err:
+	amdgpu_bo_unreserve(vm->root.bo);
+	return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
+{
+	struct amdgpu_bo_va_mapping *mapping;
+	u64 user_addr;
+	bool r;
+
+	user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT;
+
+	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)
+{
+
+	switch (queue->queue_type) {
+	case AMDGPU_HW_IP_GFX:
+		if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+			return true;
+		break;
+	case AMDGPU_HW_IP_COMPUTE:
+		if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
+			return true;
+		break;
+	case AMDGPU_HW_IP_DMA:
+		if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+		    amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+			return true;
+		break;
+	default:
+		break;
+	}
+
+	return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr)
+{
+	struct amdgpu_bo_va_mapping *mapping;
+	u64 user_addr;
+	int r;
+
+	user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> 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;
+	/*
+	 * TODO: It requires figuring out the root cause of userq va mapping
+	 * reference imbalance issue.
+	 */
+	/*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+	mapping->bo_va->queue_refcount--;
+
+	amdgpu_bo_unreserve(vm->root.bo);
+	return 0;
+
+out_err:
+	amdgpu_bo_unreserve(vm->root.bo);
+	return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue)
+{
+
+
+	amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+	amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+	amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+	switch (queue->queue_type) {
+	case AMDGPU_HW_IP_GFX:
+		amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+		amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+		break;
+	case AMDGPU_HW_IP_COMPUTE:
+		amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+		break;
+	case AMDGPU_HW_IP_DMA:
+		amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+		break;
+	default:
+		break;
+	}
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue)
+{
+	amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+	amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+	amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+	switch (queue->queue_type) {
+	case AMDGPU_HW_IP_GFX:
+		amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+		amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+		break;
+	case AMDGPU_HW_IP_COMPUTE:
+		amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+		break;
+	case AMDGPU_HW_IP_DMA:
+		amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
 			  struct amdgpu_usermode_queue *queue)
@@ -445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	queue->vm = &fpriv->vm;
 	queue->priority = priority;
 	queue->generation = amdgpu_vm_generation(adev, &fpriv->vm);
+	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;
@@ -475,7 +646,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto unlock;
 	}
 
-
 	qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);
 	if (qid < 0) {
 		drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +14 -0
index 0eb2a9c2e340..30067f80eadf 100644
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
 	enum amdgpu_userq_state state;
 	uint64_t		doorbell_handle;
 	uint64_t		doorbell_index;
+	uint64_t		queue_va;
+	uint64_t		rptr_va;
+	uint64_t		wptr_va;
+	uint64_t		eop_va;
+	uint64_t		shadow_va;
+	uint64_t		csa_va;
+
 	uint64_t		flags;
 	struct amdgpu_mqd_prop	*userq_prop;
 	struct amdgpu_userq_mgr *userq_mgr;
@@ -137,4 +144,11 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 
 int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
 			u64 expected_size);
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr);
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+			struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->hqd_active = false;
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+		queue->eop_va = compute_mqd->eop_va;
 		kfree(compute_mqd);
 	} else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
 		struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11;
@@ -283,6 +284,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		userq_props->csa_addr = mqd_gfx_v11->csa_va;
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+		queue->shadow_va = mqd_gfx_v11->shadow_va;
+		queue->csa_va = mqd_gfx_v11->csa_va;
 
 		if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
 					shadow_info.shadow_size)) {
@@ -314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
 		}
 
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
+		queue->csa_va = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
 	}
 
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 213 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  | 172 ++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  14 ++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +171 -1
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
        return r;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> 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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT;
+
+       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)
+{
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       default:
+               break;
+       }
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >> 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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue)
+{
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
@@ -445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev, &fpriv->vm);
+       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;
@@ -475,7 +646,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 +0 -0
index 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just push the address on the list. Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error on it.

Alex

Quoted 48 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +8 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr;
@@ -137,4 +144,11 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 
 int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
                        u64 expected_size);
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr);
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr);
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -1
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11;
@@ -283,6 +284,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                userq_props->csa_addr = mqd_gfx_v11->csa_va;
                userq_props->tmz_queue =
                        mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->shadow_va = mqd_gfx_v11->shadow_va;
+               queue->csa_va = mqd_gfx_v11->csa_va;
 
                if (amdgpu_userq_input_va_validate(queue->vm, mqd_gfx_v11->shadow_va,
                                        shadow_info.shadow_size)) {
@@ -314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
                }
 
                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }
 
--

2.34.1

#12 lore raw

RE: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 232 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 8 lines · Prike Liang

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 172

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

++

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

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

*vm, u64 addr,

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
        return r;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 78 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +78 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>
+ AMDGPU_GPU_PAGE_SHIFT;
+
+       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) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       default:
+               break;
+       }

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
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 63 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +63 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;

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

drm_amdgpu_userq *args)

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

Quoted 10 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev, &fpriv->vm);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       queue->queue_va = args->in.queue_va;
+       queue->rptr_va = args->in.rptr_va;
+       queue->wptr_va = args->in.wptr_va;
 
        db_info.queue_type = queue->queue_type;
        db_info.doorbell_handle = queue->doorbell_handle; @@ -475,7
+646,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 14 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -0
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just push the address on the list. Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error on it.

Is enough to track the queue_va for validating the userq VA unmap operation?
I proposed a similar solution for retrieving the userq by walking over VA list, but Christian rejected this for the overhead cause.

Quoted 58 lines

Alex

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

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4 +144,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
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

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

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

shadow_va,

                                        shadow_info.shadow_size)) { @@

-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 10 lines
                }

                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#13 lore raw

Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 246 lines · Prike

[Public]

Regards,
Prike

Quoted 232 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 8 lines · Prike Liang

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 172

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

++

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

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

*vm, u64 addr,

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
        return r;
 }
 
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 78 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +78 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>
+ AMDGPU_GPU_PAGE_SHIFT;
+
+       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) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       default:
+               break;
+       }

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
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 63 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +63 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;

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

drm_amdgpu_userq *args)

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

Quoted 10 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev, &fpriv->vm);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       queue->queue_va = args->in.queue_va;
+       queue->rptr_va = args->in.rptr_va;
+       queue->wptr_va = args->in.wptr_va;
 
        db_info.queue_type = queue->queue_type;
        db_info.doorbell_handle = queue->doorbell_handle; @@ -475,7
+646,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 14 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -0
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just push the address on the list. Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error on it.

Quoted 3 lines

Is enough to track the queue_va for validating the userq VA unmap operation?
I proposed a similar solution for retrieving the userq by walking over VA list, but Christian rejected this for the overhead cause.

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses and we don't want to unmap a virtual address that is critical for the queue before the queue is destroyed.

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it. @Christian Koenig did you have something else in mind?

Thanks,

Alex

Quoted 58 lines
Quoted 58 lines

Alex

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

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4 +144,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
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

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

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

shadow_va,

                                        shadow_info.shadow_size)) { @@

-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 10 lines
                }

                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#14 lore raw

RE: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 271 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 82 lines · On Wed, Aug 27, 2025 at 8:07 AM Liang, Prike…

[Public]

Regards,
Prike

Quoted 76 lines

-----Original Message-----
From: Alex Deucher <[email protected]<mailto:[email protected]>>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]<mailto:[email protected]>>
Cc: [email protected]<mailto:[email protected]>; Deucher, Alexander <[email protected]<mailto:[email protected]>>; Koenig, Christian <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track
helpers

Quoted 9 lines · On Tue, Aug 26, 2025 at 3:47 AM 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  | 172

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

++

Quoted 10 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int amdgpu_userq_input_va_validate(struct

amdgpu_vm

*vm, u64 addr,

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

Quoted 9 lines
        return r;

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +34 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>
Quoted 171 lines
Quoted 7 lines

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

Quoted 7 lines

+ AMDGPU_GPU_PAGE_SHIFT;

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

queue_refcount > 0)

Quoted 163 lines
Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +37 -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) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       default:
+               break;
+       }

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
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 63 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +63 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;

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

drm_amdgpu_userq *args)

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

Quoted 13 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev,

&fpriv->vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+       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; @@
-475,7
+646,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 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

push the address on the list.

Quoted 4 lines
Quoted 2 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error on it.

Is enough to track the queue_va for validating the userq VA unmap operation? I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line
Quoted 7 lines

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses and we don't want to unmap a virtual address that is critical for the queue before the queue is destroyed.

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it.
@Christian Koenig did you have something else in mind?

•       Limiting tracking to a subset of user-queue VAs would lower overhead. Would tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?
•       However, partial tracking risks missing invalid unmap events on untracked VAs.
•       Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va->queue_refcount, but if we only look up a partial VA list, we may still fail to identify the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

Thanks,
Prike

Quoted 65 lines

Thanks,

Alex

Quoted 61 lines
Quoted 61 lines

Alex

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

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4 +144,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
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

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

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

mqd_gfx_v11- shadow_va,
shadow_info.shadow_size))
{ @@
-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 10 lines
                }

                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#15 lore raw

Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 283 lines · Prike

[Public]

Regards,
Prike

Quoted 275 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 82 lines · Prike

[Public]

Regards,
Prike

Quoted 76 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 9 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  | 172

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

++

Quoted 10 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int amdgpu_userq_input_va_validate(struct

amdgpu_vm

*vm, u64 addr,

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

Quoted 9 lines
        return r;

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +34 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>
Quoted 171 lines
Quoted 7 lines

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

Quoted 7 lines

+ AMDGPU_GPU_PAGE_SHIFT;

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

queue_refcount > 0)

Quoted 163 lines
Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +37 -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) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
+                       return true;
+               break;
+       default:
+               break;
+       }

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
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 63 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +63 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;

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

drm_amdgpu_userq *args)

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

Quoted 13 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev,

&fpriv->vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+       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; @@
-475,7
+646,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 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

push the address on the list.

Quoted 4 lines
Quoted 2 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error on it.

Is enough to track the queue_va for validating the userq VA unmap operation? I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses and we don't want to unmap a virtual address that is critical for the queue before the queue is destroyed.

Quoted 6 lines
Quoted 3 lines

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it.
@Christian Koenig did you have something else in mind?

Limiting tracking to a subset of user-queue VAs would lower overhead. Would tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?

We should track all of the critical buffer VAs.

Quoted 2 lines

However, partial tracking risks missing invalid unmap events on untracked VAs.
Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va->queue_refcount, but if we only look up a partial VA list, we may still fail to identify the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

We can store the critical buffers list in the struct amdgpu_usermode_queue. Then walk the lists when we unmap the VA:

idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
    if (amdgpu_userq_critical_va(queue, va_address, va_size)) {
          preempt(queue);
          queue->status = AMDGPU_USERQ_STATE_INVALID_VA;
    }
}

Alex

Quoted 70 lines

Thanks,
Prike

Quoted 65 lines

Thanks,

Alex

Quoted 61 lines
Quoted 61 lines

Alex

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

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4 +144,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
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

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

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

mqd_gfx_v11- shadow_va,
shadow_info.shadow_size))
{ @@
-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 10 lines
                }

                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#16 lore raw

RE: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 324 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Tuesday, September 2, 2025 11:46 PM
To: Liang, Prike <[email protected]>
Cc: Koenig, Christian <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 36 lines · Prike

[Public]

Regards,
Prike

Quoted 28 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 17 lines · Prike

[Public]

Regards,
Prike

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Tue, Aug 26, 2025 at 3:47 AM Prike Liang <[email protected]>

Quoted 53 lines · wrote:
Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 10 lines

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  | 172

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

++

Quoted 10 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int amdgpu_userq_input_va_validate(struct

amdgpu_vm

*vm, u64 addr,

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

Quoted 9 lines
        return r;

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +21 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64

addr) {

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
Quoted 8 lines

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

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

queue_refcount > 0)

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -0
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+                       struct amdgpu_usermode_queue *queue) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va)
||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       case AMDGPU_HW_IP_COMPUTE:
> > > > > +               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       case AMDGPU_HW_IP_DMA:
> > > > > +               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       default:
> > > > > +               break;
> > > > > +       }
Quoted 129 lines
Quoted 129 lines
Quoted 129 lines
Quoted 129 lines

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
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 72 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +69 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
Quoted 3 lines

@@
-445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

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

Quoted 13 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev,

&fpriv->vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+       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; @@
-475,7
+646,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 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

Quoted 3 lines
Quoted 3 lines

push the address on the list.

Quoted 2 lines
Quoted 2 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error

on it.

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Is enough to track the queue_va for validating the userq VA unmap operation? I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses
and we don't want to unmap a virtual address that is critical for the queue before

the queue is destroyed.

Quoted 7 lines
Quoted 4 lines

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it. @Christian Koenig did you have something else in mind?

Limiting tracking to a subset of user-queue VAs would lower overhead. Would

tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?

We should track all of the critical buffer VAs.

Quoted 3 lines

However, partial tracking risks missing invalid unmap events on untracked VAs. Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va- queue_refcount, but if we only look up a partial VA list, we may still fail to identify

the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

We can store the critical buffers list in the struct amdgpu_usermode_queue. Then walk the lists when we unmap the VA:

Quoted 8 lines
idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
    if (amdgpu_userq_critical_va(queue, va_address, va_size)) {
          preempt(queue);
          queue->status = AMDGPU_USERQ_STATE_INVALID_VA;
    }
}

Alex

Thank you for the proposal. If there are no objections, I will update the userq retrieval method to walk the VA list instead of checking the mapping range.

Quoted 72 lines
Quoted 18 lines

Thanks,
Prike

Quoted 13 lines

Thanks,

Alex

Quoted 9 lines
Quoted 9 lines

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4 +144,11

@@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines

int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,

                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -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,

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines

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

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4 -0
+                       struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64
+addr); int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

struct drm_amdgpu_userq_mqd_gfx11
*mqd_gfx_v11; @@
-283,6 +284,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

mqd_gfx_v11- shadow_va,

Quoted 3 lines

shadow_info.shadow_size)) { @@
-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 10 lines
                }

                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#17 lore raw

Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 337 lines · Prike

[Public]

Regards,
Prike

Quoted 331 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Tuesday, September 2, 2025 11:46 PM
To: Liang, Prike <[email protected]>
Cc: Koenig, Christian <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 36 lines · Prike

[Public]

Regards,
Prike

Quoted 28 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 17 lines · Prike

[Public]

Regards,
Prike

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Tue, Aug 26, 2025 at 3:47 AM Prike Liang <[email protected]>

Quoted 53 lines · wrote:
Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 10 lines

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  | 172

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

++

Quoted 10 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int amdgpu_userq_input_va_validate(struct

amdgpu_vm

*vm, u64 addr,

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

Quoted 9 lines
        return r;

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +21 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64

addr) {

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
Quoted 8 lines

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

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

queue_refcount > 0)

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -0
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+                       struct amdgpu_usermode_queue *queue) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va)
||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       case AMDGPU_HW_IP_COMPUTE:
> > > > > +               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       case AMDGPU_HW_IP_DMA:
> > > > > +               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       default:
> > > > > +               break;
> > > > > +       }
Quoted 129 lines
Quoted 129 lines
Quoted 129 lines
Quoted 129 lines

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 72 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +69 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
Quoted 3 lines

@@
-445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

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

Quoted 13 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev,

&fpriv->vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+       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; @@
-475,7
+646,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 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

Quoted 3 lines
Quoted 3 lines

push the address on the list.

Quoted 2 lines
Quoted 2 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error

on it.

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Is enough to track the queue_va for validating the userq VA unmap operation? I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses
and we don't want to unmap a virtual address that is critical for the queue before

the queue is destroyed.

Quoted 7 lines
Quoted 4 lines

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it. @Christian Koenig did you have something else in mind?

Limiting tracking to a subset of user-queue VAs would lower overhead. Would

tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?

We should track all of the critical buffer VAs.

Quoted 3 lines

However, partial tracking risks missing invalid unmap events on untracked VAs. Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va- queue_refcount, but if we only look up a partial VA list, we may still fail to identify

the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

We can store the critical buffers list in the struct amdgpu_usermode_queue. Then walk the lists when we unmap the VA:

idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
    if (amdgpu_userq_critical_va(queue, va_address, va_size)) {
          preempt(queue);
          queue->status = AMDGPU_USERQ_STATE_INVALID_VA;
    }
}
Quoted 3 lines
Quoted 1 line

Alex

Thank you for the proposal. If there are no objections, I will update the userq retrieval method to walk the VA list instead of checking the mapping range.

Sure. @Christian Koenig any concerns on your end?

Thanks,

Alex

Quoted 73 lines
Quoted 73 lines
Quoted 19 lines

Thanks,
Prike

Quoted 13 lines

Thanks,

Alex

Quoted 9 lines
Quoted 9 lines

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4 +144,11

@@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines

int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,

                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -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,

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines

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

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4 -0
+                       struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64
+addr); int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

struct drm_amdgpu_userq_mqd_gfx11
*mqd_gfx_v11; @@
-283,6 +284,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

mqd_gfx_v11- shadow_va,

Quoted 3 lines

shadow_info.shadow_size)) { @@
-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 10 lines
                }

                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#18 lore raw

Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 339 lines · Alex Deucher
Quoted 337 lines · Prike

[Public]

Regards,
Prike

Quoted 331 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Tuesday, September 2, 2025 11:46 PM
To: Liang, Prike <[email protected]>
Cc: Koenig, Christian <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 36 lines · Prike

[Public]

Regards,
Prike

Quoted 28 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 17 lines · Prike

[Public]

Regards,
Prike

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Tue, Aug 26, 2025 at 3:47 AM Prike Liang <[email protected]>

Quoted 53 lines · wrote:
Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 10 lines

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  | 172

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

++

Quoted 10 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int amdgpu_userq_input_va_validate(struct

amdgpu_vm

*vm, u64 addr,

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

Quoted 9 lines
        return r;

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +21 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64

addr) {

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
Quoted 8 lines

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

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

queue_refcount > 0)

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -0
+               r = true;
+       else
+               r = false;
+       amdgpu_bo_unreserve(vm->root.bo);
+
+       return r;
+}
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+                       struct amdgpu_usermode_queue *queue) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
+                   amdgpu_userq_buffer_va_mapped(vm, queue->shadow_va)
||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       case AMDGPU_HW_IP_COMPUTE:
> > > > > +               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->eop_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       case AMDGPU_HW_IP_DMA:
> > > > > +               if (amdgpu_userq_buffer_va_mapped(vm, queue->queue_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->rptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->wptr_va) ||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue->csa_va))
> > > > > +                       return true;
> > > > > +               break;
> > > > > +       default:
> > > > > +               break;
> > > > > +       }
Quoted 129 lines
Quoted 129 lines
Quoted 129 lines
Quoted 129 lines

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr) {
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 72 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +69 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va mapping
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue *queue)
Quoted 3 lines

@@
-445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

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

Quoted 13 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev,

&fpriv->vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
+       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; @@
-475,7
+646,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 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

Quoted 3 lines
Quoted 3 lines

push the address on the list.

Quoted 2 lines
Quoted 2 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error

on it.

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Is enough to track the queue_va for validating the userq VA unmap operation? I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses
and we don't want to unmap a virtual address that is critical for the queue before

the queue is destroyed.

Quoted 7 lines
Quoted 4 lines

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it. @Christian Koenig did you have something else in mind?

Limiting tracking to a subset of user-queue VAs would lower overhead. Would

tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?

We should track all of the critical buffer VAs.

Quoted 3 lines

However, partial tracking risks missing invalid unmap events on untracked VAs. Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va- queue_refcount, but if we only look up a partial VA list, we may still fail to identify

the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

We can store the critical buffers list in the struct amdgpu_usermode_queue. Then walk the lists when we unmap the VA:

idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
    if (amdgpu_userq_critical_va(queue, va_address, va_size)) {
          preempt(queue);
          queue->status = AMDGPU_USERQ_STATE_INVALID_VA;
    }
}
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Alex

Thank you for the proposal. If there are no objections, I will update the userq retrieval method to walk the VA list instead of checking the mapping range.

Sure. @Christian Koenig any concerns on your end?

I talked to Christian, and he suggested we add a flag on the bo_va structure and set it for the critical buffers. If the flag is set, we'd wait for the bookkeeping fences when we unmap. Then on resume, when we validate the buffers, if those buffers are not present we skip the queue resume and set an error on the queue.

Alex

Quoted 78 lines

Thanks,

Alex

Quoted 73 lines
Quoted 73 lines
Quoted 19 lines

Thanks,
Prike

Quoted 13 lines

Thanks,

Alex

Quoted 9 lines
Quoted 9 lines

Alex

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4 +144,11

@@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines

int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,

                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -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,

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines

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

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4 -0
+                       struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64
+addr); int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

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

struct drm_amdgpu_userq_mqd_gfx11
*mqd_gfx_v11; @@
-283,6 +284,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

mqd_gfx_v11- shadow_va,

Quoted 3 lines

shadow_info.shadow_size)) { @@
-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 10 lines
                }

                userq_props->csa_addr = mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#19 lore raw

RE: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 384 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, September 4, 2025 11:26 PM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 39 lines · Alex Deucher
Quoted 37 lines · Prike

[Public]

Regards,
Prike

Quoted 31 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Tuesday, September 2, 2025 11:46 PM
To: Liang, Prike <[email protected]>
Cc: Koenig, Christian <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 19 lines · Prike

[Public]

Regards,
Prike

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Wed, Aug 27, 2025 at 8:07 AM Liang, Prike <[email protected]>

Quoted 43 lines · wrote:
Quoted 43 lines
Quoted 43 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

[Public]

Regards,
Prike

Quoted 12 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Tue, Aug 26, 2025 at 3:47 AM Prike Liang
<[email protected]>

Quoted 24 lines · wrote:
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines

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  | 172

++++++++++++++++++++-
++++++++++++++++++++drivers/gpu/drm/amd/amdgpu/amdgpu_us ++++++++++++++++++++erq.h
++++++++++++++++++++| 14

++

Quoted 11 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int

amdgpu_userq_input_va_validate(struct
amdgpu_vm

*vm, u64 addr,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
        return r;

}

+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64

addr) {

Quoted 97 lines
Quoted 50 lines
Quoted 50 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +22 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm
+*vm, u64

addr) {

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
Quoted 8 lines

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

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

queue_refcount > 0)

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

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue
+*queue) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ amdgpu_userq_buffer_va_mapped(vm,
+ queue->shadow_va)

||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue-

csa_va))

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

eop_va))

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

csa_va))

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+                       return true;
+               break;
+       default:
+               break;
+       }
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64

addr) {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va

mapping

Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 64 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +61 -0
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue
+*queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue
Quoted 3 lines

*queue) @@
-445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 10 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev,

&fpriv->vm);

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 =
Quoted 4 lines

queue->doorbell_handle; @@
-475,7
+646,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 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

push the address on the list.

Quoted 3 lines
Quoted 3 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error

on it.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Is enough to track the queue_va for validating the userq VA unmap

operation?

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses and we don't want to unmap a virtual address that is critical for the queue before

the queue is destroyed.

Quoted 6 lines
Quoted 4 lines

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it. @Christian Koenig did you have something else in mind?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Limiting tracking to a subset of user-queue VAs would lower overhead. Would

tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?

We should track all of the critical buffer VAs.

Quoted 1 line

However, partial tracking risks missing invalid unmap events on untracked

VAs.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 3 lines

Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va- queue_refcount, but if we only look up a partial VA list, we may still fail to identify

the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

We can store the critical buffers list in the struct amdgpu_usermode_queue. Then walk the lists when we unmap the VA:

idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
    if (amdgpu_userq_critical_va(queue, va_address, va_size)) {
          preempt(queue);
          queue->status = AMDGPU_USERQ_STATE_INVALID_VA;
    }
}
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Alex

Thank you for the proposal. If there are no objections, I will update the userq

Quoted 8 lines

retrieval method to walk the VA list instead of checking the mapping range.

Quoted 2 lines

Sure. @Christian Koenig any concerns on your end?

I talked to Christian, and he suggested we add a flag on the bo_va structure and set it for the critical buffers. If the flag is set, we'd wait for the bookkeeping fences when we unmap. Then on resume, when we validate the buffers, if those buffers are not present we skip the queue resume and set an error on the queue.

[Prike] Thank you, Alex, for confirming. For this solution, I plan to use bo_va->queue_refcount to track the userq VA mapping status, and the validation for restored BOs is already implemented in patch 13. I still have a few questions to confirm:

1) If a userq VA is being unmapped while bo_va->queue_refcount != 0, is it sufficient to wait on the bookkeeping fence attached to mapping->bo_va->base.bo->tbo.base.resv?
2) If that bookkeeping fence completes with an error (i.e., signal fails), should we still remove the mapping and add the VAs to the free list?
3) With this approach we no longer need to derive the userq from the VA. How should we classify this illegal unmap error and propagate it back to IGT/user space?

Quoted 88 lines

Alex

Quoted 26 lines

Thanks,

Alex

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

Thanks,
Prike

Quoted 15 lines

Thanks,

Alex

Quoted 11 lines
Quoted 11 lines

Alex

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4
+144,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,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4 -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,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

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

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

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
+                       struct amdgpu_usermode_queue
+*queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int

mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 11 lines
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11

*mqd_gfx_v11; @@
-283,6 +284,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

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

Quoted 3 lines

shadow_info.shadow_size)) { @@
-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 11 lines
                }

                userq_props->csa_addr =
mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#20 lore raw

Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 392 lines · Prike

[Public]

Regards,
Prike

Quoted 386 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, September 4, 2025 11:26 PM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 39 lines · Alex Deucher
Quoted 37 lines · Prike

[Public]

Regards,
Prike

Quoted 31 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Tuesday, September 2, 2025 11:46 PM
To: Liang, Prike <[email protected]>
Cc: Koenig, Christian <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 19 lines · Prike

[Public]

Regards,
Prike

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Wed, Aug 27, 2025 at 8:07 AM Liang, Prike <[email protected]>

Quoted 43 lines · wrote:
Quoted 43 lines
Quoted 43 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

[Public]

Regards,
Prike

Quoted 12 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Tue, Aug 26, 2025 at 3:47 AM Prike Liang
<[email protected]>

Quoted 24 lines · wrote:
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines

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  | 172

++++++++++++++++++++-
++++++++++++++++++++drivers/gpu/drm/amd/amdgpu/amdgpu_us ++++++++++++++++++++erq.h
++++++++++++++++++++| 14

++

Quoted 11 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 0aeb7a96ccbf..562d12f9d0d2 100644
@@ -76,6 +76,174 @@ int

amdgpu_userq_input_va_validate(struct
amdgpu_vm

*vm, u64 addr,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
        return r;

}

+int amdgpu_userq_buffer_va_get(struct amdgpu_vm *vm, u64

addr) {

Quoted 97 lines
Quoted 50 lines
Quoted 50 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +22 -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;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm
+*vm, u64

addr) {

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       bool r;
+
Quoted 8 lines

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

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

queue_refcount > 0)

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

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+
+bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue
+*queue) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ amdgpu_userq_buffer_va_mapped(vm,
+ queue->shadow_va)

||
> > > > > +                   amdgpu_userq_buffer_va_mapped(vm, queue-

csa_va))

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

eop_va))

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ amdgpu_userq_buffer_va_mapped(vm, queue-

csa_va))

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+                       return true;
+               break;
+       default:
+               break;
+       }
+
+       return false;
+}
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64

addr) {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>

AMDGPU_GPU_PAGE_SHIFT;

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

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

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -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;
+       /*
+        * TODO: It requires figuring out the root cause of userq va

mapping

Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 111 lines
Quoted 64 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +61 -0
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL;
+}
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue
+*queue) {
+
+
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
+       amdgpu_userq_buffer_va_put(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_put(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_put(vm, queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue->shadow_va);
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr *uq_mgr,
                          struct amdgpu_usermode_queue
Quoted 3 lines

*queue) @@
-445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 10 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation = amdgpu_vm_generation(adev,

&fpriv->vm);

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 =
Quoted 4 lines

queue->doorbell_handle; @@
-475,7
+646,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 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

push the address on the list.

Quoted 3 lines
Quoted 3 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error

on it.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Is enough to track the queue_va for validating the userq VA unmap

operation?

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses and we don't want to unmap a virtual address that is critical for the queue before

the queue is destroyed.

Quoted 6 lines
Quoted 4 lines

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check it. @Christian Koenig did you have something else in mind?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Limiting tracking to a subset of user-queue VAs would lower overhead. Would

tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?

We should track all of the critical buffer VAs.

Quoted 1 line

However, partial tracking risks missing invalid unmap events on untracked

VAs.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 3 lines

Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va- queue_refcount, but if we only look up a partial VA list, we may still fail to identify

the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

We can store the critical buffers list in the struct amdgpu_usermode_queue. Then walk the lists when we unmap the VA:

idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
    if (amdgpu_userq_critical_va(queue, va_address, va_size)) {
          preempt(queue);
          queue->status = AMDGPU_USERQ_STATE_INVALID_VA;
    }
}
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Alex

Thank you for the proposal. If there are no objections, I will update the userq

retrieval method to walk the VA list instead of checking the mapping range.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Sure. @Christian Koenig any concerns on your end?

I talked to Christian, and he suggested we add a flag on the bo_va structure and set it for the critical buffers. If the flag is set, we'd wait for the bookkeeping fences when we unmap. Then on resume, when we validate the buffers, if those buffers are not present we skip the queue resume and set an error on the queue.

[Prike] Thank you, Alex, for confirming. For this solution, I plan to use bo_va->queue_refcount to track the userq VA mapping status, and the validation for restored BOs is already implemented in patch 13. I still have a few questions to confirm:

Maybe userq_refcount so we know it's tied to user queues.

Quoted 2 lines

1) If a userq VA is being unmapped while bo_va->queue_refcount != 0, is it sufficient to wait on the bookkeeping fence attached to mapping->bo_va->base.bo->tbo.base.resv?

I think we'd want to tie it to the eviction fence. I.e., the buffer will stay mapped until the queue is evicted (preempted).

Quoted 1 line

2) If that bookkeeping fence completes with an error (i.e., signal fails), should we still remove the mapping and add the VAs to the free list?

I think so. Double check what we do for other buffers in that case.

Quoted 1 line

3) With this approach we no longer need to derive the userq from the VA. How should we classify this illegal unmap error and propagate it back to IGT/user space?

I think the error handling would be the same as before. The user wouldn't see the error until we'd try and restore the preempted queue. When we validate the buffers before restoring the queue, we'd see that the critical buffers are not present. At that point we'd skip the restore of the queue and set an error on it.

Alex

Quoted 89 lines
Quoted 89 lines

Alex

Quoted 26 lines

Thanks,

Alex

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

Thanks,
Prike

Quoted 15 lines

Thanks,

Alex

Quoted 11 lines
Quoted 11 lines

Alex

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@ -137,4
+144,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,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4 -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,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

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

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

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
+                       struct amdgpu_usermode_queue
+*queue);
 #endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 6e29e85bbf9f..42d6cd90be59 100644
@@ -262,6 +262,7 @@ static int

mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 11 lines
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11

*mqd_gfx_v11; @@
-283,6 +284,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

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

Quoted 3 lines

shadow_info.shadow_size)) { @@
-314,6 +317,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 11 lines
                }

                userq_props->csa_addr =
mqd_sdma_v11->csa_va;
+               queue->csa_va = mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#21 lore raw

RE: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

[Public]

Regards,
Prike

Quoted 443 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Friday, September 5, 2025 11:00 PM
To: Liang, Prike <[email protected]>
Cc: Koenig, Christian <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

Quoted 17 lines · Prike

[Public]

Regards,
Prike

Quoted 11 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, September 4, 2025 11:26 PM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Wed, Sep 3, 2025 at 6:19 PM Alex Deucher <[email protected]>

Quoted 20 lines · wrote:
Quoted 20 lines
Quoted 20 lines · Prike
Quoted 18 lines

[Public]

Regards,
Prike

Quoted 12 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Tuesday, September 2, 2025 11:46 PM
To: Liang, Prike <[email protected]>
Cc: Koenig, Christian <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Mon, Sep 1, 2025 at 5:13 AM Liang, Prike <[email protected]>

Quoted 77 lines · wrote:
Quoted 77 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

[Public]

Regards,
Prike

Quoted 12 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, August 28, 2025 6:13 AM
To: Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Wed, Aug 27, 2025 at 8:07 AM Liang, Prike
<[email protected]>

Quoted 45 lines · wrote:
Quoted 45 lines
Quoted 45 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

[Public]

Regards,
Prike

Quoted 12 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, August 26, 2025 11:22 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v9 08/14] drm/amdgpu: add userq object va track helpers

On Tue, Aug 26, 2025 at 3:47 AM Prike Liang <[email protected]>

Quoted 26 lines · wrote:
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 5 lines

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

Quoted 7 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 172

++++++++++++++++++++-
++++++++++++++++++++drivers/gpu/drm/amd/amdgpu/amdgp ++++++++++++++++++++u_us erq.h ++++++++++++++++++++| 14

++

Quoted 10 lines
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |   4 +
 3 files changed, 189 insertions(+), 1 deletion(-)

diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
index 0aeb7a96ccbf..562d12f9d0d2 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
@@ -76,6 +76,174 @@ int
Quoted 2 lines

amdgpu_userq_input_va_validate(struct
amdgpu_vm

*vm, u64 addr,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
        return r;

}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+int amdgpu_userq_buffer_va_get(struct amdgpu_vm
+*vm, u64

addr) {

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

AMDGPU_GPU_PAGE_SHIFT;

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm,

user_addr);

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16 -0
+       if (!mapping)
+               goto out_err;
+
+       /*
+        * Need to unify the following userq va reference.
+        *  mqd  bo
+        *  rptr bo
+        *  wptr bo
+        *  eop  bo
+        *  shadow bo
+        *  csa bo
+        */
+       /*amdgpu_bo_ref(mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount++;
+
+       amdgpu_bo_unreserve(vm->root.bo);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL; }
+
+bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm
+*vm, u64

addr) {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

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

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

user_addr);

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
Quoted 2 lines

+ if (!IS_ERR_OR_NULL(mapping) &&
+ mapping->bo_va-

queue_refcount > 0)

Quoted 9 lines

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

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

*vm,

Quoted 67 lines
Quoted 67 lines
Quoted 51 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+                       struct amdgpu_usermode_queue
+*queue) {
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               if
+ (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+
+ amdgpu_userq_buffer_va_mapped(vm,
+ queue->shadow_va)

||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

csa_va))

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+                       return true;
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               if
+ (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

eop_va))

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+                       return true;
+               break;
+       case AMDGPU_HW_IP_DMA:
+               if
+ (amdgpu_userq_buffer_va_mapped(vm, queue-

queue_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

rptr_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

wptr_va) ||

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+
+ amdgpu_userq_buffer_va_mapped(vm, queue-

csa_va))

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+                       return true;
+               break;
+       default:
+               break;
+       }
+
+       return false; }
+
+int amdgpu_userq_buffer_va_put(struct amdgpu_vm
+*vm, u64

addr) {

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       struct amdgpu_bo_va_mapping *mapping;
+       u64 user_addr;
+       int r;
+
+       user_addr = (addr & AMDGPU_GMC_HOLE_MASK) >>
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

AMDGPU_GPU_PAGE_SHIFT;

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       r = amdgpu_bo_reserve(vm->root.bo, false);
+       if (r)
+               return r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm,

user_addr);

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+       if (!mapping)
+               goto out_err;
+       /*
+        * TODO: It requires figuring out the root
+ cause of userq va

mapping

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
+        * reference imbalance issue.
+        */
+       /*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/
+       mapping->bo_va->queue_refcount--;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+       amdgpu_bo_unreserve(vm->root.bo);
+       return 0;
+
+out_err:
+       amdgpu_bo_unreserve(vm->root.bo);
+       return -EINVAL; }
+
+static void amdgpu_userq_buffer_vas_get(struct amdgpu_vm

*vm,

Quoted 37 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+                       struct amdgpu_usermode_queue
+*queue) {
+
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+       amdgpu_userq_buffer_va_get(vm, queue->queue_va);
+       amdgpu_userq_buffer_va_get(vm, queue->rptr_va);
+       amdgpu_userq_buffer_va_get(vm,
+ queue->wptr_va);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_get(vm, queue-

shadow_va);

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -0
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_get(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_get(vm, queue->csa_va);
+               break;
+       default:
+               break;
+       }
+}
+
+int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
+                       struct amdgpu_usermode_queue *queue) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -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);
+
+       switch (queue->queue_type) {
+       case AMDGPU_HW_IP_GFX:
+               amdgpu_userq_buffer_va_put(vm, queue-

shadow_va);

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -0
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
+       case AMDGPU_HW_IP_COMPUTE:
+               amdgpu_userq_buffer_va_put(vm, queue->eop_va);
+               break;
+       case AMDGPU_HW_IP_DMA:
+               amdgpu_userq_buffer_va_put(vm, queue->csa_va);
+               break;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
+       default:
+               break;
+       }
+       return 0;
+}
+
 static int
 amdgpu_userq_unmap_helper(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 63 lines
Quoted 63 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 5 lines

struct
amdgpu_usermode_queue
*queue) @@
-445,6 +613,9 @@ amdgpu_userq_create(struct drm_file *filp, union

drm_amdgpu_userq *args)

Quoted 10 lines
        queue->vm = &fpriv->vm;
        queue->priority = priority;
        queue->generation =

amdgpu_vm_generation(adev, &fpriv->vm);

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 =
Quoted 4 lines

queue->doorbell_handle; @@
-475,7
+646,6 @@ amdgpu_userq_create(struct drm_file *filp, +union drm_amdgpu_userq

*args)

Quoted 6 lines
                goto unlock;
        }

-

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

1,

AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);

Quoted 9 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 0eb2a9c2e340..30067f80eadf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h struct amdgpu_usermode_queue { · line 62

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
@@ -54,6 +54,13 @@ struct amdgpu_usermode_queue {
        enum amdgpu_userq_state state;
        uint64_t                doorbell_handle;
        uint64_t                doorbell_index;
+       uint64_t                queue_va;
+       uint64_t                rptr_va;
+       uint64_t                wptr_va;
+       uint64_t                eop_va;
+       uint64_t                shadow_va;
+       uint64_t                csa_va;

Just store a list of critical virtual addresses. Otherwise we are going to have a ton of IP specific things in here. For each critical address, just

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

push the address on the list.

Quoted 3 lines
Quoted 3 lines

Then in the VM unmap code, just walk the list for each queue and if the user tries to umap a critical buffer, preempt the queue and set an error

on it.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Is enough to track the queue_va for validating the userq VA unmap

operation?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

I proposed a similar solution for retrieving the userq by walking over VA list, but

Christian rejected this for the overhead cause.

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

I think queue creation and unmap are the only cases that we care about. We don't want to create a queue with an invalid addresses and we don't want to unmap a virtual address that is critical for the queue before

the queue is destroyed.

Quoted 3 lines
Quoted 3 lines

I think all we need to do in the unmap case is to walk the critical VA lists for each user queue associated with the VM and check

it.

Quoted 33 lines
Quoted 33 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

@Christian Koenig did you have something else in mind?

Limiting tracking to a subset of user-queue VAs would lower overhead. Would

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

tracking only queue_va, rptr_va, and wptr_va be sufficient to validate user-queue VA unmaps?

We should track all of the critical buffer VAs.

Quoted 2 lines

However, partial tracking risks missing invalid unmap events on untracked

VAs.

Quoted 20 lines
Quoted 20 lines
Quoted 9 lines
Quoted 3 lines

Although patch #14 can detect invalid user-queue VA unmaps by checking bo_va- queue_refcount, but if we only look up a partial VA list, we may still fail to identify

the specific queue whose VA was improperly unmapped, and therefore we cannot preempt and mark that queue as invalid. Thought?

We can store the critical buffers list in the struct amdgpu_usermode_queue. Then walk the lists when we unmap the VA:

Quoted 8 lines
idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
    if (amdgpu_userq_critical_va(queue, va_address, va_size)) {
          preempt(queue);
          queue->status = AMDGPU_USERQ_STATE_INVALID_VA;
    }
}

Alex

Thank you for the proposal. If there are no objections, I will update the userq

Quoted 8 lines
Quoted 8 lines

retrieval method to walk the VA list instead of checking the mapping range.

Quoted 2 lines

Sure. @Christian Koenig any concerns on your end?

I talked to Christian, and he suggested we add a flag on the bo_va structure and set it for the critical buffers. If the flag is set, we'd wait for the bookkeeping fences when we unmap. Then on resume, when we validate the buffers, if those buffers are not present we skip the queue

Quoted 8 lines

resume and set an error on the queue.

Quoted 3 lines

[Prike] Thank you, Alex, for confirming. For this solution, I plan to use bo_va- queue_refcount to track the userq VA mapping status, and the validation for

restored BOs is already implemented in patch 13. I still have a few questions to confirm:

Maybe userq_refcount so we know it's tied to user queues.

[Prike] The queue_refcount was originally introduced for KFD. Reusing it may support the KFD–KGD userq unification effort by avoiding a parallel track mechanism.

Quoted 8 lines
Quoted 3 lines

1) If a userq VA is being unmapped while bo_va->queue_refcount != 0, is it

sufficient to wait on the bookkeeping fence attached to mapping->bo_va->base.bo-

Quoted 1 line

tbo.base.resv?

I think we'd want to tie it to the eviction fence. I.e., the buffer will stay mapped until the queue is evicted (preempted).

[Prike] The eviction fence has already been added to mapping->bo_va->base.bo->tbo.base.resv.
We can wait on it by iterating the unmapping BO’s reservation (dma_resv) fences and waiting for the eviction fence to signal.

Quoted 4 lines
Quoted 1 line

2) If that bookkeeping fence completes with an error (i.e., signal fails), should we

still remove the mapping and add the VAs to the free list?

I think so. Double check what we do for other buffers in that case.

[Prike] Yes, further VA mapping cleanup is required even if the bookkeeping fence fails to signal.

Quoted 107 lines
Quoted 1 line

3) With this approach we no longer need to derive the userq from the VA. How

should we classify this illegal unmap error and propagate it back to IGT/user space?

I think the error handling would be the same as before. The user wouldn't see the error until we'd try and restore the preempted queue.
When we validate the buffers before restoring the queue, we'd see that the critical buffers are not present. At that point we'd skip the restore of the queue and set an error on it.

Alex

Quoted 95 lines
Quoted 95 lines

Alex

Quoted 28 lines

Thanks,

Alex

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

Thanks,
Prike

Quoted 17 lines

Thanks,

Alex

Quoted 13 lines
Quoted 13 lines

Alex

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -1
+
        uint64_t                flags;
        struct amdgpu_mqd_prop  *userq_prop;
        struct amdgpu_userq_mgr *userq_mgr; @@
-137,4
+144,11 @@ int

amdgpu_userq_start_sched_for_enforce_isolation(struc t amdgpu_device *adev,

int amdgpu_userq_input_va_validate(struct amdgpu_vm
*vm, u64

addr,

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
                        u64 expected_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -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,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

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

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

Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
+                       struct amdgpu_usermode_queue
+*queue);
 #endif

diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 6e29e85bbf9f..42d6cd90be59 100644

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
@@ -262,6 +262,7 @@ static int

mes_userq_mqd_create(struct

amdgpu_userq_mgr *uq_mgr,

Quoted 11 lines
                userq_props->hqd_active = false;
                userq_props->tmz_queue =
                        mqd_user->flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
+               queue->eop_va = compute_mqd->eop_va;
                kfree(compute_mqd);
        } else if (queue->queue_type == AMDGPU_HW_IP_GFX) {
                struct drm_amdgpu_userq_mqd_gfx11

*mqd_gfx_v11; @@
-283,6 +284,8 @@ static int
mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

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

AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;

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

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

Quoted 3 lines

shadow_info.shadow_size)) { @@
-314,6 +317,7 @@ static int
mes_userq_mqd_create(struct amdgpu_userq_mgr

*uq_mgr,

Quoted 12 lines
                }

                userq_props->csa_addr =
mqd_sdma_v11->csa_va;
+               queue->csa_va =
+ mqd_sdma_v11->csa_va;
                kfree(mqd_sdma_v11);
        }

--
2.34.1

#22 lore raw

PATCH v9 9/14 drm/amdgpu: track the userq bo va for its obj management

In reply to Prike Liang

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
index 562d12f9d0d2..6cdfeb224f6c 100644
@@ -306,6 +306,8 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
 	struct amdgpu_device *adev = uq_mgr->adev;
 	const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
 
+	/* Drop the userq reference. */
+	amdgpu_userq_buffer_vas_put(queue->vm, queue);
 	uq_funcs->mqd_destroy(uq_mgr, queue);
 	amdgpu_userq_fence_driver_free(queue);
 	idr_remove(&uq_mgr->userq_idr, queue_id);
@@ -646,6 +648,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto unlock;
 	}
 
+	/* refer to the userq objects vm bo*/
+	amdgpu_userq_buffer_vas_get(queue->vm, queue);
+
 	qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);
 	if (qid < 0) {
 		drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n");
Signature · 1 line
2.34.1
#23 lore raw

PATCH v9 10/14 drm/amdgpu: validate the userq va before destroying

In reply to Prike Liang

The user queue object destroy requires ensuring its VA keeps mapping prior to the queue being destroyed. Otherwise, it seems a bug in the user space or VA freed wrongly, and the kernel driver should report an invalidated state to the user IOCTL request.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
index 6cdfeb224f6c..e5891674b4d0 100644
@@ -520,6 +520,13 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		amdgpu_bo_unreserve(queue->db_obj.obj);
 	}
 	amdgpu_bo_unref(&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))
+		queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
+
 	r = amdgpu_userq_unmap_helper(uq_mgr, queue);
 	/*TODO: It requires a reset for userq hw unmap error*/
 	if (unlikely(r != AMDGPU_USERQ_STATE_UNMAPPED)) {
Signature · 1 line
2.34.1
#24 lore raw

PATCH v9 11/14 drm/amdgpu: keeping waiting userq fence infinitely

In reply to Prike Liang

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

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

PATCH v9 12/14 drm/amdgpu: clean up the amdgpu_userq_active()

In reply to Prike Liang

This is no invocation for amdgpu_userq_active().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 ----------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  2 --
 2 files changed, 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -16
index a78c2caeef41..3f8343599deb 100644
@@ -319,22 +319,6 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
 	kfree(queue);
 }
 
-int
-amdgpu_userq_active(struct amdgpu_userq_mgr *uq_mgr)
-{
-	struct amdgpu_usermode_queue *queue;
-	int queue_id;
-	int ret = 0;
-
-	mutex_lock(&uq_mgr->userq_mutex);
-	/* Resume all the queues for this process */
-	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id)
-		ret += queue->state == AMDGPU_USERQ_STATE_MAPPED;
-
-	mutex_unlock(&uq_mgr->userq_mutex);
-	return ret;
-}
-
 static struct amdgpu_usermode_queue *
 amdgpu_userq_find(struct amdgpu_userq_mgr *uq_mgr, int qid)
 {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -2
index 30067f80eadf..cf35b6140a3d 100644
@@ -123,8 +123,6 @@ void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,
 void amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 			struct amdgpu_eviction_fence *ev_fence);
 
-int amdgpu_userq_active(struct amdgpu_userq_mgr *uq_mgr);
-
 void amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *userq_mgr,
 				  struct amdgpu_eviction_fence_mgr *evf_mgr);
 
Signature · 1 line
2.34.1
#26 lore raw

PATCH v9 13/14 drm/amdgpu: validate the queue va for resuming the queue

In reply to Prike Liang

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
index 3f8343599deb..771f57d09060 100644
@@ -816,11 +816,18 @@ static int
 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 {
 	struct amdgpu_usermode_queue *queue;
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
 	int queue_id;
 	int ret = 0, r;
 
 	/* Resume all the queues for this process */
 	idr_for_each_entry(&uq_mgr->userq_idr, queue, queue_id) {
+
+		if (!amdgpu_userq_buffer_vas_mapped(&fpriv->vm, queue)) {
+			drm_file_err(uq_mgr->file, "trying restore queue without va mappping\n");
+			continue;
+		}
+
 		r = amdgpu_userq_map_helper(uq_mgr, queue);
 		if (r)
 			ret = r;
Signature · 1 line
2.34.1
#27 lore raw

PATCH v9 14/14 drm/amdgpu: validate userq va for GEM unmap

In reply to Prike Liang

This change validates the userq to see whether can be unmapped prior to the userq VA GEM unmap. The solution is based on the following idea:
1) Find out the GEM unmap VA belonds to which userq,
2) Check the userq fence fence whether is signaled,
3) If the userq attached fences signal failed, then mark it as illegal VA opt and give a warning message for this illegal userspace request.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 119 +++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  11 ++
 3 files changed, 131 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +118 -1
index 771f57d09060..af4a1857ea98 100644
@@ -676,7 +676,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		}
 	}
 
-
 	args->out.queue_id = qid;
 
 unlock:
@@ -1214,3 +1213,121 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
 }
+
+static bool amdgpu_userq_va_in_mapping_range(uint64_t va, struct amdgpu_bo_va_mapping *mapping)
+{
+	va = (va & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT;
+
+	if (va >= mapping->start && va < mapping->last)
+		return true;
+	return false;
+}
+
+/**
+ * amdgpu_userq_gem_va_unmap_queue_retrieve - find out userq by gem unmap va
+ * @queue: destinated userq for finding out from unmap va
+ * @mapping: the GEM unmap VA mapping
+ * Find out the corresponding userq by comparing
+ * the GEM unmap VA mapping with userq VAs.
+ */
+static bool amdgpu_userq_gem_va_unmap_queue_retrieve(struct amdgpu_usermode_queue *queue,
+                                                struct amdgpu_bo_va_mapping *mapping)
+{
+	switch (queue->queue_type) {
+	case AMDGPU_HW_IP_GFX:
+		if (amdgpu_userq_va_in_mapping_range(queue->queue_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->rptr_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->wptr_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->shadow_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->csa_va, mapping))
+			return true;
+		break;
+	case AMDGPU_HW_IP_COMPUTE:
+		if (amdgpu_userq_va_in_mapping_range(queue->queue_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->rptr_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->wptr_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->eop_va, mapping))
+			return true;
+		break;
+	case AMDGPU_HW_IP_DMA:
+		if (amdgpu_userq_va_in_mapping_range(queue->queue_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->rptr_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->wptr_va, mapping) ||
+		    amdgpu_userq_va_in_mapping_range(queue->csa_va, mapping))
+			return true;
+		break;
+	default:
+		break;
+	}
+
+	return false;
+}
+
+
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
+				struct amdgpu_bo_va_mapping *mapping)
+{
+	u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev);
+	struct amdgpu_usermode_queue *queue;
+	struct amdgpu_userq_mgr *uqm, *tmp;
+	int queue_id;
+	int ret;
+
+	if (!ip_mask)
+		return 0;
+
+	/**
+	 * validate the unmap va sequence:
+	 * 1) Find out the GEM unmap VA belonds to which userq,
+	 * 2) Check the userq fence whether is signaled,
+	 * 3) If the userq attached fences signal failed, then
+	 *    mark as invalid va opt and give a warning message
+	 *    for this illegal userspace request.
+	 */
+
+	if (mutex_trylock(&adev->userq_mutex)) {
+		list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
+
+			if (!mutex_trylock(&uqm->userq_mutex))
+				continue;
+
+			idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
+
+				if (!amdgpu_userq_gem_va_unmap_queue_retrieve(queue, mapping)) {
+					dev_dbg(uqm->adev->dev, "mapping: 0x%p not belond to queue id: %d\n",
+						mapping, queue_id);
+					continue;
+				}
+
+				if (queue->last_fence && !dma_fence_is_signaled(queue->last_fence)) {
+					drm_file_err(uqm->file, "an illegal VA unmap for the userq\n");
+					queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
+					ret = -ETIMEDOUT;
+					goto err;
+				}
+				/*
+				 * At here can't evict/unmap the HW userq directly since here just one kind of
+				 * VA unmapped, and some other VAs of userq may still be mapped. As to
+				 * this case, we might need to wait the userq fence signal, but the evict fence
+				 * is not bound to the queue since it may attach some other queue obj as well,
+				 * so here may always fail to wait the eviction signal.
+				 * FIXME: Here may need to unmap the userq to prevent the queue further used,
+				 * but there still requires debugging on the hang-up issue after unmapping the
+				 * HW queue here.
+				 */
+				/*amdgpu_userq_unmap_helper(uqm, queue);*/
+			}
+			mutex_unlock(&uqm->userq_mutex);
+		}
+	} else {
+			/* Maybe we need a try lock again before return*/
+			return -EBUSY;
+	}
+
+	mutex_unlock(&adev->userq_mutex);
+	return 0;
+err:
+	mutex_unlock(&uqm->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
+	return ret;
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index cf35b6140a3d..767e8562a7a1 100644
@@ -149,4 +149,6 @@ bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,
 int amdgpu_userq_buffer_va_put(struct amdgpu_vm *vm, u64 addr);
 int amdgpu_userq_buffer_vas_put(struct amdgpu_vm *vm,
 			struct amdgpu_usermode_queue *queue);
+int amdgpu_userq_gem_va_unmap_validate(struct amdgpu_device *adev,
+				struct amdgpu_bo_va_mapping *mapping);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -0
index f042372d9f2e..97952396d0e5 100644
@@ -1929,6 +1929,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 	struct amdgpu_bo_va_mapping *mapping;
 	struct amdgpu_vm *vm = bo_va->base.vm;
 	bool valid = true;
+	int r;
 
 	saddr /= AMDGPU_GPU_PAGE_SIZE;
 
@@ -1949,6 +1950,16 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 			return -ENOENT;
 	}
 
+	/* It's unlikely to happen that the mapping userq hasn't been idled
+	 * during user requests GEM unmap IOCTL except for forcing the unmap
+	 * from user space.
+	 */
+	if (bo_va->queue_refcount) {
+		r = amdgpu_userq_gem_va_unmap_validate(adev, mapping);
+		if (unlikely(r && r != -EBUSY))
+			dev_warn(adev->dev, "Here should be an improper unmap request from user space\n");
+	}
+
 	list_del(&mapping->list);
 	amdgpu_vm_it_remove(mapping, &vm->va);
 	mapping->bo_va = NULL;
Signature · 1 line
2.34.1