Skip to contentSkip to conversation

gpiolib: cdev: add fdinfo output for line request file descriptors

PATCH Superseded Bartosz Golaszewski 3 participants 7 messages Sep 20, 2022 – Sep 21, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: cdev: add fdinfo output for line request file descriptors

Add fdinfo output for file descriptors created for user-space line requests in GPIO uAPI v2. The fdinfo file now contains the name of the GPIO device that is the "parent" of the request as well as offsets of the lines requested. This allows user-space to parse the /proc/$PID/fdinfo entries and deduct the PID of the process that requested a specific line.

---
 drivers/gpio/gpiolib-cdev.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpio/gpiolib-cdev.c +18 -0
index f8041d4898d1..0f7b5562c410 100644
@@ -1497,6 +1497,21 @@ static int linereq_release(struct inode *inode, struct file *file)
 	return 0;
 }
 
+#ifdef CONFIG_PROC_FS
+static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
+{
+	struct linereq *lr = file->private_data;
+	struct device *dev = &lr->gdev->dev;
+	u16 i;
+
+	seq_printf(out, "gpio-device:\t%s\n", dev_name(dev));
+
+	for (i = 0; i < lr->num_lines; i++)
+		seq_printf(out, "gpio-line:\t%d\n",
+			   gpio_chip_hwgpio(lr->lines[i].desc));
+}
+#endif
+
 static const struct file_operations line_fileops = {
 	.release = linereq_release,
 	.read = linereq_read,
@@ -1507,6 +1522,9 @@ static const struct file_operations line_fileops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = linereq_ioctl_compat,
 #endif
+#ifdef CONFIG_PROC_FS
+	.show_fdinfo = linereq_show_fdinfo,
+#endif
 };
 
 static int linereq_create(struct gpio_device *gdev, void __user *ip)
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 6 lines · Bartosz Golaszewski

Add fdinfo output for file descriptors created for user-space line requests in GPIO uAPI v2. The fdinfo file now contains the name of the GPIO device that is the "parent" of the request as well as offsets of the lines requested. This allows user-space to parse the /proc/$PID/fdinfo entries and deduct the PID of the process that requested a specific line.

deduce

Using deduct in this sense is unusual (not sure I've ever heard it used that way, and some dictionaries don't even acknowledge it as a valid option). In common usage deduct = subtract.

Quoted 12 lines
---
 drivers/gpio/gpiolib-cdev.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpio/gpiolib-cdev.c +0 -0
index f8041d4898d1..0f7b5562c410 100644
@@ -1497,6 +1497,21 @@ static int linereq_release(struct inode *inode, struct file *file)
 	return 0;
 }

drivers/gpio/gpiolib-cdev.c static int linereq_release(struct inode *inode, struct file *file) · line 1508

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9 -0
 
+#ifdef CONFIG_PROC_FS
+static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
+{
+	struct linereq *lr = file->private_data;
+	struct device *dev = &lr->gdev->dev;
+	u16 i;
+
+	seq_printf(out, "gpio-device:\t%s\n", dev_name(dev));
+

So "device" rather than "chip"?

I realise it is the device name being reported here, but don't we use chip for this in user-space?

drivers/gpio/gpiolib-cdev.c static int linereq_release(struct inode *inode, struct file *file) · line 1512

Quoted 4 lines
drivers/gpio/gpiolib-cdev.c +4 -0
+	for (i = 0; i < lr->num_lines; i++)
+		seq_printf(out, "gpio-line:\t%d\n",
+			   gpio_chip_hwgpio(lr->lines[i].desc));
+}

So the exploded form. That works for me - in case we ever need to add more line details, and also makes it easier to grep for a particular line.

Other than the comment nit and the device/chip question it looks good to me.

Cheers,
Kent.

