Staging: clocking-wizard: Use devm_kasprintf
PATCH 1/1 Staging: clocking-wizard: Use devm_kasprintf
Use devm_kasprintf instead of kasprintf and remove various gotos by direct returns. Also, drop labels err_rm_int_clks, err_rm_int_clk and err_disable_clk as they are not used anymore.
--- .../clocking-wizard/clk-xlnx-clock-wizard.c | 32 ++++++++-------------- 1 file changed, 11 insertions(+), 21 deletions(-)
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +11 -21
@@ -189,8 +189,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (rate >WZRD_ACLK_MAX_FREQ ) {dev_err (&pdev->dev,"s_axi_aclk frequency (%lu) too high\n" , rate); - ret = -EINVAL ; - goto err_disable_clk; +return -EINVAL ; }/* we don't support fractional div/mul yet */
@@ -204,10 +203,10 @@ static int clk_wzrd_probe(struct platform_device *pdev)/* register multiplier */ reg = (readl (clk_wzrd->base +WZRD_CLK_CFG_REG (0 )) &WZRD_CLKFBOUT_MULT_MASK ) >>WZRD_CLKFBOUT_MULT_SHIFT ; - clk_name =kasprintf (GFP_KERNEL ,"%s_mul" ,dev_name (&pdev->dev)); + clk_name =devm_kasprintf (&pdev->dev,GFP_KERNEL ,"%s_mul" , +dev_name (&pdev->dev));if (!clk_name) { - ret = -ENOMEM ; - goto err_disable_clk; +return -ENOMEM ; } clk_wzrd->clks_internal[wzrd_clk_mul] = clk_register_fixed_factor( &pdev->dev, clk_name,
@@ -217,16 +216,16 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (IS_ERR (clk_wzrd->clks_internal[wzrd_clk_mul])) {dev_err (&pdev->dev,"unable to register fixed-factor clock\n" ); ret =PTR_ERR (clk_wzrd->clks_internal[wzrd_clk_mul]); - goto err_disable_clk; +return ret; }/* register div */ reg = (readl (clk_wzrd->base +WZRD_CLK_CFG_REG (0 )) &WZRD_DIVCLK_DIVIDE_MASK ) >>WZRD_DIVCLK_DIVIDE_SHIFT ; - clk_name =kasprintf (GFP_KERNEL ,"%s_mul_div" ,dev_name (&pdev->dev)); + clk_name =devm_kasprintf (&pdev->dev,GFP_KERNEL ,"%s_mul_div" , +dev_name (&pdev->dev));if (!clk_name) { - ret = -ENOMEM ; - goto err_rm_int_clk; +return -ENOMEM ; } clk_wzrd->clks_internal[wzrd_clk_mul_div] = clk_register_fixed_factor(
@@ -236,7 +235,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (IS_ERR (clk_wzrd->clks_internal[wzrd_clk_mul_div])) {dev_err (&pdev->dev,"unable to register divider clock\n" ); ret =PTR_ERR (clk_wzrd->clks_internal[wzrd_clk_mul_div]); - goto err_rm_int_clk; +return ret; }/* register div per output */
@@ -247,8 +246,7 @@ static int clk_wzrd_probe(struct platform_device *pdev) &clkout_name)) {dev_err (&pdev->dev,"clock output name not specified\n" ); - ret = -EINVAL ; - goto err_rm_int_clks; +return -EINVAL ; } reg =readl (clk_wzrd->base +WZRD_CLK_CFG_REG (2 ) + i *12 ); reg &=WZRD_CLKOUT_DIVIDE_MASK ;
@@ -263,7 +261,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)dev_err (&pdev->dev,"unable to register divider clock\n" ); ret =PTR_ERR (clk_wzrd->clkout[i]); - goto err_rm_int_clks; +return ret; } }
@@ -290,14 +288,6 @@ static int clk_wzrd_probe(struct platform_device *pdev)return 0 ; -err_rm_int_clks: -clk_unregister (clk_wzrd->clks_internal[1 ]); -err_rm_int_clk: -kfree (clk_name); -clk_unregister (clk_wzrd->clks_internal[0 ]); -err_disable_clk: -clk_disable_unprepare (clk_wzrd->axi_clk); -return ret; }
Signature · 1 line
2.1.4
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 300
Quoted 9 lines · On Saturday 03 October 2015 00:28:03 Shraddha Barke wrote:
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-8-err_rm_int_clks: -clk_unregister (clk_wzrd->clks_internal[1 ]); -err_rm_int_clk: -kfree (clk_name); -clk_unregister (clk_wzrd->clks_internal[0 ]); -err_disable_clk: -clk_disable_unprepare (clk_wzrd->axi_clk); -
Are you sure you can remove all of these?
Arnd
Quoted 14 lines · Shraddha Barke
Use
devm_kasprintfinstead of kasprintf and remove various gotos by direct returns. Also, drop labelserr_rm_int_clks,err_rm_int_clkanderr_disable_clkas they are not used anymore.Signed-off-by Shraddha Barke <[email protected]>--- .../clocking-wizard/clk-xlnx-clock-wizard.c | 32 ++++++++-------------- 1 file changed, 11 insertions(+), 21 deletions(-)drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-0
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 192
Quoted 6 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +1
-2@@ -189,8 +189,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (rate >WZRD_ACLK_MAX_FREQ ) {dev_err (&pdev->dev,"s_axi_aclk frequency (%lu) too high\n" , rate); - ret = -EINVAL ; - goto err_disable_clk; +return -EINVAL ;
Why do you make this change? On failing, once you have called clk_prepare_enable, you need to call clk_disable_unprepare.
Quoted 4 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-0}/* we don't support fractional div/mul yet */
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 209
Quoted 10 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +3
-3@@ -204,10 +203,10 @@ static int clk_wzrd_probe(struct platform_device *pdev)/* register multiplier */ reg = (readl (clk_wzrd->base +WZRD_CLK_CFG_REG (0 )) &WZRD_CLKFBOUT_MULT_MASK ) >>WZRD_CLKFBOUT_MULT_SHIFT ; - clk_name =kasprintf (GFP_KERNEL ,"%s_mul" ,dev_name (&pdev->dev)); + clk_name =devm_kasprintf (&pdev->dev,GFP_KERNEL ,"%s_mul" , +dev_name (&pdev->dev));if (!clk_name) { - ret = -ENOMEM ; - goto err_disable_clk; +return -ENOMEM ;
Likewise here. Changing kasprintf to devm_kasprintf doesn't have any impact on what needs to be done on failure of devm_kasprintf itself. It is only the subsequent failure paths that should be simplified, because they don't need to free the result of devm_kasprintf.
However, looking at the complete code, I see that the first call to kasprintf is followed by the following:
clk_wzrd->clks_internal[wzrd_clk_mul] = clk_register_fixed_factor(
&pdev->dev, clk_name,
__clk_get_name(clk_wzrd->clk_in1),
0, reg, 1);
kfree(clk_name);So in the original code, clk_name was free pretty much immediately, and in any case always in the probe function. By using the devm function, you have caused it to be only freed in the probe function if the probe function fails, and to otherwise be freed when the remove function succeeds. So you have probably greatly extended the lifetime of this data. It seems that it would be better to leave this part of the code as it is.
Quoted 12 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +1
-1} clk_wzrd->clks_internal[wzrd_clk_mul] = clk_register_fixed_factor( &pdev->dev, clk_name,@@ -217,16 +216,16 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (IS_ERR (clk_wzrd->clks_internal[wzrd_clk_mul])) {dev_err (&pdev->dev,"unable to register fixed-factor clock\n" ); ret =PTR_ERR (clk_wzrd->clks_internal[wzrd_clk_mul]); - goto err_disable_clk; +return ret; }/* register div */
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 229
Quoted 10 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +3
-3reg = (readl (clk_wzrd->base +WZRD_CLK_CFG_REG (0 )) &WZRD_DIVCLK_DIVIDE_MASK ) >>WZRD_DIVCLK_DIVIDE_SHIFT ; - clk_name =kasprintf (GFP_KERNEL ,"%s_mul_div" ,dev_name (&pdev->dev)); + clk_name =devm_kasprintf (&pdev->dev,GFP_KERNEL ,"%s_mul_div" , +dev_name (&pdev->dev));if (!clk_name) { - ret = -ENOMEM ; - goto err_rm_int_clk; +return -ENOMEM ; }
Actually, if one looks closely, there is also a kfree for this clk_name in the probe function. So this one shouldn't be changed either.
Quoted 31 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +3
-4clk_wzrd->clks_internal[wzrd_clk_mul_div] = clk_register_fixed_factor(@@ -236,7 +235,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (IS_ERR (clk_wzrd->clks_internal[wzrd_clk_mul_div])) {dev_err (&pdev->dev,"unable to register divider clock\n" ); ret =PTR_ERR (clk_wzrd->clks_internal[wzrd_clk_mul_div]); - goto err_rm_int_clk; +return ret; }/* register div per output */ @@ -247,8 +246,7 @@ static int clk_wzrd_probe(struct platform_device *pdev) &clkout_name)) {dev_err (&pdev->dev,"clock output name not specified\n" ); - ret = -EINVAL ; - goto err_rm_int_clks; +return -EINVAL ; } reg =readl (clk_wzrd->base +WZRD_CLK_CFG_REG (2 ) + i *12 ); reg &=WZRD_CLKOUT_DIVIDE_MASK ;@@ -263,7 +261,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)dev_err (&pdev->dev,"unable to register divider clock\n" ); ret =PTR_ERR (clk_wzrd->clkout[i]); - goto err_rm_int_clks; +return ret; } }@@ -290,14 +288,6 @@ static int clk_wzrd_probe(struct platform_device *pdev)
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 300
Quoted 10 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-8return 0 ; -err_rm_int_clks: -clk_unregister (clk_wzrd->clks_internal[1 ]); -err_rm_int_clk: -kfree (clk_name); -clk_unregister (clk_wzrd->clks_internal[0 ]); -err_disable_clk: -clk_disable_unprepare (clk_wzrd->axi_clk); -
Even if you could use devm_asprintf, you couldn't remove all of this. The devm_kasprintf only affects the management of clk_name.
julia
Quoted 13 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-0return ret; }Signature · 8 lines
2.1.4 -- You received this message because you are subscribed to the Google Groups "outreachy-kernel" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/1443812283-21310-1-git-send-email-shraddha.6596%40gmail.com. For more options, visit https://groups.google.com/d/optout.
Quoted 74 lines · Julia Lawall
Quoted 14 lines · Shraddha Barke
Use
devm_kasprintfinstead of kasprintf and remove various gotos by direct returns. Also, drop labelserr_rm_int_clks,err_rm_int_clkanderr_disable_clkas they are not used anymore.Signed-off-by Shraddha Barke <[email protected]>--- .../clocking-wizard/clk-xlnx-clock-wizard.c | 32 ++++++++-------------- 1 file changed, 11 insertions(+), 21 deletions(-)drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-0drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 192
Quoted 6 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +1
-2@@ -189,8 +189,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (rate >WZRD_ACLK_MAX_FREQ ) {dev_err (&pdev->dev,"s_axi_aclk frequency (%lu) too high\n" , rate); - ret = -EINVAL ; - goto err_disable_clk; +return -EINVAL ;Why do you make this change? On failing, once you have called
clk_prepare_enable, you need to callclk_disable_unprepare.Quoted 4 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-0}/* we don't support fractional div/mul yet */ drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 209
Quoted 10 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +3
-3@@ -204,10 +203,10 @@ static int clk_wzrd_probe(struct platform_device *pdev)/* register multiplier */ reg = (readl (clk_wzrd->base +WZRD_CLK_CFG_REG (0 )) &WZRD_CLKFBOUT_MULT_MASK ) >>WZRD_CLKFBOUT_MULT_SHIFT ; - clk_name =kasprintf (GFP_KERNEL ,"%s_mul" ,dev_name (&pdev->dev)); + clk_name =devm_kasprintf (&pdev->dev,GFP_KERNEL ,"%s_mul" , +dev_name (&pdev->dev));if (!clk_name) { - ret = -ENOMEM ; - goto err_disable_clk; +return -ENOMEM ;Likewise here. Changing kasprintf to
devm_kasprintfdoesn't have any impact on what needs to be done on failure ofdevm_kasprintfitself. It is only the subsequent failure paths that should be simplified, because they don't need to free the result ofdevm_kasprintf.However, looking at the complete code, I see that the first call to kasprintf is followed by the following:
clk_wzrd->clks_internal[wzrd_clk_mul] = clk_register_fixed_factor( &pdev->dev, clk_name, __clk_get_name(clk_wzrd->clk_in1), 0, reg, 1); kfree(clk_name);So in the original code,
clk_namewas free pretty much immediately, and in any case always in the probe function. By using the devm function, you have caused it to be only freed in the probe function if the probe function fails, and to otherwise be freed when the remove function succeeds. So you have probably greatly extended the lifetime of this data. It seems that it would be better to leave this part of the code as it is.Quoted 11 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +1
-1} clk_wzrd->clks_internal[wzrd_clk_mul] = clk_register_fixed_factor( &pdev->dev, clk_name,@@ -217,16 +216,16 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (IS_ERR (clk_wzrd->clks_internal[wzrd_clk_mul])) {dev_err (&pdev->dev,"unable to register fixed-factor clock\n" ); ret =PTR_ERR (clk_wzrd->clks_internal[wzrd_clk_mul]); - goto err_disable_clk; +return ret; }
Quoted 14 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 229
Quoted 11 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +3
-3/* register div */ reg = (readl (clk_wzrd->base +WZRD_CLK_CFG_REG (0 )) &WZRD_DIVCLK_DIVIDE_MASK ) >>WZRD_DIVCLK_DIVIDE_SHIFT ; - clk_name =kasprintf (GFP_KERNEL ,"%s_mul_div" ,dev_name (&pdev->dev)); + clk_name =devm_kasprintf (&pdev->dev,GFP_KERNEL ,"%s_mul_div" , +dev_name (&pdev->dev));if (!clk_name) { - ret = -ENOMEM ; - goto err_rm_int_clk; +return -ENOMEM ; }Actually, if one looks closely, there is also a kfree for this
clk_namein the probe function. So this one shouldn't be changed either.
Yes, I got it. I only just started learning about devm functions.
Shraddha
Quoted 62 lines
Quoted 32 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +3
-4clk_wzrd->clks_internal[wzrd_clk_mul_div] = clk_register_fixed_factor(@@ -236,7 +235,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)if (IS_ERR (clk_wzrd->clks_internal[wzrd_clk_mul_div])) {dev_err (&pdev->dev,"unable to register divider clock\n" ); ret =PTR_ERR (clk_wzrd->clks_internal[wzrd_clk_mul_div]); - goto err_rm_int_clk; +return ret; }/* register div per output */ @@ -247,8 +246,7 @@ static int clk_wzrd_probe(struct platform_device *pdev) &clkout_name)) {dev_err (&pdev->dev,"clock output name not specified\n" ); - ret = -EINVAL ; - goto err_rm_int_clks; +return -EINVAL ; } reg =readl (clk_wzrd->base +WZRD_CLK_CFG_REG (2 ) + i *12 ); reg &=WZRD_CLKOUT_DIVIDE_MASK ;@@ -263,7 +261,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)dev_err (&pdev->dev,"unable to register divider clock\n" ); ret =PTR_ERR (clk_wzrd->clkout[i]); - goto err_rm_int_clks; +return ret; } }@@ -290,14 +288,6 @@ static int clk_wzrd_probe(struct platform_device *pdev)drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c static int clk_wzrd_probe(struct platform_device *pdev) · line 300
Quoted 10 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-8return 0 ; -err_rm_int_clks: -clk_unregister (clk_wzrd->clks_internal[1 ]); -err_rm_int_clk: -kfree (clk_name); -clk_unregister (clk_wzrd->clks_internal[0 ]); -err_disable_clk: -clk_disable_unprepare (clk_wzrd->axi_clk); -Even if you could use
devm_asprintf, you couldn't remove all of this. Thedevm_kasprintfonly affects the management ofclk_name.julia
Quoted 13 lines
drivers/staging/clocking-wizard/clk-xlnx-clock-wizard.c +0
-1return ret; } --2.1.4
--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/1443812283-21310-1-git-send-email-shraddha.6596%40gmail.com.
For more options, visit https://groups.google.com/d/optout.