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 2/3] gpiolib: release the chip's own lines before numbing it on removal
Date: Fri, 27 Nov 2026 09:49:26 +0200	[thread overview]
Message-ID: <20261127-gpiolib-ownlines-v2-2-aambrozewicz@nvidia.com> (raw)
In-Reply-To: <20261127-gpiolib-ownlines-v2-0-aambrozewicz@nvidia.com>

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


  parent 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 [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 [this message]
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-2-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®