[PATCH v4 21/24] drm/amdkfd: add pc sampling thread to trigger trap

James Zhu James.Zhu at amd.com
Tue Feb 6 15:59:17 UTC 2024


Add a kthread to trigger pc sampling trap.

Signed-off-by: James Zhu <James.Zhu at amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c | 91 +++++++++++++++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h        |  1 +
 2 files changed, 89 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c b/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c
index 6f50ba1f8989..ea9478c3738a 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c
@@ -39,6 +39,84 @@ struct supported_pc_sample_info supported_formats[] = {
 	{ IP_VERSION(9, 4, 2), &sample_info_hosttrap_9_0_0 },
 };
 
+static int kfd_pc_sample_thread(void *param)
+{
+	struct amdgpu_device *adev;
+	struct kfd_node *node = param;
+	uint32_t timeout = 0;
+	ktime_t next_trap_time;
+
+	mutex_lock(&node->pcs_data.mutex);
+	if (node->pcs_data.hosttrap_entry.base.active_count &&
+		node->pcs_data.hosttrap_entry.base.pc_sample_info.interval &&
+		node->kfd2kgd->trigger_pc_sample_trap) {
+		switch (node->pcs_data.hosttrap_entry.base.pc_sample_info.type) {
+		case KFD_IOCTL_PCS_TYPE_TIME_US:
+			timeout = (uint32_t)node->pcs_data.hosttrap_entry.base.pc_sample_info.interval;
+			break;
+		default:
+			pr_debug("PC Sampling type %d not supported.",
+					node->pcs_data.hosttrap_entry.base.pc_sample_info.type);
+		}
+	}
+	mutex_unlock(&node->pcs_data.mutex);
+	if (!timeout)
+		return -EINVAL;
+
+	adev = node->adev;
+
+	allow_signal(SIGKILL);
+	while (!kthread_should_stop() &&
+			!READ_ONCE(node->pcs_data.hosttrap_entry.base.stop_enable) &&
+			!signal_pending(node->pcs_data.hosttrap_entry.base.pc_sample_thread)) {
+		next_trap_time = ktime_add_us(ktime_get_raw(), timeout);
+
+		node->kfd2kgd->trigger_pc_sample_trap(adev, node->vm_info.last_vmid_kfd,
+				&node->pcs_data.hosttrap_entry.base.target_simd,
+				&node->pcs_data.hosttrap_entry.base.target_wave_slot,
+				node->pcs_data.hosttrap_entry.base.pc_sample_info.method);
+		pr_debug_ratelimited("triggered a host trap.");
+
+		might_sleep();
+		do {
+			ktime_t wait_time;
+			s64 wait_ns, wait_us;
+
+			wait_time = ktime_sub(next_trap_time, ktime_get_raw());
+			wait_ns = ktime_to_ns(wait_time);
+			wait_us = ktime_to_us(wait_time);
+			if (wait_ns >= 10000)
+				usleep_range(wait_us - 10, wait_us);
+			else if (wait_ns > 0)
+				schedule();
+			else
+				break;
+		} while (1);
+	}
+	node->pcs_data.hosttrap_entry.base.pc_sample_thread = NULL;
+
+	return 0;
+}
+
+static int kfd_pc_sample_thread_start(struct kfd_node *node)
+{
+	char thread_name[16];
+	int ret = 0;
+
+	snprintf(thread_name, 16, "pcs_%08x", node->adev->ddev.render->index);
+	node->pcs_data.hosttrap_entry.base.pc_sample_thread =
+		kthread_run(kfd_pc_sample_thread, node, thread_name);
+
+	if (IS_ERR(node->pcs_data.hosttrap_entry.base.pc_sample_thread)) {
+		ret = PTR_ERR(node->pcs_data.hosttrap_entry.base.pc_sample_thread);
+		node->pcs_data.hosttrap_entry.base.pc_sample_thread = NULL;
+		pr_debug("Failed to create pc sample thread for %s with ret = %d.",
+			thread_name, ret);
+	}
+
+	return ret;
+}
+
 static int kfd_pc_sample_query_cap(struct kfd_process_device *pdd,
 					struct kfd_ioctl_pc_sample_args __user *user_args)
 {
@@ -99,6 +177,7 @@ static int kfd_pc_sample_start(struct kfd_process_device *pdd,
 					struct pc_sampling_entry *pcs_entry)
 {
 	bool pc_sampling_start = false;
+	int ret = 0;
 
 	pcs_entry->enabled = true;
 	mutex_lock(&pdd->dev->pcs_data.mutex);
@@ -112,13 +191,16 @@ static int kfd_pc_sample_start(struct kfd_process_device *pdd,
 	mutex_unlock(&pdd->dev->pcs_data.mutex);
 
 	while (pc_sampling_start) {
-		if (READ_ONCE(pdd->dev->pcs_data.hosttrap_entry.base.stop_enable))
+		if (READ_ONCE(pdd->dev->pcs_data.hosttrap_entry.base.stop_enable)) {
 			usleep_range(1000, 2000);
-		else
+		} else {
+			if (!pdd->dev->pcs_data.hosttrap_entry.base.pc_sample_thread)
+				ret = kfd_pc_sample_thread_start(pdd->dev);
 			break;
+		}
 	}
 
-	return 0;
+	return ret;
 }
 
 static int kfd_pc_sample_stop(struct kfd_process_device *pdd,
@@ -141,6 +223,9 @@ static int kfd_pc_sample_stop(struct kfd_process_device *pdd,
 		KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD);
 
 	if (pc_sampling_stop) {
+		kthread_stop(pdd->dev->pcs_data.hosttrap_entry.base.pc_sample_thread);
+		while (pdd->dev->pcs_data.hosttrap_entry.base.pc_sample_thread)
+			usleep_range(1000, 2000);
 		mutex_lock(&pdd->dev->pcs_data.mutex);
 		pdd->dev->pcs_data.hosttrap_entry.base.target_simd = 0;
 		pdd->dev->pcs_data.hosttrap_entry.base.target_wave_slot = 0;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 7bdcbe6be4fe..372727b5d945 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -276,6 +276,7 @@ struct kfd_dev_pc_sampling_data {
 	uint32_t target_wave_slot;  /* target wave slot for trap */
 	bool stop_enable;           /* pc sampling stop in process */
 	struct idr pc_sampling_idr;
+	struct task_struct *pc_sample_thread;
 	struct kfd_pc_sample_info pc_sample_info;
 };
 
-- 
2.25.1



More information about the amd-gfx mailing list