Skip to contentSkip to conversation

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

PATCH New Adrian Ambrozewicz 2 participants 2 messages Nov 25 GPIO

lore
Share & tools
lore
#0 lore raw

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

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")

---
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(-)
drivers/gpio/gpiolib.c +27 -15
index 57024a1b042b..f222b81ff756 100644
@@ -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

Signature · 1 line
2.43.0
#1 lore raw
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…

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 <[email protected]>

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.

Quoted 38 lines
drivers/gpio/gpiolib.c +19 -1
index 57024a1b042b6..f222b81ff7567 100644
@@ -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;

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 1422

Quoted 10 lines
drivers/gpio/gpiolib.c +7 -2
 
-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().

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 1425

Quoted 3 lines
drivers/gpio/gpiolib.c +0 -0
 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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1