Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Adrian Ambrozewicz" <aambrozewicz@nvidia.com>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH] gpiolib: release user space's lines when adding the chip fails late
Date: Wed, 25 Nov 2026 11:31:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-233252@kernel.org> (raw)
In-Reply-To: <20261125-gpiolib-setupdev-v1-1-aambrozewicz@nvidia.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] gpiolib: gc->free() skipped for shared GPIOs in
  gpiochip_numb_device()
- [Medium] gpiolib: double free_irq() and skipped gc->free() on ACPI
  GPIOs
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] gpiolib: early gdev list addition races with internal line
  requests [1]
--

commit a2a874bafb89861c1957ce33b2879e2d092ce02d
Author: Adrian Ambrozewicz <aambrozewicz@nvidia.com>

gpiolib: release user space's lines when adding the chip fails late

This commit extracts gpiochip_numb_device() from gpiochip_remove() and
calls it on the gpiochip_setup_dev() error path in
gpiochip_add_data_with_key() so that lines and IRQs requested by user
space after character device registration are released before tearing
down the chip.

> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index 57024a1b042b6..f222b81ff7567 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -904,6 +904,24 @@ static void gpiochip_free_remaining_irqs(struct gpio_chip *gc)
>  		gpiod_free_irqs(gc, desc);
>  }
>  
> +static void gpiochip_numb_device(struct gpio_chip *gc)
> +{
> +	struct gpio_device *gdev = gc->gpiodev;
> +
> +	/* Numb the device, cancelling all outstanding operations */
> +	rcu_assign_pointer(gdev->chip, NULL);
> +	synchronize_srcu(&gdev->srcu);
> +
> +	/*
> +	 * No new interrupts can be requested through the character device now.
> +	 * Have it free the ones it holds, also on the shared proxies, before
> +	 * forcibly freeing the rest and removing the irqchip.
> +	 */
> +	blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL);
> +	gpio_device_teardown_shared(gdev);
> +	gpiochip_free_remaining_irqs(gc);
> +}
> +
>  static void gpiodev_release(struct device *dev)
>  {
>  	struct gpio_device *gdev = to_gpio_device(dev);
> @@ -1390,13 +1408,18 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
>  	if (gpiolib_initialized) {
>  		ret = gpiochip_setup_dev(gc);
>  		if (ret)
> -			goto err_teardown_shared;
> +			goto err_numb_device;
>  	}
>  
>  	return 0;
>  
> -err_teardown_shared:
> -	gpio_device_teardown_shared(gdev);
> +err_numb_device:
> +	/*
> +	 * User space may hold lines by now. Free the hogs while the chip can
> +	 * still free them; the gpiochip_free_hogs() below then finds none.
> +	 */
> +	gpiochip_free_hogs(gc);
> +	gpiochip_numb_device(gc);

[Severity: Medium]
Does moving gpio_device_teardown_shared() into gpiochip_numb_device() skip
gc->free() for shared GPIO lines when gpiochip_setup_dev() fails?

gpiochip_setup_shared() reserves each shared pin on the parent chip via
gpiod_request_commit(desc, "shared"), which invokes gc->request().  Under
err_teardown_shared, gpio_device_teardown_shared() ran while gdev->chip
was still non-NULL.  Now gpiochip_numb_device() clears gdev->chip first,
so when gpio_device_teardown_shared() calls gpiod_free_commit(), guard.gc
is NULL and guard.gc->free() is skipped, leaking pinctrl reservations or
driver resources acquired in gc->request().

>  err_remove_irqchip:
>  	gpiochip_irqchip_remove(gc);
>  err_remove_irqchip_mask:

[Severity: Medium]
Can running gpiochip_numb_device() before gpiochip_irqchip_remove() and
acpi_gpiochip_remove() warn on a double free_irq() and skip gc->free() for
ACPI GPIOs?

gpiochip_add_irqchip() calls acpi_gpiochip_request_interrupts(), which
requests each ACPI event descriptor with gpiochip_request_own_desc(),
marks it GPIOD_FLAG_USED_AS_IRQ, requests event->irq, and sets
event->irq_requested = true.  When gpiochip_setup_dev() fails,
gpiochip_numb_device() now clears gdev->chip and force-frees event->irq
via gpiochip_free_remaining_irqs().  gpiochip_irqchip_remove() then calls
acpi_gpiochip_free_interrupts(), which sees event->irq_requested still
set and calls free_irq(event->irq, event) a second time (triggering the
"Trying to free already-free IRQ" warning in __free_irq()), and both
acpi_gpiochip_free_interrupts() and acpi_gpiochip_remove() call
gpiochip_free_own_desc() with gdev->chip already NULL, skipping
gc->free().

[Severity: High]
This problem wasn't introduced by this patch, but exposing the chip in
gpiochip_add_data_with_key() before its internal line requests complete
[1] lets a concurrent consumer grab a line first and abort
registration with -EBUSY.

gpiodev_add_to_list_unlocked(), of_gpiochip_add(), and acpi_gpiochip_add()
make the chip visible to consumers before gpiochip_hog_lines() and
gpiochip_setup_shared() run:

CPU0                              CPU1
gpiochip_add_data_with_key()
  gpiodev_add_to_list_unlocked()
  of_gpiochip_add()
                                  consumer_probe()
                                    gpiod_get()
                                      gpiod_request()
  gpiochip_hog_lines()
    returns -EBUSY
  goto err_free_hogs;

If an asynchronously probing consumer requests a line meant to be hogged
or shared, gpiochip_hog_lines() or gpiochip_setup_shared() fails with
-EBUSY and tears down the controller registration.

[1] https://sashiko.dev/#/bug/linux-0a1f2aa1-7014-44b7-8c14-12774825d6d8

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-gpiolib-setupdev-v1-1-aambrozewicz@nvidia.com?part=1

      reply	other threads:[~2026-11-25 11:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 11:04 [PATCH] gpiolib: release user space's lines when adding the chip fails late Adrian Ambrozewicz
2026-11-25 11:31 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233252@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aambrozewicz@nvidia.com \
    --cc=linux-gpio@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox