[PATCH v1 1/1] gpiolib: Make gpiod_put() error pointer aware

From: Andy Shevchenko
Date: Wed Apr 02 2025 - 11:21:04 EST


When non-optional GPIO is requested and failed, the variable that holds
the (invalid) descriptor can contain an error pointer. However, gpiod_put()
ignores that fact and tries to cleanup never requested descriptor.
Make sure gpiod_put() ignores that as well.

While at it, do the same for the gpiod_put_array().

Note, it arguable needs to be present in the stubs as those are usually
called when CONFIG_GPIOLIB=n and GPIOs are requested using gpiod_get_optional()
or similar APIs.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
drivers/gpio/gpiolib.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index eda4f51d6bb8..1e96b83d2670 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -5111,8 +5111,10 @@ EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
*/
void gpiod_put(struct gpio_desc *desc)
{
- if (desc)
- gpiod_free(desc);
+ if (IS_ERR_OR_NULL(desc))
+ return;
+
+ gpiod_free(desc);
}
EXPORT_SYMBOL_GPL(gpiod_put);

@@ -5124,6 +5126,9 @@ void gpiod_put_array(struct gpio_descs *descs)
{
unsigned int i;

+ if (IS_ERR_OR_NULL(descs))
+ return;
+
for (i = 0; i < descs->ndescs; i++)
gpiod_put(descs->desc[i]);

--
2.47.2