[PATCH i-g-t 1/4] lib/drmtest: Fix drm_close_driver()

Lucas De Marchi lucas.demarchi at intel.com
Wed Dec 18 05:13:21 UTC 2024


When the driver is closed, make sure to invalidate the opened_devices,
otherwise it may later try use that info and wrongly detect the device
as "already opened". Also make sure that if the device can't even be stat'ed,
it should be considered as "already opened" since that fd refers to something
that already went away.

While at it fix some minor typos and coding style nearby.

Signed-off-by: Lucas De Marchi <lucas.demarchi at intel.com>
---
 lib/drmtest.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/lib/drmtest.c b/lib/drmtest.c
index 2dd4540b8..c541be132 100644
--- a/lib/drmtest.c
+++ b/lib/drmtest.c
@@ -377,7 +377,7 @@ err:
 static struct {
 	int fd;
 	struct stat stat;
-}_opened_fds[64];
+} _opened_fds[64];
 
 static int _opened_fds_count;
 
@@ -393,6 +393,20 @@ static void _set_opened_fd(int idx, int fd)
 	_opened_fds_count = idx+1;
 }
 
+static void _set_closed_fd(int fd)
+{
+	unsigned int idx;
+
+	for (idx = 0; idx < ARRAY_SIZE(_opened_fds); idx++) {
+		if (_opened_fds[idx].fd == fd) {
+			memset(&_opened_fds[idx].stat, 0,
+			       sizeof(_opened_fds[idx].stat));
+			_opened_fds[idx].fd = -1;
+			break;
+		}
+	}
+}
+
 static bool _is_already_opened(const char *path, int as_idx)
 {
 	struct stat new;
@@ -401,11 +415,10 @@ static bool _is_already_opened(const char *path, int as_idx)
 	assert(as_idx <= _opened_fds_count);
 
 	/*
-	 * we cannot even stat the device, so it's of no use - let's claim it's
-	 * already opened
+	 * we cannot even stat the device, so it's of no use
 	 */
 	if (igt_debug_on(stat(path, &new) != 0))
-		return true;
+		return false;
 
 	for (int i = 0; i < as_idx; ++i) {
 		/* did we cross filesystem boundary? */
@@ -605,10 +618,9 @@ int __drm_open_driver_another(int idx, int chipset)
 			igt_warn("No card matches the filter! [%s]\n",
 				 igt_device_filter_get(idx));
 		else if (_is_already_opened(card.card, idx))
-			igt_warn("card maching filter %d is already opened\n", idx);
+			igt_warn("card matching filter %d is already opened\n", idx);
 		else
 			fd = __open_driver_exact(card.card, chipset);
-
 	} else {
 		/* no filter for device idx, let's open whatever is available */
 		fd = __open_driver("/dev/dri/card", 0, chipset, idx);
@@ -806,6 +818,8 @@ int __drm_close_driver(int fd)
 	if (is_xe_device(fd))
 		xe_device_put(fd);
 
+	_set_closed_fd(fd);
+
 	return close(fd);
 }
 
-- 
2.47.0



More information about the igt-dev mailing list