[PATCH 3/3 v2] thermal: core: Fix resources release in error paths in thermal_zone_device_register()

2017-08-08 Thread Christophe JAILLET
Reorder error handling code in order to fix some resources leaks in some
cases:
   - 'tz' would leak if 'thermal_zone_create_device_groups()' fails
   - memory allocated by 'thermal_zone_create_device_groups()' would leak
 if 'device_register()' fails

With this patch, we now have 2 error handling paths: one before
'device_register()', and one after it.
This is needed because some resources are released in 'thermal_release()'.

Signed-off-by: Christophe JAILLET 
---
Should 'ida_simple_remove()' be folded somehow into 'thermal_release()'?
---
 drivers/thermal/thermal_core.c | 23 +--
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 2523901325d0..dfb4b5570c5a 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -1209,10 +1209,8 @@ thermal_zone_device_register(const char *type, int 
trips, int mask,
ida_init(>ida);
mutex_init(>lock);
result = ida_simple_get(_tz_ida, 0, 0, GFP_KERNEL);
-   if (result < 0) {
-   kfree(tz);
-   return ERR_PTR(result);
-   }
+   if (result < 0)
+   goto free_tz;
 
tz->id = result;
strlcpy(tz->type, type, sizeof(tz->type));
@@ -1228,18 +1226,15 @@ thermal_zone_device_register(const char *type, int 
trips, int mask,
/* Add nodes that are always present via .groups */
result = thermal_zone_create_device_groups(tz, mask);
if (result)
-   goto unregister;
+   goto remove_id;
 
/* A new thermal zone needs to be updated anyway. */
atomic_set(>need_update, 1);
 
dev_set_name(>device, "thermal_zone%d", tz->id);
result = device_register(>device);
-   if (result) {
-   ida_simple_remove(_tz_ida, tz->id);
-   kfree(tz);
-   return ERR_PTR(result);
-   }
+   if (result)
+   goto remove_device_groups;
 
for (count = 0; count < trips; count++) {
if (tz->ops->get_trip_type(tz, count, _type))
@@ -1293,6 +1288,14 @@ thermal_zone_device_register(const char *type, int 
trips, int mask,
ida_simple_remove(_tz_ida, tz->id);
device_unregister(>device);
return ERR_PTR(result);
+
+remove_device_groups:
+   thermal_zone_destroy_device_groups(tz);
+remove_id:
+   ida_simple_remove(_tz_ida, tz->id);
+free_tz:
+   kfree(tz);
+   return ERR_PTR(result);
 }
 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
 
-- 
2.11.0



[PATCH 3/3 v2] thermal: core: Fix resources release in error paths in thermal_zone_device_register()

2017-08-08 Thread Christophe JAILLET
Reorder error handling code in order to fix some resources leaks in some
cases:
   - 'tz' would leak if 'thermal_zone_create_device_groups()' fails
   - memory allocated by 'thermal_zone_create_device_groups()' would leak
 if 'device_register()' fails

With this patch, we now have 2 error handling paths: one before
'device_register()', and one after it.
This is needed because some resources are released in 'thermal_release()'.

Signed-off-by: Christophe JAILLET 
---
Should 'ida_simple_remove()' be folded somehow into 'thermal_release()'?
---
 drivers/thermal/thermal_core.c | 23 +--
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 2523901325d0..dfb4b5570c5a 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -1209,10 +1209,8 @@ thermal_zone_device_register(const char *type, int 
trips, int mask,
ida_init(>ida);
mutex_init(>lock);
result = ida_simple_get(_tz_ida, 0, 0, GFP_KERNEL);
-   if (result < 0) {
-   kfree(tz);
-   return ERR_PTR(result);
-   }
+   if (result < 0)
+   goto free_tz;
 
tz->id = result;
strlcpy(tz->type, type, sizeof(tz->type));
@@ -1228,18 +1226,15 @@ thermal_zone_device_register(const char *type, int 
trips, int mask,
/* Add nodes that are always present via .groups */
result = thermal_zone_create_device_groups(tz, mask);
if (result)
-   goto unregister;
+   goto remove_id;
 
/* A new thermal zone needs to be updated anyway. */
atomic_set(>need_update, 1);
 
dev_set_name(>device, "thermal_zone%d", tz->id);
result = device_register(>device);
-   if (result) {
-   ida_simple_remove(_tz_ida, tz->id);
-   kfree(tz);
-   return ERR_PTR(result);
-   }
+   if (result)
+   goto remove_device_groups;
 
for (count = 0; count < trips; count++) {
if (tz->ops->get_trip_type(tz, count, _type))
@@ -1293,6 +1288,14 @@ thermal_zone_device_register(const char *type, int 
trips, int mask,
ida_simple_remove(_tz_ida, tz->id);
device_unregister(>device);
return ERR_PTR(result);
+
+remove_device_groups:
+   thermal_zone_destroy_device_groups(tz);
+remove_id:
+   ida_simple_remove(_tz_ida, tz->id);
+free_tz:
+   kfree(tz);
+   return ERR_PTR(result);
 }
 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
 
-- 
2.11.0