clk: check memory returned by {devm_}kasprintf()
Subsystems (6)
cover clk: check memory returned by {devm_}kasprintf()
Hi,
While browsing some code I noticed that there are places where pointer returned by devm_kasprintf() or kasprintf() is not checked. Thus I've tooked the chance and fixed this (by updating kmerr.cocci script, changes published at [1]). Along with it some other places where resources may need to be freed on failure paths were updated.
Thank you,
Claudiu Beznea
[1] https://lore.kernel.org/all/[email protected]/
Claudiu Beznea (8):
clk: vc5: check memory returned by kasprintf()
clk: cdce925: check return value of kasprintf()
clk: si5341: return error if one synth clock registration fails
clk: si5341: check return value of {devm_}kasprintf()
clk: si5341: free unused memory on probe failure
clk: keystone: sci-clk: check return value of kasprintf()
clk: ti: clkctrl: check return value of kasprintf()
clk: clocking-wizard: check return value of devm_kasprintf()
drivers/clk/clk-cdce925.c | 12 +++++++ drivers/clk/clk-si5341.c | 38 +++++++++++++--------- drivers/clk/clk-versaclock5.c | 29 +++++++++++++++++ drivers/clk/keystone/sci-clk.c | 2 ++ drivers/clk/ti/clkctrl.c | 7 ++++ drivers/clk/xilinx/clk-xlnx-clock-wizard.c | 5 +++ 6 files changed, 78 insertions(+), 15 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/8 clk: vc5: check memory returned by kasprintf()
kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).
--- drivers/clk/clk-versaclock5.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
drivers/clk/clk-versaclock5.c +29 -0
@@ -1028,6 +1028,11 @@ static int vc5_probe(struct i2c_client *client) } init.name =kasprintf (GFP_KERNEL ,"%pOFn.mux" , client->dev.of_node); +if (!init.name) { + ret = -ENOMEM ; + goto err_clk; + } + init.ops = &vc5_mux_ops; init.flags =0 ; init.parent_names = parent_names;
@@ -1042,6 +1047,10 @@ static int vc5_probe(struct i2c_client *client)memset (&init,0 ,sizeof (init)); init.name =kasprintf (GFP_KERNEL ,"%pOFn.dbl" , client->dev.of_node); +if (!init.name) { + ret = -ENOMEM ; + goto err_clk; + } init.ops = &vc5_dbl_ops; init.flags =CLK_SET_RATE_PARENT ; init.parent_names = parent_names;
@@ -1057,6 +1066,10 @@ static int vc5_probe(struct i2c_client *client)/* Register PFD */ memset (&init,0 ,sizeof (init)); init.name =kasprintf (GFP_KERNEL ,"%pOFn.pfd" , client->dev.of_node); +if (!init.name) { + ret = -ENOMEM ; + goto err_clk; + } init.ops = &vc5_pfd_ops; init.flags =CLK_SET_RATE_PARENT ; init.parent_names = parent_names;
@@ -1074,6 +1087,10 @@ static int vc5_probe(struct i2c_client *client)/* Register PLL */ memset (&init,0 ,sizeof (init)); init.name =kasprintf (GFP_KERNEL ,"%pOFn.pll" , client->dev.of_node); +if (!init.name) { + ret = -ENOMEM ; + goto err_clk; + } init.ops = &vc5_pll_ops; init.flags =CLK_SET_RATE_PARENT ; init.parent_names = parent_names;
@@ -1093,6 +1110,10 @@ static int vc5_probe(struct i2c_client *client)memset (&init,0 ,sizeof (init)); init.name =kasprintf (GFP_KERNEL ,"%pOFn.fod%d" , client->dev.of_node, idx); +if (!init.name) { + ret = -ENOMEM ; + goto err_clk; + } init.ops = &vc5_fod_ops; init.flags =CLK_SET_RATE_PARENT ; init.parent_names = parent_names;
@@ -1111,6 +1132,10 @@ static int vc5_probe(struct i2c_client *client)memset (&init,0 ,sizeof (init)); init.name =kasprintf (GFP_KERNEL ,"%pOFn.out0_sel_i2cb" , client->dev.of_node); +if (!init.name) { + ret = -ENOMEM ; + goto err_clk; + } init.ops = &vc5_clk_out_ops; init.flags =CLK_SET_RATE_PARENT ; init.parent_names = parent_names;
@@ -1137,6 +1162,10 @@ static int vc5_probe(struct i2c_client *client)memset (&init,0 ,sizeof (init)); init.name =kasprintf (GFP_KERNEL ,"%pOFn.out%d" , client->dev.of_node, idx +1 ); +if (!init.name) { + ret = -ENOMEM ; + goto err_clk; + } init.ops = &vc5_clk_out_ops; init.flags =CLK_SET_RATE_PARENT ; init.parent_names = parent_names;
Signature · 1 line
2.34.1
Re: [PATCH 1/8] clk: vc5: check memory returned by kasprintf()
Hi Claudiu,
Quoted 6 lines · On Tue, 30 May 2023 12:39:06 +0300 Claudiu Beznea…
kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).Fixes f491276a5168 ("clk: vc5: Allow Versaclock driver to support multiple instances")Signed-off-by Claudiu Beznea <[email protected]>
I think this driver would benefit a bit of refactoring, moving all those similar sections to a single function.
But for the time being your change is fine:
Signature · 3 lines
Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Re: [PATCH 1/8] clk: vc5: check memory returned by kasprintf()
In reply to Claudiu BezneaQuoting Claudiu Beznea (2023-05-30 02:39:06)
Quoted 7 lines
kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).Fixes f491276a5168 ("clk: vc5: Allow Versaclock driver to support multiple instances")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
PATCH 2/8 clk: cdce925: check return value of kasprintf()
In reply to Claudiu Bezneakasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).
Depends-on: e665f029a283 ("clk: Convert to using %pOFn instead of device_node.name")
--- drivers/clk/clk-cdce925.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/clk/clk-cdce925.c +12 -0
@@ -701,6 +701,10 @@ static int cdce925_probe(struct i2c_client *client)for (i =0 ; i < data->chip_info->num_plls; ++i) { pll_clk_name[i] =kasprintf (GFP_KERNEL ,"%pOFn.pll%d" , client->dev.of_node, i); +if (!pll_clk_name[i]) { + err = -ENOMEM ; + goto error; + } init.name = pll_clk_name[i]; data->pll[i].chip = data; data->pll[i].hw.init = &init;
@@ -742,6 +746,10 @@ static int cdce925_probe(struct i2c_client *client) init.num_parents =1 ; init.parent_names = &parent_name;/* Mux Y1 to input */ init.name =kasprintf (GFP_KERNEL ,"%pOFn.Y1" , client->dev.of_node); +if (!init.name) { + err = -ENOMEM ; + goto error; + } data->clk[0 ].chip = data; data->clk[0 ].hw.init = &init; data->clk[0 ].index =0 ;
@@ -760,6 +768,10 @@ static int cdce925_probe(struct i2c_client *client)for (i =1 ; i < data->chip_info->num_outputs; ++i) { init.name =kasprintf (GFP_KERNEL ,"%pOFn.Y%d" , client->dev.of_node, i+1 ); +if (!init.name) { + err = -ENOMEM ; + goto error; + } data->clk[i].chip = data; data->clk[i].hw.init = &init; data->clk[i].index = i;
Signature · 1 line
2.34.1
Re: [PATCH 2/8] clk: cdce925: check return value of kasprintf()
Quoting Claudiu Beznea (2023-05-30 02:39:07)
Quoted 8 lines
kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).Fixes 19fbbbbcd3a3 ("Add TI CDCE925 I2C controlled clock synthesizer driver")Depends-on:
e665f029a283("clk: Convert to using %pOFn instead of device_node.name")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
PATCH 3/8 clk: si5341: return error if one synth clock registration fails
In reply to Claudiu BezneaIn case devm_clk_hw_register() fails for one of synth clocks the probe continues. Later on, when registering output clocks which have as parents all the synth clocks, in case there is registration failure for at least one synth clock the information passed to clk core for registering output clock is not right: init.num_parents is fixed but init.parents may contain an array with less parents.
--- drivers/clk/clk-si5341.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/clk/clk-si5341.c +8 -6
@@ -1553,7 +1553,7 @@ static int si5341_probe(struct i2c_client *client)struct clk_init_data init;struct clk *input;const char *root_clock_name; -const char *synth_clock_names[SI5341_NUM_SYNTH ]; +const char *synth_clock_names[SI5341_NUM_SYNTH ] = {NULL };int err;unsigned int i;struct clk_si5341_output_config config[SI5341_MAX_NUM_OUTPUTS ];
@@ -1705,6 +1705,7 @@ static int si5341_probe(struct i2c_client *client)if (err) {dev_err (&client->dev,"synth N%u registration failed\n" , i); + goto free_clk_names; } }
@@ -1782,16 +1783,17 @@ static int si5341_probe(struct i2c_client *client) goto cleanup; } +free_clk_names:/* Free the names, clk framework makes copies */ for (i =0 ; i < data->num_synth; ++i)devm_kfree (&client->dev, (void *)synth_clock_names[i]); -return 0 ; - cleanup: -for (i =0 ; i <SI5341_MAX_NUM_OUTPUTS ; ++i) { -if (data->clk[i].vddo_reg) -regulator_disable (data->clk[i].vddo_reg); +if (err) { +for (i =0 ; i <SI5341_MAX_NUM_OUTPUTS ; ++i) { +if (data->clk[i].vddo_reg) +regulator_disable (data->clk[i].vddo_reg); + } }return err; }
Signature · 1 line
2.34.1
Re: [PATCH 3/8] clk: si5341: return error if one synth clock registration fails
Quoting Claudiu Beznea (2023-05-30 02:39:08)
Quoted 10 lines
In case
devm_clk_hw_register()fails for one of synth clocks the probe continues. Later on, when registering output clocks which have as parents all the synth clocks, in case there is registration failure for at least one synth clock the information passed to clk core for registering output clock is not right: init.num_parents is fixed but init.parents may contain an array with less parents.Fixes 3044a860fd09 ("clk: Add Si5341/Si5340 driver")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
PATCH 4/8 clk: si5341: check return value of {devm_}kasprintf()
In reply to Claudiu Beznea{devm_}kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).
--- drivers/clk/clk-si5341.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/clk/clk-si5341.c +8 -0
@@ -1697,6 +1697,10 @@ static int si5341_probe(struct i2c_client *client)for (i =0 ; i < data->num_synth; ++i) { synth_clock_names[i] =devm_kasprintf (&client->dev,GFP_KERNEL ,"%s.N%u" , client->dev.of_node->name, i); +if (!synth_clock_names[i]) { + err = -ENOMEM ; + goto free_clk_names; + } init.name = synth_clock_names[i]; data->synth[i].index = i; data->synth[i].data = data;
@@ -1715,6 +1719,10 @@ static int si5341_probe(struct i2c_client *client)for (i =0 ; i < data->num_outputs; ++i) { init.name =kasprintf (GFP_KERNEL ,"%s.%d" , client->dev.of_node->name, i); +if (!init.name) { + err = -ENOMEM ; + goto free_clk_names; + } init.flags = config[i].synth_master ?CLK_SET_RATE_PARENT :0 ; data->clk[i].index = i; data->clk[i].data = data;
Signature · 1 line
2.34.1
Re: [PATCH 4/8] clk: si5341: check return value of {devm_}kasprintf()
Quoting Claudiu Beznea (2023-05-30 02:39:09)
Quoted 7 lines
{devm_}kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (
kmerr.cocciscript).Fixes 3044a860fd09 ("clk: Add Si5341/Si5340 driver")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
PATCH 5/8 clk: si5341: free unused memory on probe failure
In reply to Claudiu BezneaPointers from synth_clock_names[] should be freed at the end of probe either on probe success or failure path.
--- drivers/clk/clk-si5341.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/clk/clk-si5341.c +7 -9
@@ -1744,7 +1744,7 @@ static int si5341_probe(struct i2c_client *client)if (err) {dev_err (&client->dev,"output %u registration failed\n" , i); - goto cleanup; + goto free_clk_names; }if (config[i].always_on)clk_prepare (data->clk[i].hw.clk);
@@ -1754,7 +1754,7 @@ static int si5341_probe(struct i2c_client *client) data);if (err) {dev_err (&client->dev,"unable to add clk provider\n" ); - goto cleanup; + goto free_clk_names; }if (initialization_required) {
@@ -1762,11 +1762,11 @@ static int si5341_probe(struct i2c_client *client)regcache_cache_only (data->regmap, false); err =regcache_sync (data->regmap);if (err <0 ) - goto cleanup; + goto free_clk_names; err =si5341_finalize_defaults (data);if (err <0 ) - goto cleanup; + goto free_clk_names; }/* wait for device to report input clock present and PLL lock */
@@ -1775,21 +1775,19 @@ static int si5341_probe(struct i2c_client *client) 10000, 250000);if (err) {dev_err (&client->dev,"Error waiting for input clock or PLL lock\n" ); - goto cleanup; + goto free_clk_names; }/* clear sticky alarm bits from initialization */ err =regmap_write (data->regmap,SI5341_STATUS_STICKY ,0 );if (err) {dev_err (&client->dev,"unable to clear sticky status\n" ); - goto cleanup; + goto free_clk_names; } err =sysfs_create_files (&client->dev.kobj, si5341_attributes); -if (err) { +if (err)dev_err (&client->dev,"unable to create sysfs files\n" ); - goto cleanup; - } free_clk_names:/* Free the names, clk framework makes copies */
Signature · 1 line
2.34.1
Re: [PATCH 5/8] clk: si5341: free unused memory on probe failure
Quoting Claudiu Beznea (2023-05-30 02:39:10)
Quoted 7 lines
Pointers from synth_clock_names[] should be freed at the end of probe either on probe success or failure path.
Fixes b7bbf6ec4940 ("clk: si5341: Allow different output VDD_SEL values")Fixes 9b13ff4340df ("clk: si5341: Add sysfs properties to allow checking/resetting device faults")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
PATCH 6/8 clk: keystone: sci-clk: check return value of kasprintf()
In reply to Claudiu Bezneakasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).
Depends-on: 96488c09b0f4 ("clk: keystone: sci-clk: cut down the clock name length")
--- drivers/clk/keystone/sci-clk.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/clk/keystone/sci-clk.c +2 -0
@@ -294,6 +294,8 @@ static int _sci_clk_build(struct sci_clk_provider *provider, name =kasprintf (GFP_KERNEL ,"clk:%d:%d" , sci_clk->dev_id, sci_clk->clk_id); +if (!name) +return -ENOMEM ; init.name = name;
Signature · 7 lines
2.34.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 6/8] clk: keystone: sci-clk: check return value of kasprintf()
Quoted 3 lines · Claudiu Beznea
kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).
Re: [PATCH 6/8] clk: keystone: sci-clk: check return value of kasprintf()
In reply to Claudiu BezneaQuoting Claudiu Beznea (2023-05-30 02:39:11)
Quoted 8 lines
kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).Fixes b745c0794e2f ("clk: keystone: Add sci-clk driver support")Depends-on:
96488c09b0f4("clk: keystone: sci-clk: cut down the clock name length")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
PATCH 7/8 clk: ti: clkctrl: check return value of kasprintf()
In reply to Claudiu Bezneakasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).
--- drivers/clk/ti/clkctrl.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/clk/ti/clkctrl.c +7 -0
@@ -258,6 +258,9 @@ static const char * __init clkctrl_get_clock_name(struct device_node *np,if (clkctrl_name && !legacy_naming) { clock_name =kasprintf (GFP_KERNEL ,"%s-clkctrl:%04x:%d" , clkctrl_name, offset, index); +if (!clock_name) +return NULL ; +strreplace (clock_name,'_' ,'-' );return clock_name;
@@ -586,6 +589,10 @@ static void __init _ti_omap4_clkctrl_setup(struct device_node *node)if (clkctrl_name) { provider->clkdm_name =kasprintf (GFP_KERNEL ,"%s_clkdm" , clkctrl_name); +if (!provider->clkdm_name) { +kfree (provider); +return ; + } goto clkdm_found; }
Signature · 7 lines
2.34.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 7/8] clk: ti: clkctrl: check return value of kasprintf()
Quoted 3 lines · Claudiu Beznea
kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).
Re: [PATCH 7/8] clk: ti: clkctrl: check return value of kasprintf()
In reply to Claudiu BezneaQuoting Claudiu Beznea (2023-05-30 02:39:12)
Quoted 8 lines
kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).Fixes 852049594b9a ("clk: ti: clkctrl: convert subclocks to use proper names also")Fixes 6c3090520554 ("clk: ti: clkctrl: Fix hidden dependency to node name")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 8/8 clk: clocking-wizard: check return value of devm_kasprintf()
In reply to Claudiu Bezneadevm_kasprintf() returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocci script).
--- drivers/clk/xilinx/clk-xlnx-clock-wizard.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/clk/xilinx/clk-xlnx-clock-wizard.c +5 -0
@@ -648,6 +648,11 @@ static int clk_wzrd_probe(struct platform_device *pdev) } clkout_name =devm_kasprintf (&pdev->dev,GFP_KERNEL ,"%s_out0" ,dev_name (&pdev->dev)); +if (!clkout_name) { + ret = -ENOMEM ; + goto err_disable_clk; + } +if (nr_outputs ==1 ) { clk_wzrd->clkout[0 ] = clk_wzrd_register_divider (&pdev->dev, clkout_name,
Signature · 7 lines
2.34.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 8/8] clk: clocking-wizard: check return value of devm_kasprintf()
Quoting Claudiu Beznea (2023-05-30 02:39:13)
Quoted 7 lines
devm_kasprintf()returns a pointer to dynamically allocated memory. Pointer could be NULL in case allocation fails. Check pointer validity. Identified with coccinelle (kmerr.cocciscript).Fixes 2046338dcbc6 ("ARM: mxs: Use soc bus infrastructure")Signed-off-by Claudiu Beznea <[email protected]>---
Applied to clk-next
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel