diff options
author | Christophe Jaillet <christophe.jaillet@wanadoo.fr> | 2017-08-08 10:39:54 -0400 |
---|---|---|
committer | Zhang Rui <rui.zhang@intel.com> | 2017-08-10 23:34:07 -0400 |
commit | 9d9ca1f9f04cf1d20427528cd3b3d05ebad47547 (patch) | |
tree | 70384f8f55ac24f69d766409c93b0aa4a58aac3b | |
parent | 6a6cd25b58dc8ce2fbded8dd83bfa7d48f06f97d (diff) |
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: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: Zhang Rui <rui.zhang@intel.com>
-rw-r--r-- | drivers/thermal/thermal_core.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 0240c44e858e..2b1b0ba393a4 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, | |||
1209 | ida_init(&tz->ida); | 1209 | ida_init(&tz->ida); |
1210 | mutex_init(&tz->lock); | 1210 | mutex_init(&tz->lock); |
1211 | result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL); | 1211 | result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL); |
1212 | if (result < 0) { | 1212 | if (result < 0) |
1213 | kfree(tz); | 1213 | goto free_tz; |
1214 | return ERR_PTR(result); | ||
1215 | } | ||
1216 | 1214 | ||
1217 | tz->id = result; | 1215 | tz->id = result; |
1218 | strlcpy(tz->type, type, sizeof(tz->type)); | 1216 | strlcpy(tz->type, type, sizeof(tz->type)); |
@@ -1228,18 +1226,15 @@ thermal_zone_device_register(const char *type, int trips, int mask, | |||
1228 | /* Add nodes that are always present via .groups */ | 1226 | /* Add nodes that are always present via .groups */ |
1229 | result = thermal_zone_create_device_groups(tz, mask); | 1227 | result = thermal_zone_create_device_groups(tz, mask); |
1230 | if (result) | 1228 | if (result) |
1231 | goto unregister; | 1229 | goto remove_id; |
1232 | 1230 | ||
1233 | /* A new thermal zone needs to be updated anyway. */ | 1231 | /* A new thermal zone needs to be updated anyway. */ |
1234 | atomic_set(&tz->need_update, 1); | 1232 | atomic_set(&tz->need_update, 1); |
1235 | 1233 | ||
1236 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); | 1234 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
1237 | result = device_register(&tz->device); | 1235 | result = device_register(&tz->device); |
1238 | if (result) { | 1236 | if (result) |
1239 | ida_simple_remove(&thermal_tz_ida, tz->id); | 1237 | goto remove_device_groups; |
1240 | kfree(tz); | ||
1241 | return ERR_PTR(result); | ||
1242 | } | ||
1243 | 1238 | ||
1244 | for (count = 0; count < trips; count++) { | 1239 | for (count = 0; count < trips; count++) { |
1245 | if (tz->ops->get_trip_type(tz, count, &trip_type)) | 1240 | if (tz->ops->get_trip_type(tz, count, &trip_type)) |
@@ -1293,6 +1288,14 @@ unregister: | |||
1293 | ida_simple_remove(&thermal_tz_ida, tz->id); | 1288 | ida_simple_remove(&thermal_tz_ida, tz->id); |
1294 | device_unregister(&tz->device); | 1289 | device_unregister(&tz->device); |
1295 | return ERR_PTR(result); | 1290 | return ERR_PTR(result); |
1291 | |||
1292 | remove_device_groups: | ||
1293 | thermal_zone_destroy_device_groups(tz); | ||
1294 | remove_id: | ||
1295 | ida_simple_remove(&thermal_tz_ida, tz->id); | ||
1296 | free_tz: | ||
1297 | kfree(tz); | ||
1298 | return ERR_PTR(result); | ||
1296 | } | 1299 | } |
1297 | EXPORT_SYMBOL_GPL(thermal_zone_device_register); | 1300 | EXPORT_SYMBOL_GPL(thermal_zone_device_register); |
1298 | 1301 | ||