Skip to contentSkip to conversation

leds: core: The -ENOTSUPP should never be seen by user space

PATCH In review Andy Shevchenko 4 participants 20 messages May 29, 2021 – Aug 3, 2021 LED

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/13 leds: core: The -ENOTSUPP should never be seen by user space

Luckily there is no user which checks for returned code and actually returns it, but since the function is exported any user may try to return an error code from it to user space, usually during probe phase,

Replace -ENOTSUPP by -EOPNOTSUPP when returning from exported function.

Fixes 13ae79bbe4c2 ("leds: core: Drivers shouldn't enforce SYNC/ASYNC brightness setting")
Cc Jacek Anaszewski <[email protected]>

---
v2: amended the commit message

 drivers/leds/led-core.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/leds/led-core.c +6 -1
index d56ff4939492..f962620a504f 100644
@@ -289,6 +289,8 @@ EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);
 
 int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
 {
+	int ret;
+
 	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
 		return -EBUSY;
 
@@ -297,7 +299,10 @@ int led_set_brightness_sync(struct led_classdev *led_cdev, unsigned int value)
 	if (led_cdev->flags & LED_SUSPENDED)
 		return 0;
 
-	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
+	ret = __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
+	if (ret == -ENOTSUPP)
+		return -EOPNOTSUPP;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(led_set_brightness_sync);
 
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 2/13 leds: el15203000: Give better margin for usleep_range()

1 microsecond with 20 millisecond parameter is too low margin for usleep_range(). Give 100 to make scheduler been less tortured.

While at it, fix indentation in cases where EL_FW_DELAY_USEC is in use. In the loop, move it to the end to avoid a conditional.

---
v2: amended the commit message

 drivers/leds/leds-el15203000.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/leds/leds-el15203000.c +4 -10
index 76b455e87574..77e76f775385 100644
@@ -97,27 +97,22 @@ static int el15203000_cmd(struct el15203000_led *led, u8 brightness)
 
 	/* to avoid SPI mistiming with firmware we should wait some time */
 	if (time_after(led->priv->delay, jiffies)) {
-		dev_dbg(led->priv->dev, "Wait %luus to sync",
-			EL_FW_DELAY_USEC);
+		dev_dbg(led->priv->dev, "Wait %luus to sync", EL_FW_DELAY_USEC);
 
-		usleep_range(EL_FW_DELAY_USEC,
-			     EL_FW_DELAY_USEC + 1);
+		usleep_range(EL_FW_DELAY_USEC, EL_FW_DELAY_USEC + 100);
 	}
 
 	cmd[0] = led->reg;
 	cmd[1] = brightness;
 
 	for (i = 0; i < ARRAY_SIZE(cmd); i++) {
-		if (i)
-			usleep_range(EL_FW_DELAY_USEC,
-				     EL_FW_DELAY_USEC + 1);
-
 		ret = spi_write(led->priv->spi, &cmd[i], sizeof(cmd[i]));
 		if (ret) {
 			dev_err(led->priv->dev,
 				"spi_write() error %d", ret);
 			break;
 		}
+		usleep_range(EL_FW_DELAY_USEC, EL_FW_DELAY_USEC + 100);
 	}
 
 	led->priv->delay = jiffies + usecs_to_jiffies(EL_FW_DELAY_USEC);
@@ -306,8 +301,7 @@ static int el15203000_probe(struct spi_device *spi)
 	priv->count	= count;
 	priv->dev	= &spi->dev;
 	priv->spi	= spi;
-	priv->delay	= jiffies -
-			  usecs_to_jiffies(EL_FW_DELAY_USEC);
+	priv->delay	= jiffies - usecs_to_jiffies(EL_FW_DELAY_USEC);
 
 	spi_set_drvdata(spi, priv);
 
Signature · 1 line
2.31.1
#2 lore raw

PATCH v2 3/13 leds: el15203000: Correct headers (of*.h -> mod_devicetable.h)

In reply to Andy Shevchenko

There is no user of of*.h headers, but mod_devicetable.h. Update header block accordingly.

---
v2: dropped OF change (Pavel), added Rb tag (Oleg)

 drivers/leds/leds-el15203000.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/leds/leds-el15203000.c +2 -1
index 77e76f775385..e81a93d57210 100644
@@ -4,8 +4,9 @@
 
 #include <linux/delay.h>
 #include <linux/leds.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
-#include <linux/of_device.h>
+#include <linux/property.h>
 #include <linux/spi/spi.h>
 
 /*
Signature · 1 line
2.31.1
#3 lore raw

PATCH v2 4/13 leds: lgm-sso: Put fwnode in any case during ->probe()

In reply to Andy Shevchenko

fwnode_get_next_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

All the same in fwnode_for_each_child_node() case.

Fixes c3987cd2bca3 ("leds: lgm: Add LED controller driver for LGM SoC")
Cc Amireddy Mallikarjuna reddy <[email protected]>

---
v2: simplified sso_led_dt_parse() fix (Pavel)

 drivers/leds/blink/leds-lgm-sso.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/leds/blink/leds-lgm-sso.c +7 -4
index e63112d445eb..a27687e0fc04 100644
@@ -633,8 +633,10 @@ __sso_led_dt_parse(struct sso_led_priv *priv, struct fwnode_handle *fw_ssoled)
 
 	fwnode_for_each_child_node(fw_ssoled, fwnode_child) {
 		led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
-		if (!led)
-			return -ENOMEM;
+		if (!led) {
+			ret = -ENOMEM;
+			goto __dt_err;
+		}
 
 		INIT_LIST_HEAD(&led->list);
 		led->priv = priv;
@@ -704,11 +706,11 @@ __sso_led_dt_parse(struct sso_led_priv *priv, struct fwnode_handle *fw_ssoled)
 		if (sso_create_led(priv, led, fwnode_child))
 			goto __dt_err;
 	}
-	fwnode_handle_put(fw_ssoled);
 
 	return 0;
+
 __dt_err:
-	fwnode_handle_put(fw_ssoled);
+	fwnode_handle_put(fwnode_child);
 	/* unregister leds */
 	list_for_each(p, &priv->led_list) {
 		led = list_entry(p, struct sso_led, list);
@@ -733,6 +735,7 @@ static int sso_led_dt_parse(struct sso_led_priv *priv)
 	fw_ssoled = fwnode_get_named_child_node(fwnode, "ssoled");
 	if (fw_ssoled) {
 		ret = __sso_led_dt_parse(priv, fw_ssoled);
+		fwnode_handle_put(fw_ssoled);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.31.1
#4 lore raw

PATCH v2 5/13 leds: lgm-sso: Don't spam logs when probe is deferred

In reply to Andy Shevchenko

When requesting GPIO line the probe can be deferred. In such case don't spam logs with an error message. This can be achieved by switching to dev_err_probe().

Fixes c3987cd2bca3 ("leds: lgm: Add LED controller driver for LGM SoC")
Cc Amireddy Mallikarjuna reddy <[email protected]>

---
v2: fixed authorship / committer (Pavel)

 drivers/leds/blink/leds-lgm-sso.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/blink/leds-lgm-sso.c +1 -1
index a27687e0fc04..877b44594b26 100644
@@ -646,7 +646,7 @@ __sso_led_dt_parse(struct sso_led_priv *priv, struct fwnode_handle *fw_ssoled)
 							      fwnode_child,
 							      GPIOD_ASIS, NULL);
 		if (IS_ERR(led->gpiod)) {
-			dev_err(dev, "led: get gpio fail!\n");
+			dev_err_probe(dev, PTR_ERR(led->gpiod), "led: get gpio fail!\n");
 			goto __dt_err;
 		}
 
Signature · 1 line
2.31.1
#5 lore raw

PATCH v2 6/13 leds: lgm-sso: Remove explicit managed GPIO resource cleanup

In reply to Andy Shevchenko

The idea of managed resources is that they will be cleaned up automatically and in the proper order. Remove explicit GPIO cleanup.

---
v2: dropped one part of the change, i.e. LED unregistration (Pavel)

 drivers/leds/blink/leds-lgm-sso.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/leds/blink/leds-lgm-sso.c +0 -3
index 877b44594b26..24f4057d5a05 100644
@@ -613,9 +613,6 @@ static void sso_led_shutdown(struct sso_led *led)
 	if (led->desc.hw_trig)
 		regmap_update_bits(priv->mmap, SSO_CON3, BIT(led->desc.pin), 0);
 
-	if (led->gpiod)
-		devm_gpiod_put(priv->dev, led->gpiod);
-
 	led->priv = NULL;
 }
 
Signature · 1 line
2.31.1
#6 lore raw

PATCH v2 7/13 leds: lgm-sso: Convert to use list_for_each_entry*() API

In reply to Andy Shevchenko

Convert to use list_for_each_entry*() API insted of open coded variants. It saves few lines of code and makes iteasier to read and maintain.

---
v2: no change

 drivers/leds/blink/leds-lgm-sso.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/leds/blink/leds-lgm-sso.c +4 -9
index 24f4057d5a05..77c933051d67 100644
@@ -623,7 +623,6 @@ __sso_led_dt_parse(struct sso_led_priv *priv, struct fwnode_handle *fw_ssoled)
 	struct device *dev = priv->dev;
 	struct sso_led_desc *desc;
 	struct sso_led *led;
-	struct list_head *p;
 	const char *tmp;
 	u32 prop;
 	int ret;
@@ -709,10 +708,8 @@ __sso_led_dt_parse(struct sso_led_priv *priv, struct fwnode_handle *fw_ssoled)
 __dt_err:
 	fwnode_handle_put(fwnode_child);
 	/* unregister leds */
-	list_for_each(p, &priv->led_list) {
-		led = list_entry(p, struct sso_led, list);
+	list_for_each_entry(led, &priv->led_list, list)
 		sso_led_shutdown(led);
-	}
 
 	return -EINVAL;
 }
@@ -843,14 +840,12 @@ static int intel_sso_led_probe(struct platform_device *pdev)
 static int intel_sso_led_remove(struct platform_device *pdev)
 {
 	struct sso_led_priv *priv;
-	struct list_head *pos, *n;
-	struct sso_led *led;
+	struct sso_led *led, *n;
 
 	priv = platform_get_drvdata(pdev);
 
-	list_for_each_safe(pos, n, &priv->led_list) {
-		list_del(pos);
-		led = list_entry(pos, struct sso_led, list);
+	list_for_each_entry_safe(led, n, &priv->led_list, list) {
+		list_del(&led->list);
 		sso_led_shutdown(led);
 	}
 
Signature · 1 line
2.31.1
#7 lore raw

PATCH v2 8/13 leds: lm3692x: Correct headers (of*.h -> mod_devicetable.h)

In reply to Andy Shevchenko

There is no user of of*.h headers, but mod_devicetable.h. Update header block accordingly.

---
v2: dopped OF change (Pavel)

 drivers/leds/leds-lm3692x.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/leds/leds-lm3692x.c +1 -2
index a02756d7ed8f..afe6fb297855 100644
@@ -7,10 +7,9 @@
 #include <linux/init.h>
 #include <linux/leds.h>
 #include <linux/log2.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
-#include <linux/of.h>
-#include <linux/of_gpio.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
Signature · 1 line
2.31.1
#8 lore raw

PATCH v2 9/13 leds: lm3697: Update header block to reflect reality

In reply to Andy Shevchenko

Currently the headers to be included look rather like a random set. Update them a bit to reflect the reality.

While at it, drop unneeded dependcy to OF.

---
v2: dropped OF change (Pavel)

 drivers/leds/leds-lm3697.c | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
drivers/leds/leds-lm3697.c +7 -2
index 970a4f34791b..292d64b2eeab 100644
@@ -2,11 +2,16 @@
 // TI LM3697 LED chip family driver
 // Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
 
+#include <linux/bits.h>
 #include <linux/gpio/consumer.h>
 #include <linux/i2c.h>
-#include <linux/of.h>
-#include <linux/of_gpio.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/types.h>
+
 #include <linux/leds-ti-lmu-common.h>
 
 #define LM3697_REV			0x0
Signature · 1 line
2.31.1
#9 lore raw

Re: [PATCH v2 09/13] leds: lm3697: Update header block to reflect reality

Hi!

Quoted 4 lines

Currently the headers to be included look rather like a random set. Update them a bit to reflect the reality.

While at it, drop unneeded dependcy to OF.

3-9 applied, thanks.
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#10 lore raw

PATCH v2 10/13 leds: lm3697: Make error handling more robust

In reply to Andy Shevchenko

It's easy to miss necessary clean up, e.g. firmware node reference counting, during error path in ->probe(). Make it more robust by moving to a single point of return.

---
v2: don't call for fwnode put on success (Pavel)

 drivers/leds/leds-lm3697.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/leds/leds-lm3697.c +3 -4
index 292d64b2eeab..a8c9322558cc 100644
@@ -226,14 +226,12 @@ static int lm3697_probe_dt(struct lm3697 *priv)
 		ret = fwnode_property_read_u32(child, "reg", &control_bank);
 		if (ret) {
 			dev_err(dev, "reg property missing\n");
-			fwnode_handle_put(child);
 			goto child_out;
 		}
 
 		if (control_bank > LM3697_CONTROL_B) {
 			dev_err(dev, "reg property is invalid\n");
 			ret = -EINVAL;
-			fwnode_handle_put(child);
 			goto child_out;
 		}
 
@@ -264,7 +262,6 @@ static int lm3697_probe_dt(struct lm3697 *priv)
 						    led->num_leds);
 		if (ret) {
 			dev_err(dev, "led-sources property missing\n");
-			fwnode_handle_put(child);
 			goto child_out;
 		}
 
@@ -289,14 +286,16 @@ static int lm3697_probe_dt(struct lm3697 *priv)
 						     &init_data);
 		if (ret) {
 			dev_err(dev, "led register err: %d\n", ret);
-			fwnode_handle_put(child);
 			goto child_out;
 		}
 
 		i++;
 	}
 
