mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Adrian Ambrozewicz <aambrozewicz@nvidia.com>
To: Bartosz Golaszewski <brgl@kernel.org>, Linus Walleij <linusw@kernel.org>
Cc: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
	Kent Gibson <warthog618@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Mika Westerberg <westeri@kernel.org>,
	Herve Codina <herve.codina@bootlin.com>,
	linux-gpio@vger.kernel.org, linux-acpi@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 0/3] gpiolib: free the lines a chip owns before numbing it
Date: Fri, 27 Nov 2026 09:49:24 +0200	[thread overview]
Message-ID: <20261127-gpiolib-ownlines-v2-0-aambrozewicz@nvidia.com> (raw)

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


             reply	other threads:[~2026-11-27  7:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  7:49 Adrian Ambrozewicz [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127-gpiolib-ownlines-v2-0-aambrozewicz@nvidia.com \
    --to=aambrozewicz@nvidia.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=herve.codina@bootlin.com \
    --cc=linusw@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=warthog618@gmail.com \
    --cc=westeri@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®