Support cooling device with ID in the OF
Subsystems (19)
v2 cover Support cooling device with ID in the OF
A cooling device can be a single cooling device or a cooling device controller with multiple cooling devices attached to it.
The thermal framework builds a connection between a cooling device and a thermal zone when there is a trip point to be mitigated by the cooling device. In the Linux kernel thermal semantic, the action of connecting the cooling device and the thermal zone is called a binding.
The description of the aforementioned connection depends on the firmware definition and in our case it is through the device tree. The cooling map describes this connection.
How is it done ?
For a single cooling device, the DT describes:
* A cooling device:
mycooling_device {
...
};* A cooling map in a thermal zone
mythermal_zone {
...
cooling-maps {
map0 {
...
cooling-device = <&mycooling_device min max>;
...
};
}
...};
For a cooling device controller with multiple cooling devices attached, the DT describes:
* A cooling device controller:
mycooling_device_ctrl {
mycooling_device1 {
...
};
mycooling_device2 {
...
};
...
mycooling_deviceN {
...
};};
* A cooling map in a thermal zone
mythermal_zone {
...
cooling-maps {
map0 {
...
cooling-device = <&mycooling_device1 min max>;
...
};
}
...};
When the cooling device "mycooling_device" is registered, then the binding function will browse all the cooling maps and matches the node device pointer to do the association (the pĥandle in the cooling map vs the cooling device node).
That is how the connection between the cooling device and the thermal zone is done.
DT maintainers consider the above description with child nodes is incorrect if a child node doesn't have its own bindings as explained in the documentation [1].
Gaurav Kohli posted a series to support the QMI TMD (Qualcomm Messaging Interface based Thermal Mitigation Devices) [2]. This device is a dedicated, centralized cooling device controller managing a set of cooling devices.
Because of the above restrictions with child nodes there is no DT bindings allowing to do the connection between the cooling device inside the cooling device controller and a thermal zone.
A QMI TMD is not a standalone cooling device neither a multiple cooling devices with their own DT description.
For this reason, the proposed changes with child nodes has been rejected [3].
As a solution, it has been proposed to fold all these child nodes into an array and add an id in the cooling maps to identify the cooling device entry in the controller node [4].
Given the thermal zone has the same mechanism [5], that is a phandle to a sensor node and an sensor specifier. The proposed approach makes sense as it is consistent with the thermal zone description.
This series provides a cleanup around the usage of the OF code which is interleaved with non-OF code. The OF and the non-OF code are separated and remain self-encapsulated.
Then the function to register a cooling device is renamed to reflect the legacy usage and a new function is introduced with the id parameter.
Finally, the DT bindings is updated with the new format and the thermal OF is changed to handle the new DT bindings.
Given the trivial changes impacting the files outside of the thermal framework, this series is designed to have all changes going through the thermal tree.
Thanks!
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/writing-bindings.rst
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/thermal/thermal-zones.yaml#n99
---
Changelog:
- V2
- Added stubs for OF functions in order to prevent to break the git bisectability compilation
- Added missing call to thermal_cooling_device_init_complete() in devm_thermal_of_cooling_device_register()
- Fixed assymetry in devm_ calls for the tegra/soctherm() driver
- Fixed kernel documentation for API changes
- Added missing static inline for a API stub
- Hardened the test with two or three arguments in the cooling spec
- Narrowed const: 2 strict in pwm-fan
---
Daniel Lezcano (11):
thermal/driver/tegra/soctherm: Use devm_ variant when registering a
cooling device
thermal/of: Move OF code where it belongs to
thermal/core: Make thermal_cooling_device_init_complete() non static thermal/core: Remove node pointer parameter when registering a tz thermal/of: Move the node pointer assignation in the OF code file hwmon:: Use non-OF thermal cooling device register function thermal/core: Put of_node field cooling device structure under Kconfig
option
thermal/of: Rename the devm_thermal_of_cooling_device_register()
function
thermal/of: Introduce cooling device of_index
thermal/of: Pass the of_index and add a function to register with an
index
thermal/of: Process cooling device index in cooling-spec
Gaurav Kohli (1):
dt-bindings: thermal: cooling-devices: Update support for 3 cells
cooling device
.../devicetree/bindings/hwmon/pwm-fan.yaml | 3 +- .../thermal/thermal-cooling-devices.yaml | 8 +- .../bindings/thermal/thermal-zones.yaml | 3 +- drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 5 +- drivers/hwmon/amc6821.c | 2 +- drivers/hwmon/aspeed-pwm-tacho.c | 5 +- drivers/hwmon/cros_ec_hwmon.c | 4 +- drivers/hwmon/dell-smm-hwmon.c | 4 +- drivers/hwmon/emc2305.c | 6 +- drivers/hwmon/gpio-fan.c | 6 +- drivers/hwmon/max6650.c | 6 +- drivers/hwmon/mlxreg-fan.c | 4 +- drivers/hwmon/npcm750-pwm-fan.c | 6 +- drivers/hwmon/pwm-fan.c | 5 +- drivers/hwmon/qnap-mcu-hwmon.c | 6 +- drivers/hwmon/tc654.c | 5 +- drivers/memory/tegra/tegra210-emc-core.c | 4 +- drivers/soc/qcom/qcom_aoss.c | 2 +- drivers/thermal/cpufreq_cooling.c | 2 +- drivers/thermal/cpuidle_cooling.c | 2 +- drivers/thermal/devfreq_cooling.c | 2 +- drivers/thermal/khadas_mcu_fan.c | 7 +- drivers/thermal/tegra/soctherm.c | 29 ++-- drivers/thermal/thermal_core.c | 62 ++------ drivers/thermal/thermal_core.h | 6 + drivers/thermal/thermal_of.c | 146 +++++++++++++++++- include/linux/thermal.h | 69 +++++++-- 27 files changed, 281 insertions(+), 128 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/12 thermal/driver/tegra/soctherm: Use devm_ variant when registering a cooling device
The driver invokes thermal_of_cooling_device_register() at probe time but without unregistering it at remove time.
As we have a devm_ variant, use it and the cooling device should be automatically removed.
While this prevents the cooling device from leaking, the unregistration is now deferred until devres_release_all() runs (after probe() returns an error or remove() completes).
A bug can be subtly introduced by this change because the driver still manually disables the hardware clocks during the probe error path and in tegra_soctherm_remove(). This creates a window where the cooling device remains registered and exposed to the thermal core and sysfs while the underlying hardware is powered down.
In order to prevent this potential issue, use the devm_ variant for the clk.
Compiled-tested only.
--- drivers/thermal/tegra/soctherm.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/thermal/tegra/soctherm.c +9 -20
@@ -1700,9 +1700,9 @@ static void soctherm_init_hw_throt_cdev(struct platform_device *pdev) stc->init = true; }else { - tcd =thermal_of_cooling_device_register (np_stcc, - (char *)name, ts, - &throt_cooling_ops); + tcd =devm_thermal_of_cooling_device_register (dev, np_stcc, + (char *)name, ts, + &throt_cooling_ops);if (IS_ERR_OR_NULL (tcd)) { dev_err(dev, "throttle-cfg: %s: failed to register cooling device\n",
@@ -2133,13 +2133,13 @@ static int tegra_soctherm_probe(struct platform_device *pdev)return PTR_ERR (tegra->reset); } - tegra->clock_tsensor =devm_clk_get (&pdev->dev,"tsensor" ); + tegra->clock_tsensor =devm_clk_get_enabled (&pdev->dev,"tsensor" );if (IS_ERR (tegra->clock_tsensor)) {dev_err (&pdev->dev,"can't get tsensor clock\n" );return PTR_ERR (tegra->clock_tsensor); } - tegra->clock_soctherm =devm_clk_get (&pdev->dev,"soctherm" ); + tegra->clock_soctherm =devm_clk_get_enabled (&pdev->dev,"soctherm" );if (IS_ERR (tegra->clock_soctherm)) {dev_err (&pdev->dev,"can't get soctherm clock\n" );return PTR_ERR (tegra->clock_soctherm);
@@ -2171,10 +2171,6 @@ static int tegra_soctherm_probe(struct platform_device *pdev)if (!tegra->thermctl_tzs)return -ENOMEM ; - err =soctherm_clk_enable (pdev, true); -if (err) -return err; -soctherm_thermtrips_parse (pdev);soctherm_init_hw_throt_cdev (pdev);
@@ -2184,10 +2180,8 @@ static int tegra_soctherm_probe(struct platform_device *pdev)for (i =0 ; i < soc->num_ttgs; ++i) {struct tegra_thermctl_zone *zone =devm_kzalloc (&pdev->dev,sizeof (*zone),GFP_KERNEL ); -if (!zone) { - err = -ENOMEM ; - goto disable_clocks; - } +if (!zone) +return -ENOMEM ; zone->reg = tegra->regs + soc->ttgs[i]->sensor_temp_offset; zone->dev = &pdev->dev;
@@ -2201,7 +2195,7 @@ static int tegra_soctherm_probe(struct platform_device *pdev) err =PTR_ERR (z);dev_err (&pdev->dev,"failed to register sensor: %d\n" , err); - goto disable_clocks; +return err; } zone->tz = z;
@@ -2210,7 +2204,7 @@ static int tegra_soctherm_probe(struct platform_device *pdev)/* Configure hw trip points */ err =tegra_soctherm_set_hwtrips (&pdev->dev, soc->ttgs[i], z);if (err) - goto disable_clocks; +return err; } err =soctherm_interrupts_init (pdev, tegra);
@@ -2218,11 +2212,6 @@ static int tegra_soctherm_probe(struct platform_device *pdev)soctherm_debug_init (pdev);return 0 ; - -disable_clocks: -soctherm_clk_enable (pdev, false); - -return err; }static void tegra_soctherm_remove (struct platform_device *pdev)
Signature · 1 line
2.43.0
PATCH v2 2/12 thermal/of: Move OF code where it belongs to
In reply to Daniel LezcanoThe functions:
- thermal_of_cooling_device_register()
- devm_thermal_of_cooling_device_register()
are related to thermal-of but they are implemented in thermal-core. Move these functions to the right file.
As the thermal-of file depends on CONFIG_THERMAL_OF=y, by moving these two functions in it, when non-OF drivers are using this API that results in a link issue. In order to prevent unnecessary circumvolutions with the changes, the stubs have been added in the header file resulting in having the non-OF drivers compiled without the CONFIG_THERMAL_OF=y using these stubs and failing to register their cooling devices.
This is temporary as the next changes introduces the functions for non-OF drivers and returning to the initial state.
--- drivers/thermal/thermal_core.c | 75 +--------------------------------- drivers/thermal/thermal_core.h | 5 +++ drivers/thermal/thermal_of.c | 72 ++++++++++++++++++++++++++++++++ include/linux/thermal.h | 47 ++++++++++++--------- 4 files changed, 105 insertions(+), 94 deletions(-)
drivers/thermal/thermal_core.c +1 -74
@@ -1056,7 +1056,7 @@ static void thermal_cooling_device_init_complete(struct thermal_cooling_device ** Return: a pointer to the created struct thermal_cooling_device or an * ERR_PTR. Caller must check return value with IS_ERR*() helpers. */ -static struct thermal_cooling_device * +struct thermal_cooling_device *__thermal_cooling_device_register (struct device_node *np,const char *type,void *devdata,const struct thermal_cooling_device_ops *ops)
@@ -1163,79 +1163,6 @@ thermal_cooling_device_register(const char *type, void *devdata, }EXPORT_SYMBOL_GPL (thermal_cooling_device_register); -/** -* thermal_of_cooling_device_register() - register an OF thermal cooling device -* @np: a pointer to a device tree node. -* @type: the thermal cooling device type. -* @devdata: device private data. -* @ops: standard thermal cooling devices callbacks. -* -* This function will register a cooling device with device tree node reference. -* This interface function adds a new thermal cooling device (fan/processor/...) -* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself -* to all the thermal zone devices registered at the same time. -* -* Return: a pointer to the created struct thermal_cooling_device or an -* ERR_PTR. Caller must check return value with IS_ERR*() helpers. -*/ -struct thermal_cooling_device * -thermal_of_cooling_device_register (struct device_node *np, -const char *type,void *devdata, -const struct thermal_cooling_device_ops *ops) -{ -return __thermal_cooling_device_register (np, type, devdata, ops); -} -EXPORT_SYMBOL_GPL (thermal_of_cooling_device_register); - -static void thermal_cooling_device_release (struct device *dev,void *res) -{ -thermal_cooling_device_unregister ( - *(struct thermal_cooling_device **)res); -} - -/** -* devm_thermal_of_cooling_device_register() - register an OF thermal cooling -* device -* @dev: a valid struct device pointer of a sensor device. -* @np: a pointer to a device tree node. -* @type: the thermal cooling device type. -* @devdata: device private data. -* @ops: standard thermal cooling devices callbacks. -* -* This function will register a cooling device with device tree node reference. -* This interface function adds a new thermal cooling device (fan/processor/...) -* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself -* to all the thermal zone devices registered at the same time. -* -* Return: a pointer to the created struct thermal_cooling_device or an -* ERR_PTR. Caller must check return value with IS_ERR*() helpers. -*/ -struct thermal_cooling_device * -devm_thermal_of_cooling_device_register (struct device *dev, -struct device_node *np, -const char *type,void *devdata, -const struct thermal_cooling_device_ops *ops) -{ -struct thermal_cooling_device **ptr, *tcd; - - ptr =devres_alloc (thermal_cooling_device_release,sizeof (*ptr), -GFP_KERNEL ); -if (!ptr) -return ERR_PTR (-ENOMEM ); - - tcd =__thermal_cooling_device_register (np, type, devdata, ops); -if (IS_ERR (tcd)) { -devres_free (ptr); -return tcd; - } - - *ptr = tcd; -devres_add (dev, ptr); - -return tcd; -} -EXPORT_SYMBOL_GPL (devm_thermal_of_cooling_device_register); -static bool thermal_cooling_device_present (struct thermal_cooling_device *cdev) {struct thermal_cooling_device *pos =NULL ;
drivers/thermal/thermal_core.h +5 -0
@@ -269,6 +269,11 @@ void thermal_zone_device_critical_shutdown(struct thermal_zone_device *tz);void thermal_governor_update_tz (struct thermal_zone_device *tz,enum thermal_notify_event reason); +struct thermal_cooling_device * +__thermal_cooling_device_register (struct device_node *np, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops); +/* Helpers */ #define for_each_trip_desc (__tz, __td) \ for (__td = __tz->trips; __td - __tz->trips < __tz->num_trips; __td++)
drivers/thermal/thermal_of.c +72 -0
75 lines not on this page · raw message
include/linux/thermal.h +27 -20
71 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 3/12 thermal/core: Make thermal_cooling_device_init_complete() non static
In reply to Daniel LezcanoIn the process of separating clearly the OF code and the thermal core code, let's split and export the thermal_cooling_device_init_complete() function so it can be used by an implementation inside the thermal OF code.
--- drivers/thermal/thermal_core.c | 12 ++++++++---- drivers/thermal/thermal_core.h | 2 ++ drivers/thermal/thermal_of.c | 12 +++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-)
drivers/thermal/thermal_core.c +8 -4
@@ -1028,7 +1028,7 @@ static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,__thermal_zone_device_update (tz,THERMAL_EVENT_UNSPECIFIED ); } -static void thermal_cooling_device_init_complete (struct thermal_cooling_device *cdev) +void thermal_cooling_device_init_complete (struct thermal_cooling_device *cdev) {struct thermal_zone_device *tz;
@@ -1127,8 +1127,6 @@ __thermal_cooling_device_register(struct device_node *np,if (current_state <= cdev->max_state)thermal_debug_cdev_add (cdev, current_state); -thermal_cooling_device_init_complete (cdev); -return cdev; out_cooling_dev:
@@ -1159,7 +1157,13 @@ struct thermal_cooling_device * thermal_cooling_device_register(const char *type, void *devdata, const struct thermal_cooling_device_ops *ops) { - return __thermal_cooling_device_register(NULL, type, devdata, ops); + struct thermal_cooling_device *cdev; + + cdev =__thermal_cooling_device_register (NULL , type, devdata, ops); +if (!IS_ERR (cdev)) +thermal_cooling_device_init_complete (cdev); + +return cdev; }EXPORT_SYMBOL_GPL (thermal_cooling_device_register);
drivers/thermal/thermal_core.h +2 -0
@@ -269,6 +269,8 @@ void thermal_zone_device_critical_shutdown(struct thermal_zone_device *tz);void thermal_governor_update_tz (struct thermal_zone_device *tz,enum thermal_notify_event reason); +void thermal_cooling_device_init_complete (struct thermal_cooling_device *cdev); +struct thermal_cooling_device * __thermal_cooling_device_register(struct device_node *np, const char *type, void *devdata,
drivers/thermal/thermal_of.c +11 -1
@@ -531,7 +531,15 @@ thermal_of_cooling_device_register(struct device_node *np, const char *type, void *devdata, const struct thermal_cooling_device_ops *ops) { - return __thermal_cooling_device_register(np, type, devdata, ops); + struct thermal_cooling_device *cdev; + + cdev =__thermal_cooling_device_register (np, type, devdata, ops); +if (IS_ERR (cdev)) +return cdev; + +thermal_cooling_device_init_complete (cdev); + +return cdev; }EXPORT_SYMBOL_GPL (thermal_of_cooling_device_register);
@@ -579,6 +587,8 @@ devm_thermal_of_cooling_device_register(struct device *dev, *ptr = tcd;devres_add (dev, ptr); +thermal_cooling_device_init_complete (tcd); +return tcd; }EXPORT_SYMBOL_GPL (devm_thermal_of_cooling_device_register);
Signature · 1 line
2.43.0
PATCH v2 4/12 thermal/core: Remove node pointer parameter when registering a tz
In reply to Daniel LezcanoNow we have a OF version for all functions registering a thermal zone or a cooling device. Let's remove the device_node pointer parameter in the core function which is inconsistent with non-OF code.
--- drivers/thermal/thermal_core.c | 52 ++++++++++++++++++++++++++++++---- drivers/thermal/thermal_core.h | 3 +- drivers/thermal/thermal_of.c | 2 +- include/linux/thermal.h | 11 +++++++ 4 files changed, 59 insertions(+), 9 deletions(-)
drivers/thermal/thermal_core.c +46 -6
@@ -1042,7 +1042,6 @@ void thermal_cooling_device_init_complete(struct thermal_cooling_device *cdev)/** * __thermal_cooling_device_register() - register a new thermal cooling device -* @np: a pointer to a device tree node. * @type: the thermal cooling device type. * @devdata: device private data. * @ops: standard thermal cooling devices callbacks.
@@ -1050,15 +1049,12 @@ void thermal_cooling_device_init_complete(struct thermal_cooling_device *cdev)* This interface function adds a new thermal cooling device (fan/processor/...) * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself * to all the thermal zone devices registered at the same time. -* It also gives the opportunity to link the cooling device to a device tree -* node, so that it can be bound to a thermal zone created out of device tree. * * Return: a pointer to the created struct thermal_cooling_device or an * ERR_PTR. Caller must check return value with IS_ERR*() helpers. */ struct thermal_cooling_device * -__thermal_cooling_device_register (struct device_node *np, -const char *type,void *devdata, +__thermal_cooling_device_register (const char *type,void *devdata,const struct thermal_cooling_device_ops *ops) {struct thermal_cooling_device *cdev;
@@ -1159,7 +1155,7 @@ thermal_cooling_device_register(const char *type, void *devdata, {struct thermal_cooling_device *cdev; - cdev =__thermal_cooling_device_register (NULL , type, devdata, ops); + cdev =__thermal_cooling_device_register (type, devdata, ops);if (!IS_ERR (cdev))thermal_cooling_device_init_complete (cdev);
@@ -1167,6 +1163,50 @@ thermal_cooling_device_register(const char *type, void *devdata, }EXPORT_SYMBOL_GPL (thermal_cooling_device_register); +static void thermal_cooling_device_release (struct device *dev,void *res) +{ +thermal_cooling_device_unregister (*(struct thermal_cooling_device **)res); +} + +/** +* devm_thermal_cooling_device_register() - register a thermal cooling device +* @dev: a valid struct device pointer of a sensor device. +* @type: the thermal cooling device type. +* @devdata: device private data. +* @ops: standard thermal cooling devices callbacks. +* +* This function will register a cooling device with device tree node reference. +* This interface function adds a new thermal cooling device (fan/processor/...) +* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself +* to all the thermal zone devices registered at the same time. +* +* Return: a pointer to the created struct thermal_cooling_device or an +* ERR_PTR. Caller must check return value with IS_ERR*() helpers. +*/ +struct thermal_cooling_device * +devm_thermal_cooling_device_register (struct device *dev,const char *type, +void *devdata,const struct thermal_cooling_device_ops *ops) +{ +struct thermal_cooling_device **ptr, *tcd; + + ptr =devres_alloc (thermal_cooling_device_release,sizeof (*ptr), +GFP_KERNEL ); +if (!ptr) +return ERR_PTR (-ENOMEM ); + + tcd =thermal_cooling_device_register (type, devdata, ops); +if (IS_ERR (tcd)) { +devres_free (ptr); +return tcd; + } + + *ptr = tcd; +devres_add (dev, ptr); + +return tcd; +} +EXPORT_SYMBOL_GPL (devm_thermal_cooling_device_register); +static bool thermal_cooling_device_present (struct thermal_cooling_device *cdev) {struct thermal_cooling_device *pos =NULL ;
drivers/thermal/thermal_core.h +1 -2
@@ -272,8 +272,7 @@ void thermal_governor_update_tz(struct thermal_zone_device *tz,void thermal_cooling_device_init_complete (struct thermal_cooling_device *cdev);struct thermal_cooling_device * -__thermal_cooling_device_register (struct device_node *np, -const char *type,void *devdata, +__thermal_cooling_device_register (const char *type,void *devdata,const struct thermal_cooling_device_ops *ops);/* Helpers */
drivers/thermal/thermal_of.c +1 -1
@@ -533,7 +533,7 @@ thermal_of_cooling_device_register(struct device_node *np, {struct thermal_cooling_device *cdev; - cdev =__thermal_cooling_device_register (np, type, devdata, ops); + cdev =__thermal_cooling_device_register (type, devdata, ops);if (IS_ERR (cdev))return cdev;
include/linux/thermal.h +11 -0
@@ -278,6 +278,11 @@ void thermal_zone_device_update(struct thermal_zone_device *,struct thermal_cooling_device *thermal_cooling_device_register (const char *,void *,const struct thermal_cooling_device_ops *); + +struct thermal_cooling_device * +devm_thermal_cooling_device_register (struct device *dev,const char *type, +void *devdata,const struct thermal_cooling_device_ops *ops); +void thermal_cooling_device_update (struct thermal_cooling_device *);void thermal_cooling_device_unregister (struct thermal_cooling_device *);struct thermal_zone_device *thermal_zone_get_zone_by_name (const char *name);
@@ -322,7 +327,13 @@ static inline struct thermal_cooling_device * thermal_cooling_device_register(const char *type, void *devdata, const struct thermal_cooling_device_ops *ops) { return ERR_PTR(-ENODEV); } +static inline struct thermal_cooling_device * +devm_thermal_cooling_device_register (struct device *dev,const char *type, +void *devdata,const struct thermal_cooling_device_ops *ops) +{ +return ERR_PTR (-ENODEV ); +}static inline void thermal_cooling_device_unregister (struct thermal_cooling_device *cdev)
Signature · 1 line
2.43.0
PATCH v2 5/12 thermal/of: Move the node pointer assignation in the OF code file
In reply to Daniel LezcanoThe node pointer being assigned to the cooling device structure is an action done by the thermal OF only and does not belong to the core framework code. Move the node pointer assignation in the thermal OF code. Consequently, the devm_thermal_of_cooling_device_register() can call its non-devm version resulting in a more intuitive design of the API.
--- drivers/thermal/thermal_core.c | 1 - drivers/thermal/thermal_of.c | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +0 -1
@@ -1085,7 +1085,6 @@ __thermal_cooling_device_register(const char *type, void *devdata,mutex_init (&cdev->lock);INIT_LIST_HEAD (&cdev->thermal_instances); - cdev->np = np; cdev->ops = ops; cdev->updated = false; cdev->device.class = &thermal_class;
drivers/thermal/thermal_of.c +2 -1
@@ -537,6 +537,7 @@ thermal_of_cooling_device_register(struct device_node *np,if (IS_ERR (cdev))return cdev; + cdev->np = np;thermal_cooling_device_init_complete (cdev);return cdev;
@@ -578,7 +579,7 @@ devm_thermal_of_cooling_device_register(struct device *dev,if (!ptr)return ERR_PTR (-ENOMEM ); - tcd =__thermal_cooling_device_register (np, type, devdata, ops); + tcd =thermal_of_cooling_device_register (np, type, devdata, ops);if (IS_ERR (tcd)) {devres_free (ptr);return tcd;
Signature · 1 line
2.43.0
PATCH v2 6/12 hwmon:: Use non-OF thermal cooling device register function
In reply to Daniel LezcanoMake HWMON drivers which are not based on device tree to use the corresponding non-OF functions.
--- drivers/hwmon/cros_ec_hwmon.c | 4 ++-- drivers/hwmon/dell-smm-hwmon.c | 4 ++-- drivers/hwmon/mlxreg-fan.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/hwmon/cros_ec_hwmon.c +2 -2
@@ -532,8 +532,8 @@ static void cros_ec_hwmon_register_fan_cooling_devices(struct device *dev, cpriv->hwmon_priv = priv; cpriv->index = i; - cdev =devm_thermal_of_cooling_device_register (dev,NULL , type, cpriv, - &cros_ec_thermal_cooling_ops); + cdev =devm_thermal_cooling_device_register (dev, type, cpriv, + &cros_ec_thermal_cooling_ops);if (IS_ERR (cdev)) {dev_warn (dev,"failed to register fan %zu as a cooling device: %pe\n" , i, cdev);
drivers/hwmon/dell-smm-hwmon.c +2 -2
@@ -1161,8 +1161,8 @@ static int dell_smm_init_cdev(struct device *dev, u8 fan_num)if (cdata) { cdata->fan_num = fan_num; cdata->data = data; - cdev =devm_thermal_of_cooling_device_register (dev,NULL , name, cdata, - &dell_smm_cooling_ops); + cdev =devm_thermal_cooling_device_register (dev, name, cdata, + &dell_smm_cooling_ops);if (IS_ERR (cdev)) {devm_kfree (dev, cdata); ret =PTR_ERR (cdev);
drivers/hwmon/mlxreg-fan.c +2 -2
@@ -583,8 +583,8 @@ static int mlxreg_fan_cooling_config(struct device *dev, struct mlxreg_fan *fan) pwm->fan = fan;/* Set minimal PWM speed. */ pwm->last_hwmon_state =MLXREG_FAN_PWM_DUTY2STATE (MLXREG_FAN_MIN_DUTY ); - pwm->cdev =devm_thermal_of_cooling_device_register (dev,NULL , mlxreg_fan_name[i], - pwm, &mlxreg_fan_cooling_ops); + pwm->cdev =devm_thermal_cooling_device_register (dev, mlxreg_fan_name[i], + pwm, &mlxreg_fan_cooling_ops);if (IS_ERR (pwm->cdev)) {dev_err (dev,"Failed to register cooling device\n" );return PTR_ERR (pwm->cdev);
Signature · 1 line
2.43.0
PATCH v2 7/12 thermal/core: Put of_node field cooling device structure under Kconfig option
In reply to Daniel LezcanoThe device node pointer in the cooling device structure is only needed by the thermal OF in order to bind a thermal zone with a cooling device. Now only the OF based drivers are using the thermal OF functions and do not call the function with a NULL device_node parameter as before. We can put this field under the CONFIG_THERMAL_OF condition and make it go away if the device tree is not used.
--- include/linux/thermal.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/thermal.h +3 -1
@@ -125,7 +125,6 @@ struct thermal_cooling_device {const char *type;unsigned long max_state;struct device device; -struct device_node *np;void *devdata;void *stats;const struct thermal_cooling_device_ops *ops;
@@ -133,6 +132,9 @@ struct thermal_cooling_device {struct mutex lock;/* protect thermal_instances list */ struct list_head thermal_instances;struct list_head node; +#ifdef CONFIG_THERMAL_OF +struct device_node *np; +#endif #ifdef CONFIG_THERMAL_DEBUGFS struct thermal_debugfs *debugfs;#endif
Signature · 1 line
2.43.0
PATCH v2 8/12 thermal/of: Rename the devm_thermal_of_cooling_device_register() function
In reply to Daniel LezcanoThe cooling devices can be composed with a cooling device controller and a set of cooling devices attached to it. Until now, the DT bindings were described using a node for the cooling device controller and child nodes for all the cooling devices.
Recently, a new set of cooling devices were proposed with the same bindings. Those were rejected because DT maintainers do not want this format anymore. In place, a cooling device will be created with an id. Whatever its meaning, the thermal OF will bind a thermal zone and a cooling device by checking the device node pointer + the id are matching the cooling map with the cooling device.
Actually this approach is consistent with the thermal which are also registered with a device and an id.
In order to do a distinction between the old binding with child nodes and the incoming new binding, let's rename the registering function with a self-explanatory name.
Rename the functions:
devm_thermal_of_cooling_device_register() -> devm_thermal_of_child_cooling_device_register()
Used the command:
find . -type f -name '*.[ch]' -exec \
sed -i 's/devm_thermal_of_cooling_device_register/\
devm_thermal_of_child_cooling_device_register/g' {} \;Did not used clang-format-diff because it does not indent correctly and checkpatch complained. Manually reindented to make checkpatch happy
--- drivers/hwmon/amc6821.c | 2 +- drivers/hwmon/aspeed-pwm-tacho.c | 5 +++-- drivers/hwmon/emc2305.c | 6 +++--- drivers/hwmon/gpio-fan.c | 6 ++++-- drivers/hwmon/max6650.c | 6 +++--- drivers/hwmon/npcm750-pwm-fan.c | 6 ++++-- drivers/hwmon/pwm-fan.c | 5 +++-- drivers/hwmon/qnap-mcu-hwmon.c | 6 +++--- drivers/hwmon/tc654.c | 5 +++-- drivers/memory/tegra/tegra210-emc-core.c | 4 ++-- drivers/soc/qcom/qcom_aoss.c | 2 +- drivers/thermal/khadas_mcu_fan.c | 7 ++++--- drivers/thermal/tegra/soctherm.c | 6 +++--- drivers/thermal/thermal_of.c | 15 +++++++++------ include/linux/thermal.h | 16 ++++++++-------- 15 files changed, 54 insertions(+), 43 deletions(-)
drivers/hwmon/amc6821.c +1 -1
@@ -1076,7 +1076,7 @@ static int amc6821_probe(struct i2c_client *client) "Failed to initialize hwmon\n");if (IS_ENABLED (CONFIG_THERMAL ) && fan_np && data->fan_cooling_levels) -return PTR_ERR_OR_ZERO (devm_thermal_of_cooling_device_register (dev, +return PTR_ERR_OR_ZERO (devm_thermal_of_child_cooling_device_register (dev, fan_np, client->name, data, &amc6821_cooling_ops));return 0 ;
drivers/hwmon/aspeed-pwm-tacho.c +3 -2
@@ -841,8 +841,9 @@ static int aspeed_create_pwm_cooling(struct device *dev, }snprintf (cdev->name,MAX_CDEV_NAME_LEN ,"%pOFn%d" , child, pwm_port); - cdev->tcdev =devm_thermal_of_cooling_device_register (dev, child, - cdev->name, cdev, &aspeed_pwm_cool_ops); + cdev->tcdev =devm_thermal_of_child_cooling_device_register (dev, child, + cdev->name, cdev, + &aspeed_pwm_cool_ops);if (IS_ERR (cdev->tcdev))return PTR_ERR (cdev->tcdev);
drivers/hwmon/emc2305.c +3 -3
@@ -309,9 +309,9 @@ static int emc2305_set_single_tz(struct device *dev, struct device_node *fan_nod pwm = data->pwm_min[cdev_idx]; data->cdev_data[cdev_idx].cdev = -devm_thermal_of_cooling_device_register (dev, fan_node, - emc2305_fan_name[idx], data, - &emc2305_cooling_ops); +devm_thermal_of_child_cooling_device_register (dev, fan_node, + emc2305_fan_name[idx], data, + &emc2305_cooling_ops);if (IS_ERR (data->cdev_data[cdev_idx].cdev)) {dev_err (dev,"Failed to register cooling device %s\n" , emc2305_fan_name[idx]);
drivers/hwmon/gpio-fan.c +4 -2
@@ -592,8 +592,10 @@ static int gpio_fan_probe(struct platform_device *pdev) }/* Optional cooling device register for Device tree platforms */ - fan_data->cdev =devm_thermal_of_cooling_device_register (dev, np, -"gpio-fan" , fan_data, &gpio_fan_cool_ops); + fan_data->cdev =devm_thermal_of_child_cooling_device_register (dev, np, +"gpio-fan" , + fan_data, + &gpio_fan_cool_ops);dev_info (dev,"GPIO fan initialized\n" );
drivers/hwmon/max6650.c +3 -3
@@ -793,9 +793,9 @@ static int max6650_probe(struct i2c_client *client)return err;if (IS_ENABLED (CONFIG_THERMAL )) { - cooling_dev =devm_thermal_of_cooling_device_register (dev, - dev->of_node, client->name, - data, &max6650_cooling_ops); + cooling_dev =devm_thermal_of_child_cooling_device_register (dev, dev->of_node, + client->name, data, + &max6650_cooling_ops);if (IS_ERR (cooling_dev)) {dev_warn (dev,"thermal cooling device register failed: %ld\n" ,PTR_ERR (cooling_dev));
drivers/hwmon/npcm750-pwm-fan.c +4 -2
@@ -857,8 +857,10 @@ static int npcm7xx_create_pwm_cooling(struct device *dev,snprintf (cdev->name,THERMAL_NAME_LENGTH ,"%pOFn%d" , child, pwm_port); - cdev->tcdev =devm_thermal_of_cooling_device_register (dev, child, - cdev->name, cdev, &npcm7xx_pwm_cool_ops); + cdev->tcdev =devm_thermal_of_child_cooling_device_register (dev, child, + cdev->name, + cdev, + &npcm7xx_pwm_cool_ops);if (IS_ERR (cdev->tcdev))return PTR_ERR (cdev->tcdev);
drivers/hwmon/pwm-fan.c +3 -2
@@ -685,8 +685,9 @@ static int pwm_fan_probe(struct platform_device *pdev) ctx->pwm_fan_state = ctx->pwm_fan_max_state;if (IS_ENABLED (CONFIG_THERMAL )) { - cdev =devm_thermal_of_cooling_device_register (dev, - dev->of_node,"pwm-fan" , ctx, &pwm_fan_cooling_ops); + cdev =devm_thermal_of_child_cooling_device_register (dev, dev->of_node, +"pwm-fan" , ctx, + &pwm_fan_cooling_ops);if (IS_ERR (cdev)) { ret =PTR_ERR (cdev); dev_err(dev,
drivers/hwmon/qnap-mcu-hwmon.c +3 -3
@@ -337,9 +337,9 @@ static int qnap_mcu_hwmon_probe(struct platform_device *pdev)* levels and only succeed with either no or correct cooling levels. */ if (IS_ENABLED (CONFIG_THERMAL ) && hwm->fan_cooling_levels) { - cdev =devm_thermal_of_cooling_device_register (dev, -to_of_node (hwm->fan_node),"qnap-mcu-hwmon" , - hwm, &qnap_mcu_hwmon_cooling_ops); + cdev =devm_thermal_of_child_cooling_device_register (dev,to_of_node (hwm->fan_node), +"qnap-mcu-hwmon" , hwm, + &qnap_mcu_hwmon_cooling_ops);if (IS_ERR (cdev))return dev_err_probe (dev,PTR_ERR (cdev),"Failed to register qnap-mcu-hwmon as cooling device\n" );
drivers/hwmon/tc654.c +3 -2
@@ -541,8 +541,9 @@ static int tc654_probe(struct i2c_client *client)if (IS_ENABLED (CONFIG_THERMAL )) {struct thermal_cooling_device *cdev; - cdev =devm_thermal_of_cooling_device_register (dev, dev->of_node, client->name, - hwmon_dev, &tc654_fan_cool_ops); + cdev =devm_thermal_of_child_cooling_device_register (dev, dev->of_node, + client->name, hwmon_dev, + &tc654_fan_cool_ops);return PTR_ERR_OR_ZERO (cdev); }
drivers/memory/tegra/tegra210-emc-core.c +2 -2
@@ -1966,8 +1966,8 @@ static int tegra210_emc_probe(struct platform_device *pdev)tegra210_emc_debugfs_init (emc); - cd =devm_thermal_of_cooling_device_register (emc->dev, np,"emc" , emc, - &tegra210_emc_cd_ops); + cd =devm_thermal_of_child_cooling_device_register (emc->dev, np,"emc" , emc, + &tegra210_emc_cd_ops);if (IS_ERR (cd)) { err =PTR_ERR (cd); dev_err(emc->dev, "failed to register cooling device: %d\n",
drivers/soc/qcom/qcom_aoss.c +1 -1
@@ -381,7 +381,7 @@ static int qmp_cooling_device_add(struct qmp *qmp, qmp_cdev->qmp = qmp; qmp_cdev->state = !qmp_cdev_max_state; qmp_cdev->name = cdev_name; - qmp_cdev->cdev =devm_thermal_of_cooling_device_register + qmp_cdev->cdev =devm_thermal_of_child_cooling_device_register (qmp->dev, node, cdev_name, qmp_cdev, &qmp_cooling_device_ops);
drivers/thermal/khadas_mcu_fan.c +4 -3
@@ -90,9 +90,10 @@ static int khadas_mcu_fan_probe(struct platform_device *pdev) ctx->mcu = mcu;platform_set_drvdata (pdev, ctx); - cdev =devm_thermal_of_cooling_device_register (dev->parent, - dev->parent->of_node,"khadas-mcu-fan" , ctx, - &khadas_mcu_fan_cooling_ops); + cdev =devm_thermal_of_child_cooling_device_register (dev->parent, + dev->parent->of_node, +"khadas-mcu-fan" , ctx, + &khadas_mcu_fan_cooling_ops);if (IS_ERR (cdev)) { ret =PTR_ERR (cdev); dev_err(dev, "Failed to register khadas-mcu-fan as cooling device: %d\n",
drivers/thermal/tegra/soctherm.c +3 -3
@@ -1700,9 +1700,9 @@ static void soctherm_init_hw_throt_cdev(struct platform_device *pdev) stc->init = true; }else { - tcd =devm_thermal_of_cooling_device_register (dev, np_stcc, - (char *)name, ts, - &throt_cooling_ops); + tcd =devm_thermal_of_child_cooling_device_register (dev, np_stcc, + (char *)name, ts, + &throt_cooling_ops);if (IS_ERR_OR_NULL (tcd)) { dev_err(dev, "throttle-cfg: %s: failed to register cooling device\n",
drivers/thermal/thermal_of.c +9 -6
@@ -550,7 +550,7 @@ static void thermal_cooling_device_release(struct device *dev, void *res) }/** -* devm_thermal_of_cooling_device_register() - register an OF thermal cooling +* devm_thermal_of_child_cooling_device_register() - register an OF thermal cooling * device * @dev: a valid struct device pointer of a sensor device. * @np: a pointer to a device tree node.
@@ -563,14 +563,17 @@ static void thermal_cooling_device_release(struct device *dev, void *res)* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself * to all the thermal zone devices registered at the same time. * +* This function should be used when a cooling controller has child +* nodes which are referenced in the thermal zone cooling map. +* * Return: a pointer to the created struct thermal_cooling_device or an * ERR_PTR. Caller must check return value with IS_ERR*() helpers. */ struct thermal_cooling_device * -devm_thermal_of_cooling_device_register (struct device *dev, -struct device_node *np, -const char *type,void *devdata, -const struct thermal_cooling_device_ops *ops) +devm_thermal_of_child_cooling_device_register (struct device *dev, +struct device_node *np, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops) {struct thermal_cooling_device **ptr, *tcd;
@@ -592,4 +595,4 @@ devm_thermal_of_cooling_device_register(struct device *dev,return tcd; } -EXPORT_SYMBOL_GPL (devm_thermal_of_cooling_device_register); +EXPORT_SYMBOL_GPL (devm_thermal_of_child_cooling_device_register);
include/linux/thermal.h +8 -8
@@ -205,10 +205,10 @@ thermal_of_cooling_device_register(struct device_node *np, const char *type, voi const struct thermal_cooling_device_ops *ops);struct thermal_cooling_device * -devm_thermal_of_cooling_device_register (struct device *dev, -struct device_node *np, -const char *type,void *devdata, -const struct thermal_cooling_device_ops *ops); +devm_thermal_of_child_cooling_device_register (struct device *dev, +struct device_node *np, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops);#else static inline
@@ -232,10 +232,10 @@ thermal_of_cooling_device_register(struct device_node *np, }static inline struct thermal_cooling_device * -devm_thermal_of_cooling_device_register (struct device *dev, -struct device_node *np, -const char *type,void *devdata, -const struct thermal_cooling_device_ops *ops) +devm_thermal_of_child_cooling_device_register (struct device *dev, +struct device_node *np, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops) {return ERR_PTR (-ENODEV ); }
Signature · 1 line
2.43.0
PATCH v2 9/12 thermal/of: Introduce cooling device of_index
In reply to Daniel LezcanoAs described in the previous change, the new incoming DT binding will describe a tuple device_node + cooling device id, instead of child nodes.
Let's augment thermal_of_cooling_device_register() to receive the cooling device id and propagate the changes to the four remaining drivers using the non-devm version of the cooling device registering function.
Add the id in the cooling device structure and store the value when registering it.
--- drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 5 +++-- drivers/thermal/cpufreq_cooling.c | 2 +- drivers/thermal/cpuidle_cooling.c | 2 +- drivers/thermal/devfreq_cooling.c | 2 +- drivers/thermal/thermal_of.c | 6 ++++-- include/linux/thermal.h | 6 ++++-- 6 files changed, 14 insertions(+), 9 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_gpu.c +3 -2
@@ -1791,8 +1791,9 @@ static int etnaviv_gpu_bind(struct device *dev, struct device *master,int ret;if (IS_ENABLED (CONFIG_DRM_ETNAVIV_THERMAL )) { - gpu->cooling =thermal_of_cooling_device_register (dev->of_node, - (char *)dev_name (dev), gpu, &cooling_ops); + gpu->cooling =thermal_of_cooling_device_register (dev->of_node,0 , +dev_name (dev), + gpu, &cooling_ops);if (IS_ERR (gpu->cooling))return PTR_ERR (gpu->cooling); }
drivers/thermal/cpufreq_cooling.c +1 -1
@@ -592,7 +592,7 @@ __cpufreq_cooling_register(struct device_node *np,if (!name) goto remove_qos_req; - cdev =thermal_of_cooling_device_register (np, name, cpufreq_cdev, + cdev =thermal_of_cooling_device_register (np,0 , name, cpufreq_cdev, cooling_ops);kfree (name);
drivers/thermal/cpuidle_cooling.c +1 -1
@@ -207,7 +207,7 @@ static int __cpuidle_cooling_register(struct device_node *np, goto out_unregister; } - cdev =thermal_of_cooling_device_register (np, name, idle_cdev, + cdev =thermal_of_cooling_device_register (np,0 , name, idle_cdev, &cpuidle_cooling_ops);if (IS_ERR (cdev)) { ret =PTR_ERR (cdev);
drivers/thermal/devfreq_cooling.c +1 -1
@@ -454,7 +454,7 @@ of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,if (!name) goto remove_qos_req; - cdev =thermal_of_cooling_device_register (np, name, dfc, ops); + cdev =thermal_of_cooling_device_register (np,0 , name, dfc, ops);kfree (name);if (IS_ERR (cdev)) {
drivers/thermal/thermal_of.c +4 -2
@@ -514,6 +514,7 @@ EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);/** * thermal_of_cooling_device_register() - register an OF thermal cooling device * @np: a pointer to a device tree node. +* @of_index: a cooling device index in the cooling controller * @type: the thermal cooling device type. * @devdata: device private data. * @ops: standard thermal cooling devices callbacks.
@@ -527,7 +528,7 @@ EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);* ERR_PTR. Caller must check return value with IS_ERR*() helpers. */ struct thermal_cooling_device * -thermal_of_cooling_device_register (struct device_node *np, +thermal_of_cooling_device_register (struct device_node *np,int of_index,const char *type,void *devdata,const struct thermal_cooling_device_ops *ops) {
@@ -538,6 +539,7 @@ thermal_of_cooling_device_register(struct device_node *np,return cdev; cdev->np = np; + cdev->of_index = of_index;thermal_cooling_device_init_complete (cdev);return cdev;
@@ -582,7 +584,7 @@ devm_thermal_of_child_cooling_device_register(struct device *dev,if (!ptr)return ERR_PTR (-ENOMEM ); - tcd =thermal_of_cooling_device_register (np, type, devdata, ops); + tcd =thermal_of_cooling_device_register (np,0 , type, devdata, ops);if (IS_ERR (tcd)) {devres_free (ptr);return tcd;
include/linux/thermal.h +4 -2
@@ -134,6 +134,7 @@ struct thermal_cooling_device {struct list_head node;#ifdef CONFIG_THERMAL_OF struct device_node *np; +int of_index;#endif #ifdef CONFIG_THERMAL_DEBUGFS struct thermal_debugfs *debugfs;
@@ -201,7 +202,8 @@ struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, invoid devm_thermal_of_zone_unregister (struct device *dev,struct thermal_zone_device *tz);struct thermal_cooling_device * -thermal_of_cooling_device_register (struct device_node *np,const char *type,void *devdata, +thermal_of_cooling_device_register (struct device_node *np,int of_index, +const char *type,void *data,const struct thermal_cooling_device_ops *ops); struct thermal_cooling_device *
@@ -224,7 +226,7 @@ static inline void devm_thermal_of_zone_unregister(struct device *dev, }static inline struct thermal_cooling_device * -thermal_of_cooling_device_register (struct device_node *np, +thermal_of_cooling_device_register (struct device_node *np,int of_index,const char *type,void *devdata,const struct thermal_cooling_device_ops *ops) {
Signature · 1 line
2.43.0
PATCH v2 10/12 thermal/of: Pass the of_index and add a function to register with an index
In reply to Daniel LezcanoIntroduce a new function devm_thermal_of_cooling_device_register() which will register a cooling device and its id.
--- drivers/thermal/thermal_of.c | 70 ++++++++++++++++++++++++++---------- include/linux/thermal.h | 13 +++++++ 2 files changed, 64 insertions(+), 19 deletions(-)
drivers/thermal/thermal_of.c +51 -19
@@ -551,6 +551,56 @@ static void thermal_cooling_device_release(struct device *dev, void *res)thermal_cooling_device_unregister (*(struct thermal_cooling_device **)res); } +static struct thermal_cooling_device * +__devm_thermal_of_cooling_device_register (struct device *dev,struct device_node *np, +int of_index,const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops) +{ +struct thermal_cooling_device **ptr, *tcd; + + ptr =devres_alloc (thermal_cooling_device_release,sizeof (*ptr), +GFP_KERNEL ); +if (!ptr) +return ERR_PTR (-ENOMEM ); + + tcd =thermal_of_cooling_device_register (np, of_index, type, devdata, ops); +if (IS_ERR (tcd)) { +devres_free (ptr); +return tcd; + } + + *ptr = tcd; +devres_add (dev, ptr); + +return tcd; +} + +/** +* devm_thermal_of_cooling_device_register() - register an OF thermal cooling device +* @dev: a valid struct device pointer of a sensor device. +* @of_index: a cooling device index in the cooling controller +* @type: the thermal cooling device type. +* @devdata: device private data. +* @ops: standard thermal cooling devices callbacks. +* +* This function will register a cooling device with device tree node reference. +* This interface function adds a new thermal cooling device (fan/processor/...) +* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself +* to all the thermal zone devices registered at the same time. +* +* Return: a pointer to the created struct thermal_cooling_device or an +* ERR_PTR. Caller must check return value with IS_ERR*() helpers. +*/ +struct thermal_cooling_device * +devm_thermal_of_cooling_device_register (struct device *dev,int of_index, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops) +{ +return __devm_thermal_of_cooling_device_register (dev, dev->of_node, of_index, + type, devdata, ops); +} +EXPORT_SYMBOL_GPL (devm_thermal_of_cooling_device_register); +/** * devm_thermal_of_child_cooling_device_register() - register an OF thermal cooling * device
@@ -577,24 +627,6 @@ devm_thermal_of_child_cooling_device_register(struct device *dev, const char *type, void *devdata, const struct thermal_cooling_device_ops *ops) { - struct thermal_cooling_device **ptr, *tcd; - - ptr =devres_alloc (thermal_cooling_device_release,sizeof (*ptr), -GFP_KERNEL ); -if (!ptr) -return ERR_PTR (-ENOMEM ); - - tcd =thermal_of_cooling_device_register (np,0 , type, devdata, ops); -if (IS_ERR (tcd)) { -devres_free (ptr); -return tcd; - } - - *ptr = tcd; -devres_add (dev, ptr); - -thermal_cooling_device_init_complete (tcd); - -return tcd; + return __devm_thermal_of_cooling_device_register(dev, np, 0, type, devdata, ops); }EXPORT_SYMBOL_GPL (devm_thermal_of_child_cooling_device_register);
include/linux/thermal.h +13 -0
@@ -206,6 +206,11 @@ thermal_of_cooling_device_register(struct device_node *np, int of_index, const char *type, void *data, const struct thermal_cooling_device_ops *ops); +struct thermal_cooling_device * +devm_thermal_of_cooling_device_register (struct device *dev,int of_index, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops); +struct thermal_cooling_device * devm_thermal_of_child_cooling_device_register(struct device *dev, struct device_node *np,
@@ -233,6 +238,14 @@ thermal_of_cooling_device_register(struct device_node *np, int of_index,return ERR_PTR (-ENODEV ); } +static inline struct thermal_cooling_device * +devm_thermal_of_cooling_device_register (struct device *dev,int of_index, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops) +{ +return ERR_PTR (-ENODEV ); +} +static inline struct thermal_cooling_device * devm_thermal_of_child_cooling_device_register(struct device *dev, struct device_node *np,
Signature · 1 line
2.43.0
PATCH v2 11/12 thermal/of: Process cooling device index in cooling-spec
In reply to Daniel LezcanoThe new DT bindings format describes a cooling device spec with the cooling device node, the id and the mitigation limits.
Depending on the version of the DT bindings, in order to bind, check with the device node pointer only or, in addition, the cooling device id.
--- drivers/thermal/thermal_of.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
drivers/thermal/thermal_of.c +22 -4
@@ -259,16 +259,34 @@ static bool thermal_of_get_cooling_spec(struct device_node *map_np, int index,of_node_put (cooling_spec.np); -if (cooling_spec.args_count <2 ) { -pr_err ("wrong reference to cooling device, missing limits\n" ); +/* +* There are two formats: +* - Legacy format : <&cdev lower upper> +* - New format : <&cdev of_index lower upper> +* +* With the new format, along with the device node pointer, +* the of_index must match with the cooling device of_index in +* order to bind +*/ +if (cooling_spec.args_count <2 || cooling_spec.args_count >3 ) { +pr_err ("Invalid number of cooling device parameters\n" );return false; } +if (cooling_spec.args_count ==3 && + cooling_spec.args[0 ] != cdev->of_index) +return false; +if (cooling_spec.np != cdev->np)return false; - c->lower = cooling_spec.args[0 ]; - c->upper = cooling_spec.args[1 ]; +if (cooling_spec.args_count !=3 ) { + c->lower = cooling_spec.args[0 ]; + c->upper = cooling_spec.args[1 ]; + }else { + c->lower = cooling_spec.args[1 ]; + c->upper = cooling_spec.args[2 ]; + } c->weight = weight;return true;
Signature · 1 line
2.43.0
PATCH v2 12/12 dt-bindings: thermal: cooling-devices: Update support for 3 cells cooling device
In reply to Daniel LezcanoFrom: Gaurav Kohli <[email protected]>
Extend the thermal cooling device binding to support a 3 cells specifier along with the 2 cells format.
Update #cooling-cells property to enum to support both 2 and 3 arguments.
Fix pwm-fan.yaml to restrict the number of cells to 'const: 2'
--- Documentation/devicetree/bindings/hwmon/pwm-fan.yaml | 3 ++- .../bindings/thermal/thermal-cooling-devices.yaml | 8 ++++++-- .../devicetree/bindings/thermal/thermal-zones.yaml | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/hwmon/pwm-fan.yaml +2 -1
@@ -63,7 +63,8 @@ properties: description: The PWM that is used to control the fan. maxItems: 1 - "#cooling-cells": true + "#cooling-cells": + const: 2 required: - compatible
Documentation/devicetree/bindings/thermal/thermal-cooling-devices.yaml +6 -2
@@ -44,10 +44,14 @@ select: true properties: "#cooling-cells": description: - Must be 2, in order to specify minimum and maximum cooling state used in + Must be 2 or 3. If 2, specifies minimum and maximum cooling state used in the cooling-maps reference. The first cell is the minimum cooling state and the second cell is the maximum cooling state requested. - const: 2 + If 3, the first cell specifies the thermal mitigation device specifier + index for devices that support multiple thermal mitigation mechanisms. + The two other cells are respectively the minimum cooling state and the + maximum cooling state. + enum: [2, 3] additionalProperties: true
Documentation/devicetree/bindings/thermal/thermal-zones.yaml +2 -1
@@ -214,7 +214,8 @@ patternProperties: device. Using the THERMAL_NO_LIMIT (-1UL) constant in the cooling-device phandle limit specifier lets the framework use the minimum and maximum cooling state for that cooling - device automatically. + device automatically. If three arguments are specified, + the first argument is the cooling device specifier. contribution: $ref: /schemas/types.yaml#/definitions/uint32
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 01/12] thermal/driver/tegra/soctherm: Use devm_ variant when registering a cooling device
In reply to Daniel LezcanoSashiko AI review found 3 potential issue(s): - [High] Hardware reset sequence completely bypassed during probe. - [High] Double clock disable during driver removal and failure to fix the described…
Sashiko AI review found 3 potential issue(s):
- [High] Hardware reset sequence completely bypassed during probe.
- [High] Double clock disable during driver removal and failure to fix the described race window.
- [Medium] Clock enablement and disablement order is reversed.
--
commit c2cefc8b595baea6413baca093c73c6b01f2bfd1
Author: Daniel Lezcano <[email protected]>
thermal/driver/tegra/soctherm: Use devm_ variant when registering a cooling device
The driver is updated to use devm_thermal_of_cooling_device_register() to automatically unregister the cooling device. To avoid a window where the cooling device remains registered while the hardware is powered down, it also switches to using devm_clk_get_enabled().
Quoted 4 lines
drivers/thermal/tegra/soctherm.c +0
-0
[ ... ]
drivers/thermal/tegra/soctherm.c static int tegra_soctherm_probe(struct platform_device *pdev) · line 2142
Quoted 13 lines
drivers/thermal/tegra/soctherm.c +2
-2@@ -2133,13 +2133,13 @@ static int tegra_soctherm_probe(struct platform_device *pdev)return PTR_ERR (tegra->reset); } - tegra->clock_tsensor =devm_clk_get (&pdev->dev,"tsensor" ); + tegra->clock_tsensor =devm_clk_get_enabled (&pdev->dev,"tsensor" );if (IS_ERR (tegra->clock_tsensor)) {dev_err (&pdev->dev,"can't get tsensor clock\n" );return PTR_ERR (tegra->clock_tsensor); } - tegra->clock_soctherm =devm_clk_get (&pdev->dev,"soctherm" ); + tegra->clock_soctherm =devm_clk_get_enabled (&pdev->dev,"soctherm" );
Does this invert the clock enablement order?
The original soctherm_clk_enable() enabled the soctherm clock before the tsensor clock:
drivers/thermal/tegra/soctherm.c:soctherm_clk_enable() {
...
if (enable) {
err = clk_prepare_enable(tegra->clock_soctherm);
if (err) {
...
}
err = clk_prepare_enable(tegra->clock_tsensor);
...
}By retrieving and enabling tsensor first with devm_clk_get_enabled(), the tsensor clock is now enabled before soctherm, and the devres LIFO cleanup will disable soctherm before tsensor.
Will this cause undefined behavior if the tsensor block relies on the main soctherm clock?
[ ... ]
drivers/thermal/tegra/soctherm.c static int tegra_soctherm_probe(struct platform_device *pdev) · line 2177
Quoted 8 lines
drivers/thermal/tegra/soctherm.c +0
-4@@ -2171,10 +2171,6 @@ static int tegra_soctherm_probe(struct platform_device *pdev)if (!tegra->thermctl_tzs)return -ENOMEM ; - err =soctherm_clk_enable (pdev, true); -if (err) -return err; -
By completely removing this call, doesn't it also bypass the hardware reset sequence?
Looking at soctherm_clk_enable(), it performs a hardware reset along with clock management:
drivers/thermal/tegra/soctherm.c:soctherm_clk_enable() {
...
reset_control_assert(tegra->reset);
if (enable) {
err = clk_prepare_enable(tegra->clock_soctherm);
...
err = clk_prepare_enable(tegra->clock_tsensor);
...
} else {
...
}
reset_control_deassert(tegra->reset);
...
}Without this sequence, will the thermal controller hardware remain in an uninitialized state?
[ ... ]
drivers/thermal/tegra/soctherm.c static int tegra_soctherm_probe(struct platform_device *pdev) · line 2217
Quoted 12 lines
drivers/thermal/tegra/soctherm.c +0
-5@@ -2218,11 +2212,6 @@ static int tegra_soctherm_probe(struct platform_device *pdev)soctherm_debug_init (pdev);return 0 ; - -disable_clocks: -soctherm_clk_enable (pdev, false); - -return err; }static void tegra_soctherm_remove (struct platform_device *pdev)
The commit message states:
Quoted 8 lines
A bug can be subtly introduced by this change because the driver still manually disables the hardware clocks during the probe error path and in
tegra_soctherm_remove(). This creates a window where the cooling device remains registered and exposed to the thermal core and sysfs while the underlying hardware is powered down.In order to prevent this potential issue, use the
devm_variant for the clk.
Does the race window described here remain open? The manual call to soctherm_clk_enable(pdev, false) is still present in tegra_soctherm_remove():
drivers/thermal/tegra/soctherm.c:tegra_soctherm_remove() {
...
soctherm_clk_enable(pdev, false);
}Since this executes before devres automatically unregisters the cooling device, the clocks are powered down while the cooling device is still registered.
Additionally, when devres later cleans up the clocks, will it cause a double-disable and clock reference count underflow since the clocks were already manually disabled?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v2 02/12] thermal/of: Move OF code where it belongs to
In reply to Daniel LezcanoSashiko AI review found 2 potential issue(s): - [High] A syntax error is introduced in `include/linux/thermal.h` when `CONFIG_THERMAL` is disabled, breaking compilation. Note: I found the fix later…
Sashiko AI review found 2 potential issue(s):
- [High] A syntax error is introduced in include/linux/thermal.h when CONFIG_THERMAL is disabled, breaking compilation. Note: I found the fix later in the series in commit fb0063667bcf5 ("thermal/core: Remove node pointer parameter when registering a tz").
- [High] The patch intentionally breaks cooling device registration for non-OF drivers on systems without CONFIG_THERMAL_OF enabled, causing runtime regressions. Note: I found the fix later in the series in commit b79ce9cd7a188 ("hwmon:: Use non-OF thermal cooling device register function"). --
commit 7dfab179cb2bd36aaace42ff2d1aeceaaeb233da
Author: Daniel Lezcano <[email protected]>
thermal/of: Move OF code where it belongs to
This commit moves thermal_of_cooling_device_register and devm_thermal_of_cooling_device_register from thermal-core to thermal-of. It adds temporary stubs returning errors for non-OF drivers when CONFIG_THERMAL_OF is disabled.
Quoted 10 lines
As the thermal-of file depends on
CONFIG_THERMAL_OF=y, by moving these two functions in it, when non-OF drivers are using this API that results in a link issue. In order to prevent unnecessary circumvolutions with the changes, the stubs have been added in the header file resulting in having the non-OF drivers compiled without theCONFIG_THERMAL_OF=yusing these stubs and failing to register their cooling devices.This is temporary as the next changes introduces the functions for non-OF drivers and returning to the initial state.
Is it acceptable to temporarily break cooling device registration for non-OF drivers when CONFIG_THERMAL_OF is disabled?
Since the stubs return ERR_PTR(-ENODEV), it looks like non-OF drivers will fail to register their cooling devices during this window.
I noticed this is fixed later in the series in commit b79ce9cd7a188, but does introducing an intentional functional regression here break git bisect?
Quoted 3 lines
include/linux/thermal.h +0
-0
[ ... ]
Quoted 10 lines
include/linux/thermal.h +0
-6@@ -305,18 +323,7 @@ thermal_cooling_device_register(const char *type, void *devdata, const struct thermal_cooling_device_ops *ops) {return ERR_PTR (-ENODEV ); } static inline struct thermal_cooling_device * -thermal_of_cooling_device_register (struct device_node *np, -const char *type,void *devdata, -const struct thermal_cooling_device_ops *ops) -{return ERR_PTR (-ENODEV ); } -static inline struct thermal_cooling_device * -devm_thermal_of_cooling_device_register(struct device *dev,
include/linux/thermal.h thermal_cooling_device_register(const char *type, void *devdata, · line 329
Quoted 10 lines
include/linux/thermal.h +1
-6-struct device_node *np, - const char *type, void *devdata, - const struct thermal_cooling_device_ops *ops) -{ - return ERR_PTR(-ENODEV); -} +static inline void thermal_cooling_device_unregister (struct thermal_cooling_device *cdev) { }
Does this leave a dangling return type that causes a compiler syntax error when CONFIG_THERMAL is disabled?
I see this was also fixed later in the series by commit fb0063667bcf5, but does leaving this invalid C syntax here hurt bisectability for builds without CONFIG_THERMAL?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v2 04/12] thermal/core: Remove node pointer parameter when registering a tz
In reply to Daniel LezcanoSashiko AI review found 4 potential issue(s): - [High] Compilation failure due to undeclared identifier `np` in `__thermal_cooling_device_register`. - [High] Compilation failure due to argument…
Sashiko AI review found 4 potential issue(s):
- [High] Compilation failure due to undeclared identifier np in __thermal_cooling_device_register.
- [High] Compilation failure due to argument count mismatch in devm_thermal_of_cooling_device_register.
- [High] Device tree (OF) cooling devices silently fail to bind to thermal zones.
- [Low] Misleading kerneldoc for devm_thermal_cooling_device_register.
--
commit fb0063667bcf5ae686659ae0c320b2fb8a2e567e
Author: Daniel Lezcano <[email protected]>
thermal/core: Remove node pointer parameter when registering a tz
This patch removes the device_node pointer parameter in the core function registering a thermal cooling device, which is inconsistent with non-OF code.
Quoted 4 lines
drivers/thermal/thermal_core.c +0
-0
[ ... ]
Quoted 7 lines
drivers/thermal/thermal_core.c +0
-2@@ -1050,15 +1049,12 @@ void thermal_cooling_device_init_complete(struct thermal_cooling_device *cdev)* This interface function adds a new thermal cooling device (fan/processor/...) * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself * to all the thermal zone devices registered at the same time. -* It also gives the opportunity to link the cooling device to a device tree -* node, so that it can be bound to a thermal zone created out of device tree. *
drivers/thermal/thermal_core.c void thermal_cooling_device_init_complete(struct thermal_cooling_device *cdev) · line 1060
Quoted 10 lines
drivers/thermal/thermal_core.c +1
-2* Return: a pointer to the created struct thermal_cooling_device or an * ERR_PTR. Caller must check return value with IS_ERR*() helpers. */ struct thermal_cooling_device * -__thermal_cooling_device_register (struct device_node *np, -const char *type,void *devdata, +__thermal_cooling_device_register (const char *type,void *devdata,const struct thermal_cooling_device_ops *ops) {struct thermal_cooling_device *cdev;
Does removing the np parameter here cause a compilation failure?
The assignment cdev->np = np; was left untouched further down in the function body, which results in an undeclared identifier error.
(Note: This regression is fixed later in the series by commit 1b7fe95232020)
[ ... ]
Quoted 7 lines
drivers/thermal/thermal_core.c +3
-0@@ -1167,6 +1163,50 @@ thermal_cooling_device_register(const char *type, void *devdata, }EXPORT_SYMBOL_GPL (thermal_cooling_device_register); +static void thermal_cooling_device_release (struct device *dev,void *res) +{ +thermal_cooling_device_unregister (*(struct thermal_cooling_device **)res);
drivers/thermal/thermal_core.c thermal_cooling_device_register(const char *type, void *devdata, · line 1178
Quoted 10 lines
drivers/thermal/thermal_core.c +10
-0+} + +/** +* devm_thermal_cooling_device_register() - register a thermal cooling device +* @dev: a valid struct device pointer of a sensor device. +* @type: the thermal cooling device type. +* @devdata: device private data. +* @ops: standard thermal cooling devices callbacks. +* +* This function will register a cooling device with device tree node reference.
This isn't a bug, but is this kerneldoc accurate?
This function is the generic (non-OF) devres variant and does not extract or assign any device tree node reference. It appears to be a copy-paste error from the OF-specific variant.
Quoted 10 lines
drivers/thermal/thermal_core.c +10
-0+* This interface function adds a new thermal cooling device (fan/processor/...) +* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself +* to all the thermal zone devices registered at the same time. +* +* Return: a pointer to the created struct thermal_cooling_device or an +* ERR_PTR. Caller must check return value with IS_ERR*() helpers. +*/ +struct thermal_cooling_device * +devm_thermal_cooling_device_register (struct device *dev,const char *type, +void *devdata,const struct thermal_cooling_device_ops *ops)
[ ... ]
drivers/thermal/thermal_core.h void thermal_governor_update_tz(struct thermal_zone_device *tz, · line 276
Quoted 12 lines
drivers/thermal/thermal_core.h +1
-2@@ -272,8 +272,7 @@ void thermal_governor_update_tz(struct thermal_zone_device *tz,void thermal_cooling_device_init_complete (struct thermal_cooling_device *cdev);struct thermal_cooling_device * -__thermal_cooling_device_register (struct device_node *np, -const char *type,void *devdata, +__thermal_cooling_device_register (const char *type,void *devdata,const struct thermal_cooling_device_ops *ops);
Does changing this prototype cause a compilation failure due to an argument count mismatch in devm_thermal_of_cooling_device_register()?
The existing call inside devm_thermal_of_cooling_device_register() in drivers/thermal/thermal_of.c was not updated and still passes 4 arguments.
(Note: This regression is fixed later in the series by commit 1b7fe95232020)
[ ... ]
drivers/thermal/thermal_of.c thermal_of_cooling_device_register(struct device_node *np, · line 538
Quoted 12 lines
drivers/thermal/thermal_of.c +1
-1@@ -533,7 +533,7 @@ thermal_of_cooling_device_register(struct device_node *np, {struct thermal_cooling_device *cdev; - cdev =__thermal_cooling_device_register (np, type, devdata, ops); + cdev =__thermal_cooling_device_register (type, devdata, ops);if (IS_ERR (cdev))return cdev;
Does this silently break device tree thermal zone cooling bindings?
Since the np parameter is removed from the core registration function, cdev->np is no longer populated there. The np parameter is passed here, but not manually assigned to cdev->np.
Because cdev is zero-allocated, cdev->np remains NULL. When thermal_cooling_device_init_complete() runs to bind the cooling device, the check if (cooling_spec.np != cdev->np) will unconditionally fail.
(Note: This regression is fixed later in the series by commit 1b7fe95232020)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH v2 05/12] thermal/of: Move the node pointer assignation in the OF code file
In reply to Daniel LezcanoSashiko AI review found 1 potential issue(s): - [High] The patch introduces a double call to `thermal_cooling_device_init_complete()` in `devm_thermal_of_cooling_device_register()`, leading to…
Sashiko AI review found 1 potential issue(s):
- [High] The patch introduces a double call to thermal_cooling_device_init_complete() in devm_thermal_of_cooling_device_register(), leading to linked list corruption. --
commit 1b7fe952320202b48dbc54a3f4bad412d0bfc910
Author: Daniel Lezcano <[email protected]>
thermal/of: Move the node pointer assignation in the OF code file
This commit moves the assignment of the device node pointer from the thermal core framework to the thermal OF code. It updates devm_thermal_of_cooling_device_register() to use thermal_of_cooling_device_register() instead of __thermal_cooling_device_register(), creating a more consistent API design.
Quoted 12 lines
drivers/thermal/thermal_of.c +1
-0@@ -537,6 +537,7 @@ thermal_of_cooling_device_register(struct device_node *np,if (IS_ERR (cdev))return cdev; + cdev->np = np;thermal_cooling_device_init_complete (cdev);return cdev;
[ ... ]
drivers/thermal/thermal_of.c devm_thermal_of_cooling_device_register(struct device *dev, · line 582
Quoted 6 lines
drivers/thermal/thermal_of.c +1
-1@@ -578,7 +579,7 @@ devm_thermal_of_cooling_device_register(struct device *dev,if (!ptr)return ERR_PTR (-ENOMEM ); - tcd =__thermal_cooling_device_register (np, type, devdata, ops); + tcd =thermal_of_cooling_device_register (np, type, devdata, ops);
Does this result in thermal_cooling_device_init_complete() being called twice for the same cooling device?
When thermal_of_cooling_device_register() is called, it successfully initializes the device and calls thermal_cooling_device_init_complete(), which adds the device to the list.
Then devm_thermal_of_cooling_device_register() explicitly calls thermal_cooling_device_init_complete() again:
drivers/thermal/thermal_of.c:devm_thermal_of_cooling_device_register() {
...
tcd = thermal_of_cooling_device_register(np, type, devdata, ops);
if (IS_ERR(tcd)) {
devres_free(ptr);
return tcd;
}
*ptr = tcd;
devres_add(dev, ptr);
thermal_cooling_device_init_complete(tcd);
...
}Could this cause list_add() to be called twice on the same node, corrupting the doubly-linked list structure?
While this issue is fixed later in the series by commit 61302b0ce72b9 (thermal/of: Pass the of_index and add a function to register with an index), should it be fixed here to avoid breaking bisectability?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v2 07/12] thermal/core: Put of_node field cooling device structure under Kconfig option
In reply to Daniel LezcanoSashiko AI review found 1 potential issue(s): - [Critical] Double initialization of the cooling device leads to linked list corruption in devm_thermal_of_cooling_device_register(). --
Sashiko AI review found 1 potential issue(s):
- [Critical] Double initialization of the cooling device leads to linked list corruption in devm_thermal_of_cooling_device_register(). --
commit ec613b33ae03267fedce00f5da67c6af4d2e4061
Author: Daniel Lezcano <[email protected]>
thermal/core: Put of_node field cooling device structure under Kconfig option
The device node pointer in the cooling device structure is only needed by the thermal OF in order to bind a thermal zone with a cooling device. Now only the OF based drivers are using the thermal OF functions and do not call the function with a NULL device_node parameter as before. We can put this field under the CONFIG_THERMAL_OF condition and make it go away if the device tree is not used.
Quoted 13 lines
include/linux/thermal.h +0
-1@@ -125,7 +125,6 @@ struct thermal_cooling_device {const char *type;unsigned long max_state;struct device device; -struct device_node *np;void *devdata;void *stats;const struct thermal_cooling_device_ops *ops;
include/linux/thermal.h struct thermal_cooling_device { · line 137
Quoted 6 lines
include/linux/thermal.h +3
-0@@ -133,6 +132,9 @@ struct thermal_cooling_device {struct mutex lock;/* protect thermal_instances list */ struct list_head thermal_instances;struct list_head node; +#ifdef CONFIG_THERMAL_OF +struct device_node *np; +#endif
This isn't a bug in this patch, but I noticed a regression introduced by an earlier patch in the series that affects thermal OF cooling device registration.
Does devm_thermal_of_cooling_device_register() call thermal_cooling_device_init_complete() twice on the same cooling device?
When devm_thermal_of_cooling_device_register() calls thermal_of_cooling_device_register(), it successfully registers the device and calls thermal_cooling_device_init_complete(cdev), which adds the node to thermal_cdev_list via list_add().
After returning, devm_thermal_of_cooling_device_register() calls thermal_cooling_device_init_complete(tcd) again with the same pointer.
Calling list_add() twice on the same node corrupts the linked list. If CONFIG_DEBUG_LIST is disabled, the node's next pointer points to itself, causing infinite loops during subsequent traversals of thermal_cdev_list.
I see this regression was fixed later in the series in commit 61302b0ce72b9 ("thermal/of: Pass the of_index and add a function to register with an index").
Quoted 3 lines
include/linux/thermal.h +0
-0#ifdef CONFIG_THERMAL_DEBUGFS struct thermal_debugfs *debugfs;#endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH v2 08/12] thermal/of: Rename the devm_thermal_of_cooling_device_register() function
In reply to Daniel LezcanoSashiko AI review found 4 potential issue(s): - [Critical] Double call to thermal_cooling_device_init_complete() corrupts thermal_cdev_list. - [High] cdev->np is stored without acquiring a…
Sashiko AI review found 4 potential issue(s):
- [Critical] Double call to thermal_cooling_device_init_complete() corrupts thermal_cdev_list.
- [High] cdev->np is stored without acquiring a reference, leading to a dangling pointer after the OF iterator loops finish.
- [Low] The mass-rename forces standalone cooling devices to use an API explicitly named and documented for child nodes.
- [Low] The non-devres equivalent of the API was not renamed, resulting in an inconsistent subsystem interface. --
commit f1de025111783e26ec2bab8fd8078236f785c8b4
Author: Daniel Lezcano <[email protected]>
thermal/of: Rename the devm_thermal_of_cooling_device_register() function
This commit renames devm_thermal_of_cooling_device_register() to devm_thermal_of_child_cooling_device_register() to differentiate between cooling devices bound via child nodes and a proposed new binding. The rename was applied globally across drivers using a sed script.
drivers/hwmon/pwm-fan.c static int pwm_fan_probe(struct platform_device *pdev) · line 690
Quoted 13 lines
drivers/hwmon/pwm-fan.c +3
-2@@ -685,8 +685,9 @@ static int pwm_fan_probe(struct platform_device *pdev) ctx->pwm_fan_state = ctx->pwm_fan_max_state;if (IS_ENABLED (CONFIG_THERMAL )) { - cdev =devm_thermal_of_cooling_device_register (dev, - dev->of_node,"pwm-fan" , ctx, &pwm_fan_cooling_ops); + cdev =devm_thermal_of_child_cooling_device_register (dev, dev->of_node, +"pwm-fan" , ctx, + &pwm_fan_cooling_ops);
This isn't a bug, but does it make sense to use an API explicitly named and documented for child nodes here?
Many standalone drivers like pwm-fan, gpio-fan, and max6650 simply pass their main device node (dev->of_node) rather than a child node, which creates a semantic contradiction with the new name.
[ ... ]
Quoted 4 lines
drivers/thermal/thermal_of.c +0
-0
[ ... ]
Quoted 10 lines
drivers/thermal/thermal_of.c +3
-0@@ -563,14 +563,17 @@ static void thermal_cooling_device_release(struct device *dev, void *res)* to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself * to all the thermal zone devices registered at the same time. * +* This function should be used when a cooling controller has child +* nodes which are referenced in the thermal zone cooling map. +* * Return: a pointer to the created struct thermal_cooling_device or an * ERR_PTR. Caller must check return value with IS_ERR*() helpers. */
drivers/thermal/thermal_of.c static void thermal_cooling_device_release(struct device *dev, void *res) · line 577
Quoted 10 lines
drivers/thermal/thermal_of.c +4
-4struct thermal_cooling_device * -devm_thermal_of_cooling_device_register (struct device *dev, -struct device_node *np, -const char *type,void *devdata, -const struct thermal_cooling_device_ops *ops) +devm_thermal_of_child_cooling_device_register (struct device *dev, +struct device_node *np, +const char *type,void *devdata, +const struct thermal_cooling_device_ops *ops) {
This isn't a bug, but could this mass-rename cause confusion since the non-devres counterpart (thermal_of_cooling_device_register) was not renamed? Leaving one unchanged while renaming the other creates an inconsistent subsystem API.
Also, since this API is now explicitly targeted at child nodes, which are often iterated over using macros like for_each_child_of_node() or for_each_child_of_node_scoped(), could this promote a use-after-free issue?
These iterators automatically drop the node reference when advancing or going out of scope. If thermal_of_cooling_device_register() stores cdev->np = np without acquiring a reference via of_node_get(), cdev->np will become a dangling pointer once the loop finishes.
drivers/thermal/thermal_of.c devm_thermal_of_cooling_device_register(struct device *dev, · line 598
Quoted 8 lines
drivers/thermal/thermal_of.c +1
-1struct thermal_cooling_device **ptr, *tcd;@@ -592,4 +595,4 @@ devm_thermal_of_cooling_device_register(struct device *dev,return tcd; } -EXPORT_SYMBOL_GPL (devm_thermal_of_cooling_device_register); +EXPORT_SYMBOL_GPL (devm_thermal_of_child_cooling_device_register);
Can the double call to thermal_cooling_device_init_complete() cause an infinite loop during list traversal?
Looking at the code just above the return in this function (affected by earlier commits):
tcd = thermal_of_cooling_device_register(np, type, devdata, ops);
if (IS_ERR(tcd)) {
devres_free(ptr);
return tcd;
}
*ptr = tcd;
devres_add(dev, ptr);
thermal_cooling_device_init_complete(tcd);thermal_of_cooling_device_register() already calls thermal_cooling_device_init_complete(), which adds the cooling device to thermal_cdev_list via list_add(). Calling it a second time here overwrites the list pointers, establishing a cyclic reference.
I noticed this regression was introduced by an earlier commit ("thermal/core: Make thermal_cooling_device_init_complete() non static") and a fix was provided later in the series in commit ("thermal/of: Pass the of_index and add a function to register with an index").
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewRe: [PATCH v2 09/12] thermal/of: Introduce cooling device of_index
In reply to Daniel LezcanoSashiko AI review found 1 potential issue(s): - [Critical] Redundant call to `thermal_cooling_device_init_complete()` in `devm_thermal_of_child_cooling_device_register()` causes list corruption and…
Sashiko AI review found 1 potential issue(s):
- [Critical] Redundant call to thermal_cooling_device_init_complete() in devm_thermal_of_child_cooling_device_register() causes list corruption and system hangs. --
commit 21654d6f9a89c28278edde82f1b1199e437fb996
Author: Daniel Lezcano <[email protected]>
thermal/of: Introduce cooling device of_index
This commit augments thermal_of_cooling_device_register() to receive a cooling device id. It stores this id in the cooling device structure and updates the remaining drivers to use the new signature.
Quoted 4 lines
drivers/thermal/thermal_of.c +0
-0
[ ... ]
Quoted 8 lines
drivers/thermal/thermal_of.c +1
-0@@ -538,6 +539,7 @@ thermal_of_cooling_device_register(struct device_node *np,return cdev; cdev->np = np; + cdev->of_index = of_index;thermal_cooling_device_init_complete (cdev);return cdev;
[ ... ]
drivers/thermal/thermal_of.c devm_thermal_of_child_cooling_device_register(struct device *dev, · line 590
Quoted 9 lines
drivers/thermal/thermal_of.c +1
-1@@ -582,7 +584,7 @@ devm_thermal_of_child_cooling_device_register(struct device *dev,if (!ptr)return ERR_PTR (-ENOMEM ); - tcd =thermal_of_cooling_device_register (np, type, devdata, ops); + tcd =thermal_of_cooling_device_register (np,0 , type, devdata, ops);if (IS_ERR (tcd)) {devres_free (ptr);return tcd;
Could this regression cause list corruption by calling thermal_cooling_device_init_complete() redundantly?
It looks like thermal_of_cooling_device_register() internally invokes thermal_cooling_device_init_complete() just above. Immediately after, devm_thermal_of_child_cooling_device_register() calls thermal_cooling_device_init_complete() again on the same object:
drivers/thermal/thermal_of.c:devm_thermal_of_child_cooling_device_register() {
...
tcd = thermal_of_cooling_device_register(np, 0, type, devdata, ops);
if (IS_ERR(tcd)) {
devres_free(ptr);
return tcd;
}
*ptr = tcd;
devres_add(dev, ptr);
thermal_cooling_device_init_complete(tcd);
...
}Does calling list_add() twice on the same node overwrite its pointers and create a cycle where new->next = new, potentially causing an infinite loop during subsequent traversals of thermal_cdev_list if debug list checking is not enabled?
I noticed this problem is present in the code at this commit, but it is fixed later in the series by commit 61302b0ce72b9 ("thermal/of: Pass the of_index and add a function to register with an index") which removes the duplicate call.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [PATCH v2 12/12] dt-bindings: thermal: cooling-devices: Update support for 3 cells cooling device
In reply to Daniel LezcanoSashiko AI review found 1 potential issue(s): - [High] Schema Validation Loophole: Relaxing the global `#cooling-cells` property allows invalid device tree configurations to pass schema validation. --
Sashiko AI review found 1 potential issue(s):
- [High] Schema Validation Loophole: Relaxing the global #cooling-cells property allows invalid device tree configurations to pass schema validation. --
commit 4d1db5332aa8215862512cf05a640de7afe86998
Author: Gaurav Kohli <[email protected]>
dt-bindings: thermal: cooling-devices: Update support for 3 cells cooling device
This commit extends the thermal cooling device binding to support a 3 cells specifier in addition to the 2 cells format. It updates the core #cooling-cells property to an enum allowing both 2 and 3 arguments, and adds an explicit constraint to pwm-fan.yaml.
Quoted 3 lines
Documentation/devicetree/bindings/thermal/thermal-cooling-devices.yaml +0
-0
[ ... ]
Quoted 4 lines
Documentation/devicetree/bindings/thermal/thermal-cooling-devices.yaml +0
-0@@ -44,10 +44,14 @@ select: true properties: "#cooling-cells": description:
Documentation/devicetree/bindings/thermal/thermal-cooling-devices.yaml select: true · line 54
Quoted 10 lines
Documentation/devicetree/bindings/thermal/thermal-cooling-devices.yaml +6
-2- Must be 2, in order to specify minimum and maximum cooling state used in + Must be 2 or 3. If 2, specifies minimum and maximum cooling state used in the cooling-maps reference. The first cell is the minimum cooling state and the second cell is the maximum cooling state requested. - const: 2 + If 3, the first cell specifies the thermal mitigation device specifier + index for devices that support multiple thermal mitigation mechanisms. + The two other cells are respectively the minimum cooling state and the + maximum cooling state. + enum: [2, 3]
By relaxing the core schema to allow 3 cells globally, does this unintentionally create a validation loophole for other cooling providers?
Many specific cooling provider bindings (such as nvidia,tegra210-emc.yaml or CPU nodes via arm/cpus.yaml) completely omitted local #cooling-cells constraints because they relied entirely on this core schema to enforce
const: 2.
Since the core schema now allows 3 cells, any device binding lacking an explicit const: 2 override will silently accept 3 cells, even if the hardware or driver only supports 2.
Have all other existing cooling providers been audited and updated to ensure they have a local const: 2 constraint where needed, similar to what was done for pwm-fan.yaml?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Re: [PATCH v2 12/12] dt-bindings: thermal: cooling-devices: Update support for 3 cells cooling device
In reply to Daniel LezcanoQuoted 11 lines · Daniel Lezcano
From: Gaurav Kohli <[email protected]>
Extend the thermal cooling device binding to support a 3 cells specifier along with the 2 cells format.
Update #cooling-cells property to enum to support both 2 and 3 arguments.
Fix
pwm-fan.yamlto restrict the number of cells to 'const: 2'Signed-off-by Gaurav Kohli <[email protected]>Signed-off-by Daniel Lezcano <[email protected]>
Best regards,
Krzysztof