+	return ret;
+
 child_out:
+	fwnode_handle_put(child);
 	return ret;
 }
 
Signature · 1 line
2.31.1
#11 lore raw

PATCH v2 11/13 leds: lt3593: Put fwnode in any case during ->probe()

In reply to Andy Shevchenko

device_get_next_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

Fixes 8cd7d6daba93 ("leds: lt3593: Add device tree probing glue")
Cc Daniel Mack <[email protected]>

---
v2: no changes

 drivers/leds/leds-lt3593.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/leds/leds-lt3593.c +2 -3
index 3bb52d3165d9..d0160fde0f94 100644
@@ -97,10 +97,9 @@ static int lt3593_led_probe(struct platform_device *pdev)
 	init_data.default_label = ":";
 
 	ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
-	if (ret < 0) {
-		fwnode_handle_put(child);
+	fwnode_handle_put(child);
+	if (ret < 0)
 		return ret;
-	}
 
 	platform_set_drvdata(pdev, led_data);
 
Signature · 1 line
2.31.1
#12 lore raw

PATCH v2 12/13 leds: rt8515: Put fwnode in any case during ->probe()

In reply to Andy Shevchenko

fwnode_get_next_available_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

Fixes e1c6edcbea13 ("leds: rt8515: Add Richtek RT8515 LED driver")
Cc Linus Walleij <[email protected]>

