thermal/drivers/rcar: add error checking in probe()
PATCH 1/1 thermal/drivers/rcar: add error checking in probe()
The thermal_zone_device_register_with_trips() can fail for a number of reasons, including allocation failures. Check for error pointers to avoid an error pointer dereference.
--- drivers/thermal/renesas/rcar_thermal.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/thermal/renesas/rcar_thermal.c +5 -0
@@ -492,6 +492,11 @@ static int rcar_thermal_probe(struct platform_device *pdev)"rcar_thermal" , trips,ARRAY_SIZE (trips), priv, &rcar_thermal_zone_ops,NULL ,0 , idle); +if (IS_ERR (priv->zone)) { + ret =PTR_ERR (priv->zone); + priv->zone =NULL ; + goto error_unregister; + } ret =thermal_zone_device_enable (priv->zone);if (ret) {
Signature · 1 line
2.53.0
Hi Dan,
Thanks for your patch!
Quoted 5 lines · Dan Carpenter
The
thermal_zone_device_register_with_trips()can fail for a number of reasons, including allocation failures. Check for error pointers to avoid an error pointer dereference.Fixes 9d617949d490 ("thermal/drivers/renesas: Group all renesas thermal drivers together")
This is not the commit you are looking for...
thermal zone devices")
drivers/thermal/renesas/rcar_thermal.c static int rcar_thermal_probe(struct platform_device *pdev) · line 499
Quoted 13 lines
Signed-off-by Dan Carpenter <[email protected]>drivers/thermal/renesas/rcar_thermal.c +5
-0@@ -492,6 +492,11 @@ static int rcar_thermal_probe(struct platform_device *pdev)"rcar_thermal" , trips,ARRAY_SIZE (trips), priv, &rcar_thermal_zone_ops,NULL ,0 , idle); +if (IS_ERR (priv->zone)) { + ret =PTR_ERR (priv->zone); + priv->zone =NULL ; + goto error_unregister; + }
This check is already present below (out of context), so it would be good to avoid duplicating it.
drivers/thermal/renesas/rcar_thermal.c static int rcar_thermal_probe(struct platform_device *pdev) · line 502
Quoted 3 lines
drivers/thermal/renesas/rcar_thermal.c +0
-0ret =thermal_zone_device_enable (priv->zone);if (ret) {
The issue is that this call was added before the error checking is done. So this should be moved below instead (to the else branch of the next chip->use_of_thermal test?).
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Yeah, thanks Geert. You're right. I'll resend.
regards,
dan carpenter
Hi Dan,
Thanks for your work.
Quoted 5 lines · Dan Carpenter
The
thermal_zone_device_register_with_trips()can fail for a number of reasons, including allocation failures. Check for error pointers to avoid an error pointer dereference.Fixes 9d617949d490 ("thermal/drivers/renesas: Group all renesas thermal drivers together")
I don't think this is correct as this commits just moves the file.
Quoted 10 lines
Signed-off-by Dan Carpenter <[email protected]>--- drivers/thermal/renesas/rcar_thermal.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/thermal/renesas/rcar_thermal.c +0
-0
drivers/thermal/renesas/rcar_thermal.c static int rcar_thermal_probe(struct platform_device *pdev) · line 499
Quoted 8 lines
drivers/thermal/renesas/rcar_thermal.c +5
-0@@ -492,6 +492,11 @@ static int rcar_thermal_probe(struct platform_device *pdev)"rcar_thermal" , trips,ARRAY_SIZE (trips), priv, &rcar_thermal_zone_ops,NULL ,0 , idle); +if (IS_ERR (priv->zone)) { + ret =PTR_ERR (priv->zone); + priv->zone =NULL ; + goto error_unregister; + }
While this indeed is an issue that should be fixed I don't think this is the correct fix. Below the if .. else .. block where this is added there already is a check for IS_ERR(priv->zone). That however does not guard against the usage of priv->zone for thermal_zone_device_enable().
We should only call thermal_zone_device_enable() if we are on a system that uses OF (gated by chip->use_of_thermal) which is the reason for the if .. else .. block in the first place. As chance have it we also have a check on chip->use_of_thermal directly after the existing IS_ERR(priv->zone) check. I think it would be better to move the call to thermal_zone_device_enable() there and avoid having two checks for IS_ERR(priv->zone)? Something like this,
drivers/thermal/renesas/rcar_thermal.c +7 -8
@@ -492,12 +492,6 @@ static int rcar_thermal_probe(struct platform_device *pdev)"rcar_thermal" , trips,ARRAY_SIZE (trips), priv, &rcar_thermal_zone_ops,NULL ,0 , idle); - - ret =thermal_zone_device_enable (priv->zone); -if (ret) { -thermal_zone_device_unregister (priv->zone); - priv->zone =ERR_PTR (ret); - } }if (IS_ERR (priv->zone)) {dev_err (dev,"can't register thermal zone\n" );
@@ -507,13 +501,16 @@ static int rcar_thermal_probe(struct platform_device *pdev) }if (chip->use_of_thermal) { + ret =thermal_zone_device_enable (priv->zone); +if (ret) + goto error_of_thermal; + ret =thermal_add_hwmon_sysfs (priv->zone);if (ret) - goto error_unregister; + goto error_of_thermal; }rcar_thermal_irq_enable (priv); -list_move_tail (&priv->list, &common->head);/* update ENR bits */
@@ -528,6 +525,8 @@ static int rcar_thermal_probe(struct platform_device *pdev)return 0 ; +error_of_thermal: +thermal_zone_device_unregister (priv->zone); error_unregister:rcar_thermal_remove (pdev);
Quoted 6 lines
drivers/thermal/renesas/rcar_thermal.c +0
-0ret =thermal_zone_device_enable (priv->zone);if (ret) {Signature · 1 line
2.53.0
Signature · 2 lines
Kind Regards, Niklas Söderlund
Quoted 37 lines · Niklas Söderlund
Hi Dan,
Thanks for your work.
Quoted 5 lines · Dan Carpenter
The
thermal_zone_device_register_with_trips()can fail for a number of reasons, including allocation failures. Check for error pointers to avoid an error pointer dereference.Fixes 9d617949d490 ("thermal/drivers/renesas: Group all renesas thermal drivers together")I don't think this is correct as this commits just moves the file.
Quoted 10 lines
Signed-off-by Dan Carpenter <[email protected]>--- drivers/thermal/renesas/rcar_thermal.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/thermal/renesas/rcar_thermal.c +0
-0drivers/thermal/renesas/rcar_thermal.c static int rcar_thermal_probe(struct platform_device *pdev) · line 499
Quoted 8 lines
drivers/thermal/renesas/rcar_thermal.c +5
-0@@ -492,6 +492,11 @@ static int rcar_thermal_probe(struct platform_device *pdev)"rcar_thermal" , trips,ARRAY_SIZE (trips), priv, &rcar_thermal_zone_ops,NULL ,0 , idle); +if (IS_ERR (priv->zone)) { + ret =PTR_ERR (priv->zone); + priv->zone =NULL ; + goto error_unregister; + }While this indeed is an issue that should be fixed I don't think this is the correct fix. Below the if .. else .. block where this is added there already is a check for
IS_ERR(priv->zone). That however does not guard against the usage ofpriv->zoneforthermal_zone_device_enable().
Quoted 3 lines
We should only call
thermal_zone_device_enable()if we are on a system that uses OF (gated bychip->use_of_thermal) which is the reason for the if .. else .. block in the first place.
Uh, what? Other way around. Only when use_of_thermal is false. This function is weirdly confusing... Also the rcar_thermal_remove() function already calls thermal_zone_device_unregister(). I'll send a v2 later.
regards,
dan carpenter