[PATCH v2 4/9] gpio: swnode: don't use the swnode's name as the key for GPIO lookup

Bartosz Golaszewski posted 9 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH v2 4/9] gpio: swnode: don't use the swnode's name as the key for GPIO lookup
Posted by Bartosz Golaszewski 3 months, 2 weeks ago
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Looking up a GPIO controller by label that is the name of the software
node is wonky at best - the GPIO controller driver is free to set
a different label than the name of its firmware node. We're already being
passed a firmware node handle attached to the GPIO device to
swnode_get_gpio_device() so use it instead for a more precise lookup.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpiolib-swnode.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/drivers/gpio/gpiolib-swnode.c b/drivers/gpio/gpiolib-swnode.c
index f21dbc28cf2c8c2d06d034b7c89d302cc52bb9b5..573b5216cfda105bafa58e04fc5ad3a38d283698 100644
--- a/drivers/gpio/gpiolib-swnode.c
+++ b/drivers/gpio/gpiolib-swnode.c
@@ -7,6 +7,7 @@
 
 #define pr_fmt(fmt) "gpiolib: swnode: " fmt
 
+#include <linux/cleanup.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/export.h>
@@ -26,23 +27,20 @@
 
 static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
 {
-	const struct software_node *gdev_node;
-	struct gpio_device *gdev;
-
-	gdev_node = to_software_node(fwnode);
-	if (!gdev_node || !gdev_node->name)
-		return ERR_PTR(-EINVAL);
+	struct gpio_device *gdev __free(gpio_device_put) =
+					gpio_device_find_by_fwnode(fwnode);
+	if (!gdev)
+		return ERR_PTR(-EPROBE_DEFER);
 
 	/*
 	 * Check for a special node that identifies undefined GPIOs, this is
 	 * primarily used as a key for internal chip selects in SPI bindings.
 	 */
 	if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) &&
-	    !strcmp(gdev_node->name, GPIOLIB_SWNODE_UNDEFINED_NAME))
+	    !strcmp(fwnode_get_name(fwnode), GPIOLIB_SWNODE_UNDEFINED_NAME))
 		return ERR_PTR(-ENOENT);
 
-	gdev = gpio_device_find_by_label(gdev_node->name);
-	return gdev ?: ERR_PTR(-EPROBE_DEFER);
+	return no_free_ptr(gdev);
 }
 
 static int swnode_gpio_get_reference(const struct fwnode_handle *fwnode,

-- 
2.48.1
Re: [PATCH v2 4/9] gpio: swnode: don't use the swnode's name as the key for GPIO lookup
Posted by Andy Shevchenko 3 months, 2 weeks ago
On Wed, Oct 22, 2025 at 03:41:03PM +0200, Bartosz Golaszewski wrote:
> 
> Looking up a GPIO controller by label that is the name of the software
> node is wonky at best - the GPIO controller driver is free to set
> a different label than the name of its firmware node. We're already being
> passed a firmware node handle attached to the GPIO device to
> swnode_get_gpio_device() so use it instead for a more precise lookup.

...

>  static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
>  {
> +	struct gpio_device *gdev __free(gpio_device_put) =
> +					gpio_device_find_by_fwnode(fwnode);
> +	if (!gdev)
> +		return ERR_PTR(-EPROBE_DEFER);
>  
>  	/*
>  	 * Check for a special node that identifies undefined GPIOs, this is
>  	 * primarily used as a key for internal chip selects in SPI bindings.
>  	 */
>  	if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) &&
> +	    !strcmp(fwnode_get_name(fwnode), GPIOLIB_SWNODE_UNDEFINED_NAME))
>  		return ERR_PTR(-ENOENT);

Now we might get EPROBE_DEREF for the cases when previously it was ENOENT.
Why is this not a problem? (I haven't found the answer neither in the comment
above, nor in the commit message)

> +	return no_free_ptr(gdev);
>  }

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2 4/9] gpio: swnode: don't use the swnode's name as the key for GPIO lookup
Posted by Bartosz Golaszewski 3 months, 2 weeks ago
On Wed, Oct 22, 2025 at 8:44 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Wed, Oct 22, 2025 at 03:41:03PM +0200, Bartosz Golaszewski wrote:
> >
> > Looking up a GPIO controller by label that is the name of the software
> > node is wonky at best - the GPIO controller driver is free to set
> > a different label than the name of its firmware node. We're already being
> > passed a firmware node handle attached to the GPIO device to
> > swnode_get_gpio_device() so use it instead for a more precise lookup.
>
> ...
>
> >  static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
> >  {
> > +     struct gpio_device *gdev __free(gpio_device_put) =
> > +                                     gpio_device_find_by_fwnode(fwnode);
> > +     if (!gdev)
> > +             return ERR_PTR(-EPROBE_DEFER);
> >
> >       /*
> >        * Check for a special node that identifies undefined GPIOs, this is
> >        * primarily used as a key for internal chip selects in SPI bindings.
> >        */
> >       if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) &&
> > +         !strcmp(fwnode_get_name(fwnode), GPIOLIB_SWNODE_UNDEFINED_NAME))
> >               return ERR_PTR(-ENOENT);
>
> Now we might get EPROBE_DEREF for the cases when previously it was ENOENT.
> Why is this not a problem? (I haven't found the answer neither in the comment
> above, nor in the commit message)
>

Yeah, maybe it should only change the last part from looking up by
label to looking up by fwnode and leave the previous checks in place.

Bart