[PATCH v2 7/9] gpio: sysfs: export the GPIO directory locally in the gpiochip<id> directory

Bartosz Golaszewski posted 9 patches 5 months, 3 weeks ago
There is a newer version of this series
[PATCH v2 7/9] gpio: sysfs: export the GPIO directory locally in the gpiochip<id> directory
Posted by Bartosz Golaszewski 5 months, 3 weeks ago
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

As a way to allow the user-space to stop referring to GPIOs by their
global numbers, introduce a parallel group of line attributes for
exported GPIO that live inside the GPIO chip class device and are
referred to by their HW offset within their parent chip.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 Documentation/ABI/obsolete/sysfs-gpio |  5 +++++
 drivers/gpio/gpiolib-sysfs.c          | 40 ++++++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/obsolete/sysfs-gpio b/Documentation/ABI/obsolete/sysfs-gpio
index ff694708a3bef787afa42dedf94faf209c44dbf0..c0bb51412a912cefe032c4e84288f99754acb1b5 100644
--- a/Documentation/ABI/obsolete/sysfs-gpio
+++ b/Documentation/ABI/obsolete/sysfs-gpio
@@ -27,6 +27,11 @@ Description:
 	    /base ... (r/o) same as N
 	    /label ... (r/o) descriptive chip name
 	    /ngpio ... (r/o) number of GPIOs; numbered N to N + (ngpio - 1)
+	    /gpio<OFFSET>
+	        /value ... always readable, writes fail for input GPIOs
+	        /direction ... r/w as: in, out (default low); write: high, low
+	        /edge ... r/w as: none, falling, rising, both
+	        /active-low ... r/w as: 0, 1
 	/chipX ... for each gpiochip; #X is the gpio device ID
 	    /export ... asks the kernel to export a GPIO at HW offset X to userspace
 	    /unexport ... to return a GPIO at HW offset X to the kernel
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index adf030f74eb163f5d8b1092d00418b84354f923f..37d58009a51333f7d6a8d600dbeaeb333df27ac3 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -47,11 +47,13 @@ struct gpiod_data {
 
 	struct mutex mutex;
 	struct kernfs_node *value_class_node;
+	struct kernfs_node *value_chip_node;
 	int irq;
 	unsigned char irq_flags;
 
 	bool direction_can_change;
 
+	struct kobject *parent;
 	struct device_attribute dir_attr;
 	struct device_attribute val_attr;
 	struct device_attribute edge_attr;
@@ -180,6 +182,7 @@ static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
 	struct gpiod_data *data = priv;
 
 	sysfs_notify_dirent(data->value_class_node);
+	kernfs_notify(data->value_chip_node);
 
 	return IRQ_HANDLED;
 }
@@ -780,13 +783,46 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
 	gdev_data = gdev_get_data(gdev);
 	if (!gdev_data) {
 		status = -ENODEV;
-		goto err_unregister_device;
+		goto err_put_dirent;
 	}
 
 	list_add(&desc_data->list, &gdev_data->exported_lines);
 
+	desc_data->attr_group.name = kasprintf(GFP_KERNEL, "gpio%u",
+					       gpio_chip_hwgpio(desc));
+	if (!desc_data->attr_group.name) {
+		status = -ENOMEM;
+		goto err_put_dirent;
+	}
+
+	desc_data->parent = &gdev_data->cdev_id->kobj;
+	status = sysfs_create_groups(desc_data->parent,
+				     desc_data->attr_groups);
+	if (status)
+		goto err_free_name;
+
+	char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
+					     gpio_chip_hwgpio(desc));
+	if (!path) {
+		status = -ENOMEM;
+		goto err_remove_groups;
+	}
+
+	desc_data->value_chip_node = kernfs_walk_and_get(desc_data->parent->sd,
+							 path);
+	if (!desc_data->value_chip_node) {
+		status = -ENODEV;
+		goto err_remove_groups;
+	}
+
 	return 0;
 
+err_remove_groups:
+	sysfs_remove_groups(desc_data->parent, desc_data->attr_groups);
+err_free_name:
+	kfree(desc_data->attr_group.name);
+err_put_dirent:
+	sysfs_put(desc_data->value_class_node);
 err_unregister_device:
 	device_unregister(desc_data->dev);
 err_free_data:
@@ -876,6 +912,8 @@ void gpiod_unexport(struct gpio_desc *desc)
 		clear_bit(FLAG_EXPORT, &desc->flags);
 		sysfs_put(desc_data->value_class_node);
 		device_unregister(desc_data->dev);
