[PATCH] tests/intel/xe_configfs: reuse pre-existing configfs group

Sk Anirban <[email protected]> Mon, 3 Aug 2026 15:19:55 +0530
Newsgroups org.freedesktop.lists.igt-dev
Message-ID <[email protected]>
Handle a pre-existing configfs group by opening and reusing it, retain
it across cleanup, and validate the fd so the test fails early with a
clear message on any error.

Signed-off-by: Sk Anirban <[email protected]>
---
 tests/intel/xe_configfs.c | 39 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)

diff --git a/tests/intel/xe_configfs.c b/tests/intel/xe_configfs.c
index 450c0a119..32e1f9e2d 100644
--- a/tests/intel/xe_configfs.c
+++ b/tests/intel/xe_configfs.c
@@ -26,6 +26,13 @@
 static char bus_addr[NAME_MAX];
 static struct pci_device *pci_dev;
 
+/*
+ * Set when the device's configfs group already existed before the test
+ * started. In that case we must not remove it during cleanup, so we leave
+ * the pre-existing configuration untouched.
+ */
+static bool configfs_group_preexisting;
+
 static bool check_registers(const uint32_t reg[], const uint32_t val[],
 			    size_t max)
 {
@@ -60,7 +67,7 @@ static void restore(int sig)
 
 	/* Drop all custom configfs settings from subtests */
 	configfs_fd = igt_configfs_open("xe");
-	if (configfs_fd >= 0)
+	if (configfs_fd >= 0 && !configfs_group_preexisting)
 		igt_fs_remove_dir(configfs_fd, bus_addr);
 	close(configfs_fd);
 
@@ -70,10 +77,19 @@ static void restore(int sig)
 
 static void set_survivability_mode(int configfs_device_fd, bool value)
 {
+	int ret;
+
 	igt_audio_driver_unload(NULL);
-	igt_kmod_unbind("xe", bus_addr);
+
+	ret = igt_kmod_unbind("xe", bus_addr);
+	igt_assert_f(!ret, "Failed to unbind xe from %s: %s\n",
+		     bus_addr, strerror(-ret));
+
 	igt_sysfs_set_boolean(configfs_device_fd, "survivability_mode", value);
-	igt_kmod_bind("xe", bus_addr);
+
+	ret = igt_kmod_bind("xe", bus_addr);
+	igt_assert_f(!ret, "Failed to bind xe to %s: %s\n",
+		     bus_addr, strerror(-ret));
 }
 
 /**
@@ -93,6 +109,9 @@ static void test_survivability_mode(int configfs_device_fd)
 
 	fd = open(path, O_RDONLY);
 	igt_assert_f(fd >= 0, "Survivability mode not set\n");
+
+	set_survivability_mode(configfs_device_fd, false);
+
 	close(fd);
 }
 
@@ -363,7 +382,15 @@ static int create_device_configfs_group(int configfs_fd)
 	int configfs_device_fd;
 
 	configfs_device_fd = igt_fs_create_dir(configfs_fd, bus_addr, mode);
-	igt_assert(configfs_device_fd);
+	if (configfs_device_fd == -EEXIST) {
+		configfs_group_preexisting = true;
+		configfs_device_fd = openat(configfs_fd, bus_addr, O_DIRECTORY);
+		if (configfs_device_fd < 0)
+			configfs_device_fd = -errno;
+	}
+	igt_assert_f(configfs_device_fd >= 0,
+		     "Failed to create/open configfs group for %s: %s\n",
+		     bus_addr, strerror(-configfs_device_fd));
 
 	return configfs_device_fd;
 }
@@ -371,7 +398,9 @@ static int create_device_configfs_group(int configfs_fd)
 static void close_configfs_group(int configfs_fd, int configfs_device_fd)
 {
 	close(configfs_device_fd);
-	igt_fs_remove_dir(configfs_fd, bus_addr);
+
+	if (!configfs_group_preexisting)
+		igt_fs_remove_dir(configfs_fd, bus_addr);
 }
 
 int igt_main()
-- 
2.43.0