Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
@ 2024-10-24 10:59 Daniel Lezcano
  2024-10-24 11:01 ` Daniel Lezcano
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Daniel Lezcano @ 2024-10-24 10:59 UTC (permalink / raw)
  To: daniel.lezcano, rafael
  Cc: Lukasz Luba, Zhang Rui, open list:THERMAL, open list

The function thermal_genl_auto() does not free the allocated message
in the error path. Fix that by putting a out label and jump to it
which will free the message instead of directly returning an error.

Reported-by: Lukasz Luba <lukasz.luba@arm.com>\a
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 tools/lib/thermal/commands.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/tools/lib/thermal/commands.c b/tools/lib/thermal/commands.c
index bcf0f14d035a..b0d4c8aca21c 100644
--- a/tools/lib/thermal/commands.c
+++ b/tools/lib/thermal/commands.c
@@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
 					 struct cmd_param *param,
 					 int cmd, int flags, void *arg)
 {
+	thermal_error_t ret = THERMAL_ERROR;
 	struct nl_msg *msg;
 	void *hdr;
 
 	msg = nlmsg_alloc();
 	if (!msg)
-		return THERMAL_ERROR;
+		goto out;
 
 	hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
 			  0, flags, cmd, THERMAL_GENL_VERSION);
 	if (!hdr)
-		return THERMAL_ERROR;
+		goto out;
 
 	if (cmd_cb && cmd_cb(msg, param))
-		return THERMAL_ERROR;
+		goto out;
 
 	if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
-		return THERMAL_ERROR;
+		goto out;
 
+	ret = THERMAL_SUCCESS;
+out:
 	nlmsg_free(msg);
 
-	return THERMAL_SUCCESS;
+	return ret;
 }
 
 thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)
-- 
2.43.0


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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 10:59 [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto() Daniel Lezcano
@ 2024-10-24 11:01 ` Daniel Lezcano
  2024-10-24 11:07 ` Lukasz Luba
  2024-10-24 12:02 ` Markus Elfring
  2 siblings, 0 replies; 10+ messages in thread
From: Daniel Lezcano @ 2024-10-24 11:01 UTC (permalink / raw)
  To: rafael; +Cc: Lukasz Luba, Zhang Rui, open list:THERMAL, open list

On 24/10/2024 12:59, Daniel Lezcano wrote:
> The function thermal_genl_auto() does not free the allocated message
> in the error path. Fix that by putting a out label and jump to it
> which will free the message instead of directly returning an error.

Please note this patch applies on top of the thresholds series.

If nobody complains about the change, I'll take care of applying this 
patch after the thermal thresholds series appears in the linux-pm branch

Thanks

   -- D.

> Reported-by: Lukasz Luba <lukasz.luba@arm.com>\a
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> ---
>   tools/lib/thermal/commands.c | 13 ++++++++-----
>   1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/lib/thermal/commands.c b/tools/lib/thermal/commands.c
> index bcf0f14d035a..b0d4c8aca21c 100644
> --- a/tools/lib/thermal/commands.c
> +++ b/tools/lib/thermal/commands.c
> @@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
>   					 struct cmd_param *param,
>   					 int cmd, int flags, void *arg)
>   {
> +	thermal_error_t ret = THERMAL_ERROR;
>   	struct nl_msg *msg;
>   	void *hdr;
>   
>   	msg = nlmsg_alloc();
>   	if (!msg)
> -		return THERMAL_ERROR;
> +		goto out;
>   
>   	hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
>   			  0, flags, cmd, THERMAL_GENL_VERSION);
>   	if (!hdr)
> -		return THERMAL_ERROR;
> +		goto out;
>   
>   	if (cmd_cb && cmd_cb(msg, param))
> -		return THERMAL_ERROR;
> +		goto out;
>   
>   	if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
> -		return THERMAL_ERROR;
> +		goto out;
>   
> +	ret = THERMAL_SUCCESS;
> +out:
>   	nlmsg_free(msg);
>   
> -	return THERMAL_SUCCESS;
> +	return ret;
>   }
>   
>   thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)


