public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register()
@ 2025-04-01 13:13 Henry Martin
  2025-04-01 14:05 ` David Lechner
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Henry Martin @ 2025-04-01 13:13 UTC (permalink / raw)
  To: david, mturquette, sboyd; +Cc: linux-clk, linux-kernel, Henry Martin

devm_kasprintf() returns NULL when memory allocation fails. Currently,
davinci_lpsc_clk_register() does not check for this case, which results
in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue and ensuring
no resources are left allocated.

Fixes: c6ed4d734bc7 ("clk: davinci: New driver for davinci PSC clocks")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 drivers/clk/davinci/psc.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/clk/davinci/psc.c b/drivers/clk/davinci/psc.c
index b48322176c21..f3ee9397bb0c 100644
--- a/drivers/clk/davinci/psc.c
+++ b/drivers/clk/davinci/psc.c
@@ -277,6 +277,11 @@ davinci_lpsc_clk_register(struct device *dev, const char *name,
 
 	lpsc->pm_domain.name = devm_kasprintf(dev, GFP_KERNEL, "%s: %s",
 					      best_dev_name(dev), name);
+	if (!lpsc->pm_domain.name) {
+		clk_hw_unregister(&lpsc->hw);
+		kfree(lpsc);
+		return ERR_PTR(-ENOMEM);
+	}
 	lpsc->pm_domain.attach_dev = davinci_psc_genpd_attach_dev;
 	lpsc->pm_domain.detach_dev = davinci_psc_genpd_detach_dev;
 	lpsc->pm_domain.flags = GENPD_FLAG_PM_CLK;
-- 
2.34.1


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

* Re: [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register()
  2025-04-01 13:13 [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register() Henry Martin
@ 2025-04-01 14:05 ` David Lechner
  2025-04-01 16:46 ` [PATCH] " Markus Elfring
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: David Lechner @ 2025-04-01 14:05 UTC (permalink / raw)
  To: Henry Martin, mturquette, sboyd; +Cc: linux-clk, linux-kernel

On 4/1/25 8:13 AM, Henry Martin wrote:
> devm_kasprintf() returns NULL when memory allocation fails. Currently,
> davinci_lpsc_clk_register() does not check for this case, which results
> in a NULL pointer dereference.
> 
> Add NULL check after devm_kasprintf() to prevent this issue and ensuring
> no resources are left allocated.
> 
> Fixes: c6ed4d734bc7 ("clk: davinci: New driver for davinci PSC clocks")
> Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> ---

Reviewed-by: David Lechner <david@lechnology.com>


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

* Re: [PATCH] clk: davinci: Add NULL check in davinci_lpsc_clk_register()
  2025-04-01 13:13 [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register() Henry Martin
  2025-04-01 14:05 ` David Lechner
@ 2025-04-01 16:46 ` Markus Elfring
  2025-04-01 17:21   ` David Lechner
  2025-04-24  3:14 ` [PATCH v1] " henry martin
  2025-06-19 23:49 ` Stephen Boyd
  3 siblings, 1 reply; 8+ messages in thread
From: Markus Elfring @ 2025-04-01 16:46 UTC (permalink / raw)
  To: Henry Martin, linux-clk
  Cc: LKML, David Lechner, Michael Turquette, Stephen Boyd

> devm_kasprintf() return NULL if memory allocation fails. Currently,
…
                call?                               failed?


> Add NULL check after devm_kasprintf() to prevent this issue.

I propose to avoid duplicate source code also for the completion of
the corresponding exception handling.

* You may avoid repeated function calls by using another label instead.
  https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources#MEM12C.Considerusingagotochainwhenleavingafunctiononerrorwhenusingandreleasingresources-CompliantSolution(copy_process()fromLinuxkernel)

* How do you think about to benefit any more from the application of the attribute “__free”?
  https://elixir.bootlin.com/linux/v6.14-rc6/source/include/linux/slab.h#L472


Regards,
Markus

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

