Skip to contentSkip to conversation

gpio: sysfs: make the sysfs export behavior consistent

RFC PATCH Superseded Bartosz Golaszewski 3 participants 6 messages Sep 27, 2024 – Oct 2, 2024 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 gpio: sysfs: make the sysfs export behavior consistent

From: Bartosz Golaszewski <[email protected]>

For drivers or board files that set gpio_chip->names, the links to the GPIO attribute group created on sysfs export will be named after the line's name set in that array. For lines that are named using device properties, the names pointer of the gpio_chip struct is never assigned so they are exported as if they're not named.

The ABI documentation does not mention the former behavior and given that the majority of modern systems use device-tree, ACPI or other way of passing GPIO names using device properties - bypassing gc->names - it's better to make the behavior consistent by always exporting lines as "gpioXYZ".

---
Story time:

I decided to learn rust. I figured I'd best find me a project to work on that would involve some proper coding but wouldn't have much impact on anything important when I inevitably get it wrong the first few times.

I decided to write a sysfs-to-libgpiod compatibility layer based on FUSE. Since Rust is hard, I started prototyping the thing in python first to at least have the logic nailed down before I tackle the rust part.

When working on the exporting part, I vagely recalled that when I used to work with GPIO sysfs somewhere between 2009 and 2012 (still with board-file based systems), named lines exported with sysfs would appear under /sys/class/gpio as symbolic links named like the line and not the usual "gpioXYZ". I realized that this is not the case now.

Quick glance at the sysfs code reveals that I didn't dream it up, but that behavior is reserved to drivers setting gc->names. This has been slowly going out of fashion with device-tree and device properties.

We could easily restore the behavior for everybody by taking the name from the descriptor we already have access to or even just assign gc->names from descriptors in gpiolib core but first: the sysfs ABI document does not mention the named links at all and second: given how this has naturally effectively been phased out over the years, it would probably cause more harm than good when the exported names suddenly change for existing users.

I'm proposing to just drop the named links alogether.

Let me know what you think.

 drivers/gpio/gpiolib-sysfs.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/gpio/gpiolib-sysfs.c +1 -5
index 17ed229412af..643620d261f5 100644
@@ -577,7 +577,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
 	struct gpio_device *gdev;
 	struct gpiod_data *data;
 	struct device *dev;
-	int status, offset;
+	int status;
 
 	/* can't export until sysfs is available ... */
 	if (!class_is_registered(&gpio_class)) {
@@ -626,10 +626,6 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
 	else
 		data->direction_can_change = false;
 
-	offset = gpio_chip_hwgpio(desc);
-	if (guard.gc->names && guard.gc->names[offset])
-		ioname = guard.gc->names[offset];
-
 	dev = device_create_with_groups(&gpio_class, &gdev->dev,
 					MKDEV(0, 0), data, gpio_groups,
 					ioname ? ioname : "gpio%u",
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For drivers or board files that set gpio_chip->names, the links to the GPIO attribute group created on sysfs export will be named after the line's name set in that array. For lines that are named using device properties, the names pointer of the gpio_chip struct is never assigned so they are exported as if they're not named.

Quoted 6 lines

The ABI documentation does not mention the former behavior and given that the majority of modern systems use device-tree, ACPI or other way of passing GPIO names using device properties - bypassing gc->names - it's better to make the behavior consistent by always exporting lines as "gpioXYZ".

I have no opinions on sysfs changes.

Quoted 4 lines

---
Story time:

Quoted 9 lines

I decided to learn rust. I figured I'd best find me a project to work on that would involve some proper coding but wouldn't have much impact on anything important when I inevitably get it wrong the first few times.

I decided to write a sysfs-to-libgpiod compatibility layer based on FUSE. Since Rust is hard, I started prototyping the thing in python first to at least have the logic nailed down before I tackle the rust part.

Something along these lines[1]?

Cheers,
Kent.

[1]https://dev.to/krjakbrjak/simulating-gpio-sysfs-interface-with-fuse-and-c-30ga

#2 lore raw
Quoted 7 lines · Kent Gibson
Quoted 6 lines

I decided to write a sysfs-to-libgpiod compatibility layer based on FUSE. Since Rust is hard, I started prototyping the thing in python first to at least have the logic nailed down before I tackle the rust part.

Quoted 7 lines

Something along these lines[1]?

Cheers,
Kent.

[1]https://dev.to/krjakbrjak/simulating-gpio-sysfs-interface-with-fuse-and-c-30ga

Well, this doesn't really do anything. I'm thinking about something consuming the libgpiod rust bindings to actually be a useful replacement for kernel sysfs.

The master plan is: provide a drop-in user-space replacement for sysfs, make users convert to using it instead of the real thing, eventually remove sysfs from the kernel and then some time after remove the compatibility layer from existence forcing everybody to now move to pure libgpiod. :)

Bart

#3 lore raw
Quoted 13 lines · Bartosz Golaszewski
Quoted 12 lines · Kent Gibson
Quoted 6 lines

I decided to write a sysfs-to-libgpiod compatibility layer based on FUSE. Since Rust is hard, I started prototyping the thing in python first to at least have the logic nailed down before I tackle the rust part.

Something along these lines[1]?

Cheers,
Kent.

Quoted 7 lines
Quoted 2 lines

[1]https://dev.to/krjakbrjak/simulating-gpio-sysfs-interface-with-fuse-and-c-30ga

Well, this doesn't really do anything. I'm thinking about something consuming the libgpiod rust bindings to actually be a useful replacement for kernel sysfs.

I haven't actually looked at that - but when you mentioned a sysfs fuse interface that rang a bell.

Quoted 6 lines

The master plan is: provide a drop-in user-space replacement for sysfs, make users convert to using it instead of the real thing, eventually remove sysfs from the kernel and then some time after remove the compatibility layer from existence forcing everybody to now move to pure libgpiod. :)

Ironically a lot of users would probably be happier with that than with the D-bus daemon. And for that reason I don't think that last step will ever happen. But that is ok too - as long as we can finally remove the sysfs interface from the kernel.

Cheers,
Kent.

Reviewed-by on v1 1/1
#4 lore raw
In reply to Bartosz Golaszewski
Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For drivers or board files that set gpio_chip->names, the links to the GPIO attribute group created on sysfs export will be named after the line's name set in that array. For lines that are named using device properties, the names pointer of the gpio_chip struct is never assigned so they are exported as if they're not named.

Quoted 7 lines

The ABI documentation does not mention the former behavior and given that the majority of modern systems use device-tree, ACPI or other way of passing GPIO names using device properties - bypassing gc->names - it's better to make the behavior consistent by always exporting lines as "gpioXYZ".

I'm in favor of this.

Quoted 1 line

Story time:

That's a good story :) Fun to see how you arrived at this.

Yours,
Linus Walleij

#5 lore raw
Quoted 17 lines · Linus Walleij
Quoted 14 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For drivers or board files that set gpio_chip->names, the links to the GPIO attribute group created on sysfs export will be named after the line's name set in that array. For lines that are named using device properties, the names pointer of the gpio_chip struct is never assigned so they are exported as if they're not named.

The ABI documentation does not mention the former behavior and given that the majority of modern systems use device-tree, ACPI or other way of passing GPIO names using device properties - bypassing gc->names - it's better to make the behavior consistent by always exporting lines as "gpioXYZ".

Quoted 5 lines
Quoted 1 line

I'm in favor of this.

I take it this also applies to v2 and am picking it up instead of this one.

Bart

Quoted 6 lines
Quoted 1 line

Story time:

That's a good story :) Fun to see how you arrived at this.

Yours,
Linus Walleij