---
v2: no changes

 drivers/leds/flash/leds-rt8515.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/leds/flash/leds-rt8515.c +3 -1
index 590bfa180d10..44904fdee3cc 100644
@@ -343,8 +343,9 @@ static int rt8515_probe(struct platform_device *pdev)
 
 	ret = devm_led_classdev_flash_register_ext(dev, fled, &init_data);
 	if (ret) {
-		dev_err(dev, "can't register LED %s\n", led->name);
+		fwnode_handle_put(child);
 		mutex_destroy(&rt->lock);
+		dev_err(dev, "can't register LED %s\n", led->name);
 		return ret;
 	}
 
@@ -362,6 +363,7 @@ static int rt8515_probe(struct platform_device *pdev)
 		 */
 	}
 
+	fwnode_handle_put(child);
 	return 0;
 }
 
Signature · 1 line
2.31.1
Reviewed-by on v2 12/13
#13 lore raw

Re: [PATCH v2 12/13] leds: rt8515: Put fwnode in any case during ->probe()

Quoted 7 lines · Andy Shevchenko

fwnode_get_next_available_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

Fixes e1c6edcbea13 ("leds: rt8515: Add Richtek RT8515 LED driver")
Cc Linus Walleij <[email protected]>

Yours,
Linus Walleij

