Re: [PATCH 02/10] gpiolib: acpi: Hold gpio_device reference in acpi_dev_gpio_irq_wake_get_by()

From: Andy Shevchenko

Date: Fri Dec 04 2026 - 06:36:00 EST


On Thu, Dec 03, 2026 at 08:55:00PM +0000, Lee Jones wrote:
> acpi_dev_gpio_irq_wake_get_by() obtains a descriptor from
> __acpi_find_gpio() without holding gpio_devices_srcu or acquiring a
> reference to desc->gdev. Because acpi_get_gpiod() releases its
> gpio_device reference before returning, a concurrent GPIO controller
> unbind can free gdev and gdev->descs before gpiod_to_irq() and the
> subsequent descriptor configuration helpers run.
>
> Expose gpio_devices_srcu internally within gpiolib, guard the
> __acpi_find_gpio() lookup with gpio_devices_srcu, and acquire a scoped
> gpio_device reference via gpio_device_get() before leaving the SRCU
> read-side critical section.

Yep, it sounds possible scenario.

...

> for (i = 0, idx = 0; idx <= index; i++) {
> struct acpi_gpio_info info = {};
> + struct gpio_device *gdev __free(gpio_device_put) = NULL;
> struct gpio_desc *desc;
>
> - /* Ignore -EPROBE_DEFER, it only matters if idx matches */
> - desc = __acpi_find_gpio(fwnode, con_id, i, true, &info);
> + scoped_guard(srcu, &gpio_devices_srcu) {
> + /* Ignore -EPROBE_DEFER, it only matters if idx matches */
> + desc = __acpi_find_gpio(fwnode, con_id, i, true, &info);
> + if (!IS_ERR(desc))
> + gdev = gpio_device_get(desc->gdev);
> + }
> if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
> return PTR_ERR(desc);

I would rewrite this as

struct acpi_gpio_info info = {};
struct gpio_desc *desc;

struct gpio_device *gdev __free(gpio_device_put) = NULL;
scoped_guard(srcu, &gpio_devices_srcu) {
desc = __acpi_find_gpio(fwnode, con_id, i, true, &info);
if (IS_ERR(desc)) {
/* Ignore -EPROBE_DEFER, it only matters if idx matches */
if (PTR_ERR(desc) != -EPROBE_DEFER)
return PTR_ERR(desc);
} else {
gdev = gpio_device_get(desc->gdev);
}
}

--
With Best Regards,
Andy Shevchenko