Skip to contentSkip to conversation

gpio: acpi: modernize resource management using cleanup.h

PATCH Superseded Marco Scardovi 2 participants 8 messages May 6 1 message not in the archive

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#1 lore raw

v2 cover gpio: acpi: modernize resource management using cleanup.h

In reply to a message not in the archive

From: Marco Scardovi <[email protected]>

*** BLURB HERE ***

Hi Mika,
thanks for the quick reply. Unfortunately I noticed too late that thunderbird had broken my patch during copy/paste. I'm pushing it again using git send-mail. I've divided tha patch in 3 smaller pieces: let me know if it is ok for you and Andy. In the meantime I'm testing it against KASAN and kmemleak as You suggested.

Let me know if there's anything else I need to improve.

Marco Scardovi (3):
gpio: acpi: modernize resource management using cleanup.h
gpio: acpi: modernize resource management using cleanup.h
gpio: acpi: modernize resource management using cleanup.h

 drivers/gpio/gpiolib-acpi-core.c | 68 ++++++++++++++++----------------
 1 file changed, 35 insertions(+), 33 deletions(-)
Signature · 1 line
2.54.0
#3 lore raw

v2 cover gpio: acpi: modernize resource management using cleanup.h

In reply to a message not in the archive

*** BLURB HERE ***

Hi Mika,
thanks for the quick reply. Unfortunately I noticed too late that thunderbird had broken my patch during copy/paste. I'm pushing it again using git send-mail. I've divided tha patch in 3 smaller pieces: let me know if it is ok for you and Andy. In the meantime I'm testing it against KASAN and kmemleak as You suggested.

Let me know if there's anything else I need to improve.

Marco Scardovi (3):
gpio: acpi: modernize resource management using cleanup.h
gpio: acpi: modernize resource management using cleanup.h
gpio: acpi: modernize resource management using cleanup.h

 drivers/gpio/gpiolib-acpi-core.c | 68 ++++++++++++++++----------------
 1 file changed, 35 insertions(+), 33 deletions(-)
Signature · 1 line
2.54.0
#4 lore raw

v2 cover gpio: acpi: modernize resource management using cleanup.h

*** BLURB HERE ***

Hi Mika,
thanks for the quick reply. Unfortunately I noticed too late that thunderbird had broken my patch during copy/paste. I'm pushing it again using git send-mail. I've divided tha patch in 3 smaller pieces: let me know if it is ok for you and Andy. In the meantime I'm testing it against KASAN and kmemleak as You suggested.

Let me know if there's anything else I need to improve.

Marco Scardovi (3):
gpio: acpi: modernize resource management using cleanup.h
gpio: acpi: modernize resource management using cleanup.h
gpio: acpi: modernize resource management using cleanup.h

 drivers/gpio/gpiolib-acpi-core.c | 68 ++++++++++++++++----------------
 1 file changed, 35 insertions(+), 33 deletions(-)
Signature · 1 line
2.54.0
#5 lore raw

PATCH v2 1/3 gpio: acpi: modernize resource management using cleanup.h

In reply to Marco Scardovi
---
 drivers/gpio/gpiolib-acpi-core.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpio/gpiolib-acpi-core.c +18 -0
index eb8a40cfb7a9..7c17bffcd4e7 100644
@@ -7,6 +7,8 @@
  *          Mika Westerberg <[email protected]>
  */
 
+#include <linux/cleanup.h>
+#include <linux/slab.h>
 #include <linux/acpi.h>
 #include <linux/dmi.h>
 #include <linux/errno.h>
@@ -23,6 +25,16 @@
 #include "gpiolib.h"
 #include "gpiolib-acpi.h"
 
