Commit 9d9ca1f9 authored by Christophe Jaillet's avatar Christophe Jaillet Committed by Zhang Rui

thermal: core: Fix resources release in error paths in thermal_zone_device_register()

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: default avatarChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: default avatarZhang Rui <rui.zhang@intel.com>
parent 6a6cd25b
...@@ -1209,10 +1209,8 @@ thermal_zone_device_register(const char *type, int trips, int mask, ...@@ -1209,10 +1209,8 @@ thermal_zone_device_register(const char *type, int trips, int mask,
ida_init(&tz->ida); ida_init(&tz->ida);
mutex_init(&tz->lock); mutex_init(&tz->lock);
result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL); result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
if (result < 0) { if (result < 0)
kfree(tz); goto free_tz;
return ERR_PTR(result);
}
tz->id = result; tz->id = result;
strlcpy(tz->type, type, sizeof(tz->type)); strlcpy(tz->type, type, sizeof(tz->type));
...@@ -1228,18 +1226,15 @@ thermal_zone_device_register(const char *type, int trips, int mask, ...@@ -1228,18 +1226,15 @@ thermal_zone_device_register(const char *type, int trips, int mask,
/* Add nodes that are always present via .groups */ /* Add nodes that are always present via .groups */
result = thermal_zone_create_device_groups(tz, mask); result = thermal_zone_create_device_groups(tz, mask);
if (result) if (result)
goto unregister; goto remove_id;
/* A new thermal zone needs to be updated anyway. */ /* A new thermal zone needs to be updated anyway. */
atomic_set(&tz->need_update, 1); atomic_set(&tz->need_update, 1);
dev_set_name(&tz->device, "thermal_zone%d", tz->id); dev_set_name(&tz->device, "thermal_zone%d", tz->id);
result = device_register(&tz->device); result = device_register(&tz->device);
if (result) { if (result)
ida_simple_remove(&thermal_tz_ida, tz->id); goto remove_device_groups;
kfree(tz);
return ERR_PTR(result);
}
for (count = 0; count < trips; count++) { for (count = 0; count < trips; count++) {
if (tz->ops->get_trip_type(tz, count, &trip_type)) if (tz->ops->get_trip_type(tz, count, &trip_type))
...@@ -1293,6 +1288,14 @@ thermal_zone_device_register(const char *type, int trips, int mask, ...@@ -1293,6 +1288,14 @@ thermal_zone_device_register(const char *type, int trips, int mask,
ida_simple_remove(&thermal_tz_ida, tz->id); ida_simple_remove(&thermal_tz_ida, tz->id);
device_unregister(&tz->device); device_unregister(&tz->device);
return ERR_PTR(result); return ERR_PTR(result);
remove_device_groups:
thermal_zone_destroy_device_groups(tz);
remove_id:
ida_simple_remove(&thermal_tz_ida, tz->id);
free_tz:
kfree(tz);
return ERR_PTR(result);
} }
EXPORT_SYMBOL_GPL(thermal_zone_device_register); EXPORT_SYMBOL_GPL(thermal_zone_device_register);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment