* [PATCH v2 0/3] gpiolib: free the lines a chip owns before numbing it
@ 2026-11-27 7:49 Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 1/3] gpiolib: shared: remove the proxies before releasing the shared line Adrian Ambrozewicz
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Adrian Ambrozewicz @ 2026-11-27 7:49 UTC (permalink / raw)
To: Bartosz Golaszewski, Linus Walleij
Cc: Bartosz Golaszewski, Kent Gibson, Andy Shevchenko,
Mika Westerberg, Herve Codina, linux-gpio, linux-acpi,
linux-kernel
When a GPIO chip goes away, gpiolib has to release the lines the chip
requested for itself (hogs, ACPI event and OpRegion lines, shared lines)
before it numbs the chip: gpiod_free_commit() calls ->free() only while
the chip is there. Commit 5018ada69a04 ("gpio: Move freeing of GPIO
hogs before numbing of the device") did that for hogs only.
gpiochip_remove() still releases ACPI and shared lines after the
numbing: their ->free() is skipped, and the ACPI event IRQs are freed
twice.
v1 was a single patch for the late error path of
gpiochip_add_data_with_key(), which leaves user space calling into a
freed chip. It copied gpiochip_remove()'s order into that path and with
it introduced both defects there, as Sashiko reported. v2 fixes the
order once and uses it on both paths:
- patch 1 removes the shared proxies before releasing their line;
- patch 2 releases the chip's own lines after it leaves the device
list and before the numbing, on removal;
- patch 3 is v1's fix, using patch 2's helpers.
Each patch needs the one before it. Based on gpio/for-current: patches 2
and 3 need "gpiolib: let the character device free its IRQs first on
chip removal" and "gpiolib: remove the shared GPIO proxies before
freeing the remaining IRQs". No Cc: stable; please decide whether any of
it should go there.
Tested under QEMU with KASAN, lockdep and kmemleak on gpio-sim, with
test-only fakes of an ACPI event and an OpRegion connection (x86_64) and
a shared line (arm64): late setup failures with uAPI v1 and v2 requests,
a line info watch and a hog; removals; a failing chardev registration;
and the race reproducer of "gpiolib: remove the shared GPIO proxies
before freeing the remaining IRQs". Without the series, KASAN reports
the use-after-free, the double IRQ free warns and the shared line's
->free() is skipped; with it, no reports.
Prepared with Claude, which helped analyse the bugs, write the
reproducers and draft the patches; I reviewed and tested every change.
Changes in v2:
- Patches 1 and 2 new; patch 3 uses patch 2's order (Sashiko on v1).
- Link to v1: https://lore.kernel.org/r/20261125-gpiolib-setupdev-v1-1-aambrozewicz@nvidia.com
Adrian Ambrozewicz (3):
gpiolib: shared: remove the proxies before releasing the shared line
gpiolib: release the chip's own lines before numbing it on removal
gpiolib: release user space's lines when adding the chip fails late
drivers/gpio/gpiolib-shared.c | 7 ++--
drivers/gpio/gpiolib.c | 61 ++++++++++++++++++++++++-----------
2 files changed, 47 insertions(+), 21 deletions(-)
base-commit: 1f7d535465a5568187cbdfec3beb734bfb5e5015
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/3] gpiolib: shared: remove the proxies before releasing the shared line
2026-11-27 7:49 [PATCH v2 0/3] gpiolib: free the lines a chip owns before numbing it Adrian Ambrozewicz
@ 2026-11-27 7:49 ` Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 2/3] gpiolib: release the chip's own lines before numbing it on removal Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 3/3] gpiolib: release user space's lines when adding the chip fails late Adrian Ambrozewicz
2 siblings, 0 replies; 4+ messages in thread
From: Adrian Ambrozewicz @ 2026-11-27 7:49 UTC (permalink / raw)
To: Bartosz Golaszewski, Linus Walleij
Cc: Bartosz Golaszewski, Kent Gibson, Andy Shevchenko,
Mika Westerberg, Herve Codina, linux-gpio, linux-acpi,
linux-kernel
Since commit 4918cc05137c ("gpio: shared: propagate configuration to
pinctrl"), gpio_device_teardown_shared() releases the shared line with
gpiod_free_commit() and only then removes the proxy devices. Until a
proxy is gone, its consumers can still drive the line through the
parent chip. The late error path of gpiochip_add_data_with_key()
calls gpio_device_teardown_shared() with the chip live, so there they
can. gpiochip_remove() calls it after numbing the chip, so those calls
fail until "gpiolib: release the chip's own lines before numbing it on
removal" moves the call ahead of the numbing.
Remove the proxies first, then release the line.
With that patch and without this one, a test-only log on a shared
gpio-sim line showed both proxies still bound when the line was
released; with this one, none.
Fixes: 4918cc05137c ("gpio: shared: propagate configuration to pinctrl")
Signed-off-by: Adrian Ambrozewicz <aambrozewicz@nvidia.com>
---
drivers/gpio/gpiolib-shared.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/gpio/gpiolib-shared.c b/drivers/gpio/gpiolib-shared.c
index f34315a2059a..c1e346a56177 100644
--- a/drivers/gpio/gpiolib-shared.c
+++ b/drivers/gpio/gpiolib-shared.c
@@ -611,9 +611,6 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
if (!device_match_fwnode(&gdev->dev, entry->fwnode))
continue;
- scoped_guard(mutex, &entry->lock)
- gpiod_free_commit(&gdev->descs[entry->offset]);
-
list_for_each_entry(ref, &entry->refs, list) {
if (ref->lookup) {
gpiod_remove_lookup_table(ref->lookup);
@@ -625,6 +622,10 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
gpio_shared_remove_adev(&ref->adev);
}
+
+ /* Release the line only once no proxy can drive it. */
+ scoped_guard(mutex, &entry->lock)
+ gpiod_free_commit(&gdev->descs[entry->offset]);
}
}
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 2/3] gpiolib: release the chip's own lines before numbing it on removal
2026-11-27 7:49 [PATCH v2 0/3] gpiolib: free the lines a chip owns before numbing it Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 1/3] gpiolib: shared: remove the proxies before releasing the shared line Adrian Ambrozewicz
@ 2026-11-27 7:49 ` Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 3/3] gpiolib: release user space's lines when adding the chip fails late Adrian Ambrozewicz
2 siblings, 0 replies; 4+ messages in thread
From: Adrian Ambrozewicz @ 2026-11-27 7:49 UTC (permalink / raw)
To: Bartosz Golaszewski, Linus Walleij
Cc: Bartosz Golaszewski, Kent Gibson, Andy Shevchenko,
Mika Westerberg, Herve Codina, linux-gpio, linux-acpi,
linux-kernel
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@nvidia.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 3/3] gpiolib: release user space's lines when adding the chip fails late
2026-11-27 7:49 [PATCH v2 0/3] gpiolib: free the lines a chip owns before numbing it Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 1/3] gpiolib: shared: remove the proxies before releasing the shared line Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 2/3] gpiolib: release the chip's own lines before numbing it on removal Adrian Ambrozewicz
@ 2026-11-27 7:49 ` Adrian Ambrozewicz
2 siblings, 0 replies; 4+ messages in thread
From: Adrian Ambrozewicz @ 2026-11-27 7:49 UTC (permalink / raw)
To: Bartosz Golaszewski, Linus Walleij
Cc: Bartosz Golaszewski, Kent Gibson, Andy Shevchenko,
Mika Westerberg, Herve Codina, linux-gpio, linux-acpi,
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.
Unwind as gpiochip_remove() does: release the lines the chip owns,
numb the device and free what user space left.
Reproduced with a test-only gpio-sim setup failure after the chardev is
added: reading a line requested then hit a KASAN use-after-free in
gpiod_get_array_value_complex(); with this change, reads return -ENODEV.
This depends on "gpiolib: release the chip's own lines before numbing
it on removal".
Fixes: d7c51b47ac11 ("gpio: userspace ABI for reading/writing GPIO lines")
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>
---
A backport of this patch to 6.12.y or 6.18.y also needs 16fdabe143fc
("gpio: Fix resource leaks on errors in gpiochip_add_data_with_key()"),
which is in 6.12.95 and 6.18.22.
drivers/gpio/gpiolib.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 1b92ace14a5a..6d3531f5833e 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1419,13 +1419,19 @@ 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_release_lines;
}
return 0;
-err_teardown_shared:
- gpio_device_teardown_shared(gdev);
+err_release_lines:
+ /* The character device may have been live: user space may hold lines. */
+ gpiochip_free_own_lines(gc);
+ gpiochip_numb_device(gc);
+ gpiochip_irqchip_remove(gc);
+ gpiochip_remove_pin_ranges(gc);
+ /* What the labels down to err_remove_of_chip undo is done above. */
+ goto err_remove_of_chip;
err_remove_irqchip:
gpiochip_irqchip_remove(gc);
err_remove_irqchip_mask:
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-27 7:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 7:49 [PATCH v2 0/3] gpiolib: free the lines a chip owns before numbing it Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 1/3] gpiolib: shared: remove the proxies before releasing the shared line Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 2/3] gpiolib: release the chip's own lines before numbing it on removal Adrian Ambrozewicz
2026-11-27 7:49 ` [PATCH v2 3/3] 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
all inboxes | Powered by JetHome®