+DEFINE_FREE(free_gpio_desc, struct gpio_desc *, {
+	if (_T)
+		gpiochip_free_own_desc(_T);
+})
+
+DEFINE_FREE(acpi_free, void *, {
+	if (_T)
+		ACPI_FREE(_T);
+})
+
 /**
  * struct acpi_gpio_event - ACPI GPIO event handler data
  *
@@ -361,6 +373,7 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
 	struct acpi_gpio_event *event;
 	irq_handler_t handler = NULL;
 	struct gpio_desc *desc;
+	struct gpio_desc *desc_guard __free(free_gpio_desc) = NULL;
 	unsigned int pin;
 	int ret, irq;
 
@@ -370,6 +383,11 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
 	handle = ACPI_HANDLE(chip->parent);
 	pin = agpio->pin_table[0];
 
+	if (pin >= chip->ngpio) {
+		dev_err(chip->parent, "Failed to request GPIO for pin 0x%04X, out of range\n", pin);
+		return AE_OK;
+	}
+
 	if (pin <= 255) {
 		char ev_name[8];
 		sprintf(ev_name, "_%c%02X",
Signature · 1 line
2.54.0
#7 lore raw

PATCH v2 2/3 gpio: acpi: modernize resource management using cleanup.h

In reply to Marco Scardovi
---
 drivers/gpio/gpiolib-acpi-core.c | 38 ++++++++++++--------------------
 1 file changed, 14 insertions(+), 24 deletions(-)
drivers/gpio/gpiolib-acpi-core.c +14 -24
index 7c17bffcd4e7..b6accbaba04d 100644
@@ -410,31 +410,26 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
 
 	desc = acpi_request_own_gpiod(chip, agpio, 0, "ACPI:Event");
 	if (IS_ERR(desc)) {
-		dev_err(chip->parent,
-			"Failed to request GPIO for pin 0x%04X, err %pe\n",
-			pin, desc);
+		dev_err(chip->parent, "Failed to request GPIO for pin 0x%04X, err %pe\n", pin, desc);
 		return AE_OK;
 	}
+	desc_guard = desc;
 
 	ret = gpiochip_lock_as_irq(chip, pin);
 	if (ret) {
-		dev_err(chip->parent,
-			"Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
-			pin, ret);
-		goto fail_free_desc;
+		dev_err(chip->parent, "Failed to lock GPIO pin 0x%04X as interrupt, err %d\n", pin, ret);
+		return AE_OK;
 	}
 
 	irq = gpiod_to_irq(desc);
 	if (irq < 0) {
-		dev_err(chip->parent,
-			"Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
-			pin, irq);
-		goto fail_unlock_irq;
+		dev_err(chip->parent, "Failed to translate GPIO pin 0x%04X to IRQ, err %d\n", pin, irq);
+		goto err_unlock;
 	}
 
 	event = kzalloc_obj(*event);
 	if (!event)
-		goto fail_unlock_irq;
+		goto err_unlock;
 
 	event->irqflags = IRQF_ONESHOT;
 	if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
@@ -462,17 +457,15 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
 	event->irq = irq;
 	event->irq_is_wake = acpi_gpio_irq_is_wake(chip->parent, agpio);
 	event->pin = pin;
-	event->desc = desc;
+	/* Transfer ownership to event, prevent auto-free */
+	event->desc = no_free_ptr(desc_guard);
 
 	list_add_tail(&event->node, &acpi_gpio->events);
 
 	return AE_OK;
 
-fail_unlock_irq:
+err_unlock:
 	gpiochip_unlock_as_irq(chip, pin);
-fail_free_desc:
-	gpiochip_free_own_desc(desc);
-
 	return AE_OK;
 }
 
@@ -1104,7 +1097,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 	struct acpi_gpio_chip *achip = region_context;
 	struct gpio_chip *chip = achip->chip;
 	struct acpi_resource_gpio *agpio;
-	struct acpi_resource *ares;
+	struct acpi_resource *ares __free(acpi_free) = NULL;
 	u16 pin_index = address;
 	acpi_status status;
 	int length;
@@ -1115,20 +1108,17 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 	if (ACPI_FAILURE(status))
 		return status;
 
-	if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
-		ACPI_FREE(ares);
+	if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO))
 		return AE_BAD_PARAMETER;
-	}
 
 	agpio = &ares->data.gpio;
 
 	if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
