Skip to contentSkip to conversation

leds-gpio: Fix error handling and memory leak

PATCH In review [email protected] 3 participants 6 messages Mar 10, 2015 – Apr 1, 2015 LED

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds-gpio: Fix error handling and memory leak

From: Corey Minyard <[email protected]>

The leds-gpio driver would not clean up properly if it failed in some places, and it wasn't freeing its private data.

---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/leds/leds-gpio.c +9 -4
index d26af0a..32f7642 100644
@@ -198,8 +198,10 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 		} else {
 			if (IS_ENABLED(CONFIG_OF) && !led.name && np)
 				led.name = np->name;
-			if (!led.name)
-				return ERR_PTR(-EINVAL);
+			if (!led.name) {
+				ret = -EINVAL;
+				goto err;
+			}
 		}
 		fwnode_property_read_string(child, "linux,default-trigger",
 					    &led.default_trigger);
@@ -217,19 +219,21 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 		if (fwnode_property_present(child, "retain-state-suspended"))
 			led.retain_state_suspended = 1;
 
-		ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
+		ret = create_gpio_led(&led, &priv->leds[priv->num_leds],
 				      dev, NULL);
 		if (ret < 0) {
 			fwnode_handle_put(child);
 			goto err;
 		}
+		priv->num_leds++;
 	}
 
 	return priv;
 
 err:
-	for (count = priv->num_leds - 2; count >= 0; count--)
+	for (count = priv->num_leds - 1; count >= 0; count--)
 		delete_gpio_led(&priv->leds[count]);
+	devm_kfree(dev, priv);
 	return ERR_PTR(ret);
 }
 
@@ -283,6 +287,7 @@ static int gpio_led_remove(struct platform_device *pdev)
 
 	for (i = 0; i < priv->num_leds; i++)
 		delete_gpio_led(&priv->leds[i]);
+	devm_kfree(&pdev->dev, priv);
 
 	return 0;
 }
Signature · 1 line
1.8.3.1
#1 lore raw

Ping. This is a rather obvious fix and can result in an oops.

-corey

Quoted 59 lines · On 03/09/2015 07:43 PM, [email protected] wrote:

From: Corey Minyard <[email protected]>

The leds-gpio driver would not clean up properly if it failed in some places, and it wasn't freeing its private data.

---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/leds/leds-gpio.c +9 -4
index d26af0a..32f7642 100644
@@ -198,8 +198,10 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 		} else {
 			if (IS_ENABLED(CONFIG_OF) && !led.name && np)
 				led.name = np->name;
-			if (!led.name)
-				return ERR_PTR(-EINVAL);
+			if (!led.name) {
+				ret = -EINVAL;
+				goto err;
+			}
 		}
 		fwnode_property_read_string(child, "linux,default-trigger",
 					    &led.default_trigger);
@@ -217,19 +219,21 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 		if (fwnode_property_present(child, "retain-state-suspended"))
 			led.retain_state_suspended = 1;
 
-		ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
+		ret = create_gpio_led(&led, &priv->leds[priv->num_leds],
 				      dev, NULL);
 		if (ret < 0) {
 			fwnode_handle_put(child);
 			goto err;
 		}
+		priv->num_leds++;
 	}
 
 	return priv;
 
 err:
-	for (count = priv->num_leds - 2; count >= 0; count--)
+	for (count = priv->num_leds - 1; count >= 0; count--)
 		delete_gpio_led(&priv->leds[count]);
+	devm_kfree(dev, priv);
 	return ERR_PTR(ret);
 }
 
@@ -283,6 +287,7 @@ static int gpio_led_remove(struct platform_device *pdev)
 
 	for (i = 0; i < priv->num_leds; i++)
 		delete_gpio_led(&priv->leds[i]);
+	devm_kfree(&pdev->dev, priv);
 
 	return 0;
 }
#2 lore raw
In reply to [email protected]
Quoted 28 lines · On Mon, Mar 9, 2015 at 5:43 PM, <[email protected]> wrote:

From: Corey Minyard <[email protected]>

The leds-gpio driver would not clean up properly if it failed in some places, and it wasn't freeing its private data.

---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/leds/leds-gpio.c +4 -2
index d26af0a..32f7642 100644
@@ -198,8 +198,10 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                } else {
                        if (IS_ENABLED(CONFIG_OF) && !led.name && np)
                                led.name = np->name;
-                       if (!led.name)
-                               return ERR_PTR(-EINVAL);
+                       if (!led.name) {
+                               ret = -EINVAL;
+                               goto err;
+                       }
                }
                fwnode_property_read_string(child, "linux,default-trigger",
                                            &led.default_trigger);

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 222

Quoted 5 lines
drivers/leds/leds-gpio.c +1 -1
@@ -217,19 +219,21 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                if (fwnode_property_present(child, "retain-state-suspended"))
                        led.retain_state_suspended = 1;
 
-               ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
+               ret = create_gpio_led(&led, &priv->leds[priv->num_leds],

Why need this change? it's correct. And your add one more line "priv->num_leds++"

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 228

Quoted 6 lines
drivers/leds/leds-gpio.c +1 -0
                                      dev, NULL);
                if (ret < 0) {
                        fwnode_handle_put(child);
                        goto err;
                }
+               priv->num_leds++;

Why need this?

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 236

Quoted 9 lines
drivers/leds/leds-gpio.c +2 -1
        }
 
        return priv;
 
 err:
-       for (count = priv->num_leds - 2; count >= 0; count--)
+       for (count = priv->num_leds - 1; count >= 0; count--)
                delete_gpio_led(&priv->leds[count]);
+       devm_kfree(dev, priv);

priv is created by devm_kzalloc(), so if driver probing return error, it will be freed automatically, you don't need call devm_free();

drivers/leds/leds-gpio.c static int gpio_led_remove(struct platform_device *pdev) · line 290

Quoted 8 lines
drivers/leds/leds-gpio.c +1 -0
        return ERR_PTR(ret);
 }
 
@@ -283,6 +287,7 @@ static int gpio_led_remove(struct platform_device *pdev)
 
        for (i = 0; i < priv->num_leds; i++)
                delete_gpio_led(&priv->leds[i]);
+       devm_kfree(&pdev->dev, priv);

No need this during remove.

Quoted 10 lines
drivers/leds/leds-gpio.c +0 -1
        return 0;
 }
--

1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-leds" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 32 lines · Bryan Wu
Quoted 31 lines · On Mon, Mar 9, 2015 at 5:43 PM, <[email protected]> wrote:

From: Corey Minyard <[email protected]>

The leds-gpio driver would not clean up properly if it failed in some places, and it wasn't freeing its private data.

---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/leds/leds-gpio.c +4 -2
index d26af0a..32f7642 100644
@@ -198,8 +198,10 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                } else {
                        if (IS_ENABLED(CONFIG_OF) && !led.name && np)
                                led.name = np->name;
-                       if (!led.name)
-                               return ERR_PTR(-EINVAL);
+                       if (!led.name) {
+                               ret = -EINVAL;
+                               goto err;
+                       }
                }
                fwnode_property_read_string(child, "linux,default-trigger",
                                            &led.default_trigger);
@@ -217,19 +219,21 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                if (fwnode_property_present(child, "retain-state-suspended"))
                        led.retain_state_suspended = 1;
 
Quoted 4 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 222

Quoted 2 lines
drivers/leds/leds-gpio.c +1 -1
-               ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
+               ret = create_gpio_led(&led, &priv->leds[priv->num_leds],

Why need this change? it's correct. And your add one more line "priv->num_leds++"

That's actually the major source of the problem. The value of priv->num_leds was not correct if it failed before this point, and there was already one "goto err" above this code and I added another to properly handle not allocating the led name. If it failed there it would leave an LED lying around but free the memory underneath it. So instead, modify the failure recovery code to be priv->num_leds-1 instead of priv->num_leds-2 and don't increment priv->num_leds until you have success.

Quoted 7 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 228

Quoted 6 lines
drivers/leds/leds-gpio.c +1 -0
                                      dev, NULL);
                if (ret < 0) {
                        fwnode_handle_put(child);
                        goto err;
                }
+               priv->num_leds++;

Why need this?

See above.

Quoted 11 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 236

Quoted 9 lines
drivers/leds/leds-gpio.c +2 -1
        }
 
        return priv;
 
 err:
