Skip to contentSkip to conversation

gpio/pinctrl: replace gpiochip_is_requested() with a safer interface

PATCH Superseded Bartosz Golaszewski 2 participants 17 messages Nov 29, 2023

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover gpio/pinctrl: replace gpiochip_is_requested() with a safer interface

From: Bartosz Golaszewski <[email protected]>

While reworking the locking in GPIOLIB I realized that locking the descriptor with users still calling gpiochip_is_requested() will still be buggy as it returns a pointer to a string that can be freed whenever the descriptor is released. Let's provide a safer alternative in the form of a function that returns a copy of the label.

Use it in all drivers and remove gpiochip_is_requested().

Bartosz Golaszewski (10):
gpiolib: provide gpiochip_dup_line_label()
gpio: wm831x: use gpiochip_dup_line_label()
gpio: wm8994: use gpiochip_dup_line_label()
gpio: stmpe: use gpiochip_dup_line_label()
pinctrl: abx500: use gpiochip_dup_line_label()
pinctrl: nomadik: use gpiochip_dup_line_label()
pinctrl: baytrail: use gpiochip_dup_line_label()
pinctrl: sppctl: use gpiochip_dup_line_label()
gpiolib: use gpiochip_dup_line_label() in for_each helpers
gpiolib: remove gpiochip_is_requested()

 drivers/gpio/gpio-stmpe.c                 |  6 +++-
 drivers/gpio/gpio-wm831x.c                | 14 ++++++---
 drivers/gpio/gpio-wm8994.c                | 13 +++++---
 drivers/gpio/gpiolib.c                    | 37 ++++++++++++++---------
 drivers/pinctrl/intel/pinctrl-baytrail.c  | 11 ++++---
 drivers/pinctrl/nomadik/pinctrl-abx500.c  |  9 ++++--
 drivers/pinctrl/nomadik/pinctrl-nomadik.c |  6 +++-
 drivers/pinctrl/sunplus/sppctl.c          | 10 +++---
 include/linux/gpio/driver.h               |  8 +++--
 9 files changed, 72 insertions(+), 42 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/10 gpiolib: provide gpiochip_dup_line_label()

From: Bartosz Golaszewski <[email protected]>

gpiochip_is_requested() not only has a misleading name but it returns a pointer to a string that is freed when the descriptor is released.

Provide a new helper meant to replace it, which returns a copy of the label string instead.

---
 drivers/gpio/gpiolib.c      | 29 +++++++++++++++++++++++++++++
 include/linux/gpio/driver.h |  1 +
 2 files changed, 30 insertions(+)
drivers/gpio/gpiolib.c +29 -0
index a5faaea6915d..8e932e6a6a8d 100644
@@ -2400,6 +2400,35 @@ const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
 }
 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
 
+/**
+ * gpiochip_dup_line_label - Get a copy of the consumer label.
+ * @gc: GPIO chip controlling this line.
+ * @offset: Hardware offset of the line.
+ *
+ * Returns:
+ * Pointer to a copy of the consumer label if the line is requested or NULL
+ * if it's not. If a valid pointer was returned, it must be freed using
+ * kfree(). In case of a memory allocation error, the function returns %ENOMEM.
+ *
+ * Must not be called from atomic context.
+ */
+char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
+{
+	const char *label;
+	char *cpy;
+
+	label = gpiochip_is_requested(gc, offset);
+	if (!label)
+		return NULL;
+
+	cpy = kstrdup(label, GFP_KERNEL);
+	if (!cpy)
+		return ERR_PTR(-ENOMEM);
+
+	return cpy;
+}
+EXPORT_SYMBOL_GPL(gpiochip_dup_line_label);
+
 /**
  * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  * @gc: GPIO chip
include/linux/gpio/driver.h +1 -0
index 100c329dc986..9796a34e2fee 100644
@@ -532,6 +532,7 @@ struct gpio_chip {
 };
 
 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset);
+char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset);
 
 /**
  * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH 01/10] gpiolib: provide gpiochip_dup_line_label()

Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

gpiochip_is_requested() not only has a misleading name but it returns a pointer to a string that is freed when the descriptor is released.

Provide a new helper meant to replace it, which returns a copy of the label string instead.

...

Quoted 9 lines

+/**
+ * gpiochip_dup_line_label - Get a copy of the consumer label.
+ * @gc: GPIO chip controlling this line.
+ * @offset: Hardware offset of the line.
+ *
+ * Returns:
+ * Pointer to a copy of the consumer label if the line is requested or NULL
+ * if it's not. If a valid pointer was returned, it must be freed using
+ * kfree(). In case of a memory allocation error, the function returns %ENOMEM.

kfree_const() ? (see below)

drivers/gpio/gpiolib.c const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset) · line 2418

Quoted 6 lines

+ * Must not be called from atomic context.
+ */

