Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [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; 5+ 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] 5+ messages in thread

end of thread, other threads:[~2026-11-27  8:08 UTC | newest]

Thread overview: 5+ 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
2026-11-27  8:08   ` sashiko-bot

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