Skip to contentSkip to conversation

gpio: Handle NULL pointers gracefully

RFC PATCH New Pratyush Yadav 2 participants 5 messages May 29, 2020 – Jun 17, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover gpio: Handle NULL pointers gracefully

Hi,

Patch [0] added devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

We need to take a stance on who is responsible for the NULL check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. Linux does not generally trust drivers and usually verifies anything it gets from them. And FWIW, I see that the clk and phy subsystems in U-Boot also perform checks like this.

The downside of the checks is of course that they increase code size. They might also slightly decrease performance. The benefit is that we don't burden drivers with taking care of this.

The patch itself is based on a similar patch by Jean-Jacques.

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200529213808.2815-2-p.yadav at ti.com/

Pratyush Yadav (1):
gpio: Handle NULL pointers gracefully

 drivers/gpio/Kconfig       |  9 ++++
 drivers/gpio/gpio-uclass.c | 86 ++++++++++++++++++++++++++++++++++----
 include/asm-generic/gpio.h |  2 +-
 3 files changed, 88 insertions(+), 9 deletions(-)

--
2.26.2

#1 lore raw

RFC PATCH 1/1 gpio: Handle NULL pointers gracefully

Prepare the way for a managed GPIO API by handling NULL pointers without crashing or failing. validate_desc() comes from Linux with the prints removed to reduce code size.

---
 drivers/gpio/Kconfig       |  9 ++++
 drivers/gpio/gpio-uclass.c | 86 ++++++++++++++++++++++++++++++++++----
 include/asm-generic/gpio.h |  2 +-
 3 files changed, 88 insertions(+), 9 deletions(-)
drivers/gpio/Kconfig +9 -0
index d87f6cc105..f8b6bcdf44 100644
@@ -36,6 +36,15 @@ config TPL_DM_GPIO
 	  particular GPIOs that they provide. The uclass interface
 	  is defined in include/asm-generic/gpio.h.
 
+config GPIO_VALIDATE_DESC
+	bool "Check if GPIO descriptor is NULL and bail out if it is"
+	depends on DM_GPIO
+	default y
+	help
+	  If a GPIO is optional, the GPIO descriptor is NULL. In that
+	  case, calls should bail out instead of causing NULL pointer
+	  access.
+
 config GPIO_HOG
 	bool "Enable GPIO hog support"
 	depends on DM_GPIO
drivers/gpio/gpio-uclass.c +78 -8

142 lines not on this page · raw message

include/asm-generic/gpio.h +1 -1
index e16c2f31d9..46007b1283 100644
@@ -149,7 +149,7 @@ struct gpio_desc {
  */
 static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
 {
-	return desc->dev != NULL;
+	return desc && desc->dev;
 }
 
 /**

--
2.26.2

#2 lore raw

RFC PATCH 1/1 gpio: Handle NULL pointers gracefully

Hi Pratyush,

Quoted 4 lines · Pratyush Yadav

Prepare the way for a managed GPIO API by handling NULL pointers without crashing or failing. validate_desc() comes from Linux with the prints removed to reduce code size.

Please can you add a little detail as to why this is needed? Are you trying to pass a NULL GPIO descriptor to the function?

Quoted 15 lines
---
 drivers/gpio/Kconfig       |  9 ++++
 drivers/gpio/gpio-uclass.c | 86 ++++++++++++++++++++++++++++++++++----
 include/asm-generic/gpio.h |  2 +-
 3 files changed, 88 insertions(+), 9 deletions(-)
drivers/gpio/Kconfig +0 -0
index d87f6cc105..f8b6bcdf44 100644
@@ -36,6 +36,15 @@ config TPL_DM_GPIO
          particular GPIOs that they provide. The uclass interface

drivers/gpio/Kconfig config TPL_DM_GPIO · line 46

Quoted 10 lines
drivers/gpio/Kconfig +8 -0
          is defined in include/asm-generic/gpio.h.
 
+config GPIO_VALIDATE_DESC
+       bool "Check if GPIO descriptor is NULL and bail out if it is"
+       depends on DM_GPIO
+       default y
+       help
+         If a GPIO is optional, the GPIO descriptor is NULL. In that
+         case, calls should bail out instead of causing NULL pointer
+         access.

Can you update the gpio function docs in the header file with this info?

Quoted 9 lines
drivers/gpio/Kconfig +1 -0
+
 config GPIO_HOG
        bool "Enable GPIO hog support"
        depends on DM_GPIO
drivers/gpio/gpio-uclass.c +0 -0
index 9eeab22eef..6b97d3aaff 100644

drivers/gpio/gpio-uclass.c line 28

Quoted 9 lines
drivers/gpio/gpio-uclass.c +6 -0
@@ -20,6 +20,25 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+/*
+ * This descriptor validation needs to be inserted verbatim into each
+ * function taking a descriptor, so we need to use a preprocessor
+ * macro to avoid endless duplication. If the desc is NULL it is an
+ * optional GPIO and calls should just bail out.

Is the macro defined elsewhere?

drivers/gpio/gpio-uclass.c line 39

Quoted 11 lines
drivers/gpio/gpio-uclass.c +11 -0
+ */
+static inline int validate_desc(const struct gpio_desc *desc)
+{
+       if (!desc)
+               return 0;
+       if (IS_ERR(desc))
+               return PTR_ERR(desc);
+       if (!desc->dev)
+               return -EINVAL;
+       return 1;
+}

#else
static inline int validate_desc(const struct gpio_desc *desc)

{
    return 1;
}
Quoted 6 lines
drivers/gpio/gpio-uclass.c +2 -0
+#endif
+
 /**
  * gpio_desc_init() - Initialize the GPIO descriptor
  *

drivers/gpio/gpio-uclass.c int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) · line 330

Quoted 10 lines
drivers/gpio/gpio-uclass.c +2 -1
@@ -303,11 +322,19 @@ int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
 
 int dm_gpio_request(struct gpio_desc *desc, const char *label)
 {
-       struct udevice *dev = desc->dev;
+       struct udevice *dev;
        struct gpio_dev_priv *uc_priv;
        char *str;
        int ret;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC

Please drop the #ifdefs and use the static inline thing from above.

drivers/gpio/gpio-uclass.c int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) · line 333

Quoted 3 lines
drivers/gpio/gpio-uclass.c +3 -0
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;

Here you are returning 0 when you did not successfully request the GPIO.You should return -ENOENT, otherwise callers have no idea what happened and will get confused.

Quoted 115 lines
drivers/gpio/gpio-uclass.c +54 -7
+#endif
+
+       dev = desc->dev;
+
        uc_priv = dev_get_uclass_priv(dev);
        if (uc_priv->name[desc->offset])
                return -EBUSY;
@@ -434,6 +461,14 @@ static int check_reserved(const struct gpio_desc *desc, const char *func)
 {
        struct gpio_dev_priv *uc_priv;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       int ret;
+
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        if (!dm_gpio_is_valid(desc))
                return -ENOENT;
 
@@ -510,6 +545,12 @@ int dm_gpio_get_value(const struct gpio_desc *desc)
 {
        int ret;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        ret = check_reserved(desc, "get_value");
        if (ret)
                return ret;
@@ -521,6 +562,12 @@ int dm_gpio_set_value(const struct gpio_desc *desc, int value)
 {
        int ret;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        ret = check_reserved(desc, "set_value");
        if (ret)
                return ret;
@@ -572,11 +619,21 @@ static int check_dir_flags(ulong flags)
 
 static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
 {
-       struct udevice *dev = desc->dev;
-       struct dm_gpio_ops *ops = gpio_get_ops(dev);
-       struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+       struct udevice *dev;
+       struct dm_gpio_ops *ops;
+       struct gpio_dev_priv *uc_priv;
        int ret = 0;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
+       dev = desc->dev;
+       ops = gpio_get_ops(dev);
+       uc_priv = dev_get_uclass_priv(dev);
+
        ret = check_dir_flags(flags);
        if (ret) {
                dev_dbg(dev,
@@ -1043,6 +1100,14 @@ int gpio_get_list_count(struct udevice *dev, const char *list_name)
 
 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
 {
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       int ret;
+
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        /* For now, we don't do any checking of dev */
        return _dm_gpio_free(desc->dev, desc->offset);
 }
@@ -1091,12 +1156,17 @@ static int gpio_renumber(struct udevice *removed_dev)
 
 int gpio_get_number(const struct gpio_desc *desc)
 {
-       struct udevice *dev = desc->dev;
        struct gpio_dev_priv *uc_priv;
 
-       if (!dev)
-               return -1;
-       uc_priv = dev->uclass_priv;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       int ret;
+
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
+       uc_priv = dev_get_uclass_priv(desc->dev);
 
        return uc_priv->gpio_base + desc->offset;
 }
