Skip to contentSkip to conversation

core: gracefully handle disappearing chips during iteration

PATCH Applied Potin Lai 2 participants 2 messages Sep 8 – Sep 9

Subsystems (2)
lore
Share & tools
lore

Potin Lai

author
#0 lore raw

PATCH 1/1 core: gracefully handle disappearing chips during iteration

When hotpluggable GPIO controllers (like USB devices) are dynamically added or removed, a race condition can occur. A chip's device node might be present when iterating through /dev, but the chip could disappear by the time we try to open it or query its lines.

Previously, this caused tools like 'gpiofind' to incorrectly abort with a 'No such device' (ENODEV) error if any chip on the system vanished during the search.

Fix this by ignoring ENODEV errors when searching for a line (in gpiod_line_find()) and by silently skipping chips that fail to open during iteration (in gpiod_chip_iter_new()).

---
 lib/helpers.c |  2 +-
 lib/iter.c    | 16 ++++------------
 2 files changed, 5 insertions(+), 13 deletions(-)
lib/helpers.c +1 -1
index 479f370..6d1836a 100644
@@ -423,7 +423,7 @@ struct gpiod_line *gpiod_line_find(const char *name)
 			return line;
 		}
 
-		if (errno != ENOENT)
+		if (errno != ENOENT && errno != ENODEV)
 			goto out;
 	}
 
lib/iter.c +4 -12
index bfd2852..7f5ad9a 100644
@@ -51,7 +51,7 @@ struct gpiod_chip_iter *gpiod_chip_iter_new(void)
 	if (!iter)
 		goto err_free_dirs;
 
-	iter->num_chips = num_chips;
+	iter->num_chips = 0;
 	iter->offset = 0;
 
 	if (num_chips == 0) {
@@ -64,23 +64,15 @@ struct gpiod_chip_iter *gpiod_chip_iter_new(void)
 		goto err_free_iter;
 
 	for (i = 0; i < num_chips; i++) {
-		iter->chips[i] = gpiod_chip_open_by_name(dirs[i]->d_name);
-		if (!iter->chips[i])
-			goto err_close_chips;
+		iter->chips[iter->num_chips] = gpiod_chip_open_by_name(dirs[i]->d_name);
+		if (iter->chips[iter->num_chips])
+			iter->num_chips++;
 	}
 
 	free_dirs(dirs, num_chips);
 
 	return iter;
 
-err_close_chips:
-	for (i = 0; i < num_chips; i++) {
-		if (iter->chips[i])
-			gpiod_chip_close(iter->chips[i]);
-	}
-
-	free(iter->chips);
-
 err_free_iter:
 	free(iter);
 

---
base-commit: f1af4b189fb4074b08eecbc50cf33306299d6a51
change-id: 20260908-gpiofind-error-enodev-7e70c48b7e9f

Best regards,

Signature · 1 line
Potin Lai <[email protected]>
#1 lore raw
Quoted 10 lines · Potin Lai

When hotpluggable GPIO controllers (like USB devices) are dynamically added or removed, a race condition can occur. A chip's device node might be present when iterating through /dev, but the chip could disappear by the time we try to open it or query its lines.

Previously, this caused tools like 'gpiofind' to incorrectly abort with a 'No such device' (ENODEV) error if any chip on the system vanished during the search.

[...]

Applied, thanks!

[1/1] core: gracefully handle disappearing chips during iteration

      https://git.kernel.org/brgl/c/40f18f16f573e6bf091d83cb1677e7386d0b8495

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>