From e398421fd03cf70c8d6e1d69a0773ee2fc426b25 Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Wed, 18 Jan 2023 14:08:25 +0530 Subject: 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 Reviewed-by: Yang Yingliang Signed-off-by: Rafael J. Wysocki --- drivers/thermal/thermal_core.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'drivers/thermal') diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index c686da8b4c68..3ce2a6e51c50 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -770,6 +770,9 @@ static void thermal_release(struct device *dev) } else if (!strncmp(dev_name(dev), "cooling_device", sizeof("cooling_device") - 1)) { cdev = to_cooling_device(dev); + thermal_cooling_device_destroy_sysfs(cdev); + kfree(cdev->type); + ida_free(&thermal_cdev_ida, cdev->id); kfree(cdev); } } @@ -905,23 +908,21 @@ __thermal_cooling_device_register(struct device_node *np, cdev->devdata = devdata; ret = cdev->ops->get_max_state(cdev, &cdev->max_state); - if (ret) { - kfree(cdev->type); - goto out_ida_remove; - } + if (ret) + goto out_cdev_type; thermal_cooling_device_setup_sysfs(cdev); ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id); - if (ret) { - kfree(cdev->type); - thermal_cooling_device_destroy_sysfs(cdev); - goto out_ida_remove; - } + if (ret) + goto out_cooling_dev; ret = device_register(&cdev->device); - if (ret) - goto out_kfree_type; + if (ret) { + /* thermal_release() handles rest of the cleanup */ + put_device(&cdev->device); + return ERR_PTR(ret); + } /* Add 'this' new cdev to the global cdev list */ mutex_lock(&thermal_list_lock); @@ -940,13 +941,10 @@ __thermal_cooling_device_register(struct device_node *np, return cdev; -out_kfree_type: +out_cooling_dev: thermal_cooling_device_destroy_sysfs(cdev); +out_cdev_type: kfree(cdev->type); - put_device(&cdev->device); - - /* thermal_release() takes care of the rest */ - cdev = NULL; out_ida_remove: ida_free(&thermal_cdev_ida, id); out_kfree_cdev: @@ -1107,10 +1105,7 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) mutex_unlock(&thermal_list_lock); - ida_free(&thermal_cdev_ida, cdev->id); device_del(&cdev->device); - thermal_cooling_device_destroy_sysfs(cdev); - kfree(cdev->type); put_device(&cdev->device); } EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); -- cgit v1.2.3 From 47e3f00074c27661268674d832f743f1940ae17c Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Wed, 18 Jan 2023 14:08:26 +0530 Subject: thermal: core: Use device_unregister() instead of device_del/put() Lets not open code device_unregister() unnecessarily. Signed-off-by: Viresh Kumar Reviewed-by: Yang Yingliang Signed-off-by: Rafael J. Wysocki --- drivers/thermal/thermal_core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'drivers/thermal') diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 3ce2a6e51c50..d77c1c45ba88 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1105,8 +1105,7 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) mutex_unlock(&thermal_list_lock); - device_del(&cdev->device); - put_device(&cdev->device); + device_unregister(&cdev->device); } EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); -- cgit v1.2.3 From 58d1c9fd0e859912ae322d24aa0baedc9030b8ed Mon Sep 17 00:00:00 2001 From: Daniel Lezcano Date: Mon, 23 Jan 2023 16:27:54 +0100 Subject: thermal/core: Fix unregistering netlink at thermal init time The thermal subsystem initialization miss an netlink unregistering function in the error. Add it. Signed-off-by: Daniel Lezcano Reviewed-by: Zhang Rui Signed-off-by: Rafael J. Wysocki --- drivers/thermal/thermal_core.c | 4 +++- drivers/thermal/thermal_netlink.c | 5 +++++ drivers/thermal/thermal_netlink.h | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'drivers/thermal') diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index d77c1c45ba88..e79ea015f8d3 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1607,7 +1607,7 @@ static int __init thermal_init(void) result = thermal_register_governors(); if (result) - goto error; + goto unregister_netlink; result = class_register(&thermal_class); if (result) @@ -1622,6 +1622,8 @@ static int __init thermal_init(void) unregister_governors: thermal_unregister_governors(); +unregister_netlink: + thermal_netlink_exit(); error: ida_destroy(&thermal_tz_ida); ida_destroy(&thermal_cdev_ida); diff --git a/drivers/thermal/thermal_netlink.c b/drivers/thermal/thermal_netlink.c index 75943b06dbe7..08bc46c3ec7b 100644 --- a/drivers/thermal/thermal_netlink.c +++ b/drivers/thermal/thermal_netlink.c @@ -699,3 +699,8 @@ int __init thermal_netlink_init(void) { return genl_register_family(&thermal_gnl_family); } + +void __init thermal_netlink_exit(void) +{ + genl_unregister_family(&thermal_gnl_family); +} diff --git a/drivers/thermal/thermal_netlink.h b/drivers/thermal/thermal_netlink.h index 1052f523188d..0a9987c3bc57 100644 --- a/drivers/thermal/thermal_netlink.h +++ b/drivers/thermal/thermal_netlink.h @@ -13,6 +13,7 @@ struct thermal_genl_cpu_caps { /* Netlink notification function */ #ifdef CONFIG_THERMAL_NETLINK int __init thermal_netlink_init(void); +void __init thermal_netlink_exit(void); int thermal_notify_tz_create(int tz_id, const char *name); int thermal_notify_tz_delete(int tz_id); int thermal_notify_tz_enable(int tz_id); @@ -115,4 +116,6 @@ static inline int thermal_genl_cpu_capability_event(int count, struct thermal_ge return 0; } +static inline void __init thermal_netlink_exit(void) {} + #endif /* CONFIG_THERMAL_NETLINK */ -- cgit v1.2.3 From b57d62862d171e402fca649882f0fec4e25bd312 Mon Sep 17 00:00:00 2001 From: Daniel Lezcano Date: Mon, 23 Jan 2023 16:27:55 +0100 Subject: thermal/core: Remove unneeded ida_destroy() As per documentation for the ida_destroy() function: "If the IDA is already empty, there is no need to call this function." The thermal framework is in the init sequence, so the ida was not yet used and consequently it is empty in case of error. There is no need to call ida_destroy(), let's remove the calls. Signed-off-by: Daniel Lezcano Reviewed-by: Zhang Rui Signed-off-by: Rafael J. Wysocki --- drivers/thermal/thermal_core.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'drivers/thermal') diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index e79ea015f8d3..99869e9dd68e 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1625,8 +1625,6 @@ unregister_governors: unregister_netlink: thermal_netlink_exit(); error: - ida_destroy(&thermal_tz_ida); - ida_destroy(&thermal_cdev_ida); mutex_destroy(&thermal_list_lock); mutex_destroy(&thermal_governor_lock); return result; -- cgit v1.2.3 From 5b8de18ee9027c647db4c1905f7fd0550d17d67a Mon Sep 17 00:00:00 2001 From: Daniel Lezcano Date: Mon, 23 Jan 2023 16:27:56 +0100 Subject: thermal/core: Move the thermal trip code to a dedicated file The thermal_core.c files contains a lot of functions handling different thermal components like the governors, the trip points, the cooling device, the OF cooling device, etc ... This organization does not help to migrate to a more sane code where there is a better self-encapsulation as all the components' internals can be directly accessed from a single file. For the sake of clarity, let's move the thermal trip points code in a dedicated thermal_trip.c file and add a function to browse all the trip points like we do with the thermal zones, the govenors and the cooling devices. The same can be done for the cooling devices and the governor code but that will come later as the current work in the thermal framework is to fix the trip point handling and use a generic trip point structure. No functional changes intended. Signed-off-by: Daniel Lezcano Reviewed-by: Zhang Rui Signed-off-by: Rafael J. Wysocki --- drivers/thermal/Makefile | 4 +- drivers/thermal/thermal_core.c | 87 ------------------ drivers/thermal/thermal_core.h | 4 + drivers/thermal/thermal_helpers.c | 62 ------------- drivers/thermal/thermal_trip.c | 182 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 188 insertions(+), 151 deletions(-) create mode 100644 drivers/thermal/thermal_trip.c (limited to 'drivers/thermal') diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 2506c6c8ca83..8b8d8517697a 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -4,8 +4,8 @@ # obj-$(CONFIG_THERMAL) += thermal_sys.o -thermal_sys-y += thermal_core.o thermal_sysfs.o \ - thermal_helpers.o +thermal_sys-y += thermal_core.o thermal_sysfs.o +thermal_sys-y += thermal_trip.o thermal_helpers.o # netlink interface to manage the thermal framework thermal_sys-$(CONFIG_THERMAL_NETLINK) += thermal_netlink.o diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 99869e9dd68e..2f6ecb7be8de 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1156,12 +1156,6 @@ static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms *delay_jiffies = round_jiffies(*delay_jiffies); } -int thermal_zone_get_num_trips(struct thermal_zone_device *tz) -{ - return tz->num_trips; -} -EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips); - int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) { int i, ret = -EINVAL; @@ -1188,87 +1182,6 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) } EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp); -int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, - struct thermal_trip *trip) -{ - int ret; - - if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip) - return -EINVAL; - - if (tz->trips) { - *trip = tz->trips[trip_id]; - return 0; - } - - if (tz->ops->get_trip_hyst) { - ret = tz->ops->get_trip_hyst(tz, trip_id, &trip->hysteresis); - if (ret) - return ret; - } else { - trip->hysteresis = 0; - } - - ret = tz->ops->get_trip_temp(tz, trip_id, &trip->temperature); - if (ret) - return ret; - - return tz->ops->get_trip_type(tz, trip_id, &trip->type); -} -EXPORT_SYMBOL_GPL(__thermal_zone_get_trip); - -int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, - struct thermal_trip *trip) -{ - int ret; - - mutex_lock(&tz->lock); - ret = __thermal_zone_get_trip(tz, trip_id, trip); - mutex_unlock(&tz->lock); - - return ret; -} -EXPORT_SYMBOL_GPL(thermal_zone_get_trip); - -int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, - const struct thermal_trip *trip) -{ - struct thermal_trip t; - int ret; - - if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips) - return -EINVAL; - - ret = __thermal_zone_get_trip(tz, trip_id, &t); - if (ret) - return ret; - - if (t.type != trip->type) - return -EINVAL; - - if (t.temperature != trip->temperature && tz->ops->set_trip_temp) { - ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature); - if (ret) - return ret; - } - - if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) { - ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis); - if (ret) - return ret; - } - - if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis)) - tz->trips[trip_id] = *trip; - - thermal_notify_tz_trip_change(tz->id, trip_id, trip->type, - trip->temperature, trip->hysteresis); - - __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED); - - return 0; -} - /** * thermal_zone_device_register_with_trips() - register a new thermal zone device * @type: the thermal zone device type diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 26350206a98d..7af54382e915 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -52,6 +52,10 @@ int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *, int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), void *thermal_governor); +int __for_each_thermal_trip(struct thermal_zone_device *, + int (*cb)(struct thermal_trip *, void *), + void *); + struct thermal_zone_device *thermal_zone_get_by_id(int id); struct thermal_attr { diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index 8977d5ddc23c..0f648131b0b5 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -146,68 +146,6 @@ unlock: } EXPORT_SYMBOL_GPL(thermal_zone_get_temp); -/** - * __thermal_zone_set_trips - Computes the next trip points for the driver - * @tz: a pointer to a thermal zone device structure - * - * The function computes the next temperature boundaries by browsing - * the trip points. The result is the closer low and high trip points - * to the current temperature. These values are passed to the backend - * driver to let it set its own notification mechanism (usually an - * interrupt). - * - * This function must be called with tz->lock held. Both tz and tz->ops - * must be valid pointers. - * - * It does not return a value - */ -void __thermal_zone_set_trips(struct thermal_zone_device *tz) -{ - struct thermal_trip trip; - int low = -INT_MAX, high = INT_MAX; - int i, ret; - - lockdep_assert_held(&tz->lock); - - if (!tz->ops->set_trips) - return; - - for (i = 0; i < tz->num_trips; i++) { - int trip_low; - - ret = __thermal_zone_get_trip(tz, i , &trip); - if (ret) - return; - - trip_low = trip.temperature - trip.hysteresis; - - if (trip_low < tz->temperature && trip_low > low) - low = trip_low; - - if (trip.temperature > tz->temperature && - trip.temperature < high) - high = trip.temperature; - } - - /* No need to change trip points */ - if (tz->prev_low_trip == low && tz->prev_high_trip == high) - return; - - tz->prev_low_trip = low; - tz->prev_high_trip = high; - - dev_dbg(&tz->device, - "new temperature boundaries: %d < x < %d\n", low, high); - - /* - * Set a temperature window. When this window is left the driver - * must inform the thermal core via thermal_zone_device_update. - */ - ret = tz->ops->set_trips(tz, low, high); - if (ret) - dev_err(&tz->device, "Failed to set trips: %d\n", ret); -} - static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int target) { diff --git a/drivers/thermal/thermal_trip.c b/drivers/thermal/thermal_trip.c new file mode 100644 index 000000000000..907f3a4d7bc8 --- /dev/null +++ b/drivers/thermal/thermal_trip.c @@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2008 Intel Corp + * Copyright (C) 2008 Zhang Rui + * Copyright (C) 2008 Sujith Thomas + * Copyright 2022 Linaro Limited + * + * Thermal trips handling + */ +#include "thermal_core.h" + +int __for_each_thermal_trip(struct thermal_zone_device *tz, + int (*cb)(struct thermal_trip *, void *), + void *data) +{ + int i, ret; + struct thermal_trip trip; + + lockdep_assert_held(&tz->lock); + + for (i = 0; i < tz->num_trips; i++) { + + ret = __thermal_zone_get_trip(tz, i, &trip); + if (ret) + return ret; + + ret = cb(&trip, data); + if (ret) + return ret; + } + + return 0; +} + +int thermal_zone_get_num_trips(struct thermal_zone_device *tz) +{ + return tz->num_trips; +} +EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips); + +/** + * __thermal_zone_set_trips - Computes the next trip points for the driver + * @tz: a pointer to a thermal zone device structure + * + * The function computes the next temperature boundaries by browsing + * the trip points. The result is the closer low and high trip points + * to the current temperature. These values are passed to the backend + * driver to let it set its own notification mechanism (usually an + * interrupt). + * + * This function must be called with tz->lock held. Both tz and tz->ops + * must be valid pointers. + * + * It does not return a value + */ +void __thermal_zone_set_trips(struct thermal_zone_device *tz) +{ + struct thermal_trip trip; + int low = -INT_MAX, high = INT_MAX; + int i, ret; + + lockdep_assert_held(&tz->lock); + + if (!tz->ops->set_trips) + return; + + for (i = 0; i < tz->num_trips; i++) { + int trip_low; + + ret = __thermal_zone_get_trip(tz, i , &trip); + if (ret) + return; + + trip_low = trip.temperature - trip.hysteresis; + + if (trip_low < tz->temperature && trip_low > low) + low = trip_low; + + if (trip.temperature > tz->temperature && + trip.temperature < high) + high = trip.temperature; + } + + /* No need to change trip points */ + if (tz->prev_low_trip == low && tz->prev_high_trip == high) + return; + + tz->prev_low_trip = low; + tz->prev_high_trip = high; + + dev_dbg(&tz->device, + "new temperature boundaries: %d < x < %d\n", low, high); + + /* + * Set a temperature window. When this window is left the driver + * must inform the thermal core via thermal_zone_device_update. + */ + ret = tz->ops->set_trips(tz, low, high); + if (ret) + dev_err(&tz->device, "Failed to set trips: %d\n", ret); +} + +int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, + struct thermal_trip *trip) +{ + int ret; + + if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip) + return -EINVAL; + + if (tz->trips) { + *trip = tz->trips[trip_id]; + return 0; + } + + if (tz->ops->get_trip_hyst) { + ret = tz->ops->get_trip_hyst(tz, trip_id, &trip->hysteresis); + if (ret) + return ret; + } else { + trip->hysteresis = 0; + } + + ret = tz->ops->get_trip_temp(tz, trip_id, &trip->temperature); + if (ret) + return ret; + + return tz->ops->get_trip_type(tz, trip_id, &trip->type); +} +EXPORT_SYMBOL_GPL(__thermal_zone_get_trip); + +int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, + struct thermal_trip *trip) +{ + int ret; + + mutex_lock(&tz->lock); + ret = __thermal_zone_get_trip(tz, trip_id, trip); + mutex_unlock(&tz->lock); + + return ret; +} +EXPORT_SYMBOL_GPL(thermal_zone_get_trip); + +int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, + const struct thermal_trip *trip) +{ + struct thermal_trip t; + int ret; + + if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips) + return -EINVAL; + + ret = __thermal_zone_get_trip(tz, trip_id, &t); + if (ret) + return ret; + + if (t.type != trip->type) + return -EINVAL; + + if (t.temperature != trip->temperature && tz->ops->set_trip_temp) { + ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature); + if (ret) + return ret; + } + + if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) { + ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis); + if (ret) + return ret; + } + + if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis)) + tz->trips[trip_id] = *trip; + + thermal_notify_tz_trip_change(tz->id, trip_id, trip->type, + trip->temperature, trip->hysteresis); + + __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED); + + return 0; +} -- cgit v1.2.3 From 9e0a9be24bdd61a160631227ee995b579be566a5 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Mon, 23 Jan 2023 21:44:03 +0100 Subject: thermal: Fail object registration if thermal class is not registered If thermal_class is not registered with the driver core, there is no way to expose the interfaces used by the thermal control framework, so prevent thermal zones and cooling devices from being registered in that case by returning an error from object registration functions. For this purpose, use a thermal_class pointer that will be NULL if the class is not registered. To avoid wasting memory in that case, allocate the thermal class object dynamically and if it fails to register, free it and clear the thermal_class pointer to NULL. Signed-off-by: Rafael J. Wysocki Acked-by: Daniel Lezcano Reviewed-by: Zhang Rui Reviewed-by: Greg Kroah-Hartman --- drivers/thermal/thermal_core.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'drivers/thermal') diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 2f6ecb7be8de..0675df54c8e6 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -777,10 +777,7 @@ static void thermal_release(struct device *dev) } } -static struct class thermal_class = { - .name = "thermal", - .dev_release = thermal_release, -}; +static struct class *thermal_class; static inline void print_bind_err_msg(struct thermal_zone_device *tz, @@ -883,6 +880,9 @@ __thermal_cooling_device_register(struct device_node *np, !ops->set_cur_state) return ERR_PTR(-EINVAL); + if (!thermal_class) + return ERR_PTR(-ENODEV); + cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); if (!cdev) return ERR_PTR(-ENOMEM); @@ -904,7 +904,7 @@ __thermal_cooling_device_register(struct device_node *np, cdev->np = np; cdev->ops = ops; cdev->updated = false; - cdev->device.class = &thermal_class; + cdev->device.class = thermal_class; cdev->devdata = devdata; ret = cdev->ops->get_max_state(cdev, &cdev->max_state); @@ -1256,6 +1256,9 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips) return ERR_PTR(-EINVAL); + if (!thermal_class) + return ERR_PTR(-ENODEV); + tz = kzalloc(sizeof(*tz), GFP_KERNEL); if (!tz) return ERR_PTR(-ENOMEM); @@ -1277,7 +1280,7 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t tz->ops = ops; tz->tzp = tzp; - tz->device.class = &thermal_class; + tz->device.class = thermal_class; tz->devdata = devdata; tz->trips = trips; tz->num_trips = num_trips; @@ -1522,9 +1525,21 @@ static int __init thermal_init(void) if (result) goto unregister_netlink; - result = class_register(&thermal_class); - if (result) + thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL); + if (!thermal_class) { + result = -ENOMEM; + goto unregister_governors; + } + + thermal_class->name = "thermal"; + thermal_class->dev_release = thermal_release; + + result = class_register(thermal_class); + if (result) { + kfree(thermal_class); + thermal_class = NULL; goto unregister_governors; + } result = register_pm_notifier(&thermal_pm_nb); if (result) -- cgit v1.2.3 From 5bbafd436266136a67224b98e6ad864c4ddd762d Mon Sep 17 00:00:00 2001 From: ye xingchen Date: Tue, 17 Jan 2023 10:40:26 +0800 Subject: thermal: core: Use sysfs_emit_at() instead of scnprintf() Follow the advice in Documentation/filesystems/sysfs.rst that show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space. Signed-off-by: ye xingchen [ rjw: Subject and changelog edits ] Signed-off-by: Rafael J. Wysocki --- drivers/thermal/thermal_core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'drivers/thermal') diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 0675df54c8e6..55679fd86505 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -229,10 +229,9 @@ int thermal_build_list_of_policies(char *buf) mutex_lock(&thermal_governor_lock); list_for_each_entry(pos, &thermal_governor_list, governor_list) { - count += scnprintf(buf + count, PAGE_SIZE - count, "%s ", - pos->name); + count += sysfs_emit_at(buf, count, "%s ", pos->name); } - count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); + count += sysfs_emit_at(buf, count, "\n"); mutex_unlock(&thermal_governor_lock); -- cgit v1.2.3