-	    function == ACPI_WRITE)) {
-		ACPI_FREE(ares);
+	    function == ACPI_WRITE))
 		return AE_BAD_PARAMETER;
-	}
 
 	length = min(agpio->pin_table_length, pin_index + bits);
+	status = AE_OK;
 	for (i = pin_index; i < length; ++i) {
 		unsigned int pin = agpio->pin_table[i];
 		struct acpi_gpio_connection *conn;
Signature · 1 line
2.54.0
#8 lore raw

PATCH v2 3/3 gpio: acpi: modernize resource management using cleanup.h

In reply to Marco Scardovi
---
 drivers/gpio/gpiolib-acpi-core.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/gpio/gpiolib-acpi-core.c +3 -9
index b6accbaba04d..7b6476b90f13 100644
@@ -1126,7 +1126,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 		u16 word, shift;
 		bool found;
 
-		mutex_lock(&achip->conn_lock);
+		guard(mutex)(&achip->conn_lock);
 
 		found = false;
 		list_for_each_entry(conn, &achip->conns, node) {
@@ -1158,17 +1158,15 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 		if (!found) {
 			desc = acpi_request_own_gpiod(chip, agpio, i, "ACPI:OpRegion");
 			if (IS_ERR(desc)) {
-				mutex_unlock(&achip->conn_lock);
 				status = AE_ERROR;
-				goto out;
+				break;
 			}
 
 			conn = kzalloc_obj(*conn);
 			if (!conn) {
 				gpiochip_free_own_desc(desc);
-				mutex_unlock(&achip->conn_lock);
 				status = AE_NO_MEMORY;
-				goto out;
+				break;
 			}
 
 			conn->pin = pin;
@@ -1176,8 +1174,6 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 			list_add_tail(&conn->node, &achip->conns);
 		}
 
-		mutex_unlock(&achip->conn_lock);
-
 		/*
 		 * For the cases when OperationRegion() consists of more than
 		 * 64 bits calculate the word and bit shift to use that one to
@@ -1196,8 +1192,6 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 		}
 	}
 
-out:
-	ACPI_FREE(ares);
 	return status;
 }
 
Signature · 1 line
2.54.0
#2 lore raw
In reply to Marco Scardovi
Quoted 6 lines · Marco Scardovi

thanks for the quick reply. Unfortunately I noticed too late that thunderbird had broken my patch during copy/paste. I'm pushing it again using git send-mail. I've divided tha patch in 3 smaller pieces: let me know if it is ok for you and Andy. In the meantime I'm testing it against KASAN and kmemleak as You suggested.

Let me know if there's anything else I need to improve.

Slow down, please! The time between two versions should be at least 24h! On top of that, Mika Cc'ed me with this and I only answered a few minutes ago, meaning that not earlier than on Friday the next version should come. I'm not going to review this. I believe most of my comments still apply. Also note, that the email thread is a mess now, something went wrong. And do not forget to remove placeholders when filing the contents of a cover letter.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
In reply to Marco Scardovi

On Wed, May 06, 2026 at 02:35:34PM +0200, Marco Scardovi wrote:

No, this is wrong and completely missed the guidelines! Please, spend near hours to study the Submitting Patches and Coding Style kernel documentation before preparing and sending a v3.

...

Quoted 5 lines
drivers/gpio/gpiolib-acpi-core.c +2 -0
+#include <linux/cleanup.h>
+#include <linux/slab.h>
 #include <linux/acpi.h>
 #include <linux/dmi.h>
 #include <linux/errno.h>

...

drivers/gpio/gpiolib-acpi-core.c line 36

Quoted 9 lines
drivers/gpio/gpiolib-acpi-core.c +9 -0
+DEFINE_FREE(free_gpio_desc, struct gpio_desc *, {
+	if (_T)
+		gpiochip_free_own_desc(_T);
+})
+
+DEFINE_FREE(acpi_free, void *, {
+	if (_T)
+		ACPI_FREE(_T);
+})

And yes, these are still subject to comments I gave against v1.

Signature · 2 lines
With Best Regards,
Andy Shevchenko