clk: davinci: Add NULL check in davinci_lpsc_clk_register()
Subsystems (2)
PATCH 1/1 clk: davinci: Add NULL check in davinci_lpsc_clk_register()
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.
--- drivers/clk/davinci/psc.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/clk/davinci/psc.c +5 -0
@@ -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 ;
Signature · 1 line
2.34.1
Quoted 10 lines · 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 <[email protected]>---
Quoted 1 line
devm_kasprintf()return NULL if memory allocation fails. Currently,
…
call? failed?
Quoted 1 line
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
David Lechner
maintainerQuoted 5 lines · Markus Elfring
Quoted 1 line
devm_kasprintf()return NULL if memory allocation fails. Currently,…
call? failed?
Quoted 7 lines
Quoted 1 line
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.
Quoted 3 lines
* 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.
Quoted 4 lines
Regards,
Markus
Quoted 7 lines
Quoted 5 lines
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.
Quoted 4 lines
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.
Quoted 4 lines
Quoted 2 lines
* 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#L472Not 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
David Lechner
maintainerQuoted 4 lines · Markus Elfring
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.
Quoted 3 lines
Regards,
Markus
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
Quoted 33 lines · Henry Martin <[email protected]> 于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 <[email protected]>--- drivers/clk/davinci/psc.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/clk/davinci/psc.c +5
-1@@ -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
Quoting Henry Martin (2025-04-01 06:13:41)
Quoted 10 lines
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 <[email protected]>---
Applied to clk-next