Skip to content

Commit e398421

Browse files
vireshkrafaeljw
authored andcommitted
thermal: core: Move cdev cleanup to thermal_release()
thermal_release() already frees cdev, let it do rest of the cleanup as well in order to simplify the error paths in __thermal_cooling_device_register(). Signed-off-by: Viresh Kumar <[email protected]> Reviewed-by: Yang Yingliang <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent a2c81dc commit e398421

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

drivers/thermal/thermal_core.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,9 @@ static void thermal_release(struct device *dev)
770770
} else if (!strncmp(dev_name(dev), "cooling_device",
771771
sizeof("cooling_device") - 1)) {
772772
cdev = to_cooling_device(dev);
773+
thermal_cooling_device_destroy_sysfs(cdev);
774+
kfree(cdev->type);
775+
ida_free(&thermal_cdev_ida, cdev->id);
773776
kfree(cdev);
774777
}
775778
}
@@ -905,23 +908,21 @@ __thermal_cooling_device_register(struct device_node *np,
905908
cdev->devdata = devdata;
906909

907910
ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
908-
if (ret) {
909-
kfree(cdev->type);
910-
goto out_ida_remove;
911-
}
911+
if (ret)
912+
goto out_cdev_type;
912913

913914
thermal_cooling_device_setup_sysfs(cdev);
914915

915916
ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
916-
if (ret) {
917-
kfree(cdev->type);
918-
thermal_cooling_device_destroy_sysfs(cdev);
919-
goto out_ida_remove;
920-
}
917+
if (ret)
918+
goto out_cooling_dev;
921919

922920
ret = device_register(&cdev->device);
923-
if (ret)
924-
goto out_kfree_type;
921+
if (ret) {
922+
/* thermal_release() handles rest of the cleanup */
923+
put_device(&cdev->device);
924+
return ERR_PTR(ret);
925+
}
925926

926927
/* Add 'this' new cdev to the global cdev list */
927928
mutex_lock(&thermal_list_lock);
@@ -940,13 +941,10 @@ __thermal_cooling_device_register(struct device_node *np,
940941

941942
return cdev;
942943

943-
out_kfree_type:
944+
out_cooling_dev:
944945
thermal_cooling_device_destroy_sysfs(cdev);
946+
out_cdev_type:
945947
kfree(cdev->type);
946-
put_device(&cdev->device);
947-
948-
/* thermal_release() takes care of the rest */
949-
cdev = NULL;
950948
out_ida_remove:
951949
ida_free(&thermal_cdev_ida, id);
952950
out_kfree_cdev:
@@ -1107,10 +1105,7 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
11071105

11081106
mutex_unlock(&thermal_list_lock);
11091107

1110-
ida_free(&thermal_cdev_ida, cdev->id);
11111108
device_del(&cdev->device);
1112-
thermal_cooling_device_destroy_sysfs(cdev);
1113-
kfree(cdev->type);
11141109
put_device(&cdev->device);
11151110
}
11161111
EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);

0 commit comments

Comments
 (0)