drivers/gpio/gpiolib.c +4 -0
+char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
+{
+	const char *label;
+	char *cpy;

Why not "copy"?

drivers/gpio/gpiolib.c const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset) · line 2424

Quoted 6 lines
drivers/gpio/gpiolib.c +5 -0
+
+	label = gpiochip_is_requested(gc, offset);
+	if (!label)
+		return NULL;
 
+	cpy = kstrdup(label, GFP_KERNEL);

You probably want to have kstrdup_const(). However, I haven't checked if we have such use cases.

drivers/gpio/gpiolib.c const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset) · line 2429

Quoted 5 lines
drivers/gpio/gpiolib.c +5 -0
+	if (!cpy)
+		return ERR_PTR(-ENOMEM);
+
+	return cpy;
+}

So, how does this differ from the previous one? You need to hold a reference to the descriptor before copying and release it after.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH 01/10] gpiolib: provide gpiochip_dup_line_label()

Quoted 38 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

gpiochip_is_requested() not only has a misleading name but it returns a pointer to a string that is freed when the descriptor is released.

Provide a new helper meant to replace it, which returns a copy of the label string instead.

...

Quoted 9 lines

+/**
+ * gpiochip_dup_line_label - Get a copy of the consumer label.
+ * @gc: GPIO chip controlling this line.
+ * @offset: Hardware offset of the line.
+ *
+ * Returns:
+ * Pointer to a copy of the consumer label if the line is requested or NULL
+ * if it's not. If a valid pointer was returned, it must be freed using
+ * kfree(). In case of a memory allocation error, the function returns %ENOMEM.

kfree_const() ? (see below)

drivers/gpio/gpiolib.c const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset) · line 2418

Quoted 6 lines

+ * Must not be called from atomic context.
+ */

drivers/gpio/gpiolib.c +4 -0
+char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
+{
+     const char *label;
+     char *cpy;

Why not "copy"?

Quoted 4 lines
drivers/gpio/gpiolib.c +4 -0
+
+     label = gpiochip_is_requested(gc, offset);
+     if (!label)
+             return NULL;
Quoted 4 lines
Quoted 1 line

+ cpy = kstrdup(label, GFP_KERNEL);

You probably want to have kstrdup_const(). However, I haven't checked if we have such use cases.

I thought about it but I'm thinking that it would be confusing to users and lead to bugs. This is not used very much and only for debugfs output. Let's keep it simple.

Quoted 10 lines

drivers/gpio/gpiolib.c const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset) · line 2429

Quoted 6 lines
drivers/gpio/gpiolib.c +5 -0
+     if (!cpy)
+             return ERR_PTR(-ENOMEM);
+
+     return cpy;
+}

So, how does this differ from the previous one? You need to hold a reference to the descriptor before copying and release it after.

The last patch reworks it to hold the obsolete gpio_lock and the upcoming changes will make this perform the copy under the descriptor lock and return it once it's released.

Bart

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#4 lore raw

PATCH 2/10 gpio: wm831x: use gpiochip_dup_line_label()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

---
 drivers/gpio/gpio-wm831x.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/gpio/gpio-wm831x.c +9 -5
index 7eaf8a28638c..f7d5120ff8f1 100644
@@ -8,6 +8,7 @@
  *
  */
 
