Skip to content

Commit 9d9ca1f

Browse files
tititiou36zhang-rui
authored andcommitted
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 <[email protected]> Signed-off-by: Zhang Rui <[email protected]>
1 parent 6a6cd25 commit 9d9ca1f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

drivers/thermal/thermal_core.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,8 @@ thermal_zone_device_register(const char *type, int trips, int mask,
12091209
ida_init(&tz->ida);
12101210
mutex_init(&tz->lock);
12111211
result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1212-
if (result < 0) {
1213-
kfree(tz);
1214-
return ERR_PTR(result);
1215-
}
1212+
if (result < 0)
1213+
goto free_tz;
12161214

12171215
tz->id = result;
12181216
strlcpy(tz->type, type, sizeof(tz->type));
@@ -1228,18 +1226,15 @@ thermal_zone_device_register(const char *type, int trips, int mask,
12281226
/* Add nodes that are always present via .groups */
12291227
result = thermal_zone_create_device_groups(tz, mask);
12301228
if (result)
1231-
goto unregister;
1229+
goto remove_id;
12321230

12331231
/* A new thermal zone needs to be updated anyway. */
12341232
atomic_set(&tz->need_update, 1);
12351233

12361234
dev_set_name(&tz->device, "thermal_zone%d", tz->id);
12371235
result = device_register(&tz->device);
1238-
if (result) {
1239-
ida_simple_remove(&thermal_tz_ida, tz->id);
1240-
kfree(tz);
1241-
return ERR_PTR(result);
1242-
}
1236+
if (result)
1237+
goto remove_device_groups;
12431238

12441239
for (count = 0; count < trips; count++) {
12451240
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,
12931288
ida_simple_remove(&thermal_tz_ida, tz->id);
12941289
device_unregister(&tz->device);
12951290
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);
12961299
}
12971300
EXPORT_SYMBOL_GPL(thermal_zone_device_register);
12981301

0 commit comments

Comments
 (0)