* Re: [PATCH] clk: davinci: Add NULL check in davinci_lpsc_clk_register()
  2025-04-01 16:46 ` [PATCH] " Markus Elfring
@ 2025-04-01 17:21   ` David Lechner
  2025-04-02  7:48     ` Markus Elfring
  0 siblings, 1 reply; 8+ messages in thread
From: David Lechner @ 2025-04-01 17:21 UTC (permalink / raw)
  To: Markus Elfring, Henry Martin, linux-clk
  Cc: LKML, Michael Turquette, Stephen Boyd

On 4/1/25 11:46 AM, Markus Elfring wrote:
>> devm_kasprintf() return NULL if memory allocation fails. Currently,
> …
>                 call?                               failed?
> 
> 
>> Add NULL check after devm_kasprintf() to prevent this issue.
> 
> I propose to avoid duplicate source code also for the completion of
> the corresponding exception handling.
> 
> * You may avoid repeated function calls by using another label instead.
>   https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources#MEM12C.Considerusingagotochainwhenleavingafunctiononerrorwhenusingandreleasingresources-CompliantSolution(copy_process()fromLinuxkernel)

That would be OK too. I didn't worry about it in this case though
since we are only duplicating 1 very short line of code. And the
smaller diff has a better chance of successfully backporting to older
stable kernels that will also pick up this patch.

> 
> * How do you think about to benefit any more from the application of the attribute “__free”?
>   https://elixir.bootlin.com/linux/v6.14-rc6/source/include/linux/slab.h#L472

Not a good fit for this specific use case.

> 
> 
> Regards,
> Markus


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

* Re: clk: davinci: Add NULL check in davinci_lpsc_clk_register()
  2025-04-01 17:21   ` David Lechner
@ 2025-04-02  7:48     ` Markus Elfring
  2025-04-02 14:05       ` David Lechner
  0 siblings, 1 reply; 8+ messages in thread
From: Markus Elfring @ 2025-04-02  7:48 UTC (permalink / raw)
  To: David Lechner, Henry Martin, linux-clk
  Cc: LKML, Michael Turquette, Stephen Boyd

>> I propose to avoid duplicate source code also for the completion of
>> the corresponding exception handling.
>>
>> * You may avoid repeated function calls by using another label instead.
>>   https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources#MEM12C.Considerusingagotochainwhenleavingafunctiononerrorwhenusingandreleasingresources-CompliantSolution(copy_process()fromLinuxkernel)
>
> That would be OK too.

Thanks for another bit of positive feedback.


>                       I didn't worry about it in this case though
> since we are only duplicating 1 very short line of code. And the
> smaller diff has a better chance of successfully backporting to older
> stable kernels that will also pick up this patch.

Such development concerns can be clarified further.


>> * How do you think about to benefit any more from the application of the attribute “__free”?
>>   https://elixir.bootlin.com/linux/v6.14-rc6/source/include/linux/slab.h#L472
>
> Not a good fit for this specific use case.

The acceptance might be growing for such a software design option.


Would you like to clarify any more why the function “kzalloc” is still called here
(instead of the variant “devm_kzalloc”)?
https://elixir.bootlin.com/linux/v6.14-rc6/source/drivers/clk/davinci/psc.c#L242

Regards,
Markus

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

* Re: clk: davinci: Add NULL check in davinci_lpsc_clk_register()
  2025-04-02  7:48     ` Markus Elfring
@ 2025-04-02 14:05       ` David Lechner
  0 siblings, 0 replies; 8+ messages in thread
From: David Lechner @ 2025-04-02 14:05 UTC (permalink / raw)
  To: Markus Elfring, Henry Martin, linux-clk
  Cc: LKML, Michael Turquette, Stephen Boyd

On 4/2/25 2:48 AM, Markus Elfring wrote:

> 
> Would you like to clarify any more why the function “kzalloc” is still called here
> (instead of the variant “devm_kzalloc”)?
> https://elixir.bootlin.com/linux/v6.14-rc6/source/drivers/clk/davinci/psc.c#L242

It is a case of "if it isn't broke, don't fix it". While there is room for
some small improvements like that, it does come at a cost of time and energy
to make those improvements.

> 
> Regards,
> Markus


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

* Re: [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register()
  2025-04-01 13:13 [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register() Henry Martin
  2025-04-01 14:05 ` David Lechner
  2025-04-01 16:46 ` [PATCH] " Markus Elfring
@ 2025-04-24  3:14 ` henry martin
  2025-06-19 23:49 ` Stephen Boyd
  3 siblings, 0 replies; 8+ messages in thread
From: henry martin @ 2025-04-24  3:14 UTC (permalink / raw)
  To: mturquette, sboyd; +Cc: linux-clk, linux-kernel, david

Hi Michael, Stephen,

I wanted to follow up on my previous patch submission to check if there are any
additional feedback or changes you'd like me to address. If so, I’d be happy to
incorporate them and send a v2.

Regards,
Henry

Henry Martin <bsdhenrymartin@gmail.com> 于2025年4月1日周二 21:13写道:
>
> devm_kasprintf() returns NULL when memory allocation fails. Currently,
> davinci_lpsc_clk_register() does not check for this case, which results
> in a NULL pointer dereference.
>
> Add NULL check after devm_kasprintf() to prevent this issue and ensuring
> no resources are left allocated.
>
> Fixes: c6ed4d734bc7 ("clk: davinci: New driver for davinci PSC clocks")
> Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> ---
>  drivers/clk/davinci/psc.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/drivers/clk/davinci/psc.c b/drivers/clk/davinci/psc.c
> index b48322176c21..f3ee9397bb0c 100644
> --- a/drivers/clk/davinci/psc.c
> +++ b/drivers/clk/davinci/psc.c
> @@ -277,6 +277,11 @@ davinci_lpsc_clk_register(struct device *dev, const char *name,
>
>         lpsc->pm_domain.name = devm_kasprintf(dev, GFP_KERNEL, "%s: %s",
>                                               best_dev_name(dev), name);
> +       if (!lpsc->pm_domain.name) {
> +               clk_hw_unregister(&lpsc->hw);
> +               kfree(lpsc);
> +               return ERR_PTR(-ENOMEM);
> +       }
>         lpsc->pm_domain.attach_dev = davinci_psc_genpd_attach_dev;
>         lpsc->pm_domain.detach_dev = davinci_psc_genpd_detach_dev;
>         lpsc->pm_domain.flags = GENPD_FLAG_PM_CLK;
> --
> 2.34.1
>

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

* Re: [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register()
  2025-04-01 13:13 [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register() Henry Martin
                   ` (2 preceding siblings ...)
  2025-04-24  3:14 ` [PATCH v1] " henry martin
@ 2025-06-19 23:49 ` Stephen Boyd
  3 siblings, 0 replies; 8+ messages in thread
From: Stephen Boyd @ 2025-06-19 23:49 UTC (permalink / raw)
  To: Henry Martin, david, mturquette; +Cc: linux-clk, linux-kernel, Henry Martin

Quoting Henry Martin (2025-04-01 06:13:41)
> devm_kasprintf() returns NULL when memory allocation fails. Currently,
> davinci_lpsc_clk_register() does not check for this case, which results
> in a NULL pointer dereference.
> 
> Add NULL check after devm_kasprintf() to prevent this issue and ensuring
> no resources are left allocated.
> 
> Fixes: c6ed4d734bc7 ("clk: davinci: New driver for davinci PSC clocks")
> Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> ---

Applied to clk-next

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

end of thread, other threads:[~2025-06-19 23:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01 13:13 [PATCH v1] clk: davinci: Add NULL check in davinci_lpsc_clk_register() Henry Martin
2025-04-01 14:05 ` David Lechner
2025-04-01 16:46 ` [PATCH] " Markus Elfring
2025-04-01 17:21   ` David Lechner
2025-04-02  7:48     ` Markus Elfring
2025-04-02 14:05       ` David Lechner
2025-04-24  3:14 ` [PATCH v1] " henry martin
2025-06-19 23:49 ` Stephen Boyd

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