Skip to contentSkip to conversation

drm/amdkfd: fix double lock acquisition in set_perfcount

PATCH New Perry Yuan 1 participant 2 messages Nov 5

Subsystems (3)
lore
Share & tools
lore

Perry Yuan

author
#0 lore raw

PATCH v2 1/2 drm/amdkfd: fix double lock acquisition in set_perfcount

From: Benjamin Welton <[email protected]>

Separate the locking from update_queue() to allow updating queues from code that already holds the DQM lock. set_perfcount() takes the DQM lock and then calls pqm_update_mqd() for every queue, which ends up in update_queue() taking the same lock again and hangs.

This change was in the original mailing list commit for set_perfcount but was not included in gerrit.

Fixes a789761de305 ("amd/amdkfd: Add kfd_ioctl_profiler to contain profiler kernel driver changes")
---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 72 ++++++++++---------
 1 file changed, 39 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +39 -33
index ce67e56efa8c..f4fb99b599da 100644
@@ -346,29 +346,6 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
 	return r;
 }
 
-static void set_perfcount(struct device_queue_manager *dqm, int enable)
-{
-	struct device_process_node *cur;
-	struct qcm_process_device *qpd;
-	struct queue *q;
-	struct mqd_update_info minfo = { 0 };
-
-	if (!dqm)
-		return;
-
-	minfo.update_flag = (enable == 1 ? UPDATE_FLAG_PERFCOUNT_ENABLE :
-						 UPDATE_FLAG_PERFCOUNT_DISABLE);
-	dqm_lock(dqm);
-	list_for_each_entry(cur, &dqm->queues, list) {
-		qpd = cur->qpd;
-		list_for_each_entry(q, &qpd->queues_list, list) {
-			pqm_update_mqd(qpd->pqm, q->properties.queue_id,
-						&minfo);
-		}
-	}
-	dqm_unlock(dqm);
-}
-
 static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 			    struct qcm_process_device *qpd)
 {
@@ -1116,7 +1093,7 @@ static int destroy_queue_nocpsch(struct device_queue_manager *dqm,
 	return retval;
 }
 
-static int update_queue(struct device_queue_manager *dqm, struct queue *q,
+static int update_queue_locked(struct device_queue_manager *dqm, struct queue *q,
 			struct mqd_update_info *minfo)
 {
 	int retval = 0;
@@ -1125,11 +1102,9 @@ static int update_queue(struct device_queue_manager *dqm, struct queue *q,
 	struct kfd_process_device *pdd;
 	bool prev_active = false;
 
-	dqm_lock(dqm);
 	pdd = kfd_get_process_device_data(q->device, q->process);
 	if (!pdd) {
-		retval = -ENODEV;
-		goto out_unlock;
+		return -ENODEV;
 	}
 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
 			q->properties.type)];
@@ -1147,13 +1122,12 @@ static int update_queue(struct device_queue_manager *dqm, struct queue *q,
 
 		/* queue is reset so inaccessable  */
 		if (pdd->has_reset_queue) {
-			retval = -EACCES;
-			goto out_unlock;
+			return -EACCES;
 		}
 
 		if (retval) {
 			dev_err(dev, "unmap queue failed\n");
-			goto out_unlock;
+			return retval;
 		}
 	} else if (prev_active &&
 		   (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
@@ -1162,7 +1136,7 @@ static int update_queue(struct device_queue_manager *dqm, struct queue *q,
 
 		if (!dqm->sched_running) {
 			WARN_ONCE(1, "Update non-HWS queue while stopped\n");
-			goto out_unlock;
+			return retval;
 		}
 
 		retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
@@ -1172,7 +1146,7 @@ static int update_queue(struct device_queue_manager *dqm, struct queue *q,
 				KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
 		if (retval) {
 			dev_err(dev, "destroy mqd failed\n");
-			goto out_unlock;
+			return retval;
 		}
 	}
 
@@ -1220,11 +1194,43 @@ static int update_queue(struct device_queue_manager *dqm, struct queue *q,
 						   &q->properties, current->mm);
 	}
 
-out_unlock:
+	return retval;
+}
+
+static int update_queue(struct device_queue_manager *dqm, struct queue *q,
+			struct mqd_update_info *minfo)
+{
+	int retval;
+
+	dqm_lock(dqm);
+	retval = update_queue_locked(dqm, q, minfo);
 	dqm_unlock(dqm);
+
 	return retval;
 }
 
+static void set_perfcount(struct device_queue_manager *dqm, int enable)
+{
+	struct device_process_node *cur;
+	struct qcm_process_device *qpd;
+	struct queue *q;
+	struct mqd_update_info minfo = { 0 };
+
+	if (!dqm)
+		return;
+
+	minfo.update_flag = (enable == 1 ? UPDATE_FLAG_PERFCOUNT_ENABLE :
+						 UPDATE_FLAG_PERFCOUNT_DISABLE);
+	dqm_lock(dqm);
+	list_for_each_entry(cur, &dqm->queues, list) {
+		qpd = cur->qpd;
+		list_for_each_entry(q, &qpd->queues_list, list) {
+			update_queue_locked(dqm, q, &minfo);
+		}
+	}
+	dqm_unlock(dqm);
+}
+
 /* suspend_single_queue does not lock the dqm like the
  * evict_process_queues_cpsch or evict_process_queues_nocpsch. You should
  * lock the dqm before calling, and unlock after calling.
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 2/2 drm/amdkfd: preempt the HWS runlist once in set_perfcount

On the non-MES HWS path, set_perfcount() calls update_queue_locked() for every user queue. Each call unmaps and remaps all dynamic queues, so a single perfcount toggle preempts the runlist once per user queue. This adds repeated stalls to compute workloads during profiling setup and teardown.

Unmap the runlist once, update the compute MQDs, and map it once. Share the MQD update and activity/GWS accounting logic with update_queue_locked() to preserve queue counts. The MQD callback recomputes is_active, and a concurrent UPDATE_QUEUE caller can change queue properties before taking the DQM lock, so perfcount updates must still account for activity changes.

Skip processes marked with has_reset_queue after preemption, preserving the existing update_queue_locked() guard. Remap after the batch even if the last process was skipped. Keep the per-queue update path for MES and non-HWS scheduling.

v2:
- Add Yifan's Reviewed-by
- Add the missing dependency "drm/amdkfd: fix double lock acquisition in set_perfcount" as patch 1/2

Fixes a789761de305 ("amd/amdkfd: Add kfd_ioctl_profiler to contain profiler kernel driver changes")
---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 85 +++++++++++++------
 1 file changed, 58 insertions(+), 27 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +58 -27
index f4fb99b599da..c9a303f536c5 100644
@@ -1093,6 +1093,41 @@ static int destroy_queue_nocpsch(struct device_queue_manager *dqm,
 	return retval;
 }
 
+/* Caller must hold the DQM lock and ensure the queue is unmapped. */
+static void update_queue_mqd(struct device_queue_manager *dqm, struct queue *q,
+			     struct qcm_process_device *qpd,
+			     struct mqd_update_info *minfo)
+{
+	struct mqd_manager *mqd_mgr =
+		dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)];
+	bool prev_active = q->properties.is_active;
+
+	mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
+
+	/*
+	 * Queue properties can change before the updater takes the DQM lock,
+	 * so even a perfcount update can change queue activity. Keep the
+	 * counters used to build the runlist in sync with the updated state.
+	 */
+	if (q->properties.is_active && !prev_active) {
+		increment_queue_count(dqm, qpd, q);
+	} else if (!q->properties.is_active && prev_active) {
+		decrement_queue_count(dqm, qpd, q);
+	} else if (q->gws && !q->properties.is_gws) {
+		if (q->properties.is_active) {
+			dqm->gws_queue_count++;
+			qpd->mapped_gws_queue = true;
+		}
+		q->properties.is_gws = true;
+	} else if (!q->gws && q->properties.is_gws) {
+		if (q->properties.is_active) {
+			dqm->gws_queue_count--;
+			qpd->mapped_gws_queue = false;
+		}
+		q->properties.is_gws = false;
+	}
+}
+
 static int update_queue_locked(struct device_queue_manager *dqm, struct queue *q,
 			struct mqd_update_info *minfo)
 {
@@ -1109,7 +1144,7 @@ static int update_queue_locked(struct device_queue_manager *dqm, struct queue *q
 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
 			q->properties.type)];
 
-	/* Save previous activity state for counters */
+	/* Save the activity state for queue removal. */
 	prev_active = q->properties.is_active;
 
 	/* Make sure the queue is unmapped before updating the MQD */
@@ -1150,31 +1185,7 @@ static int update_queue_locked(struct device_queue_manager *dqm, struct queue *q
 		}
 	}
 
-	mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
-
-	/*
-	 * check active state vs. the previous state and modify
-	 * counter accordingly. map_queues_cpsch uses the
-	 * dqm->active_queue_count to determine whether a new runlist must be
-	 * uploaded.
-	 */
-	if (q->properties.is_active && !prev_active) {
-		increment_queue_count(dqm, &pdd->qpd, q);
-	} else if (!q->properties.is_active && prev_active) {
-		decrement_queue_count(dqm, &pdd->qpd, q);
-	} else if (q->gws && !q->properties.is_gws) {
-		if (q->properties.is_active) {
-			dqm->gws_queue_count++;
-			pdd->qpd.mapped_gws_queue = true;
-		}
-		q->properties.is_gws = true;
-	} else if (!q->gws && q->properties.is_gws) {
-		if (q->properties.is_active) {
-			dqm->gws_queue_count--;
-			pdd->qpd.mapped_gws_queue = false;
-		}
-		q->properties.is_gws = false;
-	}
+	update_queue_mqd(dqm, q, &pdd->qpd, minfo);
 
 	if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
 		if (!dqm->dev->kfd->shared_resources.enable_mes)
@@ -1215,19 +1226,39 @@ static void set_perfcount(struct device_queue_manager *dqm, int enable)
 	struct qcm_process_device *qpd;
 	struct queue *q;
 	struct mqd_update_info minfo = { 0 };
+	bool batch;
 
 	if (!dqm)
 		return;
 
 	minfo.update_flag = (enable == 1 ? UPDATE_FLAG_PERFCOUNT_ENABLE :
 						 UPDATE_FLAG_PERFCOUNT_DISABLE);
+	/* Preempt the non-MES HWS runlist once for all compute MQD updates. */
+	batch = dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS &&
+		!dqm->dev->kfd->shared_resources.enable_mes;
+
 	dqm_lock(dqm);
+	if (batch && unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
+					0, USE_DEFAULT_GRACE_PERIOD, false))
+		goto out;
+
 	list_for_each_entry(cur, &dqm->queues, list) {
 		qpd = cur->qpd;
+		if (batch && qpd_to_pdd(qpd)->has_reset_queue)
+			continue;
 		list_for_each_entry(q, &qpd->queues_list, list) {
-			update_queue_locked(dqm, q, &minfo);
+			if (!batch) {
+				update_queue_locked(dqm, q, &minfo);
+				continue;
+			}
+			if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
+				update_queue_mqd(dqm, q, qpd, &minfo);
 		}
 	}
+
+	if (batch)
+		map_queues_cpsch(dqm);
+out:
 	dqm_unlock(dqm);
 }
 
Signature · 1 line
2.34.1