+		sysfs_remove_groups(desc_data->parent, desc_data->attr_groups);
+		kernfs_put(desc_data->value_chip_node);
 
 		/*
 		 * Release irq after deregistration to prevent race with

-- 
2.48.1
Re: [PATCH v2 7/9] gpio: sysfs: export the GPIO directory locally in the gpiochip<id> directory
Posted by kernel test robot 5 months, 3 weeks ago
Hi Bartosz,

kernel test robot noticed the following build errors:

[auto build test ERROR on cb908f3699fb137e28017a8fdf506c35762b3eb6]

url:    https://github.com/intel-lab-lkp/linux/commits/Bartosz-Golaszewski/gpio-sysfs-add-a-parallel-class-device-for-each-GPIO-chip-using-device-IDs/20250623-170412
base:   cb908f3699fb137e28017a8fdf506c35762b3eb6
patch link:    https://lore.kernel.org/r/20250623-gpio-sysfs-chip-export-v2-7-d592793f8964%40linaro.org
patch subject: [PATCH v2 7/9] gpio: sysfs: export the GPIO directory locally in the gpiochip<id> directory
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20250624/202506240548.l2wS2XW6-lkp@intel.com/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250624/202506240548.l2wS2XW6-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202506240548.l2wS2XW6-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/gpio/gpiolib-sysfs.c:802:3: error: cannot jump from this goto statement to its label
     802 |                 goto err_free_name;
         |                 ^
   drivers/gpio/gpiolib-sysfs.c:804:8: note: jump bypasses initialization of variable with __attribute__((cleanup))
     804 |         char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
         |               ^
   drivers/gpio/gpiolib-sysfs.c:795:3: error: cannot jump from this goto statement to its label
     795 |                 goto err_put_dirent;
         |                 ^
   drivers/gpio/gpiolib-sysfs.c:804:8: note: jump bypasses initialization of variable with __attribute__((cleanup))
     804 |         char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
         |               ^
   drivers/gpio/gpiolib-sysfs.c:786:3: error: cannot jump from this goto statement to its label
     786 |                 goto err_put_dirent;
         |                 ^
   drivers/gpio/gpiolib-sysfs.c:804:8: note: jump bypasses initialization of variable with __attribute__((cleanup))
     804 |         char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
         |               ^
   drivers/gpio/gpiolib-sysfs.c:780:3: error: cannot jump from this goto statement to its label
     780 |                 goto err_unregister_device;
         |                 ^
   drivers/gpio/gpiolib-sysfs.c:804:8: note: jump bypasses initialization of variable with __attribute__((cleanup))
     804 |         char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
         |               ^
   drivers/gpio/gpiolib-sysfs.c:773:3: error: cannot jump from this goto statement to its label
     773 |                 goto err_free_data;
         |                 ^
   drivers/gpio/gpiolib-sysfs.c:804:8: note: jump bypasses initialization of variable with __attribute__((cleanup))
     804 |         char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
         |               ^
   drivers/gpio/gpiolib-sysfs.c:733:3: error: cannot jump from this goto statement to its label
     733 |                 goto err_clear_bit;
         |                 ^
   drivers/gpio/gpiolib-sysfs.c:804:8: note: jump bypasses initialization of variable with __attribute__((cleanup))
     804 |         char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
         |               ^
   drivers/gpio/gpiolib-sysfs.c:727:3: error: cannot jump from this goto statement to its label
     727 |                 goto err_clear_bit;
         |                 ^
   drivers/gpio/gpiolib-sysfs.c:804:8: note: jump bypasses initialization of variable with __attribute__((cleanup))
     804 |         char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
         |               ^
   7 errors generated.


vim +802 drivers/gpio/gpiolib-sysfs.c

   677	
   678	/**
   679	 * gpiod_export - export a GPIO through sysfs
   680	 * @desc: GPIO to make available, already requested
   681	 * @direction_may_change: true if userspace may change GPIO direction
   682	 * Context: arch_initcall or later
   683	 *
   684	 * When drivers want to make a GPIO accessible to userspace after they
   685	 * have requested it -- perhaps while debugging, or as part of their
   686	 * public interface -- they may use this routine.  If the GPIO can
   687	 * change direction (some can't) and the caller allows it, userspace
   688	 * will see "direction" sysfs attribute which may be used to change
   689	 * the gpio's direction.  A "value" attribute will always be provided.
   690	 *
   691	 * Returns:
   692	 * 0 on success, or negative errno on failure.
   693	 */
   694	int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
   695	{
   696		struct gpiodev_data *gdev_data;
   697		struct gpiod_data *desc_data;
   698		struct gpio_device *gdev;
   699		struct attribute **attrs;
   700		int status;
   701	
   702		/* can't export until sysfs is available ... */
   703		if (!class_is_registered(&gpio_class)) {
   704			pr_debug("%s: called too early!\n", __func__);
   705			return -ENOENT;
   706		}
   707	
   708		if (!desc) {
   709			pr_debug("%s: invalid gpio descriptor\n", __func__);
   710			return -EINVAL;
   711		}
   712	
   713		CLASS(gpio_chip_guard, guard)(desc);
   714		if (!guard.gc)
   715			return -ENODEV;
   716	
   717		if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
   718			return -EPERM;
   719	
   720		gdev = desc->gdev;
   721	
   722		guard(mutex)(&sysfs_lock);
   723	
   724		if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
   725			gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
   726			status = -EPERM;
   727			goto err_clear_bit;
   728		}
   729	
   730		desc_data = kzalloc(sizeof(*desc_data), GFP_KERNEL);
   731		if (!desc_data) {
   732			status = -ENOMEM;
   733			goto err_clear_bit;
   734		}
   735	
   736		desc_data->desc = desc;
   737		mutex_init(&desc_data->mutex);
   738		if (guard.gc->direction_input && guard.gc->direction_output)
   739			desc_data->direction_can_change = direction_may_change;
   740		else
   741			desc_data->direction_can_change = false;
   742	
   743		gpiod_attr_init(&desc_data->dir_attr, "direction",
   744				direction_show, direction_store);
   745		gpiod_attr_init(&desc_data->val_attr, "value", value_show, value_store);
   746		gpiod_attr_init(&desc_data->edge_attr, "edge", edge_show, edge_store);
   747		gpiod_attr_init(&desc_data->active_low_attr, "active_low",
   748					active_low_show, active_low_store);
   749	
   750		attrs = desc_data->attrs;
   751		desc_data->attr_group.is_visible = gpio_is_visible;
   752		attrs[GPIO_SYSFS_LINE_ATTR_DIRECTION] = &desc_data->dir_attr.attr;
   753		attrs[GPIO_SYSFS_LINE_ATTR_VALUE] = &desc_data->val_attr.attr;
   754		attrs[GPIO_SYSFS_LINE_ATTR_EDGE] = &desc_data->edge_attr.attr;
   755		attrs[GPIO_SYSFS_LINE_ATTR_ACTIVE_LOW] =
   756					&desc_data->active_low_attr.attr;
   757	
   758		desc_data->attr_group.attrs = desc_data->attrs;
   759		desc_data->attr_groups[0] = &desc_data->attr_group;
   760	
   761		/*
   762		 * Note: we need to continue passing desc_data here as there's still
   763		 * at least one known user of gpiod_export_link() in the tree. This
   764		 * function still uses class_find_device() internally.
   765		 */
   766		desc_data->dev = device_create_with_groups(&gpio_class, &gdev->dev,
   767							   MKDEV(0, 0), desc_data,
   768							   desc_data->attr_groups,
   769							   "gpio%u",
   770							   desc_to_gpio(desc));
   771		if (IS_ERR(desc_data->dev)) {
   772			status = PTR_ERR(desc_data->dev);
   773			goto err_free_data;
   774		}
   775	
   776		desc_data->value_class_node = sysfs_get_dirent(desc_data->dev->kobj.sd,
   777							       "value");
   778		if (!desc_data->value_class_node) {
   779			status = -ENODEV;
   780			goto err_unregister_device;
   781		}
   782	
   783		gdev_data = gdev_get_data(gdev);
   784		if (!gdev_data) {
   785			status = -ENODEV;
   786			goto err_put_dirent;
   787		}
   788	
   789		list_add(&desc_data->list, &gdev_data->exported_lines);
   790	
   791		desc_data->attr_group.name = kasprintf(GFP_KERNEL, "gpio%u",
   792						       gpio_chip_hwgpio(desc));
   793		if (!desc_data->attr_group.name) {
   794			status = -ENOMEM;
   795			goto err_put_dirent;
   796		}
   797	
   798		desc_data->parent = &gdev_data->cdev_id->kobj;
   799		status = sysfs_create_groups(desc_data->parent,
   800					     desc_data->attr_groups);
   801		if (status)
 > 802			goto err_free_name;
   803	
   804		char *path __free(kfree) = kasprintf(GFP_KERNEL, "gpio%u/value",
   805						     gpio_chip_hwgpio(desc));
   806		if (!path) {
   807			status = -ENOMEM;
   808			goto err_remove_groups;
   809		}
   810	
   811		desc_data->value_chip_node = kernfs_walk_and_get(desc_data->parent->sd,
   812								 path);
   813		if (!desc_data->value_chip_node) {
   814			status = -ENODEV;
   815			goto err_remove_groups;
   816		}
   817	
   818		return 0;
   819	
   820	err_remove_groups:
   821		sysfs_remove_groups(desc_data->parent, desc_data->attr_groups);
   822	err_free_name:
   823		kfree(desc_data->attr_group.name);
   824	err_put_dirent:
   825		sysfs_put(desc_data->value_class_node);
   826	err_unregister_device:
   827		device_unregister(desc_data->dev);
   828	err_free_data:
   829		kfree(desc_data);
   830	err_clear_bit:
   831		clear_bit(FLAG_EXPORT, &desc->flags);
   832		gpiod_dbg(desc, "%s: status %d\n", __func__, status);
   833		return status;
   834	}
   835	EXPORT_SYMBOL_GPL(gpiod_export);
   836	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki