All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] leds-gpio: Fix error handling and memory leak
@ 2015-03-10  0:43 minyard
  2015-03-26 14:55 ` Corey Minyard
  2015-03-27  1:20 ` Bryan Wu
  0 siblings, 2 replies; 6+ messages in thread
From: minyard @ 2015-03-10  0:43 UTC (permalink / raw)
  To: Raphael Assenat; +Cc: linux-leds, Linux Kernel, Corey Minyard

From: Corey Minyard <cminyard@mvista.com>

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

Signed-off-by: Corey Minyard <cminyard@mvista.com>
---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index d26af0a..32f7642 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -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;
 }
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds-gpio: Fix error handling and memory leak
  2015-03-10  0:43 [PATCH] leds-gpio: Fix error handling and memory leak minyard
@ 2015-03-26 14:55 ` Corey Minyard
  2015-03-27  1:20 ` Bryan Wu
  1 sibling, 0 replies; 6+ messages in thread
From: Corey Minyard @ 2015-03-26 14:55 UTC (permalink / raw)
  To: minyard, Raphael Assenat; +Cc: linux-leds, Linux Kernel

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

-corey

On 03/09/2015 07:43 PM, minyard@acm.org wrote:
> From: Corey Minyard <cminyard@mvista.com>
>
> The leds-gpio driver would not clean up properly if it failed in some
> places, and it wasn't freeing its private data.
>
> Signed-off-by: Corey Minyard <cminyard@mvista.com>
> ---
>  drivers/leds/leds-gpio.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
> index d26af0a..32f7642 100644
> --- a/drivers/leds/leds-gpio.c
> +++ b/drivers/leds/leds-gpio.c
> @@ -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;
>  }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds-gpio: Fix error handling and memory leak
  2015-03-10  0:43 [PATCH] leds-gpio: Fix error handling and memory leak minyard
  2015-03-26 14:55 ` Corey Minyard
@ 2015-03-27  1:20 ` Bryan Wu
  2015-03-27  3:08   ` Corey Minyard
  1 sibling, 1 reply; 6+ messages in thread
From: Bryan Wu @ 2015-03-27  1:20 UTC (permalink / raw)
  To: minyard; +Cc: Raphael Assenat, Linux LED Subsystem, Linux Kernel, Corey Minyard

On Mon, Mar 9, 2015 at 5:43 PM,  <minyard@acm.org> wrote:
> From: Corey Minyard <cminyard@mvista.com>
>
> The leds-gpio driver would not clean up properly if it failed in some
> places, and it wasn't freeing its private data.
>
> Signed-off-by: Corey Minyard <cminyard@mvista.com>
> ---
>  drivers/leds/leds-gpio.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
> index d26af0a..32f7642 100644
> --- a/drivers/leds/leds-gpio.c
> +++ b/drivers/leds/leds-gpio.c
> @@ -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],

Why need this change? it's correct. And your add one more line
"priv->num_leds++"
>                                       dev, NULL);
>                 if (ret < 0) {
>                         fwnode_handle_put(child);
>                         goto err;
>                 }
> +               priv->num_leds++;
Why need this?

>         }
>
>         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();

>         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.

>
>         return 0;
>  }
> --
> 1.8.3.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-leds" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds-gpio: Fix error handling and memory leak
  2015-03-27  1:20 ` Bryan Wu
@ 2015-03-27  3:08   ` Corey Minyard
  2015-03-30 22:16     ` Bryan Wu
  0 siblings, 1 reply; 6+ messages in thread
From: Corey Minyard @ 2015-03-27  3:08 UTC (permalink / raw)
  To: Bryan Wu
  Cc: Raphael Assenat, Linux LED Subsystem, Linux Kernel, Corey Minyard

On 03/26/2015 08:20 PM, Bryan Wu wrote:
> On Mon, Mar 9, 2015 at 5:43 PM,  <minyard@acm.org> wrote:
>> From: Corey Minyard <cminyard@mvista.com>
>>
>> The leds-gpio driver would not clean up properly if it failed in some
>> places, and it wasn't freeing its private data.
>>
>> Signed-off-by: Corey Minyard <cminyard@mvista.com>
>> ---
>>  drivers/leds/leds-gpio.c | 13 +++++++++----
>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
>> index d26af0a..32f7642 100644
>> --- a/drivers/leds/leds-gpio.c
>> +++ b/drivers/leds/leds-gpio.c
>> @@ -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],
> 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.

>>                                       dev, NULL);
>>                 if (ret < 0) {
>>                         fwnode_handle_put(child);
>>                         goto err;
>>                 }
>> +               priv->num_leds++;
> Why need this?

See above.

>>         }
>>
>>         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

>>         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.
>
>>         return 0;
>>  }
>> --
>> 1.8.3.1
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-leds" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds-gpio: Fix error handling and memory leak
  2015-03-27  3:08   ` Corey Minyard
@ 2015-03-30 22:16     ` Bryan Wu
  2015-04-01 21:03       ` Corey Minyard
  0 siblings, 1 reply; 6+ messages in thread
From: Bryan Wu @ 2015-03-30 22:16 UTC (permalink / raw)
  To: minyard; +Cc: Raphael Assenat, Linux LED Subsystem, Linux Kernel, Corey Minyard

On Thu, Mar 26, 2015 at 8:08 PM, Corey Minyard <minyard@acm.org> wrote:
> On 03/26/2015 08:20 PM, Bryan Wu wrote:
>> On Mon, Mar 9, 2015 at 5:43 PM,  <minyard@acm.org> wrote:
>>> From: Corey Minyard <cminyard@mvista.com>
>>>
>>> The leds-gpio driver would not clean up properly if it failed in some
>>> places, and it wasn't freeing its private data.
>>>
>>> Signed-off-by: Corey Minyard <cminyard@mvista.com>
>>> ---
>>>  drivers/leds/leds-gpio.c | 13 +++++++++----
>>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
>>> index d26af0a..32f7642 100644
>>> --- a/drivers/leds/leds-gpio.c
>>> +++ b/drivers/leds/leds-gpio.c
>>> @@ -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],
>> 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.
>
>>>                                       dev, NULL);
>>>                 if (ret < 0) {
>>>                         fwnode_handle_put(child);
>>>                         goto err;
>>>                 }
>>> +               priv->num_leds++;
>> Why need this?
>
> See above.
>
>>>         }
>>>
>>>         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?
>

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

Thanks,
-Bryan


> Thanks,
>
> -corey
>
>>>         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.
>>
>>>         return 0;
>>>  }
>>> --
>>> 1.8.3.1
>>>
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe linux-leds" in
>>> the body of a message to majordomo@vger.kernel.org
>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds-gpio: Fix error handling and memory leak
  2015-03-30 22:16     ` Bryan Wu
@ 2015-04-01 21:03       ` Corey Minyard
  0 siblings, 0 replies; 6+ messages in thread
From: Corey Minyard @ 2015-04-01 21:03 UTC (permalink / raw)
  To: Bryan Wu, minyard; +Cc: Raphael Assenat, Linux LED Subsystem, Linux Kernel

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

On 03/30/2015 05:16 PM, Bryan Wu wrote:
> On Thu, Mar 26, 2015 at 8:08 PM, Corey Minyard <minyard@acm.org> wrote:
>> On 03/26/2015 08:20 PM, Bryan Wu wrote:
>>> On Mon, Mar 9, 2015 at 5:43 PM,  <minyard@acm.org> wrote:
>>>> From: Corey Minyard <cminyard@mvista.com>
>>>>
>>>> The leds-gpio driver would not clean up properly if it failed in some
>>>> places, and it wasn't freeing its private data.
>>>>
>>>> Signed-off-by: Corey Minyard <cminyard@mvista.com>
>>>> ---
>>>>  drivers/leds/leds-gpio.c | 13 +++++++++----
>>>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
>>>> index d26af0a..32f7642 100644
>>>> --- a/drivers/leds/leds-gpio.c
>>>> +++ b/drivers/leds/leds-gpio.c
>>>> @@ -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],
>>> 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.
>>
>>>>                                       dev, NULL);
>>>>                 if (ret < 0) {
>>>>                         fwnode_handle_put(child);
>>>>                         goto err;
>>>>                 }
>>>> +               priv->num_leds++;
>>> Why need this?
>> See above.
>>
>>>>         }
>>>>
>>>>         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?
>>
> I see, please provide a new patch. I'm going to merge this fix soon.
>
> Thanks,
> -Bryan
>
>
>> Thanks,
>>
>> -corey
>>
>>>>         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.
>>>
>>>>         return 0;
>>>>  }
>>>> --
>>>> 1.8.3.1
>>>>
>>>> --
>>>> To unsubscribe from this list: send the line "unsubscribe linux-leds" in
>>>> the body of a message to majordomo@vger.kernel.org
>>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-04-01 21:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-10  0:43 [PATCH] leds-gpio: Fix error handling and memory leak minyard
2015-03-26 14:55 ` Corey Minyard
2015-03-27  1:20 ` Bryan Wu
2015-03-27  3:08   ` Corey Minyard
2015-03-30 22:16     ` Bryan Wu
2015-04-01 21:03       ` Corey Minyard

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.