-       for (count = priv->num_leds - 2; count >= 0; count--)
+       for (count = priv->num_leds - 1; count >= 0; count--)
                delete_gpio_led(&priv->leds[count]);
+       devm_kfree(dev, priv);

priv is created by devm_kzalloc(), so if driver probing return error, it will be freed automatically, you don't need call devm_free();

Ah, ok. Then this is unnecessary. Do want a new patch?

Thanks,

-corey

Quoted 19 lines

drivers/leds/leds-gpio.c static int gpio_led_remove(struct platform_device *pdev) · line 290

Quoted 8 lines
drivers/leds/leds-gpio.c +1 -0
        return ERR_PTR(ret);
 }
 
@@ -283,6 +287,7 @@ static int gpio_led_remove(struct platform_device *pdev)
 
        for (i = 0; i < priv->num_leds; i++)
                delete_gpio_led(&priv->leds[i]);
+       devm_kfree(&pdev->dev, priv);

No need this during remove.

Quoted 9 lines
drivers/leds/leds-gpio.c +0 -1
        return 0;
 }
--

1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-leds" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 66 lines · Corey Minyard
Quoted 32 lines · Bryan Wu
Quoted 31 lines · On Mon, Mar 9, 2015 at 5:43 PM, <[email protected]> wrote:

From: Corey Minyard <[email protected]>

The leds-gpio driver would not clean up properly if it failed in some places, and it wasn't freeing its private data.

---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/leds/leds-gpio.c +4 -2
index d26af0a..32f7642 100644
@@ -198,8 +198,10 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                } else {
                        if (IS_ENABLED(CONFIG_OF) && !led.name && np)
                                led.name = np->name;
-                       if (!led.name)
-                               return ERR_PTR(-EINVAL);
+                       if (!led.name) {
+                               ret = -EINVAL;
+                               goto err;
+                       }
                }
                fwnode_property_read_string(child, "linux,default-trigger",
                                            &led.default_trigger);
@@ -217,19 +219,21 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                if (fwnode_property_present(child, "retain-state-suspended"))
                        led.retain_state_suspended = 1;
 
Quoted 4 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 222

Quoted 2 lines
drivers/leds/leds-gpio.c +1 -1
-               ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
+               ret = create_gpio_led(&led, &priv->leds[priv->num_leds],

Why need this change? it's correct. And your add one more line "priv->num_leds++"

That's actually the major source of the problem. The value of priv->num_leds was not correct if it failed before this point, and there was already one "goto err" above this code and I added another to properly handle not allocating the led name. If it failed there it would leave an LED lying around but free the memory underneath it. So instead, modify the failure recovery code to be priv->num_leds-1 instead of priv->num_leds-2 and don't increment priv->num_leds until you have success.

Quoted 7 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 228

Quoted 6 lines
drivers/leds/leds-gpio.c +1 -0
                                      dev, NULL);
                if (ret < 0) {
                        fwnode_handle_put(child);
                        goto err;
                }
+               priv->num_leds++;

Why need this?

See above.

Quoted 9 lines
Quoted 9 lines
drivers/leds/leds-gpio.c +2 -1
        }
 
        return priv;
 
 err:
-       for (count = priv->num_leds - 2; count >= 0; count--)
+       for (count = priv->num_leds - 1; count >= 0; count--)
                delete_gpio_led(&priv->leds[count]);
+       devm_kfree(dev, priv);
Quoted 5 lines
Quoted 2 lines

priv is created by devm_kzalloc(), so if driver probing return error, it will be freed automatically, you don't need call devm_free();

Ah, ok. Then this is unnecessary. Do want a new patch?

I see, please provide a new patch. I'm going to merge this fix soon.

Thanks,
-Bryan

Quoted 24 lines

Thanks,

-corey

Quoted 19 lines

drivers/leds/leds-gpio.c static int gpio_led_remove(struct platform_device *pdev) · line 290

Quoted 8 lines
drivers/leds/leds-gpio.c +1 -0
        return ERR_PTR(ret);
 }
 
@@ -283,6 +287,7 @@ static int gpio_led_remove(struct platform_device *pdev)
 
        for (i = 0; i < priv->num_leds; i++)
                delete_gpio_led(&priv->leds[i]);
+       devm_kfree(&pdev->dev, priv);

No need this during remove.

Quoted 9 lines
drivers/leds/leds-gpio.c +0 -1
        return 0;
 }
--

1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-leds" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

Sorry, I'm traveling and it's been rather busy.

I thought about this some more, and I've realized that the devm_kfree() calls are probably still a good idea. Those are associated with the device, not the LED.

For instance, I noticed that on a failure in the probe code the probe code would get called multiple times. So that data would be left in the device, even though it's not used.

Similarly, the pdev doesn't go away if you remove the led-gpio module and then re-install it. Again, the data would be left in the device.

Does that make sense? If so, the original patch is best.

-corey

Quoted 98 lines · Bryan Wu
Quoted 59 lines · Corey Minyard
Quoted 32 lines · Bryan Wu
Quoted 31 lines · On Mon, Mar 9, 2015 at 5:43 PM, <[email protected]> wrote:

From: Corey Minyard <[email protected]>

The leds-gpio driver would not clean up properly if it failed in some places, and it wasn't freeing its private data.

---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/leds/leds-gpio.c +4 -2
index d26af0a..32f7642 100644
@@ -198,8 +198,10 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                } else {
                        if (IS_ENABLED(CONFIG_OF) && !led.name && np)
                                led.name = np->name;
-                       if (!led.name)
-                               return ERR_PTR(-EINVAL);
+                       if (!led.name) {
+                               ret = -EINVAL;
+                               goto err;
+                       }
                }
                fwnode_property_read_string(child, "linux,default-trigger",
                                            &led.default_trigger);
@@ -217,19 +219,21 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
                if (fwnode_property_present(child, "retain-state-suspended"))
                        led.retain_state_suspended = 1;
 
Quoted 4 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 222

Quoted 2 lines
drivers/leds/leds-gpio.c +1 -1
-               ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
+               ret = create_gpio_led(&led, &priv->leds[priv->num_leds],

Why need this change? it's correct. And your add one more line "priv->num_leds++"

That's actually the major source of the problem. The value of priv->num_leds was not correct if it failed before this point, and there was already one "goto err" above this code and I added another to properly handle not allocating the led name. If it failed there it would leave an LED lying around but free the memory underneath it. So instead, modify the failure recovery code to be priv->num_leds-1 instead of priv->num_leds-2 and don't increment priv->num_leds until you have success.

Quoted 7 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 228

Quoted 6 lines
drivers/leds/leds-gpio.c +1 -0
                                      dev, NULL);
                if (ret < 0) {
                        fwnode_handle_put(child);
                        goto err;
                }
+               priv->num_leds++;

Why need this?

See above.

Quoted 4 lines
Quoted 4 lines
drivers/leds/leds-gpio.c +0 -0
        }
 
        return priv;
 
Quoted 9 lines
Quoted 7 lines

drivers/leds/leds-gpio.c static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) · line 236

Quoted 5 lines
drivers/leds/leds-gpio.c +2 -1
 err:
-       for (count = priv->num_leds - 2; count >= 0; count--)
+       for (count = priv->num_leds - 1; count >= 0; count--)
                delete_gpio_led(&priv->leds[count]);
+       devm_kfree(dev, priv);

priv is created by devm_kzalloc(), so if driver probing return error, it will be freed automatically, you don't need call devm_free();

Ah, ok. Then this is unnecessary. Do want a new patch?

I see, please provide a new patch. I'm going to merge this fix soon.

Thanks,
-Bryan

Quoted 23 lines

Thanks,

-corey

Quoted 19 lines

drivers/leds/leds-gpio.c static int gpio_led_remove(struct platform_device *pdev) · line 290

Quoted 8 lines
drivers/leds/leds-gpio.c +1 -0
        return ERR_PTR(ret);
 }
 
@@ -283,6 +287,7 @@ static int gpio_led_remove(struct platform_device *pdev)
 
        for (i = 0; i < priv->num_leds; i++)
                delete_gpio_led(&priv->leds[i]);
+       devm_kfree(&pdev->dev, priv);

No need this during remove.

Quoted 9 lines
drivers/leds/leds-gpio.c +0 -1
        return 0;
 }
--

1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-leds" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html