Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH] gpiolib: release user space's lines when adding the chip fails late
@ 2026-11-25 11:04 Adrian Ambrozewicz
  0 siblings, 0 replies; only message in thread
From: Adrian Ambrozewicz @ 2026-11-25 11:04 UTC (permalink / raw)
  To: Bartosz Golaszewski, Linus Walleij
  Cc: Bartosz Golaszewski, Kent Gibson, Andy Shevchenko, Herve Codina,
	linux-gpio, linux-kernel, Sashiko

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-25 11:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 11:04 [PATCH] gpiolib: release user space's lines when adding the chip fails late Adrian Ambrozewicz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox