Skip to contentSkip to conversation

drm/amdgpu: add remove all process queues support for MES v12.1

PATCH In review Mukul Joshi 3 participants 4 messages Dec 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 drm/amdgpu: add remove all process queues support for MES v12.1

MES recently added support to remove all queues of a process with a single REMOVE_QUEUE request. Use it to optimize queue removal during process eviction and termination.

Add the new fields to the MES v12 REMOVE_QUEUE packet definition and to struct mes_remove_queue_input, program them in mes_v12_1_remove_hw_queue(), and add amdgpu_mes_remove_all_process_queues_supported() so callers can check whether the running MES firmware supports it. It is supported on GC 12.1 with MES scheduler firmware version 0x87 or newer.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c       |  9 +++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h       |  3 +++
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c        | 13 +++++++++----
 drivers/gpu/drm/amd/include/mes_v12_api_def.h | 10 +++++++++-
 4 files changed, 30 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +9 -0
index 27d6371d8d79..9ff3c1dd4842 100644
@@ -966,6 +966,15 @@ bool amdgpu_mes_queue_reset_by_mes_supported(struct amdgpu_device *adev)
 		((ip_maj == 12 && ip_min == 1) && mes_sched >= 0x7b);
 }
 
+bool amdgpu_mes_remove_all_process_queues_supported(struct amdgpu_device *adev)
+{
+	u32 ip_maj = IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0));
+	u32 ip_min = IP_VERSION_MIN(amdgpu_ip_version(adev, GC_HWIP, 0));
+	u32 mes_sched = adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;
+
+	return ip_maj == 12 && ip_min == 1 && mes_sched >= 0x87;
+}
+
 /* Fix me -- node_id is used to identify the correct MES instances in the future */
 static int amdgpu_mes_set_enforce_isolation(struct amdgpu_device *adev,
 					    uint32_t node_id, bool enable)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +3 -0
index 89051073321d..b29633ede051 100644
@@ -333,6 +333,8 @@ struct mes_remove_queue_input {
 	uint32_t	queue_type;
 	bool		remove_queue_after_reset;
 	uint32_t	gang_context_array_index;
+	bool		remove_all_process_queues;
+	uint32_t	pasid;
 };
 
 struct mes_map_legacy_queue_input {
@@ -738,6 +740,7 @@ static inline void amdgpu_mes_unlock(struct amdgpu_mes *mes)
 
 bool amdgpu_mes_suspend_resume_all_supported(struct amdgpu_device *adev);
 bool amdgpu_mes_queue_reset_by_mes_supported(struct amdgpu_device *adev);
+bool amdgpu_mes_remove_all_process_queues_supported(struct amdgpu_device *adev);
 
 int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +9 -4
index 84a3693db6ad..378d4278377c 100644
@@ -363,10 +363,15 @@ static int mes_v12_1_remove_hw_queue(struct amdgpu_mes *mes,
 	mes_remove_queue_pkt.header.opcode = MES_SCH_API_REMOVE_QUEUE;
 	mes_remove_queue_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
 
-	mes_remove_queue_pkt.doorbell_offset = input->doorbell_offset;
-	mes_remove_queue_pkt.gang_context_addr = input->gang_context_addr;
-	mes_remove_queue_pkt.queue_type =
-		convert_to_mes_queue_type(input->queue_type);
+	if (input->remove_all_process_queues) {
+		mes_remove_queue_pkt.remove_all_process_queues = 1;
+		mes_remove_queue_pkt.pasid = input->pasid;
+	} else {
+		mes_remove_queue_pkt.doorbell_offset = input->doorbell_offset;
+		mes_remove_queue_pkt.gang_context_addr = input->gang_context_addr;
+		mes_remove_queue_pkt.queue_type =
+			convert_to_mes_queue_type(input->queue_type);
+	}
 
 	return mes_v12_1_submit_pkt_and_poll_completion(mes,
 			xcc_id, AMDGPU_MES_SCHED_PIPE,
drivers/gpu/drm/amd/include/mes_v12_api_def.h +9 -1
index 090c9142674e..01d07b942347 100644
@@ -411,7 +411,13 @@ union MESAPI__REMOVE_QUEUE {
 			uint32_t preempt_legacy_gfx_queue : 1;
 			uint32_t unmap_legacy_queue	  : 1;
 			uint32_t remove_queue_after_reset : 1;
-			uint32_t reserved		  : 27;
+			/*
+			 * Remove all queues of the process identified by pasid,
+			 * doorbell_offset/gang_context_addr/gang_context_array_index
+			 * are ignored
+			 */
+			uint32_t remove_all_process_queues : 1;
+			uint32_t reserved		  : 26;
 		};
 		struct MES_API_STATUS		api_status;
 
@@ -424,6 +430,8 @@ union MESAPI__REMOVE_QUEUE {
 		enum MES_QUEUE_TYPE		queue_type;
 		uint64_t			timestamp;
 		uint32_t			gang_context_array_index;
+		/* Only valid when remove_all_process_queues is set */
+		uint32_t			pasid;
 	};
 
 	uint32_t max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
Signature · 1 line
2.56.0
#1 lore raw

PATCH v2 2/2 drm/amdkfd: remove all queues of a process with a single MES call

On process eviction, termination and scheduler halt/stop, KFD sends one MES REMOVE_QUEUE per active queue. If MES supports it, remove all queues of a process, by PASID, with a single request instead.

remove_queue_mes() sends this request when called with a NULL queue. On failure, the same request is passed to the MES queue reset path, which sends it again if no hung queue is found. If hung queues were reset instead, the other queues of the process are resumed, so send the request once more.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 139 ++++++++++++++----
 1 file changed, 113 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +113 -26
index 58c7da599752..35d7aef68f62 100644
@@ -79,7 +79,10 @@ static struct queue *find_queue_by_doorbell_offset(struct device_queue_manager *
 						   u32 doorbell_offset);
 static void set_queue_as_reset(struct device_queue_manager *dqm, struct queue *q,
 			       struct qcm_process_device *qpd, bool verbose);
-static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q);
+static int reset_queues_mes(struct device_queue_manager *dqm,
+			    struct kfd_process_device *pdd,
+			    struct mes_remove_queue_input *queue_input,
+			    unsigned int *hung_queue_count);
 
 static inline
 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
@@ -299,17 +302,35 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 	return r;
 }
 
+/* A queue that is active and not reset is still in MES */
+static bool qpd_has_mes_queue(struct qcm_process_device *qpd)
+{
+	struct queue *q;
+
+	list_for_each_entry(q, &qpd->queues_list, list)
+		if (q->properties.is_active && !q->properties.is_reset)
+			return true;
+
+	return false;
+}
+
+/*
+ * Remove queue q from MES. If q is NULL, remove all queues of the process
+ * with a single request, see amdgpu_mes_remove_all_process_queues_supported().
+ * This must be done while the queues are still marked active.
+ */
 static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, struct queue *q,
 					    struct qcm_process_device *qpd,
 					    bool is_for_reset,
 					    bool flush_mes_queue)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
+	unsigned int num_hung = 0;
 	int r;
 	struct mes_remove_queue_input queue_input;
 
-	/* queue was already removed during reset */
-	if (q->properties.is_reset)
+	/* queue(s) already removed during reset */
+	if (q ? q->properties.is_reset : !qpd_has_mes_queue(qpd))
 		return 0;
 
 	if (!dqm->sched_running || dqm->sched_halt)
@@ -318,12 +339,17 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
 		return -EIO;
 
 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
-	queue_input.doorbell_offset = q->properties.doorbell_off;
-	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
-	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
-	queue_input.remove_queue_after_reset = flush_mes_queue;
+	if (q) {
+		queue_input.doorbell_offset = q->properties.doorbell_off;
+		queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+		queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
+		queue_input.remove_queue_after_reset = flush_mes_queue;
+		queue_input.gang_context_array_index = q->gang_ctx_array_index;
+	} else {
+		queue_input.remove_all_process_queues = true;
+		queue_input.pasid = qpd_to_pdd(qpd)->pasid;
+	}
 	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
-	queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
 	amdgpu_mes_lock(&adev->mes);
 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
@@ -335,10 +361,26 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
 		return r;
 
 	/* remove_hw_queue failure indicates a queue hang. reset the queue */
-	r = reset_queues_mes(dqm, q);
+	queue_input.remove_queue_after_reset = false;
+	r = reset_queues_mes(dqm, qpd_to_pdd(qpd), &queue_input, &num_hung);
+
+	/* hung queues were reset and removed; remove the rest of the process */
+	if (!r && !q && num_hung && qpd_has_mes_queue(qpd)) {
+		if (!down_read_trylock(&adev->reset_domain->sem))
+			return -EIO;
+		amdgpu_mes_lock(&adev->mes);
+		r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
+		amdgpu_mes_unlock(&adev->mes);
+		up_read(&adev->reset_domain->sem);
+	}
+
 	if (r && amdgpu_gpu_recovery) {
-		dev_err(adev->dev, "failed to remove queue from MES, doorbell=0x%x\n",
-			q->properties.doorbell_off);
+		if (q)
+			dev_err(adev->dev, "failed to remove queue from MES, doorbell=0x%x\n",
+				q->properties.doorbell_off);
+		else
+			dev_err(adev->dev, "failed to remove queues of pasid 0x%x from MES\n",
+				queue_input.pasid);
 		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
 		kfd_hws_hang(dqm);
 	}
@@ -363,6 +405,8 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 
 static int remove_all_kfd_queues_mes(struct device_queue_manager *dqm)
 {
+	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
+	bool remove_all = amdgpu_mes_remove_all_process_queues_supported(adev);
 	struct device_process_node *cur;
 	struct device *dev = dqm->dev->adev->dev;
 	struct qcm_process_device *qpd;
@@ -371,6 +415,16 @@ static int remove_all_kfd_queues_mes(struct device_queue_manager *dqm)
 
 	list_for_each_entry(cur, &dqm->queues, list) {
 		qpd = cur->qpd;
+		if (remove_all) {
+			retval = remove_queue_mes(dqm, NULL, qpd);
+			if (retval) {
+				dev_err(dev, "%s: Failed to remove queues of pasid 0x%x for dev %d",
+					__func__, qpd_to_pdd(qpd)->pasid, dqm->dev->id);
+				return retval;
+			}
+			continue;
+		}
+
 		list_for_each_entry(q, &qpd->queues_list, list) {
 			if (q->properties.is_active) {
 				retval = remove_queue_mes(dqm, q, qpd);
@@ -452,15 +506,21 @@ int kfd_reset_queue_mes(struct device_queue_manager *dqm, int queue_type,
 	return reset_queue_mes(dqm, q, queue_type, pipe, queue, db);
 }
 
-static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
+/*
+ * queue_input is the remove request for the bad queue(s). If MES doesn't
+ * detect a hung queue, it is sent again before the queues are resumed.
+ * hung_queue_count (optional) returns the number of hung queues.
+ */
+static int reset_queues_mes(struct device_queue_manager *dqm,
+			    struct kfd_process_device *pdd,
+			    struct mes_remove_queue_input *queue_input,
+			    unsigned int *hung_queue_count)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
 	struct drm_wedge_task_info *info = NULL;
 	struct amdgpu_task_info *ti = NULL;
-	struct kfd_process_device *pdd;
 	unsigned int num_hung = 0;
 	int r = 0;
-	struct mes_remove_queue_input queue_input;
 
 	if (!amdgpu_mes_queue_reset_by_mes_supported(adev)) {
 		r = -ENOTRECOVERABLE;
@@ -471,18 +531,13 @@ static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
 	if (WARN_ON(dqm->detect_hang_count > 0))
 		return 0;
 
-	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
-	queue_input.doorbell_offset = q->properties.doorbell_off;
-	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
-	queue_input.gang_context_array_index = q->gang_ctx_array_index;
-	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
-	queue_input.remove_queue_after_reset = false;
-	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
 	/* pass the known bad queue info to the reset function */
-	r = amdgpu_gfx_reset_mes_compute(adev, dqm->dev->xcp, NULL, NULL, NULL, &num_hung, &queue_input);
+	r = amdgpu_gfx_reset_mes_compute(adev, dqm->dev->xcp, NULL, NULL, NULL, &num_hung,
+					 queue_input);
 	if (r)
 		goto fail;
-	pdd = kfd_get_process_device_data(q->device, q->process);
+	if (hung_queue_count)
+		*hung_queue_count = num_hung;
 	if (pdd) {
 		ti = amdgpu_vm_get_task_info_pasid(adev, pdd->pasid);
 		if (ti) {
@@ -1493,6 +1548,7 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
 	struct queue *q;
 	struct device *dev = dqm->dev->adev->dev;
 	struct kfd_process_device *pdd;
+	bool remove_all;
 	int retval = 0;
 
 	dqm_lock(dqm);
@@ -1514,6 +1570,17 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
 	if (dqm->dev->kfd->shared_resources.enable_mes)
 		pdd->last_evict_timestamp = get_jiffies_64();
 
+	remove_all = dqm->dev->kfd->shared_resources.enable_mes &&
+		     amdgpu_mes_remove_all_process_queues_supported(dqm->dev->adev);
+	if (remove_all) {
+		retval = remove_queue_mes(dqm, NULL, qpd);
+		if (retval) {
+			dev_err(dev, "Failed to evict queues of pasid 0x%x\n",
+				pdd->pasid);
+			goto out;
+		}
+	}
+
 	/* Mark all queues as evicted. Deactivate all active queues on
 	 * the qpd.
 	 */
@@ -1525,7 +1592,7 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
 		q->properties.is_active = false;
 		decrement_queue_count(dqm, qpd, q);
 
-		if (dqm->dev->kfd->shared_resources.enable_mes) {
+		if (dqm->dev->kfd->shared_resources.enable_mes && !remove_all) {
 			retval = remove_queue_mes(dqm, q, qpd);
 			if (retval) {
 				dev_err(dev, "Failed to evict queue %d\n",
@@ -3286,6 +3353,7 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
 	enum kfd_unmap_queues_filter filter =
 		KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES;
 	bool found = false;
+	bool remove_all;
 
 	dqm_lock(dqm);
 
@@ -3298,6 +3366,15 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
 		filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
 	}
 
+	remove_all = dqm->dev->kfd->shared_resources.enable_mes &&
+		     amdgpu_mes_remove_all_process_queues_supported(dqm->dev->adev);
+	if (remove_all) {
+		retval = remove_queue_mes(dqm, NULL, qpd);
+		if (retval)
+			dev_err(dev, "Failed to remove queues of pasid 0x%x\n",
+				qpd_to_pdd(qpd)->pasid);
+	}
+
 	/* Clear all user mode queues */
 	list_for_each_entry(q, &qpd->queues_list, list) {
 		if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
@@ -3308,7 +3385,7 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
 		if (q->properties.is_active) {
 			decrement_queue_count(dqm, qpd, q);
 
-			if (dqm->dev->kfd->shared_resources.enable_mes) {
+			if (dqm->dev->kfd->shared_resources.enable_mes && !remove_all) {
 				retval = remove_queue_mes(dqm, q, qpd);
 				if (retval)
 					dev_err(dev, "Failed to remove queue %d\n",
@@ -3610,7 +3687,17 @@ int kfd_dqm_suspend_bad_queue_mes(struct kfd_node *knode, u32 pasid, u32 doorbel
 
 		list_for_each_entry(q, &qpd->queues_list, list) {
 			if (q->doorbell_id == doorbell_id && q->properties.is_active) {
-				if (reset_queues_mes(dqm, q))
+				struct mes_remove_queue_input queue_input;
+
+				memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
+				queue_input.doorbell_offset = q->properties.doorbell_off;
+				queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+				queue_input.gang_context_array_index = q->gang_ctx_array_index;
+				queue_input.queue_type =
+					convert_to_amdgpu_ring_type(q->properties.type);
+				queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
+
+				if (reset_queues_mes(dqm, pdd, &queue_input, NULL))
 					dev_dbg(knode->adev->dev,
 					"Fail to remove bad queue. doorbell_id 0x%x\n", q->doorbell_id);
 				q->properties.is_evicted = true;
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCHv2 2/2] drm/amdkfd: remove all queues of a process with a single MES call

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/amdkfd: missing MES remove-all helper and struct fields break build - [High] drm/amdkfd…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/amdkfd: missing MES remove-all helper and struct fields break build
- [High] drm/amdkfd: evict_process_queues_cpsch() skips deactivation on MES failure
- [Medium] drm/amdkfd: remove_queue_mes() checks stale CP count before decrement
- [Medium] drm/amdkfd: PASID-wide MES queue removal lacks KFD-only scoping
--

commit e0606d38e1af80c9aaba07fcf657c4bd8a4b5eac
Author: Mukul Joshi <[email protected]>

drm/amdkfd: remove all queues of a process with a single MES call

This commit updates KFD to remove all active MES queues of a process in a single request by PASID during eviction, termination, and scheduler halt/stop when MES supports it. It also adapts the MES queue reset path to retry the bulk request or remove remaining queues after resetting hung ones.

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -0
index cc7a1f770408f..f8f7291e4266d 100644

[ ... ]

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2 -4
@@ -318,12 +339,17 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
 		return -EIO;
 
 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
-	queue_input.doorbell_offset = q->properties.doorbell_off;
-	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
-	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
-	queue_input.remove_queue_after_reset = flush_mes_queue;
+	if (q) {
+		queue_input.doorbell_offset = q->properties.doorbell_off;

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st · line 326

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +8 -1
+		queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+		queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
+		queue_input.remove_queue_after_reset = flush_mes_queue;
+		queue_input.gang_context_array_index = q->gang_ctx_array_index;
+	} else {
+		queue_input.remove_all_process_queues = true;
+		queue_input.pasid = qpd_to_pdd(qpd)->pasid;
+	}
 	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
-	queue_input.gang_context_array_index = q->gang_ctx_array_index;

[Severity: High]
Are prerequisite amdgpu MES patches missing from this commit?

struct mes_remove_queue_input in drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h does not have remove_all_process_queues or pasid fields, and amdgpu_mes_remove_all_process_queues_supported() is neither declared nor defined anywhere in the tree. This breaks the build whenever CONFIG_HSA_AMD is enabled.

[Severity: Medium]
Can this PASID-wide MES removal also unmap KGD usermode queues that share the same VM?

A process can acquire a DRM render file's VM in KFD via kfd_process_device_init_vm(), which sets pdd->pasid = avm->pasid, and also create KGD userqueues on the same fpriv->vm via amdgpu_userq_create(). mes_userq_map() registers those KGD userqueues in MES under the same PASID, and the bulk request here passes only pasid and xcc_id with no KFD-only discriminator.

If MES firmware removes all queues for that PASID rather than scoping the request to KFD queues, KGD userqueues would be unmapped while amdgpu_userq still tracks them as mapped. KFD restore paths only re-add KFD queues, and a later amdgpu_userq_destroy() failure to unmap the already-removed queue schedules uq_mgr->reset_work for a full GPU reset.

[ ... ]

Quoted 19 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11 -0
@@ -1514,6 +1570,17 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
 	if (dqm->dev->kfd->shared_resources.enable_mes)
 		pdd->last_evict_timestamp = get_jiffies_64();
 
+	remove_all = dqm->dev->kfd->shared_resources.enable_mes &&
+		     amdgpu_mes_remove_all_process_queues_supported(dqm->dev->adev);
+	if (remove_all) {
+		retval = remove_queue_mes(dqm, NULL, qpd);
+		if (retval) {
+			dev_err(dev, "Failed to evict queues of pasid 0x%x\n",
+				pdd->pasid);
+			goto out;
+		}
+	}
+
 	/* Mark all queues as evicted. Deactivate all active queues on
 	 * the qpd.
 	 */

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int evict_process_queues_cpsch(struct device_queue_manager *dqm, · line 1595

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -1
@@ -1525,7 +1592,7 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
 		q->properties.is_active = false;
 		decrement_queue_count(dqm, qpd, q);
 
-		if (dqm->dev->kfd->shared_resources.enable_mes) {
+		if (dqm->dev->kfd->shared_resources.enable_mes && !remove_all) {

[Severity: High]
Does bailing out to out here leave every queue marked active and unevicted while qpd->evicted has already been incremented to 1?

When remove_queue_mes() returns -EIO (for instance during a concurrent GPU reset), kfd_process_evict_queues() treats -EIO as a completed eviction and expects the queues to have been marked evicted and inactive. Because the loop below is skipped, q->properties.is_active stays true and decrement_queue_count() is never called; a subsequent restore_process_queues_cpsch() then calls increment_queue_count() again, corrupting active_queue_count, active_cp_queue_count, and gws_queue_count.

On a non--EIO error, kfd_process_evict_queues() does not restore this PDD, leaving qpd->evicted stuck at 1 while its queues remain active so future evictions are silently skipped.

[ ... ]

Quoted 17 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +9 -0
@@ -3298,6 +3366,15 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
 		filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
 	}
 
+	remove_all = dqm->dev->kfd->shared_resources.enable_mes &&
+		     amdgpu_mes_remove_all_process_queues_supported(dqm->dev->adev);
+	if (remove_all) {
+		retval = remove_queue_mes(dqm, NULL, qpd);
+		if (retval)
+			dev_err(dev, "Failed to remove queues of pasid 0x%x\n",
+				qpd_to_pdd(qpd)->pasid);
+	}
+
 	/* Clear all user mode queues */
 	list_for_each_entry(q, &qpd->queues_list, list) {
 		if (q->properties.type == KFD_QUEUE_TYPE_SDMA)

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int process_termination_cpsch(struct device_queue_manager *dqm, · line 3388

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -1
@@ -3308,7 +3385,7 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
 		if (q->properties.is_active) {
 			decrement_queue_count(dqm, qpd, q);
 
-			if (dqm->dev->kfd->shared_resources.enable_mes) {
+			if (dqm->dev->kfd->shared_resources.enable_mes && !remove_all) {

[Severity: Medium]
Can calling remove_queue_mes() before decrement_queue_count() here and in evict_process_queues_cpsch() leave dqm->notify_unmap_work armed on GFX11?

remove_queue_mes() only cancels dqm->notify_unmap_work when dqm->active_cp_queue_count <= get_cp_queues_num(dqm). Because decrement_queue_count() now runs after remove_queue_mes(), the check sees the stale pre-decrement count and skips cancellation even when eviction or termination clears oversubscription, causing mes_notify_unmap_work_handler() to fire a spurious amdgpu_mes_notify_unmap_queue() RPC.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCHv2 2/2] drm/amdkfd: remove all queues of a process with a single MES call

In reply to Mukul Joshi

Series is

Quoted 283 lines · Mukul Joshi

On process eviction, termination and scheduler halt/stop, KFD sends one MES REMOVE_QUEUE per active queue. If MES supports it, remove all queues of a process, by PASID, with a single request instead.

remove_queue_mes() sends this request when called with a NULL queue. On failure, the same request is passed to the MES queue reset path, which sends it again if no hung queue is found. If hung queues were reset instead, the other queues of the process are resumed, so send the request once more.

---
  .../drm/amd/amdkfd/kfd_device_queue_manager.c | 139 ++++++++++++++----
  1 file changed, 113 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +113 -26
index 58c7da599752..35d7aef68f62 100644
@@ -79,7 +79,10 @@ static struct queue *find_queue_by_doorbell_offset(struct device_queue_manager *
  						   u32 doorbell_offset);
  static void set_queue_as_reset(struct device_queue_manager *dqm, struct queue *q,
  			       struct qcm_process_device *qpd, bool verbose);
-static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q);
+static int reset_queues_mes(struct device_queue_manager *dqm,
+			    struct kfd_process_device *pdd,
+			    struct mes_remove_queue_input *queue_input,
+			    unsigned int *hung_queue_count);
 
  static inline
  enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
@@ -299,17 +302,35 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
  	return r;
  }
 
+/* A queue that is active and not reset is still in MES */
+static bool qpd_has_mes_queue(struct qcm_process_device *qpd)
+{
+	struct queue *q;
+
+	list_for_each_entry(q, &qpd->queues_list, list)
+		if (q->properties.is_active && !q->properties.is_reset)
+			return true;
+
+	return false;
+}
+
+/*
+ * Remove queue q from MES. If q is NULL, remove all queues of the process
+ * with a single request, see amdgpu_mes_remove_all_process_queues_supported().
+ * This must be done while the queues are still marked active.
+ */
  static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, struct queue *q,
  					    struct qcm_process_device *qpd,
  					    bool is_for_reset,
  					    bool flush_mes_queue)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
+	unsigned int num_hung = 0;
  	int r;
  	struct mes_remove_queue_input queue_input;
 
-	/* queue was already removed during reset */
-	if (q->properties.is_reset)
+	/* queue(s) already removed during reset */
+	if (q ? q->properties.is_reset : !qpd_has_mes_queue(qpd))
  		return 0;
 
  	if (!dqm->sched_running || dqm->sched_halt)
@@ -318,12 +339,17 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
  		return -EIO;
 
  	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
-	queue_input.doorbell_offset = q->properties.doorbell_off;
-	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
-	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
-	queue_input.remove_queue_after_reset = flush_mes_queue;
+	if (q) {
+		queue_input.doorbell_offset = q->properties.doorbell_off;
+		queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+		queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
+		queue_input.remove_queue_after_reset = flush_mes_queue;
+		queue_input.gang_context_array_index = q->gang_ctx_array_index;
+	} else {
+		queue_input.remove_all_process_queues = true;
+		queue_input.pasid = qpd_to_pdd(qpd)->pasid;
+	}
  	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
-	queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
  	amdgpu_mes_lock(&adev->mes);
  	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
@@ -335,10 +361,26 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
  		return r;
 
  	/* remove_hw_queue failure indicates a queue hang. reset the queue */
-	r = reset_queues_mes(dqm, q);
+	queue_input.remove_queue_after_reset = false;
+	r = reset_queues_mes(dqm, qpd_to_pdd(qpd), &queue_input, &num_hung);
+
+	/* hung queues were reset and removed; remove the rest of the process */
+	if (!r && !q && num_hung && qpd_has_mes_queue(qpd)) {
+		if (!down_read_trylock(&adev->reset_domain->sem))
+			return -EIO;
+		amdgpu_mes_lock(&adev->mes);
+		r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
+		amdgpu_mes_unlock(&adev->mes);
+		up_read(&adev->reset_domain->sem);
+	}
+
  	if (r && amdgpu_gpu_recovery) {
-		dev_err(adev->dev, "failed to remove queue from MES, doorbell=0x%x\n",
-			q->properties.doorbell_off);
+		if (q)
+			dev_err(adev->dev, "failed to remove queue from MES, doorbell=0x%x\n",
+				q->properties.doorbell_off);
+		else
+			dev_err(adev->dev, "failed to remove queues of pasid 0x%x from MES\n",
+				queue_input.pasid);
  		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
  		kfd_hws_hang(dqm);
  	}
@@ -363,6 +405,8 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 
  static int remove_all_kfd_queues_mes(struct device_queue_manager *dqm)
  {
+	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
+	bool remove_all = amdgpu_mes_remove_all_process_queues_supported(adev);
  	struct device_process_node *cur;
  	struct device *dev = dqm->dev->adev->dev;
  	struct qcm_process_device *qpd;
@@ -371,6 +415,16 @@ static int remove_all_kfd_queues_mes(struct device_queue_manager *dqm)
 
  	list_for_each_entry(cur, &dqm->queues, list) {
  		qpd = cur->qpd;
+		if (remove_all) {
+			retval = remove_queue_mes(dqm, NULL, qpd);
+			if (retval) {
+				dev_err(dev, "%s: Failed to remove queues of pasid 0x%x for dev %d",
+					__func__, qpd_to_pdd(qpd)->pasid, dqm->dev->id);
+				return retval;
+			}
+			continue;
+		}
+
  		list_for_each_entry(q, &qpd->queues_list, list) {
  			if (q->properties.is_active) {
  				retval = remove_queue_mes(dqm, q, qpd);
@@ -452,15 +506,21 @@ int kfd_reset_queue_mes(struct device_queue_manager *dqm, int queue_type,
  	return reset_queue_mes(dqm, q, queue_type, pipe, queue, db);
  }
 
-static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
+/*
+ * queue_input is the remove request for the bad queue(s). If MES doesn't
+ * detect a hung queue, it is sent again before the queues are resumed.
+ * hung_queue_count (optional) returns the number of hung queues.
+ */
+static int reset_queues_mes(struct device_queue_manager *dqm,
+			    struct kfd_process_device *pdd,
+			    struct mes_remove_queue_input *queue_input,
+			    unsigned int *hung_queue_count)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
  	struct drm_wedge_task_info *info = NULL;
  	struct amdgpu_task_info *ti = NULL;
-	struct kfd_process_device *pdd;
  	unsigned int num_hung = 0;
  	int r = 0;
-	struct mes_remove_queue_input queue_input;
 
  	if (!amdgpu_mes_queue_reset_by_mes_supported(adev)) {
  		r = -ENOTRECOVERABLE;
@@ -471,18 +531,13 @@ static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
  	if (WARN_ON(dqm->detect_hang_count > 0))
  		return 0;
 
-	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
-	queue_input.doorbell_offset = q->properties.doorbell_off;
-	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
-	queue_input.gang_context_array_index = q->gang_ctx_array_index;
-	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
-	queue_input.remove_queue_after_reset = false;
-	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
  	/* pass the known bad queue info to the reset function */
-	r = amdgpu_gfx_reset_mes_compute(adev, dqm->dev->xcp, NULL, NULL, NULL, &num_hung, &queue_input);
+	r = amdgpu_gfx_reset_mes_compute(adev, dqm->dev->xcp, NULL, NULL, NULL, &num_hung,
+					 queue_input);
  	if (r)
  		goto fail;
-	pdd = kfd_get_process_device_data(q->device, q->process);
+	if (hung_queue_count)
+		*hung_queue_count = num_hung;
  	if (pdd) {
  		ti = amdgpu_vm_get_task_info_pasid(adev, pdd->pasid);
  		if (ti) {
@@ -1493,6 +1548,7 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
  	struct queue *q;
  	struct device *dev = dqm->dev->adev->dev;
  	struct kfd_process_device *pdd;
+	bool remove_all;
  	int retval = 0;
 
  	dqm_lock(dqm);
@@ -1514,6 +1570,17 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
  	if (dqm->dev->kfd->shared_resources.enable_mes)
  		pdd->last_evict_timestamp = get_jiffies_64();
 
+	remove_all = dqm->dev->kfd->shared_resources.enable_mes &&
+		     amdgpu_mes_remove_all_process_queues_supported(dqm->dev->adev);
+	if (remove_all) {
+		retval = remove_queue_mes(dqm, NULL, qpd);
+		if (retval) {
+			dev_err(dev, "Failed to evict queues of pasid 0x%x\n",
+				pdd->pasid);
+			goto out;
+		}
+	}
+
  	/* Mark all queues as evicted. Deactivate all active queues on
  	 * the qpd.
  	 */
@@ -1525,7 +1592,7 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
  		q->properties.is_active = false;
  		decrement_queue_count(dqm, qpd, q);
 
-		if (dqm->dev->kfd->shared_resources.enable_mes) {
+		if (dqm->dev->kfd->shared_resources.enable_mes && !remove_all) {
  			retval = remove_queue_mes(dqm, q, qpd);
  			if (retval) {
  				dev_err(dev, "Failed to evict queue %d\n",
@@ -3286,6 +3353,7 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
  	enum kfd_unmap_queues_filter filter =
  		KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES;
  	bool found = false;
+	bool remove_all;
 
  	dqm_lock(dqm);
 
@@ -3298,6 +3366,15 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
  		filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
  	}
 
+	remove_all = dqm->dev->kfd->shared_resources.enable_mes &&
+		     amdgpu_mes_remove_all_process_queues_supported(dqm->dev->adev);
+	if (remove_all) {
+		retval = remove_queue_mes(dqm, NULL, qpd);
+		if (retval)
+			dev_err(dev, "Failed to remove queues of pasid 0x%x\n",
+				qpd_to_pdd(qpd)->pasid);
+	}
+
  	/* Clear all user mode queues */
  	list_for_each_entry(q, &qpd->queues_list, list) {
  		if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
@@ -3308,7 +3385,7 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
  		if (q->properties.is_active) {
  			decrement_queue_count(dqm, qpd, q);
 
-			if (dqm->dev->kfd->shared_resources.enable_mes) {
+			if (dqm->dev->kfd->shared_resources.enable_mes && !remove_all) {
  				retval = remove_queue_mes(dqm, q, qpd);
  				if (retval)
  					dev_err(dev, "Failed to remove queue %d\n",
@@ -3610,7 +3687,17 @@ int kfd_dqm_suspend_bad_queue_mes(struct kfd_node *knode, u32 pasid, u32 doorbel
 
  		list_for_each_entry(q, &qpd->queues_list, list) {
  			if (q->doorbell_id == doorbell_id && q->properties.is_active) {
-				if (reset_queues_mes(dqm, q))
+				struct mes_remove_queue_input queue_input;
+
+				memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
+				queue_input.doorbell_offset = q->properties.doorbell_off;
+				queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+				queue_input.gang_context_array_index = q->gang_ctx_array_index;
+				queue_input.queue_type =
+					convert_to_amdgpu_ring_type(q->properties.type);
+				queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
+
+				if (reset_queues_mes(dqm, pdd, &queue_input, NULL))
  					dev_dbg(knode->adev->dev,
  					"Fail to remove bad queue. doorbell_id 0x%x\n", q->doorbell_id);
  				q->properties.is_evicted = true;