#14 lore raw

Re: [PATCH v2 12/13] leds: rt8515: Put fwnode in any case during ->probe()

Tue, Jun 01, 2021 at 12:06:05PM +0200, Linus Walleij kirjoitti:

Quoted 7 lines
Quoted 4 lines · Andy Shevchenko

fwnode_get_next_available_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

Quoted 5 lines
Quoted 3 lines
Fixes e1c6edcbea13 ("leds: rt8515: Add Richtek RT8515 LED driver")
Cc Linus Walleij <[email protected]>

Thanks!

Pavel, can you, please, review this batch? I think I addressed most of your comments if not all.

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

Re: [PATCH v2 12/13] leds: rt8515: Put fwnode in any case during ->probe()

Quoted 12 lines · On Sat 2021-06-05 12:46:11, [email protected] wrote:

Tue, Jun 01, 2021 at 12:06:05PM +0200, Linus Walleij kirjoitti:

Quoted 11 lines
Quoted 7 lines · Andy Shevchenko

fwnode_get_next_available_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

Fixes e1c6edcbea13 ("leds: rt8515: Add Richtek RT8515 LED driver")
Cc Linus Walleij <[email protected]>
Quoted 6 lines
Quoted 1 line

Thanks!

Pavel, can you, please, review this batch? I think I addressed most of your comments if not all.

10-12 applied, thanks.
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#16 lore raw

Re: [PATCH v2 12/13] leds: rt8515: Put fwnode in any case during ->probe()

In reply to [email protected]
Quoted 12 lines · On Sat 2021-06-05 12:46:11, [email protected] wrote:

Tue, Jun 01, 2021 at 12:06:05PM +0200, Linus Walleij kirjoitti:

Quoted 11 lines
Quoted 7 lines · Andy Shevchenko

fwnode_get_next_available_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

Fixes e1c6edcbea13 ("leds: rt8515: Add Richtek RT8515 LED driver")
Cc Linus Walleij <[email protected]>
Quoted 6 lines
Quoted 1 line

Thanks!

Pavel, can you, please, review this batch? I think I addressed most of your comments if not all.

Your original email is: From: [email protected] . I don't believe that's right.

Best regards,
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#17 lore raw

PATCH v2 13/13 leds: sgm3140: Put fwnode in any case during ->probe()

In reply to Andy Shevchenko

fwnode_get_next_child_node() bumps a reference counting of a returned variable. We have to balance it whenever we return to the caller.

Fixes cef8ec8cbd21 ("leds: add sgm3140 driver")
Cc Luca Weiss <[email protected]>

---
v2: no changes

 drivers/leds/leds-sgm3140.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/leds/leds-sgm3140.c +2 -6
index f4f831570f11..df9402071695 100644
@@ -266,12 +266,8 @@ static int sgm3140_probe(struct platform_device *pdev)
 					   child_node,
 					   fled_cdev, NULL,
 					   &v4l2_sd_cfg);
-	if (IS_ERR(priv->v4l2_flash)) {
-		ret = PTR_ERR(priv->v4l2_flash);
-		goto err;
-	}
-
-	return ret;
+	fwnode_handle_put(child_node);
+	return PTR_ERR_OR_ZERO(priv->v4l2_flash);
 
 err:
 	fwnode_handle_put(child_node);
Signature · 1 line
2.31.1
#18 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

Luckily there is no user which checks for returned code and actually returns it, but since the function is exported any user may try to return an error code from it to user space, usually during probe phase,

Replace -ENOTSUPP by -EOPNOTSUPP when returning from exported function.

There were no issue reported by bots, no comments from people (except one tag), can we do something about this series or should I amend it?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#19 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

Luckily there is no user which checks for returned code and actually returns it, but since the function is exported any user may try to return an error code from it to user space, usually during probe phase,

Replace -ENOTSUPP by -EOPNOTSUPP when returning from exported function.

There were no issue reported by bots, no comments from people (except one tag), can we do something about this series or should I amend it?

Pavel, do I need to resend this series? It mostly should be the part of v5.14 cycle (half of it is the fixes).

Signature · 2 lines
With Best Regards,
Andy Shevchenko