[PATCH] lib/amdgpu: improve pci_unplug test

vitaly.prosyak at amd.com vitaly.prosyak at amd.com
Thu Jan 9 03:32:22 UTC 2025


From: Vitaly Prosyak <vitaly.prosyak at amd.com>

Replace the 1-second sleep with a semaphore for synchronization, using wait
and post operations from the worker and main threads. This change results in
cleaner code and ensures precise timing, so PCI unplug event is triggered as
expected during job submissions.

Cc: Jesse Zhang  <jesse.zhang at amd.com>
Cc: Christian Koenig <christian.koenig at amd.com>
Cc: Alexander Deucher <alexander.deucher at amd.com>

Signed-off-by: Vitaly Prosyak <vitaly.prosyak at amd.com>
---
 lib/amdgpu/amd_pci_unplug.c | 10 +++++++---
 lib/amdgpu/amd_pci_unplug.h |  3 +++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/lib/amdgpu/amd_pci_unplug.c b/lib/amdgpu/amd_pci_unplug.c
index 107e1cbbd..5155c9474 100644
--- a/lib/amdgpu/amd_pci_unplug.c
+++ b/lib/amdgpu/amd_pci_unplug.c
@@ -238,6 +238,8 @@ amdgpu_nop_cs(void *handle)
 	ibs_request.ibs = &ib_info;
 	ibs_request.resources = bo_list;
 
+	sem_post(&unplug->semaphore);
+
 	while (unplug->do_cs)
 		amdgpu_cs_submit(context, 0, &ibs_request, 1);
 
@@ -263,11 +265,12 @@ amdgpu_create_cs_thread(struct amd_pci_unplug *unplug)
 
 	unplug->do_cs = true;
 
-	r = pthread_create(thread, NULL, &amdgpu_nop_cs, unplug);
+	r = sem_init(&unplug->semaphore, 0, 0);
 	igt_assert_eq(r, 0);
 
-	/* Give thread enough time to start*/
-	usleep(1000000);
+	r = pthread_create(thread, NULL, &amdgpu_nop_cs, unplug);
+	igt_assert_eq(r, 0);
+	sem_wait(&unplug->semaphore);
 	return thread;
 }
 
@@ -277,6 +280,7 @@ amdgpu_wait_cs_thread(struct amd_pci_unplug *unplug, pthread_t *thread)
 	unplug->do_cs = false;
 
 	pthread_join(*thread, NULL);
+	sem_destroy(&unplug->semaphore);
 	free(thread);
 }
 
diff --git a/lib/amdgpu/amd_pci_unplug.h b/lib/amdgpu/amd_pci_unplug.h
index 35d4dce3a..3799a6ec9 100644
--- a/lib/amdgpu/amd_pci_unplug.h
+++ b/lib/amdgpu/amd_pci_unplug.h
@@ -24,6 +24,8 @@
 #ifndef AMD_PCI_UNPLUG_H
 #define AMD_PCI_UNPLUG_H
 
+#include <semaphore.h>
+
 #include <amdgpu.h>
 #include <amdgpu_drm.h>
 #include "amd_ip_blocks.h"
@@ -42,6 +44,7 @@ struct amd_pci_unplug {
 	amdgpu_device_handle device_handle;
 	amdgpu_device_handle device_handle2;
 	volatile bool do_cs;
+	sem_t		semaphore;
 };
 
 void
-- 
2.34.1



More information about the igt-dev mailing list