-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 10:59 [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto() Daniel Lezcano
  2024-10-24 11:01 ` Daniel Lezcano
@ 2024-10-24 11:07 ` Lukasz Luba
  2024-10-24 12:02 ` Markus Elfring
  2 siblings, 0 replies; 10+ messages in thread
From: Lukasz Luba @ 2024-10-24 11:07 UTC (permalink / raw)
  To: Daniel Lezcano; +Cc: Zhang Rui, open list:THERMAL, open list, rafael

Hi Daniel,

On 10/24/24 11:59, Daniel Lezcano wrote:
> The function thermal_genl_auto() does not free the allocated message
> in the error path. Fix that by putting a out label and jump to it
> which will free the message instead of directly returning an error.
> 
> Reported-by: Lukasz Luba <lukasz.luba@arm.com>\a
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> ---
>   tools/lib/thermal/commands.c | 13 ++++++++-----
>   1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/lib/thermal/commands.c b/tools/lib/thermal/commands.c
> index bcf0f14d035a..b0d4c8aca21c 100644
> --- a/tools/lib/thermal/commands.c
> +++ b/tools/lib/thermal/commands.c
> @@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
>   					 struct cmd_param *param,
>   					 int cmd, int flags, void *arg)
>   {
> +	thermal_error_t ret = THERMAL_ERROR;
>   	struct nl_msg *msg;
>   	void *hdr;
>   
>   	msg = nlmsg_alloc();
>   	if (!msg)
> -		return THERMAL_ERROR;
> +		goto out;
>   
>   	hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
>   			  0, flags, cmd, THERMAL_GENL_VERSION);
>   	if (!hdr)
> -		return THERMAL_ERROR;
> +		goto out;
>   
>   	if (cmd_cb && cmd_cb(msg, param))
> -		return THERMAL_ERROR;
> +		goto out;
>   
>   	if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
> -		return THERMAL_ERROR;
> +		goto out;
>   
> +	ret = THERMAL_SUCCESS;
> +out:
>   	nlmsg_free(msg);
>   
> -	return THERMAL_SUCCESS;
> +	return ret;
>   }
>   
>   thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)

LGTM,

Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>

Regards,
Lukasz

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 10:59 [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto() Daniel Lezcano
  2024-10-24 11:01 ` Daniel Lezcano
  2024-10-24 11:07 ` Lukasz Luba
@ 2024-10-24 12:02 ` Markus Elfring
  2024-10-24 12:57   ` Daniel Lezcano
  2 siblings, 1 reply; 10+ messages in thread
From: Markus Elfring @ 2024-10-24 12:02 UTC (permalink / raw)
  To: Daniel Lezcano, linux-pm, Lukasz Luba, Rafael J. Wysocki; +Cc: LKML, Zhang Rui

> The function thermal_genl_auto() does not free the allocated message
> in the error path. Fix that by putting a out label and jump to it
> which will free the message instead of directly returning an error.

Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145> +++ b/tools/lib/thermal/commands.c
> @@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
>  					 struct cmd_param *param,
>  					 int cmd, int flags, void *arg)
>  {
> +	thermal_error_t ret = THERMAL_ERROR;
>  	struct nl_msg *msg;
>  	void *hdr;
>
>  	msg = nlmsg_alloc();
>  	if (!msg)
> -		return THERMAL_ERROR;
> +		goto out;
…

Is it really reasonable to pass a null pointer (from a failed function call)
to a subsequent nlmsg_free() call?

Can it be more appropriate to return directly in such an error case?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.12-rc4#n532

Regards,
Markus

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 12:02 ` Markus Elfring
@ 2024-10-24 12:57   ` Daniel Lezcano
  2024-10-24 13:18     ` Rafael J. Wysocki
  0 siblings, 1 reply; 10+ messages in thread
From: Daniel Lezcano @ 2024-10-24 12:57 UTC (permalink / raw)
  To: Markus Elfring, linux-pm, Lukasz Luba, Rafael J. Wysocki; +Cc: LKML, Zhang Rui

On 24/10/2024 14:02, Markus Elfring wrote:
>> The function thermal_genl_auto() does not free the allocated message
>> in the error path. Fix that by putting a out label and jump to it
>> which will free the message instead of directly returning an error.
> 
> Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145
> 
> 
> …
>> +++ b/tools/lib/thermal/commands.c
>> @@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
>>   					 struct cmd_param *param,
>>   					 int cmd, int flags, void *arg)
>>   {
>> +	thermal_error_t ret = THERMAL_ERROR;
>>   	struct nl_msg *msg;
>>   	void *hdr;
>>
>>   	msg = nlmsg_alloc();
>>   	if (!msg)
>> -		return THERMAL_ERROR;
>> +		goto out;
> …
> 
> Is it really reasonable to pass a null pointer (from a failed function call)
> to a subsequent nlmsg_free() call?

You are right, I should return here :S



> Can it be more appropriate to return directly in such an error case?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.12-rc4#n532
> 
> Regards,
> Markus


-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 12:57   ` Daniel Lezcano
@ 2024-10-24 13:18     ` Rafael J. Wysocki
  2024-10-24 13:27       ` Lukasz Luba
  2024-10-24 14:21       ` Daniel Lezcano
  0 siblings, 2 replies; 10+ messages in thread
From: Rafael J. Wysocki @ 2024-10-24 13:18 UTC (permalink / raw)
  To: Daniel Lezcano
  Cc: Markus Elfring, linux-pm, Lukasz Luba, Rafael J. Wysocki, LKML,
	Zhang Rui

On Thu, Oct 24, 2024 at 2:57 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>
> On 24/10/2024 14:02, Markus Elfring wrote:
> >> The function thermal_genl_auto() does not free the allocated message
> >> in the error path. Fix that by putting a out label and jump to it
> >> which will free the message instead of directly returning an error.
> >
> > Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145
> >
> >
> > …
> >> +++ b/tools/lib/thermal/commands.c
> >> @@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
> >>                                       struct cmd_param *param,
> >>                                       int cmd, int flags, void *arg)
> >>   {
> >> +    thermal_error_t ret = THERMAL_ERROR;
> >>      struct nl_msg *msg;
> >>      void *hdr;
> >>
> >>      msg = nlmsg_alloc();
> >>      if (!msg)
> >> -            return THERMAL_ERROR;
> >> +            goto out;
> > …
> >
> > Is it really reasonable to pass a null pointer (from a failed function call)
> > to a subsequent nlmsg_free() call?
>
> You are right, I should return here :S

Do you want to respin it?

Alternatively, I can fix it up when applying the patch.

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 13:18     ` Rafael J. Wysocki
@ 2024-10-24 13:27       ` Lukasz Luba
  2024-10-24 14:21       ` Daniel Lezcano
  1 sibling, 0 replies; 10+ messages in thread
From: Lukasz Luba @ 2024-10-24 13:27 UTC (permalink / raw)
  To: Rafael J. Wysocki, Daniel Lezcano
  Cc: Markus Elfring, linux-pm, LKML, Zhang Rui



On 10/24/24 14:18, Rafael J. Wysocki wrote:
> On Thu, Oct 24, 2024 at 2:57 PM Daniel Lezcano
> <daniel.lezcano@linaro.org> wrote:
>>
>> On 24/10/2024 14:02, Markus Elfring wrote:
>>>> The function thermal_genl_auto() does not free the allocated message
>>>> in the error path. Fix that by putting a out label and jump to it
>>>> which will free the message instead of directly returning an error.
>>>
>>> Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145
>>>
>>>
>>> …
>>>> +++ b/tools/lib/thermal/commands.c
>>>> @@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
>>>>                                        struct cmd_param *param,
>>>>                                        int cmd, int flags, void *arg)
>>>>    {
>>>> +    thermal_error_t ret = THERMAL_ERROR;
>>>>       struct nl_msg *msg;
>>>>       void *hdr;
>>>>
>>>>       msg = nlmsg_alloc();
>>>>       if (!msg)
>>>> -            return THERMAL_ERROR;
>>>> +            goto out;
>>> …
>>>
>>> Is it really reasonable to pass a null pointer (from a failed function call)
>>> to a subsequent nlmsg_free() call?
>>
>> You are right, I should return here :S
> 
> Do you want to respin it?
> 
> Alternatively, I can fix it up when applying the patch.

The nlmskg_free() function should handle that similar to kfree().
Under the hood there is a check in skb_unref():
if (unlikely(!skb))

AFAIK the kfree() is used similar way and NULL is part of generic
case sometimes, not handles with extra code.

Up to you. You can keep my review tag in both cases.

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 13:18     ` Rafael J. Wysocki
  2024-10-24 13:27       ` Lukasz Luba
@ 2024-10-24 14:21       ` Daniel Lezcano
  2024-10-24 15:19         ` Markus Elfring
  1 sibling, 1 reply; 10+ messages in thread
From: Daniel Lezcano @ 2024-10-24 14:21 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Markus Elfring, linux-pm, Lukasz Luba, LKML, Zhang Rui

On 24/10/2024 15:18, Rafael J. Wysocki wrote:
> On Thu, Oct 24, 2024 at 2:57 PM Daniel Lezcano
> <daniel.lezcano@linaro.org> wrote:
>>
>> On 24/10/2024 14:02, Markus Elfring wrote:
>>>> The function thermal_genl_auto() does not free the allocated message
>>>> in the error path. Fix that by putting a out label and jump to it
>>>> which will free the message instead of directly returning an error.
>>>
>>> Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.12-rc4#n145
>>>
>>>
>>> …
>>>> +++ b/tools/lib/thermal/commands.c
>>>> @@ -375,27 +375,30 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cm
>>>>                                        struct cmd_param *param,
>>>>                                        int cmd, int flags, void *arg)
>>>>    {
>>>> +    thermal_error_t ret = THERMAL_ERROR;
>>>>       struct nl_msg *msg;
>>>>       void *hdr;
>>>>
>>>>       msg = nlmsg_alloc();
>>>>       if (!msg)
>>>> -            return THERMAL_ERROR;
>>>> +            goto out;
>>> …
>>>
>>> Is it really reasonable to pass a null pointer (from a failed function call)
>>> to a subsequent nlmsg_free() call?
>>
>> You are right, I should return here :S
> 
> Do you want to respin it?
> 
> Alternatively, I can fix it up when applying the patch.

If you don't mind I would prefer to apply the lib patches

For correctness, I'll send a V2 with the return fixed


-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 14:21       ` Daniel Lezcano
@ 2024-10-24 15:19         ` Markus Elfring
  2024-10-24 16:54           ` Daniel Lezcano
  0 siblings, 1 reply; 10+ messages in thread
From: Markus Elfring @ 2024-10-24 15:19 UTC (permalink / raw)
  To: Daniel Lezcano, Rafael J. Wysocki, linux-pm; +Cc: LKML, Lukasz Luba, Zhang Rui

> For correctness, I'll send a V2 with the return fixed

Would you become similarly interested to increase the application of
scope-based resource management also for the affected software component?

Regards,
Markus

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

* Re: [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto()
  2024-10-24 15:19         ` Markus Elfring
@ 2024-10-24 16:54           ` Daniel Lezcano
  0 siblings, 0 replies; 10+ messages in thread
From: Daniel Lezcano @ 2024-10-24 16:54 UTC (permalink / raw)
  To: Markus Elfring, Rafael J. Wysocki, linux-pm; +Cc: LKML, Lukasz Luba, Zhang Rui

On 24/10/2024 17:19, Markus Elfring wrote:
>> For correctness, I'll send a V2 with the return fixed
> 
> Would you become similarly interested to increase the application of
> scope-based resource management also for the affected software component?

Yes, I know it exist but I did not take the time to read the 
documentation, neither had the reflex to take care of that. Something I 
have to do :)

It seems like this function is a good candidate :)

-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

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

end of thread, other threads:[~2024-10-24 16:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-24 10:59 [PATCH] thermal/lib: Fix memory leak on error in thermal_genl_auto() Daniel Lezcano
2024-10-24 11:01 ` Daniel Lezcano
2024-10-24 11:07 ` Lukasz Luba
2024-10-24 12:02 ` Markus Elfring
2024-10-24 12:57   ` Daniel Lezcano
2024-10-24 13:18     ` Rafael J. Wysocki
2024-10-24 13:27       ` Lukasz Luba
2024-10-24 14:21       ` Daniel Lezcano
2024-10-24 15:19         ` Markus Elfring
2024-10-24 16:54           ` Daniel Lezcano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox