Re: [PATCH] spi: bcm2835: Do not call gpiod_put() on invalid descriptor

From: Andy Shevchenko
Date: Wed Apr 02 2025 - 10:59:38 EST


On Wed, Apr 02, 2025 at 01:36:28PM +0200, Bartosz Golaszewski wrote:
> On Wed, Apr 2, 2025 at 12:43 AM Florian Fainelli
> <florian.fainelli@xxxxxxxxxxxx> wrote:
> >
> > If we are unable to lookup the chip-select GPIO, the error path will
> > call bcm2835_spi_cleanup() which unconditionally calls gpiod_put() on
> > the cs->gpio variable which we just determined was invalid.

...

> > - gpiod_put(bs->cs_gpio);
> > + if (!IS_ERR(bs->cs_gpio))
> > + gpiod_put(bs->cs_gpio);

> We could also just set it to NULL on error in bcm2835_spi_setup() but
> I'm fine either way.

I think this patch papers over the real issue:
1) the cleanup call does everything and not split to have the exact reversed order of the setup;
2) the GPIO here as far as I understand is not optional and on errors may contain an error pointer
but gpiod_put() ignores that.

TL;DR: I think the proper fix is to make gpio_put() to accept an error pointer as NULL. I.o.w.
if (desc) --> if (!IS_ERR_OR_NULL(desc)) in all conditionals related to gpiod*put*() calls.

--
With Best Regards,
Andy Shevchenko