Quoted 18 lines
drivers/gpio/gpiolib-cdev.c +5 -0
+#endif
+
 static const struct file_operations line_fileops = {
 	.release = linereq_release,
 	.read = linereq_read,
@@ -1507,6 +1522,9 @@ static const struct file_operations line_fileops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = linereq_ioctl_compat,
 #endif
+#ifdef CONFIG_PROC_FS
+	.show_fdinfo = linereq_show_fdinfo,
+#endif
 };
 
 static int linereq_create(struct gpio_device *gdev, void __user *ip)
Signature · 1 line
2.34.1
#2 lore raw
In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

Add fdinfo output for file descriptors created for user-space line requests in GPIO uAPI v2. The fdinfo file now contains the name of the GPIO device that is the "parent" of the request as well as offsets of the lines requested. This allows user-space to parse the /proc/$PID/fdinfo entries and deduct the PID of the process that requested a specific line.

In principle I'm fine, but see below.

Quoted 15 lines
---
 drivers/gpio/gpiolib-cdev.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpio/gpiolib-cdev.c +2 -0
index f8041d4898d1..0f7b5562c410 100644
@@ -1497,6 +1497,21 @@ static int linereq_release(struct inode *inode, struct file *file)
 	return 0;
 }
 
+#ifdef CONFIG_PROC_FS
+static void linereq_show_fdinfo(struct seq_file *out, struct file *file)

drivers/gpio/gpiolib-cdev.c static int linereq_release(struct inode *inode, struct file *file) · line 1511

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10 -0
+{
+	struct linereq *lr = file->private_data;
+	struct device *dev = &lr->gdev->dev;
+	u16 i;
+
+	seq_printf(out, "gpio-device:\t%s\n", dev_name(dev));
+
+	for (i = 0; i < lr->num_lines; i++)
+		seq_printf(out, "gpio-line:\t%d\n",
+			   gpio_chip_hwgpio(lr->lines[i].desc));

Hmm... Not sure which variant is better (as for machine parsing and for human), but I was thinking of

	gpio-lines: 1,4,6, ...

Also don't forget that sizes over PAGE_SIZE in sysfs sometimes problematic and racy.(the commit 888be6067b97 ("ACPI: sysfs: Fix a buffer overrun problem with description_show()") for the reference).

Quoted 19 lines
drivers/gpio/gpiolib-cdev.c +6 -0
+}
+#endif
+
 static const struct file_operations line_fileops = {
 	.release = linereq_release,
 	.read = linereq_read,
@@ -1507,6 +1522,9 @@ static const struct file_operations line_fileops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = linereq_ioctl_compat,
 #endif
+#ifdef CONFIG_PROC_FS
+	.show_fdinfo = linereq_show_fdinfo,
+#endif
 };
 
 static int linereq_create(struct gpio_device *gdev, void __user *ip)
Signature · 1 line
2.34.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 1 line · Andy Shevchenko

On Tue, Sep 20, 2022 at 03:54:35PM +0200, Bartosz Golaszewski wrote:

...

Quoted 3 lines

Also don't forget that sizes over PAGE_SIZE in sysfs sometimes problematic and racy.(the commit 888be6067b97 ("ACPI: sysfs: Fix a buffer overrun problem with description_show()") for the reference).

This is not the commit I wanted to point to... But suddenly can't find easily the one I remembered happened in the kernel.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Tue, Sep 20, 2022 at 03:54:35PM +0200, Bartosz Golaszewski wrote:

...

Quoted 6 lines
Quoted 3 lines

Also don't forget that sizes over PAGE_SIZE in sysfs sometimes problematic and racy.(the commit 888be6067b97 ("ACPI: sysfs: Fix a buffer overrun problem with description_show()") for the reference).

This is not the commit I wanted to point to... But suddenly can't find easily the one I remembered happened in the kernel.

Found the one:
00ee22c28915 ("PM / wakeup: Use seq_open() to show wakeup stats")

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Andy Shevchenko
Quoted 23 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

Add fdinfo output for file descriptors created for user-space line requests in GPIO uAPI v2. The fdinfo file now contains the name of the GPIO device that is the "parent" of the request as well as offsets of the lines requested. This allows user-space to parse the /proc/$PID/fdinfo entries and deduct the PID of the process that requested a specific line.