+#include <linux/cleanup.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/module.h>
@@ -160,18 +161,21 @@ static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 	for (i = 0; i < chip->ngpio; i++) {
 		int gpio = i + chip->base;
 		int reg;
-		const char *label, *pull, *powerdomain;
+		const char *pull, *powerdomain;
 
 		/* We report the GPIO even if it's not requested since
 		 * we're also reporting things like alternate
 		 * functions which apply even when the GPIO is not in
 		 * use as a GPIO.
 		 */
-		label = gpiochip_is_requested(chip, i);
-		if (!label)
-			label = "Unrequested";
+		char *label __free(kfree) = gpiochip_dup_line_label(chip, i);
+		if (IS_ERR(label)) {
+			dev_err(wm831x->dev, "Failed to duplicate label\n");
+			continue;
+		}
 
-		seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
+		seq_printf(s, " gpio-%-3d (%-20.20s) ",
+			   gpio, label ?: "Unrequested");
 
 		reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
 		if (reg < 0) {
Signature · 1 line
2.40.1
#5 lore raw

PATCH 3/10 gpio: wm8994: use gpiochip_dup_line_label()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

---
 drivers/gpio/gpio-wm8994.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/gpio/gpio-wm8994.c +8 -5
index f4a474cef32d..bf05c9b5882b 100644
@@ -8,6 +8,7 @@
  *
  */
 
+#include <linux/cleanup.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/module.h>
@@ -193,18 +194,20 @@ static void wm8994_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 	for (i = 0; i < chip->ngpio; i++) {
 		int gpio = i + chip->base;
 		int reg;
-		const char *label;
 
 		/* We report the GPIO even if it's not requested since
 		 * we're also reporting things like alternate
 		 * functions which apply even when the GPIO is not in
 		 * use as a GPIO.
 		 */
-		label = gpiochip_is_requested(chip, i);
-		if (!label)
-			label = "Unrequested";
+		char *label __free(kfree) = gpiochip_dup_line_label(chip, i);
+		if (IS_ERR(label)) {
+			dev_err(wm8994->dev, "Failed to duplicate label\n");
+			continue;
+		}
 
-		seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
+		seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio,
+			   label ?: "Unrequested");
 
 		reg = wm8994_reg_read(wm8994, WM8994_GPIO_1 + i);
 		if (reg < 0) {
Signature · 1 line
2.40.1
#6 lore raw

PATCH 4/10 gpio: stmpe: use gpiochip_dup_line_label()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

---
 drivers/gpio/gpio-stmpe.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-stmpe.c +5 -1
index 27cc4da53565..6c5ee81d71b3 100644
@@ -5,6 +5,7 @@
  * Author: Rabin Vincent <[email protected]> for ST-Ericsson
  */
 
+#include <linux/cleanup.h>
 #include <linux/init.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
@@ -255,7 +256,6 @@ static void stmpe_dbg_show_one(struct seq_file *s,
 {
 	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
 	struct stmpe *stmpe = stmpe_gpio->stmpe;
-	const char *label = gpiochip_is_requested(gc, offset);
 	bool val = !!stmpe_gpio_get(gc, offset);
 	u8 bank = offset / 8;
 	u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank];
@@ -263,6 +263,10 @@ static void stmpe_dbg_show_one(struct seq_file *s,
 	int ret;
 	u8 dir;
 
+	char *label __free(kfree) = gpiochip_dup_line_label(gc, offset);
+	if (IS_ERR(label))
+		return;
+
 	ret = stmpe_reg_read(stmpe, dir_reg);
 	if (ret < 0)
 		return;
Signature · 1 line
2.40.1
#7 lore raw

PATCH 5/10 pinctrl: abx500: use gpiochip_dup_line_label()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

---
 drivers/pinctrl/nomadik/pinctrl-abx500.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pinctrl/nomadik/pinctrl-abx500.c +7 -2
index d3c32d809bac..80e3ac333136 100644
@@ -6,7 +6,9 @@
  *
  * Driver allows to use AxB5xx unused pins to be used as GPIO
  */
+
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/err.h>
 #include <linux/gpio/driver.h>
 #include <linux/init.h>
@@ -453,12 +455,11 @@ static void abx500_gpio_dbg_show_one(struct seq_file *s,
 				     unsigned offset, unsigned gpio)
 {
 	struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
-	const char *label = gpiochip_is_requested(chip, offset - 1);
 	u8 gpio_offset = offset - 1;
 	int mode = -1;
 	bool is_out;
 	bool pd;
-	int ret;
+	int ret = -ENOMEM;
 
 	const char *modes[] = {
 		[ABX500_DEFAULT]	= "default",
@@ -474,6 +475,10 @@ static void abx500_gpio_dbg_show_one(struct seq_file *s,
 		[ABX500_GPIO_PULL_UP]		= "pull up",
 	};
 
+	char *label __free(kfree) = gpiochip_dup_line_label(chip, offset - 1);
+	if (IS_ERR(label))
+		goto out;
+
 	ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
 			gpio_offset, &is_out);
 	if (ret < 0)
Signature · 1 line
2.40.1
#8 lore raw

PATCH 6/10 pinctrl: nomadik: use gpiochip_dup_line_label()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

---
 drivers/pinctrl/nomadik/pinctrl-nomadik.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pinctrl/nomadik/pinctrl-nomadik.c +5 -1
index 863732287b1e..7911353ac97d 100644
@@ -8,6 +8,7 @@
  * Copyright (C) 2011-2013 Linus Walleij <[email protected]>
  */
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -917,7 +918,6 @@ static void nmk_gpio_dbg_show_one(struct seq_file *s,
 	struct pinctrl_dev *pctldev, struct gpio_chip *chip,
 	unsigned offset, unsigned gpio)
 {
-	const char *label = gpiochip_is_requested(chip, offset);
 	struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip);
 	int mode;
 	bool is_out;
@@ -934,6 +934,10 @@ static void nmk_gpio_dbg_show_one(struct seq_file *s,
 		[NMK_GPIO_ALT_C+4]	= "altC4",
 	};
 
+	char *label = gpiochip_dup_line_label(chip, offset);
+	if (IS_ERR(label))
+		return;
+
 	clk_enable(nmk_chip->clk);
 	is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset));
 	pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & BIT(offset));
Signature · 1 line
2.40.1
#9 lore raw

PATCH 7/10 pinctrl: baytrail: use gpiochip_dup_line_label()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

---
 drivers/pinctrl/intel/pinctrl-baytrail.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/pinctrl/intel/pinctrl-baytrail.c +6 -5
index 3cd0798ee631..3c8c02043481 100644
@@ -9,6 +9,7 @@
 #include <linux/acpi.h>
 #include <linux/array_size.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/gpio/driver.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
@@ -1173,7 +1174,6 @@ static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 		const char *pull_str = NULL;
 		const char *pull = NULL;
 		unsigned long flags;
-		const char *label;
 		unsigned int pin;
 
 		pin = vg->soc->pins[i].number;
@@ -1200,9 +1200,10 @@ static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 			seq_printf(s, "Pin %i: can't retrieve community\n", pin);
 			continue;
 		}
-		label = gpiochip_is_requested(chip, i);
-		if (!label)
-			label = "Unrequested";
+
+		char *label __free(kfree) = gpiochip_dup_line_label(chip, i);
+		if (IS_ERR(label))
+			continue;
 
 		switch (conf0 & BYT_PULL_ASSIGN_MASK) {
 		case BYT_PULL_ASSIGN_UP:
@@ -1231,7 +1232,7 @@ static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 		seq_printf(s,
 			   " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
 			   pin,
-			   label,
+			   label ?: "Unrequested",
 			   val & BYT_INPUT_EN ? "  " : "in",
 			   val & BYT_OUTPUT_EN ? "   " : "out",
 			   str_hi_lo(val & BYT_LEVEL),
Signature · 1 line
2.40.1
#10 lore raw

Re: [PATCH 07/10] pinctrl: baytrail: use gpiochip_dup_line_label()

Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

I don't think it will be any collision with other Bay Trail patches, but I would like to have an immutable branch that I can pull into my tree (Intel pin control drivers).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

PATCH 8/10 pinctrl: sppctl: use gpiochip_dup_line_label()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new gpiochip_dup_line_label() helper to safely retrieve the descriptor label.

---
 drivers/pinctrl/sunplus/sppctl.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/pinctrl/sunplus/sppctl.c +5 -5
index bb5ef391dbe4..ae156f779a16 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Sunplus Tech / Tibbo Tech.
  */
 
+#include <linux/cleanup.h>
 #include <linux/bitfield.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -500,16 +501,15 @@ static int sppctl_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
 
 static void sppctl_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 {
-	const char *label;
 	int i;
 
 	for (i = 0; i < chip->ngpio; i++) {
-		label = gpiochip_is_requested(chip, i);
-		if (!label)
-			label = "";
+		char *label __free(kfree) = gpiochip_dup_line_label(chip, i);
+		if (IS_ERR(label))
+			continue;
 
 		seq_printf(s, " gpio-%03d (%-16.16s | %-16.16s)", i + chip->base,
-			   chip->names[i], label);
+			   chip->names[i], label ?: "");
 		seq_printf(s, " %c", sppctl_gpio_get_direction(chip, i) ? 'I' : 'O');
 		seq_printf(s, ":%d", sppctl_gpio_get(chip, i));
 		seq_printf(s, " %s", sppctl_first_get(chip, i) ? "gpi" : "mux");
Signature · 1 line
2.40.1
#12 lore raw

PATCH 9/10 gpiolib: use gpiochip_dup_line_label() in for_each helpers

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Rework for_each_requested_gpio_in_range() to use the new helper to retrieve a dynamically allocated copy of the descriptor label and free it at the end of each iteration.

---
 include/linux/gpio/driver.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/gpio/driver.h +4 -2
index 9796a34e2fee..6405f6d454af 100644
@@ -543,8 +543,10 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset);
  * @label:	label of current GPIO
  */
 #define for_each_requested_gpio_in_range(chip, i, base, size, label)			\
-	for (i = 0; i < size; i++)							\
-		if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
+	for (i = 0; i < size; i++, kfree(label))					\
+		if ((label = gpiochip_dup_line_label(chip, base + i)) == NULL) {}	\
+		else if (IS_ERR(label)) {}						\
+		else
 
 /* Iterates over all requested GPIO of the given @chip */
 #define for_each_requested_gpio(chip, i, label)						\
Signature · 1 line
2.40.1
#13 lore raw

Re: [PATCH 09/10] gpiolib: use gpiochip_dup_line_label() in for_each helpers

Quoted 17 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Rework for_each_requested_gpio_in_range() to use the new helper to retrieve a dynamically allocated copy of the descriptor label and free it at the end of each iteration.

---
 include/linux/gpio/driver.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/gpio/driver.h +0 -0
index 9796a34e2fee..6405f6d454af 100644

include/linux/gpio/driver.h char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset); · line 550

Quoted 10 lines
include/linux/gpio/driver.h +4 -2
@@ -543,8 +543,10 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset);
  * @label:     label of current GPIO
  */
 #define for_each_requested_gpio_in_range(chip, i, base, size, label)                   \
-       for (i = 0; i < size; i++)                                                      \
-               if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
+       for (i = 0; i < size; i++, kfree(label))                                        \
+               if ((label = gpiochip_dup_line_label(chip, base + i)) == NULL) {}       \
+               else if (IS_ERR(label)) {}                                              \
+               else
 

Ah, cr*p, it will leak if we break out of the loop, why did I think it was correct?

Any ideas how to handle this one? I was thinking something like:

    for (i = 0, char *p __free(kfree) = label; i < size; i++)

would work but it doesn't.

Bart

Quoted 5 lines
include/linux/gpio/driver.h +0 -1
 /* Iterates over all requested GPIO of the given @chip */
 #define for_each_requested_gpio(chip, i, label)                                                \
--

2.40.1

#14 lore raw

Re: [PATCH 09/10] gpiolib: use gpiochip_dup_line_label() in for_each helpers

Quoted 1 line · Bartosz Golaszewski

On Wed, Nov 29, 2023 at 3:24 PM Bartosz Golaszewski <[email protected]> wrote:

...

Quoted 5 lines

Any ideas how to handle this one? I was thinking something like:

    for (i = 0, char *p __free(kfree) = label; i < size; i++)

would work but it doesn't.

Probably you want to ask Peter Z for this.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#15 lore raw

Re: [PATCH 09/10] gpiolib: use gpiochip_dup_line_label() in for_each helpers

Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Bartosz Golaszewski

On Wed, Nov 29, 2023 at 3:24 PM Bartosz Golaszewski <[email protected]> wrote:

...

Quoted 8 lines
Quoted 5 lines

Any ideas how to handle this one? I was thinking something like:

    for (i = 0, char *p __free(kfree) = label; i < size; i++)

would work but it doesn't.

Probably you want to ask Peter Z for this.

Before I do, I'll give DEFINE_CLASS() a chance as it looks like it could be the answer looking at how scoped_guard works.

Bart

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#16 lore raw

PATCH 10/10 gpiolib: remove gpiochip_is_requested()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We have no external users of gpiochip_is_requested(). Let's remove it and replace its internal calls with direct testing of the REQUESTED flag.

---
 drivers/gpio/gpiolib.c      | 46 ++++++++++---------------------------
 include/linux/gpio/driver.h |  1 -
 2 files changed, 12 insertions(+), 35 deletions(-)
drivers/gpio/gpiolib.c +12 -34
index 8e932e6a6a8d..3070a4f7bbb1 100644
@@ -1085,7 +1085,7 @@ void gpiochip_remove(struct gpio_chip *gc)
 
 	spin_lock_irqsave(&gpio_lock, flags);
 	for (i = 0; i < gdev->ngpio; i++) {
-		if (gpiochip_is_requested(gc, i))
+		if (test_bit(FLAG_REQUESTED, &gdev->descs[i].flags))
 			break;
 	}
 	spin_unlock_irqrestore(&gpio_lock, flags);
@@ -2373,33 +2373,6 @@ void gpiod_free(struct gpio_desc *desc)
 	gpio_device_put(desc->gdev);
 }
 
-/**
- * gpiochip_is_requested - return string iff signal was requested
- * @gc: controller managing the signal
- * @offset: of signal within controller's 0..(ngpio - 1) range
- *
- * Returns NULL if the GPIO is not currently requested, else a string.
- * The string returned is the label passed to gpio_request(); if none has been
- * passed it is a meaningless, non-NULL constant.
- *
- * This function is for use by GPIO controller drivers.  The label can
- * help with diagnostics, and knowing that the signal is used as a GPIO
- * can help avoid accidentally multiplexing it to another controller.
- */
-const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
-{
-	struct gpio_desc *desc;
-
-	desc = gpiochip_get_desc(gc, offset);
-	if (IS_ERR(desc))
-		return NULL;
-
-	if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
-		return NULL;
-	return desc->label;
-}
-EXPORT_SYMBOL_GPL(gpiochip_is_requested);
-
 /**
  * gpiochip_dup_line_label - Get a copy of the consumer label.
  * @gc: GPIO chip controlling this line.
@@ -2414,16 +2387,21 @@ EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  */
 char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset)
 {
-	const char *label;
+	struct gpio_desc *desc;
 	char *cpy;
 
-	label = gpiochip_is_requested(gc, offset);
-	if (!label)
+	desc = gpiochip_get_desc(gc, offset);
+	if (IS_ERR(desc))
 		return NULL;
 
-	cpy = kstrdup(label, GFP_KERNEL);
-	if (!cpy)
-		return ERR_PTR(-ENOMEM);
+	scoped_guard(spinlock_irqsave, &gpio_lock) {
+		if (!test_bit(FLAG_REQUESTED, &desc->flags))
+			return NULL;
+
+		cpy = kstrdup(desc->label, GFP_KERNEL);
+		if (!cpy)
+			return ERR_PTR(-ENOMEM);
+	}
 
 	return cpy;
 }
include/linux/gpio/driver.h +0 -1
index 6405f6d454af..1679e6fa5469 100644
@@ -531,7 +531,6 @@ struct gpio_chip {
 #endif /* CONFIG_OF_GPIO */
 };
 
-const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset);
 char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset);
 
 /**
Signature · 1 line
2.40.1