include/asm-generic/gpio.h +0 -0
index e16c2f31d9..46007b1283 100644

include/asm-generic/gpio.h struct gpio_desc { · line 152

Quoted 5 lines
include/asm-generic/gpio.h +1 -1
@@ -149,7 +149,7 @@ struct gpio_desc {
  */
 static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
 {
-       return desc->dev != NULL;
+       return desc && desc->dev;

Needs IS_ENABLED(CONFIG_GPIO_VALIDATE_DESC) somewhere here.

Regards,
Simon

#3 lore raw

RFC PATCH 1/1 gpio: Handle NULL pointers gracefully

Quoted 10 lines · Simon Glass

Hi Pratyush,

Quoted 4 lines · Pratyush Yadav

Prepare the way for a managed GPIO API by handling NULL pointers without crashing or failing. validate_desc() comes from Linux with the prints removed to reduce code size.

Please can you add a little detail as to why this is needed? Are you trying to pass a NULL GPIO descriptor to the function?

Copy-pasting from the cover letter:

Patch [0] added devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

We need to take a stance on who is responsible for the NULL check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. Linux does not generally trust drivers and usually verifies anything it gets from them. And FWIW, I see that the clk and phy subsystems in U-Boot also perform checks like this.

The downside of the checks is of course that they increase code size. They might also slightly decrease performance. The benefit is that we don't burden drivers with taking care of this.

The patch itself is based on a similar patch by Jean-Jacques.

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200529213808.2815-2-p.yadav at ti.com/

Maybe I should put this in the commit message?

Quoted 16 lines
Quoted 16 lines
---
 drivers/gpio/Kconfig       |  9 ++++
 drivers/gpio/gpio-uclass.c | 86 ++++++++++++++++++++++++++++++++++----
 include/asm-generic/gpio.h |  2 +-
 3 files changed, 88 insertions(+), 9 deletions(-)
drivers/gpio/Kconfig +0 -0
index d87f6cc105..f8b6bcdf44 100644
@@ -36,6 +36,15 @@ config TPL_DM_GPIO
          particular GPIOs that they provide. The uclass interface
          is defined in include/asm-generic/gpio.h.
 
Quoted 10 lines

drivers/gpio/Kconfig config TPL_DM_GPIO · line 46

Quoted 8 lines
drivers/gpio/Kconfig +8 -0
+config GPIO_VALIDATE_DESC
+       bool "Check if GPIO descriptor is NULL and bail out if it is"
+       depends on DM_GPIO
+       default y
+       help
+         If a GPIO is optional, the GPIO descriptor is NULL. In that
+         case, calls should bail out instead of causing NULL pointer
+         access.

Can you update the gpio function docs in the header file with this info?

Ok.

Quoted 12 lines
Quoted 12 lines
drivers/gpio/Kconfig +1 -0
+
 config GPIO_HOG
        bool "Enable GPIO hog support"
        depends on DM_GPIO
drivers/gpio/gpio-uclass.c +0 -0
index 9eeab22eef..6b97d3aaff 100644
@@ -20,6 +20,25 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
Quoted 8 lines

drivers/gpio/gpio-uclass.c line 28

Quoted 6 lines
drivers/gpio/gpio-uclass.c +6 -0
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+/*
+ * This descriptor validation needs to be inserted verbatim into each
+ * function taking a descriptor, so we need to use a preprocessor
+ * macro to avoid endless duplication. If the desc is NULL it is an
+ * optional GPIO and calls should just bail out.

Is the macro defined elsewhere?

It comes from the previous version of this patch. The macro has been removed in this version. Will fix.

Quoted 33 lines

drivers/gpio/gpio-uclass.c line 39

Quoted 11 lines
drivers/gpio/gpio-uclass.c +11 -0
+ */
+static inline int validate_desc(const struct gpio_desc *desc)
+{
+       if (!desc)
+               return 0;
+       if (IS_ERR(desc))
+               return PTR_ERR(desc);
+       if (!desc->dev)
+               return -EINVAL;
+       return 1;
+}

#else
static inline int validate_desc(const struct gpio_desc *desc)

{
    return 1;
}
Quoted 15 lines
drivers/gpio/gpio-uclass.c +3 -1
+#endif
+
 /**
  * gpio_desc_init() - Initialize the GPIO descriptor
  *
@@ -303,11 +322,19 @@ int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
 
 int dm_gpio_request(struct gpio_desc *desc, const char *label)
 {
-       struct udevice *dev = desc->dev;
+       struct udevice *dev;
        struct gpio_dev_priv *uc_priv;
        char *str;
        int ret;
 
Quoted 3 lines

drivers/gpio/gpio-uclass.c int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) · line 330

Quoted 1 line
drivers/gpio/gpio-uclass.c +1 -0
+#ifdef CONFIG_GPIO_VALIDATE_DESC

Please drop the #ifdefs and use the static inline thing from above.

Ok.

Quoted 7 lines

drivers/gpio/gpio-uclass.c int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) · line 333

Quoted 3 lines
drivers/gpio/gpio-uclass.c +3 -0
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;

Here you are returning 0 when you did not successfully request the GPIO.You should return -ENOENT, otherwise callers have no idea what happened and will get confused.

Ok. To be clear, it is ok to return 0 in other places this check is done, right?

Quoted 108 lines
Quoted 108 lines
drivers/gpio/gpio-uclass.c +54 -7
+#endif
+
+       dev = desc->dev;
+
        uc_priv = dev_get_uclass_priv(dev);
        if (uc_priv->name[desc->offset])
                return -EBUSY;
@@ -434,6 +461,14 @@ static int check_reserved(const struct gpio_desc *desc, const char *func)
 {
        struct gpio_dev_priv *uc_priv;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       int ret;
+
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        if (!dm_gpio_is_valid(desc))
                return -ENOENT;
 
@@ -510,6 +545,12 @@ int dm_gpio_get_value(const struct gpio_desc *desc)
 {
        int ret;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        ret = check_reserved(desc, "get_value");
        if (ret)
                return ret;
@@ -521,6 +562,12 @@ int dm_gpio_set_value(const struct gpio_desc *desc, int value)
 {
        int ret;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        ret = check_reserved(desc, "set_value");
        if (ret)
                return ret;
@@ -572,11 +619,21 @@ static int check_dir_flags(ulong flags)
 
 static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
 {
-       struct udevice *dev = desc->dev;
-       struct dm_gpio_ops *ops = gpio_get_ops(dev);
-       struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+       struct udevice *dev;
+       struct dm_gpio_ops *ops;
+       struct gpio_dev_priv *uc_priv;
        int ret = 0;
 
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
+       dev = desc->dev;
+       ops = gpio_get_ops(dev);
+       uc_priv = dev_get_uclass_priv(dev);
+
        ret = check_dir_flags(flags);
        if (ret) {
                dev_dbg(dev,
@@ -1043,6 +1100,14 @@ int gpio_get_list_count(struct udevice *dev, const char *list_name)
 
 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
 {
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       int ret;
+
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
        /* For now, we don't do any checking of dev */
        return _dm_gpio_free(desc->dev, desc->offset);
 }
@@ -1091,12 +1156,17 @@ static int gpio_renumber(struct udevice *removed_dev)
 
 int gpio_get_number(const struct gpio_desc *desc)
 {
-       struct udevice *dev = desc->dev;
        struct gpio_dev_priv *uc_priv;
 
-       if (!dev)
-               return -1;
-       uc_priv = dev->uclass_priv;
+#ifdef CONFIG_GPIO_VALIDATE_DESC
+       int ret;
+
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif
+
+       uc_priv = dev_get_uclass_priv(desc->dev);
 
Quoted 14 lines

include/asm-generic/gpio.h struct gpio_desc { · line 152

Quoted 12 lines
drivers/gpio/gpio-uclass.c +0 -0
        return uc_priv->gpio_base + desc->offset;
 }
include/asm-generic/gpio.h +1 -1
index e16c2f31d9..46007b1283 100644
@@ -149,7 +149,7 @@ struct gpio_desc {
  */
 static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
 {
-       return desc->dev != NULL;
+       return desc && desc->dev;

Needs IS_ENABLED(CONFIG_GPIO_VALIDATE_DESC) somewhere here.

Since this already checks for desc->dev != NULL, I figured it would be OK to check for desc being valid too before dereferencing it without adding a config check. Are you sure we want to make the desc check optional here?

Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments India
#4 lore raw

RFC PATCH 1/1 gpio: Handle NULL pointers gracefully

Hi Pratyush,

Quoted 26 lines · Pratyush Yadav
Quoted 10 lines · Simon Glass

Hi Pratyush,

Quoted 4 lines · Pratyush Yadav

Prepare the way for a managed GPIO API by handling NULL pointers without crashing or failing. validate_desc() comes from Linux with the prints removed to reduce code size.

Please can you add a little detail as to why this is needed? Are you trying to pass a NULL GPIO descriptor to the function?

Copy-pasting from the cover letter:

Patch [0] added devm_gpiod_get_index_optional() which would return NULL when when no GPIO was assigned to the requested function. This is convenient for drivers that need to handle optional GPIOs.

We need to take a stance on who is responsible for the NULL check: the driver or the GPIO core? Do we want to trust drivers to take care of the NULL checks, or do we want to distrust them and make sure they don't send us anything bogus in the GPIO core. Linux does not generally trust drivers and usually verifies anything it gets from them. And FWIW, I see that the clk and phy subsystems in U-Boot also perform checks like this.

Quoted 3 lines

The downside of the checks is of course that they increase code size. They might also slightly decrease performance. The benefit is that we don't burden drivers with taking care of this.

U-Boot has code-size constraints so I would rather rely on automated testing than run-time checks. Linux has unlimited code size and few automated tests so it is a different situation.

Quoted 5 lines

The patch itself is based on a similar patch by Jean-Jacques.

[0] https://patchwork.ozlabs.org/project/uboot/patch/20200529213808.2815-2-p.yadav at ti.com/

Maybe I should put this in the commit message?

Yes
[..]

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
drivers/gpio/gpio-uclass.c +2 -1
+
 /**
  * gpio_desc_init() - Initialize the GPIO descriptor
  *
@@ -303,11 +322,19 @@ int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
 
 int dm_gpio_request(struct gpio_desc *desc, const char *label)
 {
-       struct udevice *dev = desc->dev;
+       struct udevice *dev;
        struct gpio_dev_priv *uc_priv;
        char *str;
        int ret;
 
Quoted 3 lines

Review int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) · line 330

Quoted 1 line
(quoted diff) +1 -0
+#ifdef CONFIG_GPIO_VALIDATE_DESC

Please drop the #ifdefs and use the static inline thing from above.

Ok.

Quoted 4 lines
Quoted 3 lines
drivers/gpio/gpio-uclass.c +3 -0
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
Quoted 6 lines
Quoted 3 lines

Here you are returning 0 when you did not successfully request the GPIO.You should return -ENOENT, otherwise callers have no idea what happened and will get confused.

Ok. To be clear, it is ok to return 0 in other places this check is done, right?

I'm not a fan of that, but I think you can put the checking in your own layer?

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/gpio/gpio-uclass.c +4 -0
+#endif
+
+       dev = desc->dev;
+
        uc_priv = dev_get_uclass_priv(dev);
        if (uc_priv->name[desc->offset])
                return -EBUSY;
@@ -434,6 +461,14 @@ static int check_reserved(const struct gpio_desc *desc, const char *func)
 {
        struct gpio_dev_priv *uc_priv;
 
Quoted 1 line
Quoted 1 line

Review static int check_reserved(const struct gpio_desc *desc, const char *func) · line 464

Quoted 1 line
(quoted diff) +1 -0
+#ifdef CONFIG_GPIO_VALIDATE_DESC

if (CONFIG_IS_ENABLED(...))

Quoted 6 lines
Quoted 6 lines

drivers/gpio/gpio-uclass.c static int check_reserved(const struct gpio_desc *desc, const char *func) · line 470

Quoted 6 lines
drivers/gpio/gpio-uclass.c +6 -0
+       int ret;
+
+       ret = validate_desc(desc);
+       if (ret <= 0)
+               return ret;
+#endif

So if validate_desc() is a static function you can always call it, and have it do nothing (and return 0) if the CONFIG is not enabled.

[..]

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
include/asm-generic/gpio.h +0 -0
index e16c2f31d9..46007b1283 100644
Quoted 8 lines

include/asm-generic/gpio.h struct gpio_desc { · line 152

Quoted 6 lines
include/asm-generic/gpio.h +1 -1
@@ -149,7 +149,7 @@ struct gpio_desc {
  */
 static inline bool dm_gpio_is_valid(const struct gpio_desc *desc)
 {
-       return desc->dev != NULL;
+       return desc && desc->dev;

Needs IS_ENABLED(CONFIG_GPIO_VALIDATE_DESC) somewhere here.

Quoted 4 lines

Since this already checks for desc->dev != NULL, I figured it would be OK to check for desc being valid too before dereferencing it without adding a config check. Are you sure we want to make the desc check optional here?

Yes because you are changing the meaning here. I am a little worried as to where this is heading.

Regards,
Simon