In principle I'm fine, but see below.

Quoted 13 lines
---
 drivers/gpio/gpiolib-cdev.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpio/gpiolib-cdev.c +0 -0
index f8041d4898d1..0f7b5562c410 100644
@@ -1497,6 +1497,21 @@ static int linereq_release(struct inode *inode, struct file *file)
      return 0;
 }
 
Quoted 16 lines

drivers/gpio/gpiolib-cdev.c static int linereq_release(struct inode *inode, struct file *file) · line 1511

Quoted 12 lines
drivers/gpio/gpiolib-cdev.c +12 -0
+#ifdef CONFIG_PROC_FS
+static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
+{
+     struct linereq *lr = file->private_data;
+     struct device *dev = &lr->gdev->dev;
+     u16 i;
+
+     seq_printf(out, "gpio-device:\t%s\n", dev_name(dev));
+
+     for (i = 0; i < lr->num_lines; i++)
+             seq_printf(out, "gpio-line:\t%d\n",
+                        gpio_chip_hwgpio(lr->lines[i].desc));

Hmm... Not sure which variant is better (as for machine parsing and for human), but I was thinking of

IMO it's pretty clear that the variant in this patch is easier for machine parsing - one less tokenization.

Quoted 6 lines
        gpio-lines: 1,4,6, ...

Also don't forget that sizes over PAGE_SIZE in sysfs sometimes problematic and racy.(the commit 888be6067b97 ("ACPI: sysfs: Fix a buffer overrun problem with description_show()") for the reference).

In most systems PAGE_SIZE will be at least 4096 bytes. With this patch a single "gpio-line:\t65535\n" is at most 17 bytes long x max 64 lines = 1088 bytes. We're still way below the size where it would be problematic.

Bart

Quoted 25 lines
Quoted 17 lines
drivers/gpio/gpiolib-cdev.c +6 -1
+}
+#endif
+
 static const struct file_operations line_fileops = {
      .release = linereq_release,
      .read = linereq_read,
@@ -1507,6 +1522,9 @@ static const struct file_operations line_fileops = {
 #ifdef CONFIG_COMPAT
      .compat_ioctl = linereq_ioctl_compat,
 #endif
+#ifdef CONFIG_PROC_FS
+     .show_fdinfo = linereq_show_fdinfo,
+#endif
 };
 
 static int linereq_create(struct gpio_device *gdev, void __user *ip)
--
Quoted 2 lines

2.34.1

--
With Best Regards,
Andy Shevchenko

#6 lore raw
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Tue, Sep 20, 2022 at 03:54:35PM +0200, Bartosz Golaszewski wrote:

...

Quoted 11 lines
Quoted 8 lines

drivers/gpio/gpiolib-cdev.c static int linereq_release(struct inode *inode, struct file *file) · line 1511

Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +5 -0
+     seq_printf(out, "gpio-device:\t%s\n", dev_name(dev));
+
+     for (i = 0; i < lr->num_lines; i++)
+             seq_printf(out, "gpio-line:\t%d\n",
+                        gpio_chip_hwgpio(lr->lines[i].desc));

Hmm... Not sure which variant is better (as for machine parsing and for human), but I was thinking of

IMO it's pretty clear that the variant in this patch is easier for machine parsing - one less tokenization.

For human it's harder in my opinion... But okay.

Quoted 10 lines
Quoted 5 lines
        gpio-lines: 1,4,6, ...

Also don't forget that sizes over PAGE_SIZE in sysfs sometimes problematic and racy.(the commit 888be6067b97 ("ACPI: sysfs: Fix a buffer overrun problem with description_show()") for the reference).

In most systems PAGE_SIZE will be at least 4096 bytes. With this patch a single "gpio-line:\t65535\n" is at most 17 bytes long x max 64 lines = 1088 bytes. We're still way below the size where it would be problematic.

Okay, for now it's 64, but it will be problematic starting from ~300 requested lines. Also, in case we add something more, who knows?

Signature · 2 lines
With Best Regards,
Andy Shevchenko