[PATCH v2 2/3] gpiolib: release the chip's own lines before numbing it on removal
From: Adrian Ambrozewicz
Date: Fri Nov 27 2026 - 02:49:49 EST
Commit 5018ada69a04 ("gpio: Move freeing of GPIO hogs before numbing of
the device") made gpiochip_remove() free the hogs before it clears
gdev->chip, so that gpiod_free_commit() still calls ->free() for them.
ACPI event lines, ACPI OpRegion connections and shared lines are still
released after that: ->free() is skipped for them, and the ACPI event
IRQs, already freed by gpiochip_free_remaining_irqs(), are freed again.
Release all lines the chip requested for itself, the hogs included,
after the chip has left the device list, so no new lookup finds the
chip, and before the numbing. Move the numbing into a helper for the
next patch.
Reproduced with a test-only ACPI-style event on gpio-sim ("Trying to
free already-free IRQ" on removal) and a shared gpio-sim line on arm64
(->free() skipped); with this change, neither.
This depends on "gpiolib: shared: remove the proxies before releasing
the shared line".
Fixes: fdeb8e1547cb ("gpio: reflect base and ngpio into gpio_device")
Fixes: ec8b6f55b981 ("gpio: free irqs that are still requested when the chip is being removed")
Fixes: 4918cc05137c ("gpio: shared: propagate configuration to pinctrl")
Signed-off-by: Adrian Ambrozewicz <aambrozewicz@xxxxxxxxxx>
---
drivers/gpio/gpiolib.c | 49 +++++++++++++++++++++++++++++-------------
1 file changed, 34 insertions(+), 15 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 57024a1b042b..1b92ace14a5a 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -904,6 +904,35 @@ static void gpiochip_free_remaining_irqs(struct gpio_chip *gc)
gpiod_free_irqs(gc, desc);
}
+/*
+ * Release the lines the chip requested for itself: hogs, ACPI events and
+ * OpRegion connections, shared lines. gpiod_free_commit() calls ->free()
+ * only while the chip is there, so this goes before the numbing.
+ */
+static void gpiochip_free_own_lines(struct gpio_chip *gc)
+{
+ gpiochip_free_hogs(gc);
+ acpi_gpiochip_free_interrupts(gc);
+ acpi_gpiochip_remove(gc);
+ gpio_device_teardown_shared(gc->gpiodev);
+}
+
+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 before forcibly freeing the rest.
+ */
+ blocking_notifier_call_chain(&gdev->device_notifier, 0, NULL);
+ gpiochip_free_remaining_irqs(gc);
+}
+
static void gpiodev_release(struct device *dev)
{
struct gpio_device *gdev = to_gpio_device(dev);
@@ -1449,26 +1478,15 @@ void gpiochip_remove(struct gpio_chip *gc)
/* FIXME: should the legacy sysfs handling be moved to gpio_device? */
gpiochip_sysfs_unregister(gc);
- gpiochip_free_hogs(gc);
scoped_guard(mutex, &gpio_devices_lock)
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);
+ /* No lookup finds the chip now, and it can still free its lines. */
+ gpiochip_free_own_lines(gc);
+ gpiochip_numb_device(gc);
gpiochip_irqchip_remove(gc);
- acpi_gpiochip_remove(gc);
of_gpiochip_remove(gc);
gpiochip_remove_pin_ranges(gc);
gpiochip_free_valid_mask(gc);
@@ -2335,7 +2353,8 @@ static int gpiochip_add_irqchip(struct gpio_chip *gc,
* gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
* @gc: the gpiochip to remove the irqchip from
*
- * This is called only from gpiochip_remove()
+ * acpi_gpiochip_free_interrupts() below finds nothing once
+ * gpiochip_free_own_lines() has run; it is for the error paths before that.
*/
static void gpiochip_irqchip_remove(struct gpio_chip *gc)
{
--
2.43.0