Re: [PATCH] gpiolib: Avoid side effects in gpio_is_visible()

From: Linus Walleij
Date: Fri May 12 2023 - 03:57:34 EST


On Fri, May 12, 2023 at 6:28 AM Chris Packham
<chris.packham@xxxxxxxxxxxxxxxxxxx> wrote:

> Calling gpiod_to_irq() creates an irq_desc for the GPIO.

Normally gpiod_to_irq() should not have side effects, it's just
a helper function that is there to translate a descriptor to the
corresponding IRQ number.

> This is not
> something that should happen when just exporting the GPIO via sysfs. The
> effect of this was observed as triggering a warning in
> gpiochip_disable_irq() when kexec()ing after exporting a GPIO.
>
> Remove the call to gpiod_to_irq() from gpio_is_visible(). The actual
> intended creation of the irq_desc comes via edge_store() when requested
> by the user.
>
> Fixes: ebbeba120ab2 ("gpio: sysfs: fix gpio attribute-creation race")
> Signed-off-by: Chris Packham <chris.packham@xxxxxxxxxxxxxxxxxxx>

I have a hard time understanding this fix.

The problem is rather this see gpiolib.c:

int gpiod_to_irq(const struct gpio_desc *desc)
{
struct gpio_chip *gc;
int offset;

/*
* Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
* requires this function to not return zero on an invalid descriptor
* but rather a negative error number.
*/
if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
return -EINVAL;

gc = desc->gdev->chip;
offset = gpio_chip_hwgpio(desc);
if (gc->to_irq) {
int retirq = gc->to_irq(gc, offset);

Here gc->to_irq() is called unconditionally.

Since this is using gpiolib_irqchip this ->to_irq() will be
gpiochip_to_irq() and that finally ends in the call:

return irq_create_mapping(domain, offset);

which seems to be the problem here. Why is this a problem?
The IRQ mappings are dynamic, meaning they are created
on-demand, but for this hardware it should be fine to essentially
just call irq_create_mapping() on all of them as the device
is created, we just don't do it in order to save space.

I don't understand why calling irq_create_mapping(domain, offset);
creates a problem? It's just a mapping between a GPIO and the
corresponding IRQ. What am I missing here?

Yours,
Linus Walleij