Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Adrian Ambrozewicz <aambrozewicz@nvidia.com>
To: Bartosz Golaszewski <brgl@kernel.org>, Linus Walleij <linusw@kernel.org>
Cc: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
	Kent Gibson <warthog618@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Herve Codina <herve.codina@bootlin.com>,
	linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH] gpiolib: release user space's lines when adding the chip fails late
Date: Wed, 25 Nov 2026 13:04:43 +0200	[thread overview]
Message-ID: <20261125-gpiolib-setupdev-v1-1-aambrozewicz@nvidia.com> (raw)

gpiochip_setup_dev() registers the character device and then the sysfs
interface. If the latter fails, the error path removes the irqchip and
returns, but user space may already hold a line request with edge
detection. Unlike gpiochip_remove(), the error path neither clears
gdev->chip nor has the character device free its IRQs, so the request
keeps calling into a chip the driver may already have freed, and its
IRQ outlives the irqchip.

Free the hogs while the chip can still free them, then numb the device,
remove the shared proxies and free the IRQs as gpiochip_remove() does,
in a helper both now use.

Reproduced with a test-only change that fails gpio-sim setup a second
after the character device is added: reading the values of a line
requested then hits a KASAN slab-use-after-free of the gpio-sim chip,
freed by devres. With this change, reads return -ENODEV.

Fixes: ec8b6f55b981 ("gpio: free irqs that are still requested when the chip is being removed")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20261016-gpiolib-chip-removal-v2-0-aambrozewicz@nvidia.com?part=6
Signed-off-by: Adrian Ambrozewicz <aambrozewicz@nvidia.com>
---
The fix needs two commits of the chip-removal series in gpio/for-current:
"gpiolib: let the character device free its IRQs first on chip removal"
and "gpiolib: remove the shared GPIO proxies before freeing the remaining
IRQs". It is the separate fix for Sashiko's comment on v2 6/6 of that
series.

I left out Cc: stable for you to decide: the path needs the sysfs
registration to fail, in practice an allocation failure. A backport
would also need commit 2e002a1d05b1 ("gpiolib: keep the line state
workqueue alive until device release"), and before 6.19, which has no
shared GPIO proxies, a reworked error path; I can prepare one if you
want it.

Prepared with Cursor and Claude Code, using Claude, which helped analyse
the bug, write the reproducer and draft the patch; I reviewed and tested
the change.

 drivers/gpio/gpiolib.c | 42 +++++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 57024a1b042b..f222b81ff756 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);
 err_remove_irqchip:
 	gpiochip_irqchip_remove(gc);
 err_remove_irqchip_mask:
@@ -1455,18 +1478,7 @@ void gpiochip_remove(struct gpio_chip *gc)
 		list_del_rcu(&gdev->list);
 	synchronize_srcu(&gpio_devices_srcu);
 
-	/* 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);
+	gpiochip_numb_device(gc);
 	gpiochip_irqchip_remove(gc);
 	acpi_gpiochip_remove(gc);
 	of_gpiochip_remove(gc);

base-commit: 1f7d535465a5568187cbdfec3beb734bfb5e5015
-- 
2.43.0


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

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

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=20261125-gpiolib-setupdev-v1-1-aambrozewicz@nvidia.com \
    --to=aambrozewicz@nvidia.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=herve.codina@bootlin.com \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=warthog618@gmail.com \
    /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