Add thermal thresholds support
cover Add thermal thresholds support
The trip points are a firmware description of the temperature limits of a specific thermal zone where we associate an action which is done by the kernel. The time resolution is low.
The userspace has to deal with a more complex thermal management based on heuristics from different information coming from different places. The logic is much more complex but based on a bigger time resolution, usually one second based.
The purpose of the userspace is to monitor the temperatures from different places and take actions. However, it can not be constantly reading the temperature to detect when a temperature threshold has been reached. This is especially bad for mobile or embedded system as that will lead to an unacceptable number of wakeup to check the temperature with nothing to do.
On the other side, the sensors are now most of the time interrupt driven. That means the thermal framework will use the temperature trip points to program the sensor to trigger an interrupt when a temperature limit is crossed.
Unfortunately, the userspace can not benefit this feature and current solutions found here and there, iow out-of-tree, are to add fake trip points in the firmware and enable the writable trip points.
This is bad for different reasons, the trip points are for in-kernel actions, the semantic of their types is used by the thermal framework and by adding trip points in the device tree is a way to overcome the current limitation but tampering with how the thermal framework is supposed to work. The writable trip points is a way to adjust a temperature limit given a specific platform if the firmware is not accurate enough and TBH it is more a debug feature from my POV.
The thresholds mechanism is a way to have the userspace to tell thermal framework to send a notification when a temperature limit is crossed. There is no id, no hysteresis, just the temperature and the direction of the limit crossing. That means we can be notified when a threshold is crossed the way up only, or the way down only or both ways. That allows to create hysteresis values if it is needed.
A threshold can be added, deleted or flushed. The latter means all thresholds belonging to a thermal zone will be deleted.
When a threshold is added:
- if the same threshold (temperature and direction) exists, an error is returned
- if a threshold is specified with the same temperature but a different direction, the specified direction is added
- if there is no threshold with the same temperature then it is created
When a threshold is deleted:
- if the same threshold (temperature and direction) exists, it is deleted
- if a threshold is specified with the same temperature but a different direction, the specified direction is removed
- if there is no threshold with the same temperature, then an error is returned
When the threshold are flushed:
- All thresholds related to a thermal zone are deleted
When a threshold is crossed:
- the userspace does not need to know which threshold(s) have been crossed, it will be notified with the thermal zone identifier, the current temperature and the previous temperature
- if multiple thresholds have been crossed between two updates only one notification will be send to the userspace, it is pointless to send a notification per thresholds crossed as the userspace can handle that easily when it has the temperature delta information
All aforementioned actions and events lead to a notification to the userspace. A threshold change (add, delete and flush) is notified to the userspace with the process id responsible of the action.
Along with the kernel changes, the thermal library has been extended to provide the different API to deal with the new threshold netlink events and commands.
In addition, the thermal-engine skeleton uses these new API by flushing and adding thresholds as well as getting the notification about these actions.
Overall the series has been tested with the thermal-engine skeleton and some selftests which are not part of this series.
Daniel Lezcano (7):
thermal/core: Encapsulate more handle_thermal_trip thermal/core: Add thresholds support
thermal/core: Connect the threshold with the core thermal/netlink: Add the commands and the events for the thresholds tools/lib/thermal: Make more generic the command encoding function tools/lib/thermal: Add the threshold netlink ABI tools/thermal/thermal-engine: Take into account the thresholds API
drivers/thermal/Kconfig | 15 ++ drivers/thermal/Makefile | 3 + drivers/thermal/thermal_core.c | 109 ++++---- drivers/thermal/thermal_core.h | 6 +- drivers/thermal/thermal_netlink.c | 239 ++++++++++++++++- drivers/thermal/thermal_netlink.h | 7 + drivers/thermal/thermal_thresholds.c | 246 ++++++++++++++++++ drivers/thermal/thermal_thresholds.h | 54 ++++ drivers/thermal/thermal_trip.c | 12 +- include/linux/thermal.h | 3 + include/uapi/linux/thermal.h | 30 ++- tools/lib/thermal/commands.c | 167 +++++++++++- tools/lib/thermal/events.c | 58 ++++- tools/lib/thermal/include/thermal.h | 40 +++ tools/lib/thermal/libthermal.map | 5 + tools/lib/thermal/thermal.c | 17 ++ tools/thermal/thermal-engine/thermal-engine.c | 109 +++++++- 17 files changed, 1016 insertions(+), 104 deletions(-) create mode 100644 drivers/thermal/thermal_thresholds.c create mode 100644 drivers/thermal/thermal_thresholds.h
Signature · 1 line
2.43.0
PATCH 1/7 thermal/core: Encapsulate more handle_thermal_trip
In order to set the scene for the thresholds support which have to manipulate the low and high temperature boundaries for the interrupt support, we must pass the low and high value the incoming thresholds routine.
Instead of looping in the trip descriptors in thermal_zone_device_update(), we move the loop in the handle_thermal_trip() function and use it to set the low and high values.
As these variables can be set directly in the handle_thermal_trip(), we can get rid of a descriptors loop found in the thermal_set_trips() function as low and high are set in handle_thermal_trip().
--- drivers/thermal/thermal_core.c | 104 +++++++++++++++++++-------------- drivers/thermal/thermal_core.h | 2 +- drivers/thermal/thermal_trip.c | 12 +--- 3 files changed, 62 insertions(+), 56 deletions(-)
drivers/thermal/thermal_core.c +60 -44
@@ -425,59 +425,74 @@ static void handle_critical_trips(struct thermal_zone_device *tz, }static void handle_thermal_trip (struct thermal_zone_device *tz, -struct thermal_trip_desc *td,struct list_head *way_up_list, -struct list_head *way_down_list) +struct list_head *way_down_list, +int *low,int *high) { -const struct thermal_trip *trip = &td->trip; +struct thermal_trip_desc *td;int old_threshold; -if (trip->temperature ==THERMAL_TEMP_INVALID ) -return ; +for_each_trip_desc (tz, td) { -/* -* If the trip temperature or hysteresis has been updated recently, -* the threshold needs to be computed again using the new values. -* However, its initial value still reflects the old ones and that -* is what needs to be compared with the previous zone temperature -* to decide which action to take. -*/ - old_threshold = td->threshold; - td->threshold = trip->temperature; +const struct thermal_trip *trip = &td->trip; + +if (trip->temperature ==THERMAL_TEMP_INVALID ) +continue ; + +if (tz->last_temperature < old_threshold || + tz->last_temperature ==THERMAL_TEMP_INIT ) +continue ; -if (tz->last_temperature >= old_threshold && - tz->last_temperature !=THERMAL_TEMP_INIT ) {/* -* Mitigation is under way, so it needs to stop if the zone -* temperature falls below the low temperature of the trip. -* In that case, the trip temperature becomes the new threshold. +* If the trip temperature or hysteresis has been updated recently, +* the threshold needs to be computed again using the new values. +* However, its initial value still reflects the old ones and that +* is what needs to be compared with the previous zone temperature +* to decide which action to take. */ -if (tz->temperature < trip->temperature - trip->hysteresis) { -list_add (&td->notify_list_node, way_down_list); - td->notify_temp = trip->temperature - trip->hysteresis; + old_threshold = td->threshold; + td->threshold = trip->temperature; -if (trip->type ==THERMAL_TRIP_PASSIVE ) { - tz->passive--; -WARN_ON (tz->passive <0 ); +if (tz->last_temperature >= old_threshold && + tz->last_temperature !=THERMAL_TEMP_INVALID ) { +/* +* Mitigation is under way, so it needs to stop if the zone +* temperature falls below the low temperature of the trip. +* In that case, the trip temperature becomes the new threshold. +*/ +if (tz->temperature < trip->temperature - trip->hysteresis) { +list_add (&td->notify_list_node, way_down_list); + td->notify_temp = trip->temperature - trip->hysteresis; + +if (trip->type ==THERMAL_TRIP_PASSIVE ) { + tz->passive--; +WARN_ON (tz->passive <0 ); + } + }else { + td->threshold -= trip->hysteresis; } - }else { + }else if (tz->temperature >= trip->temperature) { +/* +* There is no mitigation under way, so it needs to be started +* if the zone temperature exceeds the trip one. The new +* threshold is then set to the low temperature of the trip. +*/ +list_add_tail (&td->notify_list_node, way_up_list); + td->notify_temp = trip->temperature; td->threshold -= trip->hysteresis; + +if (trip->type ==THERMAL_TRIP_PASSIVE ) + tz->passive++; +else if (trip->type ==THERMAL_TRIP_CRITICAL || + trip->type ==THERMAL_TRIP_HOT ) +handle_critical_trips (tz, trip); } - }else if (tz->temperature >= trip->temperature) { -/* -* There is no mitigation under way, so it needs to be started -* if the zone temperature exceeds the trip one. The new -* threshold is then set to the low temperature of the trip. -*/ -list_add_tail (&td->notify_list_node, way_up_list); - td->notify_temp = trip->temperature; - td->threshold -= trip->hysteresis; - -if (trip->type ==THERMAL_TRIP_PASSIVE ) - tz->passive++; -else if (trip->type ==THERMAL_TRIP_CRITICAL || - trip->type ==THERMAL_TRIP_HOT ) -handle_critical_trips (tz, trip); + +if (td->threshold < tz->temperature && td->threshold > *low) + *low = td->threshold; + +if (td->threshold > tz->temperature && td->threshold < *high) + *high = td->threshold; } }
@@ -545,6 +560,8 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz, {struct thermal_governor *governor =thermal_get_tz_governor (tz);struct thermal_trip_desc *td; +int low = -INT_MAX , high =INT_MAX ; +LIST_HEAD (way_down_list);LIST_HEAD (way_up_list);int temp, ret;
@@ -580,10 +597,9 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz, tz->notify_event = event; -for_each_trip_desc (tz, td) -handle_thermal_trip (tz, td, &way_up_list, &way_down_list); +handle_thermal_trip (tz, &way_up_list, &way_down_list, &low, &high); -thermal_zone_set_trips (tz); +thermal_zone_set_trips (tz, low, high);list_sort (NULL , &way_up_list, thermal_trip_notify_cmp);list_for_each_entry (td, &way_up_list, notify_list_node)
drivers/thermal/thermal_core.h +1 -1
@@ -259,7 +259,7 @@ void thermal_governor_update_tz(struct thermal_zone_device *tz,const char *thermal_trip_type_name (enum thermal_trip_type trip_type); -void thermal_zone_set_trips (struct thermal_zone_device *tz); +void thermal_zone_set_trips (struct thermal_zone_device *tz,int low,int high);int thermal_zone_trip_id (const struct thermal_zone_device *tz,const struct thermal_trip *trip); void thermal_zone_trip_updated(struct thermal_zone_device *tz,
drivers/thermal/thermal_trip.c +1 -11
@@ -76,10 +76,8 @@ EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);* * It does not return a value */ -void thermal_zone_set_trips (struct thermal_zone_device *tz) +void thermal_zone_set_trips (struct thermal_zone_device *tz,int low,int high) { -const struct thermal_trip_desc *td; -int low = -INT_MAX , high =INT_MAX ;int ret;lockdep_assert_held (&tz->lock);
@@ -87,14 +85,6 @@ void thermal_zone_set_trips(struct thermal_zone_device *tz)if (!tz->ops.set_trips)return ; -for_each_trip_desc (tz, td) { -if (td->threshold < tz->temperature && td->threshold > low) - low = td->threshold; - -if (td->threshold > tz->temperature && td->threshold < high) - high = td->threshold; - } -/* No need to change trip points */ if (tz->prev_low_trip == low && tz->prev_high_trip == high)return ;
Signature · 1 line
2.43.0
Re: [PATCH v1 1/7] thermal/core: Encapsulate more handle_thermal_trip
Quoted 198 lines · CEST Daniel Lezcano
In order to set the scene for the thresholds support which have to manipulate the low and high temperature boundaries for the interrupt support, we must pass the low and high value the incoming thresholds routine.
Instead of looping in the trip descriptors in
thermal_zone_device_update(), we move the loop in thehandle_thermal_trip()function and use it to set the low and high values.As these variables can be set directly in the
handle_thermal_trip(), we can get rid of a descriptors loop found in thethermal_set_trips()function as low and high are set inhandle_thermal_trip().Signed-off-by Daniel Lezcano <[email protected]>--- drivers/thermal/thermal_core.c | 104 +++++++++++++++++++-------------- drivers/thermal/thermal_core.h | 2 +- drivers/thermal/thermal_trip.c | 12 +--- 3 files changed, 62 insertions(+), 56 deletions(-)drivers/thermal/thermal_core.c +60
-44@@ -425,59 +425,74 @@ static void handle_critical_trips(struct thermal_zone_device *tz, }static void handle_thermal_trip (struct thermal_zone_device *tz, -struct thermal_trip_desc *td,struct list_head *way_up_list, -struct list_head *way_down_list) +struct list_head *way_down_list, +int *low,int *high) { -const struct thermal_trip *trip = &td->trip; +struct thermal_trip_desc *td;int old_threshold; -if (trip->temperature ==THERMAL_TEMP_INVALID ) -return ; +for_each_trip_desc (tz, td) { -/* -* If the trip temperature or hysteresis has been updated recently, -* the threshold needs to be computed again using the new values. -* However, its initial value still reflects the old ones and that -* is what needs to be compared with the previous zone temperature -* to decide which action to take. -*/ - old_threshold = td->threshold; - td->threshold = trip->temperature; +const struct thermal_trip *trip = &td->trip; + +if (trip->temperature ==THERMAL_TEMP_INVALID ) +continue ; + +if (tz->last_temperature < old_threshold || + tz->last_temperature ==THERMAL_TEMP_INIT ) +continue ; -if (tz->last_temperature >= old_threshold && - tz->last_temperature !=THERMAL_TEMP_INIT ) {/* -* Mitigation is under way, so it needs to stop if the zone -* temperature falls below the low temperature of the trip. -* In that case, the trip temperature becomes the new threshold. +* If the trip temperature or hysteresis has been updated recently, +* the threshold needs to be computed again using the new values. +* However, its initial value still reflects the old ones and that +* is what needs to be compared with the previous zone temperature +* to decide which action to take. */ -if (tz->temperature < trip->temperature - trip->hysteresis) { -list_add (&td->notify_list_node, way_down_list); - td->notify_temp = trip->temperature - trip->hysteresis; + old_threshold = td->threshold; + td->threshold = trip->temperature; -if (trip->type ==THERMAL_TRIP_PASSIVE ) { - tz->passive--; -WARN_ON (tz->passive <0 ); +if (tz->last_temperature >= old_threshold && + tz->last_temperature !=THERMAL_TEMP_INVALID ) { +/* +* Mitigation is under way, so it needs to stop if the zone +* temperature falls below the low temperature of the trip. +* In that case, the trip temperature becomes the new threshold. +*/ +if (tz->temperature < trip->temperature - trip->hysteresis) { +list_add (&td->notify_list_node, way_down_list); + td->notify_temp = trip->temperature - trip->hysteresis; + +if (trip->type ==THERMAL_TRIP_PASSIVE ) { + tz->passive--; +WARN_ON (tz->passive <0 ); + } + }else { + td->threshold -= trip->hysteresis; } - }else { + }else if (tz->temperature >= trip->temperature) { +/* +* There is no mitigation under way, so it needs to be started +* if the zone temperature exceeds the trip one. The new +* threshold is then set to the low temperature of the trip. +*/ +list_add_tail (&td->notify_list_node, way_up_list); + td->notify_temp = trip->temperature; td->threshold -= trip->hysteresis; + +if (trip->type ==THERMAL_TRIP_PASSIVE ) + tz->passive++; +else if (trip->type ==THERMAL_TRIP_CRITICAL || + trip->type ==THERMAL_TRIP_HOT ) +handle_critical_trips (tz, trip); } - }else if (tz->temperature >= trip->temperature) { -/* -* There is no mitigation under way, so it needs to be started -* if the zone temperature exceeds the trip one. The new -* threshold is then set to the low temperature of the trip. -*/ -list_add_tail (&td->notify_list_node, way_up_list); - td->notify_temp = trip->temperature; - td->threshold -= trip->hysteresis; - -if (trip->type ==THERMAL_TRIP_PASSIVE ) - tz->passive++; -else if (trip->type ==THERMAL_TRIP_CRITICAL || - trip->type ==THERMAL_TRIP_HOT ) -handle_critical_trips (tz, trip); + +if (td->threshold < tz->temperature && td->threshold > *low) + *low = td->threshold; + +if (td->threshold > tz->temperature && td->threshold < *high) + *high = td->threshold; } }@@ -545,6 +560,8 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz, {struct thermal_governor *governor =thermal_get_tz_governor (tz);struct thermal_trip_desc *td; +int low = -INT_MAX , high =INT_MAX ; +LIST_HEAD (way_down_list);LIST_HEAD (way_up_list);int temp, ret;@@ -580,10 +597,9 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz, tz->notify_event = event; -for_each_trip_desc (tz, td) -handle_thermal_trip (tz, td, &way_up_list, &way_down_list); +handle_thermal_trip (tz, &way_up_list, &way_down_list, &low, &high); -thermal_zone_set_trips (tz); +thermal_zone_set_trips (tz, low, high);list_sort (NULL , &way_up_list, thermal_trip_notify_cmp);list_for_each_entry (td, &way_up_list, notify_list_node)drivers/thermal/thermal_core.h +1
-1@@ -259,7 +259,7 @@ void thermal_governor_update_tz(struct thermal_zone_device *tz,const char *thermal_trip_type_name (enum thermal_trip_type trip_type); -void thermal_zone_set_trips (struct thermal_zone_device *tz); +void thermal_zone_set_trips (struct thermal_zone_device *tz,int low,int high);int thermal_zone_trip_id (const struct thermal_zone_device *tz,const struct thermal_trip *trip); void thermal_zone_trip_updated(struct thermal_zone_device *tz,drivers/thermal/thermal_trip.c +1
-4@@ -76,10 +76,8 @@ EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);* * It does not return a value */ -void thermal_zone_set_trips (struct thermal_zone_device *tz) +void thermal_zone_set_trips (struct thermal_zone_device *tz,int low,int high) { -const struct thermal_trip_desc *td; -int low = -INT_MAX , high =INT_MAX ;int ret;lockdep_assert_held (&tz->lock);@@ -87,14 +85,6 @@ void thermal_zone_set_trips(struct thermal_zone_device *tz)if (!tz->ops.set_trips)return ; -for_each_trip_desc (tz, td) {
drivers/thermal/thermal_trip.c void thermal_zone_set_trips(struct thermal_zone_device *tz) · line 90
Quoted 10 lines
drivers/thermal/thermal_trip.c +0
-7-if (td->threshold < tz->temperature && td->threshold > low) - low = td->threshold; - -if (td->threshold > tz->temperature && td->threshold < high) - high = td->threshold; - } -/* No need to change trip points */ if (tz->prev_low_trip == low && tz->prev_high_trip == high)return ;
Well, why not do the (untested) change below instead, which is way simpler?
The thermal_zone_set_trips() kerneldoc needs to be either updated or dropped because it is not applicable any more after this and I think it's better to just drop it.
--- drivers/thermal/thermal_core.c | 12 ++++++++++-- drivers/thermal/thermal_core.h | 2 +- drivers/thermal/thermal_trip.c | 27 +-------------------------- 3 files changed, 12 insertions(+), 29 deletions(-)
drivers/thermal/thermal_core.c +10 -2
@@ -551,6 +551,7 @@ void __thermal_zone_device_update(structstruct thermal_trip_desc *td;LIST_HEAD (way_down_list);LIST_HEAD (way_up_list); +int low = -INT_MAX , high =INT_MAX ;int temp, ret;if (tz->suspended)
@@ -584,10 +585,17 @@ void __thermal_zone_device_update(struct tz->notify_event = event; -for_each_trip_desc (tz, td) +for_each_trip_desc (tz, td) {handle_thermal_trip (tz, td, &way_up_list, &way_down_list); -thermal_zone_set_trips (tz); +if (td->threshold < tz->temperature && td->threshold > low) + low = td->threshold; + +if (td->threshold >= tz->temperature && td->threshold < high) + high = td->threshold; + } + +thermal_zone_set_trips (tz, low, high);list_sort (NULL , &way_up_list, thermal_trip_notify_cmp);list_for_each_entry (td, &way_up_list, notify_list_node)
drivers/thermal/thermal_core.h +1 -1
@@ -254,7 +254,7 @@ void thermal_governor_update_tz(struct tconst char *thermal_trip_type_name (enum thermal_trip_type trip_type); -void thermal_zone_set_trips (struct thermal_zone_device *tz); +void thermal_zone_set_trips (struct thermal_zone_device *tz,int low,int high);int thermal_zone_trip_id (const struct thermal_zone_device *tz,const struct thermal_trip *trip);int __thermal_zone_get_temp (struct thermal_zone_device *tz,int *temp);
drivers/thermal/thermal_trip.c +1 -26
@@ -55,25 +55,8 @@ int thermal_zone_for_each_trip(struct th }EXPORT_SYMBOL_GPL (thermal_zone_for_each_trip); -/** -* 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) +void thermal_zone_set_trips (struct thermal_zone_device *tz,int low,int high) { -const struct thermal_trip_desc *td; -int low = -INT_MAX , high =INT_MAX ;int ret;lockdep_assert_held (&tz->lock);
@@ -81,14 +64,6 @@ void thermal_zone_set_trips(struct thermif (!tz->ops.set_trips)return ; -for_each_trip_desc (tz, td) { -if (td->threshold < tz->temperature && td->threshold > low) - low = td->threshold; - -if (td->threshold > tz->temperature && td->threshold < high) - high = td->threshold; - } -/* No need to change trip points */ if (tz->prev_low_trip == low && tz->prev_high_trip == high)return ;
Re: [PATCH v1 1/7] thermal/core: Encapsulate more handle_thermal_trip
Hi Rafael,
Quoted 17 lines · Rafael J. Wysocki
Quoted 16 lines · CEST Daniel Lezcano
In order to set the scene for the thresholds support which have to manipulate the low and high temperature boundaries for the interrupt support, we must pass the low and high value the incoming thresholds routine.
Instead of looping in the trip descriptors in
thermal_zone_device_update(), we move the loop in thehandle_thermal_trip()function and use it to set the low and high values.As these variables can be set directly in the
handle_thermal_trip(), we can get rid of a descriptors loop found in thethermal_set_trips()function as low and high are set inhandle_thermal_trip().Signed-off-by Daniel Lezcano <[email protected]>---
[ ... ]
Quoted 4 lines
Quoted 4 lines
(quoted diff) +1
-2- for_each_trip_desc(tz, td) - handle_thermal_trip(tz, td, &way_up_list, &way_down_list); + handle_thermal_trip(tz, &way_up_list, &way_down_list, &low, &high);
[ ... ]
Quoted 1 line
Well, why not do the (untested) change below instead, which is way simpler?
Right, I did your proposed changed initially. The patch looks very complicated but it is just the result of the difference between the change above and below. It is code reorg, without functional changes (except two loops => one loop).
It looked to me more consistent to move the for_each_trip_desc() inside handle_thermal_trip() in order to:
- encapsulate the trip code more and have one line call
- be consistent with the thermal_threshold_handle() which is also one
line call
If you prefer, I can split the changes, but it is extra work for little benefit. I pushed the changes in the git tree, the resulting code from these changes are:
and
Let me know if you prefer to do a smaller change or go forward in the code encapsulation
Quoted 3 lines
The
thermal_zone_set_trips()kerneldoc needs to be either updated or dropped because it is not applicable any more after this and I think it's better to just drop it.
Sure
Quoted 3 lines
drivers/thermal/thermal_core.c +1
-1-for_each_trip_desc (tz, td) +for_each_trip_desc (tz, td) {handle_thermal_trip (tz, td, &way_up_list, &way_down_list);
[ ... ]
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
Rafael J. Wysocki
maintainerRe: [PATCH v1 1/7] thermal/core: Encapsulate more handle_thermal_trip
Hi Daniel,
Quoted 30 lines · Daniel Lezcano
Hi Rafael,
Quoted 17 lines · Rafael J. Wysocki
Quoted 16 lines · CEST Daniel Lezcano
In order to set the scene for the thresholds support which have to manipulate the low and high temperature boundaries for the interrupt support, we must pass the low and high value the incoming thresholds routine.
Instead of looping in the trip descriptors in
thermal_zone_device_update(), we move the loop in thehandle_thermal_trip()function and use it to set the low and high values.As these variables can be set directly in the
handle_thermal_trip(), we can get rid of a descriptors loop found in thethermal_set_trips()function as low and high are set inhandle_thermal_trip().Signed-off-by Daniel Lezcano <[email protected]>---
[ ... ]
Quoted 4 lines
Review
Quoted 4 lines
(quoted diff) +1
-2- for_each_trip_desc(tz, td) - handle_thermal_trip(tz, td, &way_up_list, &way_down_list); + handle_thermal_trip(tz, &way_up_list, &way_down_list, &low, &high);
Quoted 5 lines
[ ... ]
Quoted 1 line
Well, why not do the (untested) change below instead, which is way simpler?
Right, I did your proposed changed initially.
OK
Quoted 8 lines
The patch looks very
complicated but it is just the result of the difference between the change above and below. It is code reorg, without functional changes (except two loops => one loop).It looked to me more consistent to move the
for_each_trip_desc()insidehandle_thermal_trip()in order to:
- encapsulate the trip code more and have one line call
I don't agree with this.
The purpose is questionable and the code becomes more complex overall. And I'm totally not a fan of passing values through pointers if that is avoidable.
Quoted 2 lines
- be consistent with the
thermal_threshold_handle()which is also one
line call
That is a somewhat different story.
Quoted 4 lines
If you prefer, I can split the changes, but it is extra work for little benefit. I pushed the changes in the git tree, the resulting code from these changes are:
Quoted 8 lines
and
Let me know if you prefer to do a smaller change or go forward in the code encapsulation
I'd prefer to make a smaller change (obviously).
Quoted 5 lines
Quoted 3 lines
The
thermal_zone_set_trips()kerneldoc needs to be either updated or dropped because it is not applicable any more after this and I think it's better to just drop it.Sure
BTW, there is a problem in thermal_zone_set_trips() now if the zone temperature is exactly equal to one of the trip's thresholds because it will then skip that trip.
Say there are three trips, A, B, C sorted in ascending temperature order with no hysteresis. Say the zone temperature is exactly equal to the temperature of B. thermal_zone_set_trips() will set the boundaries to A and C, so the crossing of B will not be caught.
IMV, both comparisons with the zone temperature in thermal_zone_set_trips() need to be made not strict, that is
if (td->threshold <= tz->temperature && td->threshold > low)
low = td->threshold;
if (td->threshold => tz->temperature && td->threshold < high)
high = td->threshold;in order to catch all of the trip crossing events.
Of course, in the example above, this will cause thermal_zone_set_trips() to set both the boundaries to B, but that's OK because it will trigger an interrupt when the zone temperature becomes different from the B threshold regardless of which way it goes and that will allow a new interval to be set (aither {A, B} if it goes down or {B, C} if it goes up).
IMV this change needs to be made in -stable, so I'll send a patch for it to be applied before any other changes in thermal_zone_set_trips().
kernel test robot
build botRe: [PATCH v1 1/7] thermal/core: Encapsulate more handle_thermal_trip
In reply to Daniel Lezcanokernel test robot noticed the following build warnings:
Hi Daniel,
kernel test robot noticed the following build warnings:
[auto build test WARNING on rafael-pm/thermal]
[also build test WARNING on linus/master v6.11-rc1 next-20240729] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Daniel-Lezcano/thermal-core-Encapsulate-more-handle_thermal_trip/20240730-005842 base: https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal patch link: https://lore.kernel.org/r/20240729150259.1089814-2-daniel.lezcano%40linaro.org patch subject: [PATCH v1 1/7] thermal/core: Encapsulate more handle_thermal_trip config: x86_64-defconfig (https://download.01.org/0day-ci/archive/20240730/[email protected]/config) compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240730/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/thermal/thermal_trip.c:80: warning: Function parameter or struct member 'low' not described in 'thermal_zone_set_trips' >> drivers/thermal/thermal_trip.c:80: warning: Function parameter or struct member 'high' not described in 'thermal_zone_set_trips'
vim +80 drivers/thermal/thermal_trip.c
5b8de18ee9027c Daniel Lezcano 2023-01-23 63
5b8de18ee9027c Daniel Lezcano 2023-01-23 64 /**
28d5cc12671c8b Rafael J. Wysocki 2024-06-07 65 * thermal_zone_set_trips - Computes the next trip points for the driver
5b8de18ee9027c Daniel Lezcano 2023-01-23 66 * @tz: a pointer to a thermal zone device structure
5b8de18ee9027c Daniel Lezcano 2023-01-23 67 *
5b8de18ee9027c Daniel Lezcano 2023-01-23 68 * The function computes the next temperature boundaries by browsing
5b8de18ee9027c Daniel Lezcano 2023-01-23 69 * the trip points. The result is the closer low and high trip points
5b8de18ee9027c Daniel Lezcano 2023-01-23 70 * to the current temperature. These values are passed to the backend
5b8de18ee9027c Daniel Lezcano 2023-01-23 71 * driver to let it set its own notification mechanism (usually an
5b8de18ee9027c Daniel Lezcano 2023-01-23 72 * interrupt).
5b8de18ee9027c Daniel Lezcano 2023-01-23 73 *
5b8de18ee9027c Daniel Lezcano 2023-01-23 74 * This function must be called with tz->lock held. Both tz and tz->ops
5b8de18ee9027c Daniel Lezcano 2023-01-23 75 * must be valid pointers.
5b8de18ee9027c Daniel Lezcano 2023-01-23 76 *
5b8de18ee9027c Daniel Lezcano 2023-01-23 77 * It does not return a value
5b8de18ee9027c Daniel Lezcano 2023-01-23 78 */
4c090556217f53 Daniel Lezcano 2024-07-29 79 void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high)
5b8de18ee9027c Daniel Lezcano 2023-01-23 @80 {
0c0c4740c9d266 Rafael J. Wysocki 2023-12-04 81 int ret;
5b8de18ee9027c Daniel Lezcano 2023-01-23 82
5b8de18ee9027c Daniel Lezcano 2023-01-23 83 lockdep_assert_held(&tz->lock);
5b8de18ee9027c Daniel Lezcano 2023-01-23 84
698a1eb1f75eb6 Rafael J. Wysocki 2024-02-22 85 if (!tz->ops.set_trips)
5b8de18ee9027c Daniel Lezcano 2023-01-23 86 return;
5b8de18ee9027c Daniel Lezcano 2023-01-23 87
5b8de18ee9027c Daniel Lezcano 2023-01-23 88 /* No need to change trip points */
5b8de18ee9027c Daniel Lezcano 2023-01-23 89 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
5b8de18ee9027c Daniel Lezcano 2023-01-23 90 return;
5b8de18ee9027c Daniel Lezcano 2023-01-23 91
5b8de18ee9027c Daniel Lezcano 2023-01-23 92 tz->prev_low_trip = low;
5b8de18ee9027c Daniel Lezcano 2023-01-23 93 tz->prev_high_trip = high;
5b8de18ee9027c Daniel Lezcano 2023-01-23 94
5b8de18ee9027c Daniel Lezcano 2023-01-23 95 dev_dbg(&tz->device,
5b8de18ee9027c Daniel Lezcano 2023-01-23 96 "new temperature boundaries: %d < x < %d\n", low, high);
5b8de18ee9027c Daniel Lezcano 2023-01-23 97
5b8de18ee9027c Daniel Lezcano 2023-01-23 98 /*
5b8de18ee9027c Daniel Lezcano 2023-01-23 99 * Set a temperature window. When this window is left the driver
5b8de18ee9027c Daniel Lezcano 2023-01-23 100 * must inform the thermal core via thermal_zone_device_update.
5b8de18ee9027c Daniel Lezcano 2023-01-23 101 */
698a1eb1f75eb6 Rafael J. Wysocki 2024-02-22 102 ret = tz->ops.set_trips(tz, low, high);
5b8de18ee9027c Daniel Lezcano 2023-01-23 103 if (ret)
5b8de18ee9027c Daniel Lezcano 2023-01-23 104 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
5b8de18ee9027c Daniel Lezcano 2023-01-23 105 }
5b8de18ee9027c Daniel Lezcano 2023-01-23 106 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH 2/7 thermal/core: Add thresholds support
In reply to Daniel LezcanoThe trip points are a firmware description of the temperature limits of a specific thermal zone where we associate an action which is done by the kernel. The time resolution is low.
The userspace has to deal with a more complex thermal management based on heuristics from different information coming from different places. The logic is much more complex but based on a bigger time resolution, usually one second based.
The purpose of the userspace is to monitor the temperatures from different places and take actions. However, it can not be constantly reading the temperature to detect when a temperature threshold has been reached. This is especially bad for mobile or embedded system as that will lead to an unacceptable number of wakeup to check the temperature with nothing to do.
On the other side, the sensors are now most of the time interrupt driven. That means the thermal framework will use the temperature trip points to program the sensor to trigger an interrupt when a temperature limit is crossed.
Unfortunately, the userspace can not benefit this feature and current solutions found here and there, iow out-of-tree, are to add fake trip points in the firmware and enable the writable trip points.
This is bad for different reasons, the trip points are for in-kernel actions, the semantic of their types is used by the thermal framework and by adding trip points in the device tree is a way to overcome the current limitation but tampering with how the thermal framework is supposed to work. The writable trip points is a way to adjust a temperature limit given a specific platform if the firmware is not accurate enough and TBH it is more a debug feature from my POV.
The thresholds mechanism is a way to have the userspace to tell thermal framework to send a notification when a temperature limit is crossed. There is no id, no hysteresis, just the temperature and the direction of the limit crossing. That means we can be notified when a threshold is crossed the way up only, or the way down only or both ways. That allows to create hysteresis values if it is needed.
A threshold can be added, deleted or flushed. The latter means all thresholds belonging to a thermal zone will be deleted.
When a threshold is added:
- if the same threshold (temperature and direction) exists, an error is returned
- if a threshold is specified with the same temperature but a different direction, the specified direction is added
- if there is no threshold with the same temperature then it is created
When a threshold is deleted:
- if the same threshold (temperature and direction) exists, it is deleted
- if a threshold is specified with the same temperature but a different direction, the specified direction is removed
- if there is no threshold with the same temperature, then an error is returned
When the threshold are flushed:
- All thresholds related to a thermal zone are deleted
When a threshold is crossed:
- the userspace does not need to know which threshold(s) have been crossed, it will be notified with the current temperature and the previous temperature
- if multiple thresholds have been crossed between two updates only one notification will be send to the userspace, it is pointless to send a notification per thresholds crossed as the userspace can handle that easily when it has the temperature delta information
--- drivers/thermal/Kconfig | 15 ++ drivers/thermal/Makefile | 3 + drivers/thermal/thermal_core.h | 4 + drivers/thermal/thermal_thresholds.c | 240 +++++++++++++++++++++++++++ drivers/thermal/thermal_thresholds.h | 57 +++++++ include/linux/thermal.h | 3 + 6 files changed, 322 insertions(+) create mode 100644 drivers/thermal/thermal_thresholds.c create mode 100644 drivers/thermal/thermal_thresholds.h
drivers/thermal/Kconfig +15 -0
@@ -40,6 +40,21 @@ config THERMAL_DEBUGFS Say Y to allow the thermal subsystem to collect diagnostic information that can be accessed via debugfs. +config THERMAL_THRESHOLDS + bool "Thermal thresholds notification mechanism" + depends on THERMAL_NETLINK + help + The userspace implements thermal engines which needs to get + notified when temperature thresholds are crossed the way up + and down. These notification allow them to analyze the + thermal situation of the platform and take decision to + fulfill specific thermal profile like 'balanced', + 'performance' or 'power saving'. In addition, the + temperature of the skin sensor is very important in this + case and must be monitored as well. + + If in doubt, say Y + config THERMAL_EMERGENCY_POWEROFF_DELAY_MS int "Emergency poweroff delay in milli-seconds" default 0
drivers/thermal/Makefile +3 -0
@@ -7,6 +7,9 @@ obj-$(CONFIG_THERMAL) += thermal_sys.o thermal_sys-y += thermal_core.o thermal_sysfs.o thermal_sys-y += thermal_trip.o thermal_helpers.o +# thermal thresholds +thermal_sys-$(CONFIG_THERMAL_THRESHOLDS) += thermal_thresholds.o + # netlink interface to manage the thermal framework thermal_sys-$(CONFIG_THERMAL_NETLINK) += thermal_netlink.o
drivers/thermal/thermal_core.h +4 -0
@@ -13,6 +13,7 @@#include <linux/thermal.h> #include "thermal_netlink.h" +#include "thermal_thresholds.h" #include "thermal_debugfs.h" struct thermal_trip_desc {
@@ -132,6 +133,9 @@ struct thermal_zone_device {bool resuming;#ifdef CONFIG_THERMAL_DEBUGFS struct thermal_debugfs *debugfs; +#endif +#ifdef CONFIG_THERMAL_THRESHOLDS +struct thresholds *thresholds;#endif struct thermal_trip_desc trips[] __counted_by(num_trips); };
drivers/thermal/thermal_thresholds.c new file +240 -0
240 lines not on this page · raw message
drivers/thermal/thermal_thresholds.h new file +57 -0
57 lines not on this page · raw message
include/linux/thermal.h +3 -0
@@ -55,6 +55,9 @@ enum thermal_notify_event {THERMAL_TZ_BIND_CDEV ,/* Cooling dev is bind to the thermal zone */ THERMAL_TZ_UNBIND_CDEV ,/* Cooling dev is unbind from the thermal zone */ THERMAL_INSTANCE_WEIGHT_CHANGED ,/* Thermal instance weight changed */ +THERMAL_THRESHOLD_ADDED ,/* Threshold added */ +THERMAL_THRESHOLD_DELETED ,/* Threshold deleted */ + THERMAL_THRESHOLD_FLUSHED, /* All thresholds deleted */ };/**
Signature · 1 line
2.43.0
kernel test robot
build botRe: [PATCH v1 2/7] thermal/core: Add thresholds support
kernel test robot noticed the following build warnings:
Hi Daniel,
kernel test robot noticed the following build warnings:
[auto build test WARNING on rafael-pm/thermal]
[also build test WARNING on linus/master v6.11-rc1 next-20240729] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Daniel-Lezcano/thermal-core-Encapsulate-more-handle_thermal_trip/20240730-005842 base: https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal patch link: https://lore.kernel.org/r/20240729150259.1089814-3-daniel.lezcano%40linaro.org patch subject: [PATCH v1 2/7] thermal/core: Add thresholds support config: alpha-allyesconfig (https://download.01.org/0day-ci/archive/20240730/[email protected]/config) compiler: alpha-linux-gcc (GCC) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240730/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
drivers/thermal/thermal_thresholds.c: In function 'thermal_thresholds_init':
drivers/thermal/thermal_thresholds.c:22:22: error: implicit declaration of function 'kmalloc'; did you mean 'mm_alloc'? [-Werror=implicit-function-declaration]
22 | thresholds = kmalloc(sizeof(*thresholds), GFP_KERNEL);
| ^~~~~~~
| mm_alloc
>> drivers/thermal/thermal_thresholds.c:22:20: warning: assignment to 'struct thresholds *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
22 | thresholds = kmalloc(sizeof(*thresholds), GFP_KERNEL);
| ^
drivers/thermal/thermal_thresholds.c: In function 'thermal_thresholds_exit':
drivers/thermal/thermal_thresholds.c:35:9: error: implicit declaration of function 'kfree' [-Werror=implicit-function-declaration]
35 | kfree(tz->thresholds);
| ^~~~~
drivers/thermal/thermal_thresholds.c: In function 'thermal_thresholds_add':
>> drivers/thermal/thermal_thresholds.c:185:19: warning: assignment to 'struct threshold *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
185 | t = kmalloc(sizeof(*t), GFP_KERNEL);
| ^cc1: some warnings being treated as errors
vim +22 drivers/thermal/thermal_thresholds.c
17
18 int thermal_thresholds_init(struct thermal_zone_device *tz)
19 {
20 struct thresholds *thresholds;
21
> 22 thresholds = kmalloc(sizeof(*thresholds), GFP_KERNEL);
23 if (!thresholds)
24 return -ENOMEM;
25
26 INIT_LIST_HEAD(&thresholds->list);
27 tz->thresholds = thresholds;
28
29 return 0;
30 }
31
32 void thermal_thresholds_exit(struct thermal_zone_device *tz)
33 {
34 thermal_thresholds_flush(tz);
35 kfree(tz->thresholds);
36 tz->thresholds = NULL;
37 }
38
39 static int __thermal_thresholds_cmp(void *data,
40 const struct list_head *l1,
41 const struct list_head *l2)
42 {
43 struct threshold *t1 = container_of(l1, struct threshold, list);
44 struct threshold *t2 = container_of(l2, struct threshold, list);
45
46 return t1->temperature - t2->temperature;
47 }
48
49 static struct threshold *__thermal_thresholds_find(const struct thresholds *thresholds, int temperature)
50 {
51 struct threshold *t;
52
53 list_for_each_entry(t, &thresholds->list, list)
54 if (t->temperature == temperature)
55 return t;
56
57 return NULL;
58 }
59
60 static bool __thermal_threshold_is_crossed(struct threshold *threshold, int temperature,
61 int last_temperature, int direction,
62 int *low, int *high)
63 {
64 if (temperature > threshold->temperature && threshold->temperature > *low &&
65 (THERMAL_THRESHOLD_WAY_DOWN & threshold->direction))
66 *low = threshold->temperature;
67
68 if (temperature < threshold->temperature && threshold->temperature < *high &&
69 (THERMAL_THRESHOLD_WAY_UP & threshold->direction))
70 *high = threshold->temperature;
71
72 if (temperature < threshold->temperature &&
73 last_temperature >= threshold->temperature &&
74 (threshold->direction & direction))
75 return true;
76
77 if (temperature >= threshold->temperature &&
78 last_temperature < threshold->temperature &&
79 (threshold->direction & direction))
80 return true;
81
82 return false;
83 }
84
85 static bool thermal_thresholds_handle_raising(struct thresholds *thresholds, int temperature,
86 int last_temperature, int *low, int *high)
87 {
88 struct threshold *t;
89
90 list_for_each_entry(t, &thresholds->list, list) {
91 if (__thermal_threshold_is_crossed(t, temperature, last_temperature,
92 THERMAL_THRESHOLD_WAY_UP, low, high))
93 return true;
94 }
95
96 return false;
97 }
98
99 static bool thermal_thresholds_handle_dropping(struct thresholds *thresholds, int temperature,
100 int last_temperature, int *low, int *high)
101 {
102 struct threshold *t;
103
104 list_for_each_entry_reverse(t, &thresholds->list, list) {
105 if (__thermal_threshold_is_crossed(t, temperature, last_temperature,
106 THERMAL_THRESHOLD_WAY_DOWN, low, high))
107 return true;
108 }
109
110 return false;
111 }
112
113 void thermal_thresholds_flush(struct thermal_zone_device *tz)
114 {
115 struct thresholds *thresholds = tz->thresholds;
116 struct threshold *entry, *tmp;
117
118 lockdep_assert_held(&tz->lock);
119
120 list_for_each_entry_safe(entry, tmp, &thresholds->list, list) {
121 list_del(&entry->list);
122 kfree(entry);
123 }
124
125 __thermal_zone_device_update(tz, THERMAL_THRESHOLD_FLUSHED);
126 }
127
128 int thermal_thresholds_handle(struct thermal_zone_device *tz, int *low, int *high)
129 {
130 struct thresholds *thresholds = tz->thresholds;
131
132 int temperature = tz->temperature;
133 int last_temperature = tz->last_temperature;
134 bool notify;
135
136 lockdep_assert_held(&tz->lock);
137
138 /*
139 * We need a second update in order to detect a threshold being crossed
140 */
141 if (last_temperature == THERMAL_TEMP_INVALID)
142 return 0;
143
144 /*
145 * The temperature is stable, so obviously we can not have
146 * crossed a threshold.
147 */
148 if (last_temperature == temperature)
149 return 0;
150
151 /*
152 * Since last update the temperature:
153 * - increased : thresholds are crossed the way up
154 * - decreased : thresholds are crossed the way down
155 */
156 if (temperature > last_temperature)
157 notify = thermal_thresholds_handle_raising(thresholds, temperature,
158 last_temperature, low, high);
159 else
160 notify = thermal_thresholds_handle_dropping(thresholds, temperature,
161 last_temperature, low, high);
162
163 if (notify)
164 pr_debug("A threshold has been crossed the way %s, with a temperature=%d, last_temperature=%d\n",
165 temperature > last_temperature ? "up" : "down", temperature, last_temperature);
166
167 return 0;
168 }
169
170 int thermal_thresholds_add(struct thermal_zone_device *tz, int temperature, int direction)
171 {
172 struct thresholds *thresholds = tz->thresholds;
173 struct threshold *t;
174
175 lockdep_assert_held(&tz->lock);
176
177 t = __thermal_thresholds_find(thresholds, temperature);
178 if (t) {
179 if (t->direction == direction)
180 return -EEXIST;
181
182 t->direction |= direction;
183 } else {
184
> 185 t = kmalloc(sizeof(*t), GFP_KERNEL); 186 if (!t)
187 return -ENOMEM;
188
189 INIT_LIST_HEAD(&t->list);
190 t->temperature = temperature;
191 t->direction = direction;
192 list_add(&t->list, &thresholds->list);
193 list_sort(NULL, &thresholds->list, __thermal_thresholds_cmp);
194 }
195
196 __thermal_zone_device_update(tz, THERMAL_THRESHOLD_ADDED);
197
198 return 0;
199 }
200
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v1 2/7] thermal/core: Add thresholds support
In reply to Daniel Lezcanokernel test robot noticed the following build errors:
Hi Daniel,
kernel test robot noticed the following build errors:
[auto build test ERROR on rafael-pm/thermal]
[also build test ERROR on linus/master v6.11-rc1 next-20240729] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Daniel-Lezcano/thermal-core-Encapsulate-more-handle_thermal_trip/20240730-005842 base: https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal patch link: https://lore.kernel.org/r/20240729150259.1089814-3-daniel.lezcano%40linaro.org patch subject: [PATCH v1 2/7] thermal/core: Add thresholds support config: um-allmodconfig (https://download.01.org/0day-ci/archive/20240730/[email protected]/config) compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project ccae7b461be339e717d02f99ac857cf0bc7d17fc) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240730/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> drivers/thermal/thermal_thresholds.c:22:15: error: call to undeclared function 'kmalloc'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
22 | thresholds = kmalloc(sizeof(*thresholds), GFP_KERNEL);
| ^
drivers/thermal/thermal_thresholds.c:22:15: note: did you mean 'mm_alloc'?
include/linux/sched/mm.h:16:26: note: 'mm_alloc' declared here
16 | extern struct mm_struct *mm_alloc(void);
| ^
>> drivers/thermal/thermal_thresholds.c:22:13: error: incompatible integer to pointer conversion assigning to 'struct thresholds *' from 'int' [-Wint-conversion]
22 | thresholds = kmalloc(sizeof(*thresholds), GFP_KERNEL);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/thermal/thermal_thresholds.c:35:2: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
35 | kfree(tz->thresholds);
| ^
drivers/thermal/thermal_thresholds.c:35:2: note: did you mean 'kvfree'?
include/linux/rcutiny.h:99:13: note: 'kvfree' declared here
99 | extern void kvfree(const void *addr);
| ^
drivers/thermal/thermal_thresholds.c:122:3: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
122 | kfree(entry);
| ^
drivers/thermal/thermal_thresholds.c:185:7: error: call to undeclared function 'kmalloc'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
185 | t = kmalloc(sizeof(*t), GFP_KERNEL);
| ^
>> drivers/thermal/thermal_thresholds.c:185:5: error: incompatible integer to pointer conversion assigning to 'struct threshold *' from 'int' [-Wint-conversion]
185 | t = kmalloc(sizeof(*t), GFP_KERNEL);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/thermal/thermal_thresholds.c:214:3: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
214 | kfree(t);
| ^7 errors generated.
vim +/kmalloc +22 drivers/thermal/thermal_thresholds.c
17
18 int thermal_thresholds_init(struct thermal_zone_device *tz)
19 {
20 struct thresholds *thresholds;
21
> 22 thresholds = kmalloc(sizeof(*thresholds), GFP_KERNEL);
23 if (!thresholds)
24 return -ENOMEM;
25
26 INIT_LIST_HEAD(&thresholds->list);
27 tz->thresholds = thresholds;
28
29 return 0;
30 }
31
32 void thermal_thresholds_exit(struct thermal_zone_device *tz)
33 {
34 thermal_thresholds_flush(tz);
> 35 kfree(tz->thresholds);
36 tz->thresholds = NULL;
37 }
38
39 static int __thermal_thresholds_cmp(void *data,
40 const struct list_head *l1,
41 const struct list_head *l2)
42 {
43 struct threshold *t1 = container_of(l1, struct threshold, list);
44 struct threshold *t2 = container_of(l2, struct threshold, list);
45
46 return t1->temperature - t2->temperature;
47 }
48
49 static struct threshold *__thermal_thresholds_find(const struct thresholds *thresholds, int temperature)
50 {
51 struct threshold *t;
52
53 list_for_each_entry(t, &thresholds->list, list)
54 if (t->temperature == temperature)
55 return t;
56
57 return NULL;
58 }
59
60 static bool __thermal_threshold_is_crossed(struct threshold *threshold, int temperature,
61 int last_temperature, int direction,
62 int *low, int *high)
63 {
64 if (temperature > threshold->temperature && threshold->temperature > *low &&
65 (THERMAL_THRESHOLD_WAY_DOWN & threshold->direction))
66 *low = threshold->temperature;
67
68 if (temperature < threshold->temperature && threshold->temperature < *high &&
69 (THERMAL_THRESHOLD_WAY_UP & threshold->direction))
70 *high = threshold->temperature;
71
72 if (temperature < threshold->temperature &&
73 last_temperature >= threshold->temperature &&
74 (threshold->direction & direction))
75 return true;
76
77 if (temperature >= threshold->temperature &&
78 last_temperature < threshold->temperature &&
79 (threshold->direction & direction))
80 return true;
81
82 return false;
83 }
84
85 static bool thermal_thresholds_handle_raising(struct thresholds *thresholds, int temperature,
86 int last_temperature, int *low, int *high)
87 {
88 struct threshold *t;
89
90 list_for_each_entry(t, &thresholds->list, list) {
91 if (__thermal_threshold_is_crossed(t, temperature, last_temperature,
92 THERMAL_THRESHOLD_WAY_UP, low, high))
93 return true;
94 }
95
96 return false;
97 }
98
99 static bool thermal_thresholds_handle_dropping(struct thresholds *thresholds, int temperature,
100 int last_temperature, int *low, int *high)
101 {
102 struct threshold *t;
103
104 list_for_each_entry_reverse(t, &thresholds->list, list) {
105 if (__thermal_threshold_is_crossed(t, temperature, last_temperature,
106 THERMAL_THRESHOLD_WAY_DOWN, low, high))
107 return true;
108 }
109
110 return false;
111 }
112
113 void thermal_thresholds_flush(struct thermal_zone_device *tz)
114 {
115 struct thresholds *thresholds = tz->thresholds;
116 struct threshold *entry, *tmp;
117
118 lockdep_assert_held(&tz->lock);
119
120 list_for_each_entry_safe(entry, tmp, &thresholds->list, list) {
121 list_del(&entry->list);
122 kfree(entry);
123 }
124
125 __thermal_zone_device_update(tz, THERMAL_THRESHOLD_FLUSHED);
126 }
127
128 int thermal_thresholds_handle(struct thermal_zone_device *tz, int *low, int *high)
129 {
130 struct thresholds *thresholds = tz->thresholds;
131
132 int temperature = tz->temperature;
133 int last_temperature = tz->last_temperature;
134 bool notify;
135
136 lockdep_assert_held(&tz->lock);
137
138 /*
139 * We need a second update in order to detect a threshold being crossed
140 */
141 if (last_temperature == THERMAL_TEMP_INVALID)
142 return 0;
143
144 /*
145 * The temperature is stable, so obviously we can not have
146 * crossed a threshold.
147 */
148 if (last_temperature == temperature)
149 return 0;
150
151 /*
152 * Since last update the temperature:
153 * - increased : thresholds are crossed the way up
154 * - decreased : thresholds are crossed the way down
155 */
156 if (temperature > last_temperature)
157 notify = thermal_thresholds_handle_raising(thresholds, temperature,
158 last_temperature, low, high);
159 else
160 notify = thermal_thresholds_handle_dropping(thresholds, temperature,
161 last_temperature, low, high);
162
163 if (notify)
164 pr_debug("A threshold has been crossed the way %s, with a temperature=%d, last_temperature=%d\n",
165 temperature > last_temperature ? "up" : "down", temperature, last_temperature);
166
167 return 0;
168 }
169
170 int thermal_thresholds_add(struct thermal_zone_device *tz, int temperature, int direction)
171 {
172 struct thresholds *thresholds = tz->thresholds;
173 struct threshold *t;
174
175 lockdep_assert_held(&tz->lock);
176
177 t = __thermal_thresholds_find(thresholds, temperature);
178 if (t) {
179 if (t->direction == direction)
180 return -EEXIST;
181
182 t->direction |= direction;
183 } else {
184
> 185 t = kmalloc(sizeof(*t), GFP_KERNEL); 186 if (!t)
187 return -ENOMEM;
188
189 INIT_LIST_HEAD(&t->list);
190 t->temperature = temperature;
191 t->direction = direction;
192 list_add(&t->list, &thresholds->list);
193 list_sort(NULL, &thresholds->list, __thermal_thresholds_cmp);
194 }
195
196 __thermal_zone_device_update(tz, THERMAL_THRESHOLD_ADDED);
197
198 return 0;
199 }
200
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH 3/7 thermal/core: Connect the threshold with the core
In reply to Daniel LezcanoInitialize, de-initialize and handle the threshold in the same place than the trip points.
--- drivers/thermal/thermal_core.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/thermal/thermal_core.c +9 -0
@@ -599,6 +599,8 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz,handle_thermal_trip (tz, &way_up_list, &way_down_list, &low, &high); +thermal_thresholds_handle (tz, &low, &high); +thermal_zone_set_trips (tz, low, high);list_sort (NULL , &way_up_list, thermal_trip_notify_cmp);
@@ -1569,6 +1571,10 @@ thermal_zone_device_register_with_trips(const char *type, goto unregister; } + result =thermal_thresholds_init (tz); +if (result) + goto remove_hwmon; +mutex_lock (&thermal_list_lock);mutex_lock (&tz->lock);list_add_tail (&tz->node, &thermal_tz_list);
@@ -1589,6 +1595,8 @@ thermal_zone_device_register_with_trips(const char *type,return tz; +remove_hwmon: +thermal_remove_hwmon_sysfs (tz); unregister:device_del (&tz->device); release_device:
@@ -1677,6 +1685,7 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)thermal_set_governor (tz,NULL ); +thermal_thresholds_exit (tz);thermal_remove_hwmon_sysfs (tz);ida_free (&thermal_tz_ida, tz->id);ida_destroy (&tz->ida);
Signature · 1 line
2.43.0
PATCH 4/7 thermal/netlink: Add the commands and the events for the thresholds
In reply to Daniel LezcanoThe thresholds exist but there is no notification neither action code related to them yet.
These changes implement the netlink for the notifications when the thresholds are crossed, added, deleted or flushed as well as the commands which allows to get the list of the thresholds, flush them, add and delete.
As different processes in userspace can interact with the thresholds, the process id responsible of the action (add, delete or flush) will be added in the notification. This way a thermal engine is able to detect if another process is interfering with the thresholds. A process id of zero is the kernel as it is by convention usually.
--- drivers/thermal/thermal_netlink.c | 239 ++++++++++++++++++++++++++- drivers/thermal/thermal_netlink.h | 7 + drivers/thermal/thermal_thresholds.c | 36 ++-- drivers/thermal/thermal_thresholds.h | 15 +- include/uapi/linux/thermal.h | 30 +++- 5 files changed, 290 insertions(+), 37 deletions(-)
drivers/thermal/thermal_netlink.c +233 -6
@@ -9,6 +9,7 @@#include <linux/module.h> #include <linux/notifier.h> #include <linux/kernel.h> +#include <net/sock.h> #include <net/genetlink.h> #include <uapi/linux/thermal.h>
@@ -49,12 +50,19 @@ static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = [THERMAL_GENL_ATTR_CPU_CAPABILITY_ID] = { .type = NLA_U32 }, [THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE] = { .type = NLA_U32 }, [THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY] = { .type = NLA_U32 }, + +/* Thresholds */ + [THERMAL_GENL_ATTR_THRESHOLD] = { .type = NLA_NESTED }, + [THERMAL_GENL_ATTR_THRESHOLD_TEMP] = { .type = NLA_U32 }, + [THERMAL_GENL_ATTR_THRESHOLD_WAY] = { .type = NLA_U32 }, + [THERMAL_GENL_ATTR_THRESHOLD_PID] = { .type = NLA_U32 }, };struct param {struct nlattr **attrs;struct sk_buff *msg;const char *name; +pid_t pid;int tz_id;int cdev_id;int trip_id;
@@ -62,6 +70,8 @@ struct param {int trip_type;int trip_hyst;int temp; +int last_temp; +int direction;int cdev_state;int cdev_max_state;struct thermal_genl_cpu_caps *cpu_capabilities;
@@ -234,6 +244,36 @@ static int thermal_genl_event_cpu_capability_change(struct param *p)return -EMSGSIZE ; } +static int thermal_genl_event_threshold_add (struct param *p) +{ +if (nla_put_u32 (p->msg,THERMAL_GENL_ATTR_TZ_ID , p->tz_id) || +nla_put_u32 (p->msg,THERMAL_GENL_ATTR_THRESHOLD_TEMP , p->temp) || +nla_put_u32 (p->msg,THERMAL_GENL_ATTR_THRESHOLD_WAY , p->direction) || +nla_put_u32 (p->msg,THERMAL_GENL_ATTR_THRESHOLD_PID , p->pid)) +return -EMSGSIZE ; + +return 0 ; +} + +static int thermal_genl_event_threshold_flush (struct param *p) +{ +if (nla_put_u32 (p->msg,THERMAL_GENL_ATTR_TZ_ID , p->tz_id), +nla_put_u32 (p->msg,THERMAL_GENL_ATTR_THRESHOLD_PID , p->pid)) +return -EMSGSIZE ; + +return 0 ; +} + +static int thermal_genl_event_threshold_up (struct param *p) +{ +if (nla_put_u32 (p->msg,THERMAL_GENL_ATTR_TZ_ID , p->tz_id) || +nla_put_u32 (p->msg,THERMAL_GENL_ATTR_TZ_LAST_TEMP , p->last_temp) || +nla_put_u32 (p->msg,THERMAL_GENL_ATTR_TZ_TEMP , p->temp)) +return -EMSGSIZE ; + +return 0 ; +} +int thermal_genl_event_tz_delete (struct param *p) __attribute__((alias ("thermal_genl_event_tz" )));
@@ -246,6 +286,12 @@ int thermal_genl_event_tz_disable(struct param *p)int thermal_genl_event_tz_trip_down (struct param *p) __attribute__((alias ("thermal_genl_event_tz_trip_up" ))); +int thermal_genl_event_threshold_delete (struct param *p) + __attribute__((alias ("thermal_genl_event_threshold_add" ))); + +int thermal_genl_event_threshold_down (struct param *p) + __attribute__((alias ("thermal_genl_event_threshold_up" ))); +static cb_t event_cb[] = { [THERMAL_GENL_EVENT_TZ_CREATE ] = thermal_genl_event_tz_create, [THERMAL_GENL_EVENT_TZ_DELETE ] = thermal_genl_event_tz_delete,
@@ -259,6 +305,11 @@ static cb_t event_cb[] = { [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update, [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change, [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change, + [THERMAL_GENL_EVENT_THRESHOLD_ADD] = thermal_genl_event_threshold_add, + [THERMAL_GENL_EVENT_THRESHOLD_DELETE] = thermal_genl_event_threshold_delete, + [THERMAL_GENL_EVENT_THRESHOLD_FLUSH] = thermal_genl_event_threshold_flush, + [THERMAL_GENL_EVENT_THRESHOLD_DOWN] = thermal_genl_event_threshold_down, + [THERMAL_GENL_EVENT_THRESHOLD_UP] = thermal_genl_event_threshold_up, };/*
@@ -401,6 +452,43 @@ int thermal_genl_cpu_capability_event(int count, }EXPORT_SYMBOL_GPL (thermal_genl_cpu_capability_event); +int thermal_notify_threshold_add (const struct thermal_zone_device *tz, +int temperature,int direction,int pid) +{ +struct param p = { .tz_id = tz->id, .temp = temperature, .direction = direction, .pid = pid }; + +return thermal_genl_send_event (THERMAL_GENL_EVENT_THRESHOLD_ADD , &p); +} + +int thermal_notify_threshold_delete (const struct thermal_zone_device *tz, +int temperature,int direction,int pid) +{ +struct param p = { .tz_id = tz->id, .temp = temperature, .direction = direction, .pid = pid }; + +return thermal_genl_send_event (THERMAL_GENL_EVENT_THRESHOLD_DELETE , &p); +} + +int thermal_notify_threshold_flush (const struct thermal_zone_device *tz,int pid) +{ +struct param p = { .tz_id = tz->id, .pid = pid }; + +return thermal_genl_send_event (THERMAL_GENL_EVENT_THRESHOLD_FLUSH , &p); +} + +int thermal_notify_threshold_down (const struct thermal_zone_device *tz) +{ +struct param p = { .tz_id = tz->id, .temp = tz->temperature, .last_temp = tz->last_temperature }; + +return thermal_genl_send_event (THERMAL_GENL_EVENT_THRESHOLD_DOWN , &p); +} + +int thermal_notify_threshold_up (const struct thermal_zone_device *tz) +{ +struct param p = { .tz_id = tz->id, .temp = tz->temperature, .last_temp = tz->last_temperature }; + +return thermal_genl_send_event (THERMAL_GENL_EVENT_THRESHOLD_UP , &p); +} +/*************************** Command encoding ********************************/ static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
@@ -575,12 +663,130 @@ static int thermal_genl_cmd_cdev_get(struct param *p)return ret; } +static int __thermal_genl_cmd_threshold_get (struct threshold *threshold,void *arg) +{ +struct sk_buff *msg = arg; + +if (nla_put_u32 (msg,THERMAL_GENL_ATTR_THRESHOLD_TEMP , threshold->temperature) || +nla_put_u32 (msg,THERMAL_GENL_ATTR_THRESHOLD_WAY , threshold->direction)) +return -1 ; + +return 0 ; +} + +static int thermal_genl_cmd_threshold_get (struct param *p) +{ +struct thermal_zone_device *tz; +struct sk_buff *msg = p->msg; +struct nlattr *start_trip; +int id, ret; + +if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID ]) +return -EINVAL ; + + id =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_TZ_ID ]); + + tz =thermal_zone_get_by_id (id); +if (!tz) +return -EINVAL ; + + start_trip =nla_nest_start (msg,THERMAL_GENL_ATTR_THRESHOLD ); +if (!start_trip) +return -EMSGSIZE ; + +mutex_lock (&tz->lock); + ret =thermal_thresholds_for_each (tz, __thermal_genl_cmd_threshold_get, msg); +mutex_unlock (&tz->lock); + +if (ret) +return -EMSGSIZE ; + +nla_nest_end (msg, start_trip); + +return 0 ; +} + +static int thermal_genl_cmd_threshold_add (struct param *p) +{ +struct thermal_zone_device *tz; +int id, temp, direction, ret =0 ; + +if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID ] || + !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP ] || + !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_WAY ]) +return -EINVAL ; + + id =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_TZ_ID ]); + temp =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP ]); + direction =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_THRESHOLD_WAY ]); + + tz =thermal_zone_get_by_id (id); +if (!tz) +return -EINVAL ; + +mutex_lock (&tz->lock); + ret =thermal_thresholds_add (tz, temp, direction, p->pid); +mutex_unlock (&tz->lock); + +return ret; +} + +static int thermal_genl_cmd_threshold_delete (struct param *p) +{ +struct thermal_zone_device *tz; +int id, temp, direction, ret =0 ; + +if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID ] || + !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP ] || + !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_WAY ]) +return -EINVAL ; + + id =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_TZ_ID ]); + temp =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP ]); + direction =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_THRESHOLD_WAY ]); + + tz =thermal_zone_get_by_id (id); +if (!tz) +return -EINVAL ; + +mutex_lock (&tz->lock); + ret =thermal_thresholds_delete (tz, temp, direction, p->pid); +mutex_unlock (&tz->lock); + +return ret; +} + +static int thermal_genl_cmd_threshold_flush (struct param *p) +{ +struct thermal_zone_device *tz; +int id; + +if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID ]) +return -EINVAL ; + + id =nla_get_u32 (p->attrs[THERMAL_GENL_ATTR_TZ_ID ]); + + tz =thermal_zone_get_by_id (id); +if (!tz) +return -EINVAL ; + +mutex_lock (&tz->lock); +thermal_thresholds_flush (tz, p->pid); +mutex_unlock (&tz->lock); + +return 0 ; +} +static cb_t cmd_cb[] = { - [THERMAL_GENL_CMD_TZ_GET_ID ] = thermal_genl_cmd_tz_get_id, - [THERMAL_GENL_CMD_TZ_GET_TRIP ] = thermal_genl_cmd_tz_get_trip, - [THERMAL_GENL_CMD_TZ_GET_TEMP ] = thermal_genl_cmd_tz_get_temp, - [THERMAL_GENL_CMD_TZ_GET_GOV ] = thermal_genl_cmd_tz_get_gov, - [THERMAL_GENL_CMD_CDEV_GET ] = thermal_genl_cmd_cdev_get, + [THERMAL_GENL_CMD_TZ_GET_ID ] = thermal_genl_cmd_tz_get_id, + [THERMAL_GENL_CMD_TZ_GET_TRIP ] = thermal_genl_cmd_tz_get_trip, + [THERMAL_GENL_CMD_TZ_GET_TEMP ] = thermal_genl_cmd_tz_get_temp, + [THERMAL_GENL_CMD_TZ_GET_GOV ] = thermal_genl_cmd_tz_get_gov, + [THERMAL_GENL_CMD_CDEV_GET ] = thermal_genl_cmd_cdev_get, + [THERMAL_GENL_CMD_THRESHOLD_GET ] = thermal_genl_cmd_threshold_get, + [THERMAL_GENL_CMD_THRESHOLD_ADD ] = thermal_genl_cmd_threshold_add, + [THERMAL_GENL_CMD_THRESHOLD_DELETE ] = thermal_genl_cmd_threshold_delete, + [THERMAL_GENL_CMD_THRESHOLD_FLUSH ] = thermal_genl_cmd_threshold_flush, }; static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
@@ -623,6 +829,7 @@ static int thermal_genl_cmd_doit(struct sk_buff *skb,if (!msg)return -ENOMEM ; p.msg = msg; + p.pid =task_tgid_vnr (current); hdr =genlmsg_put_reply (msg, info, &thermal_genl_family,0 , cmd);if (!hdr)
@@ -691,6 +898,26 @@ static const struct genl_small_ops thermal_genl_ops[] = { .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, .dumpit = thermal_genl_cmd_dumpit, }, + { + .cmd = THERMAL_GENL_CMD_THRESHOLD_GET, + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, + .doit = thermal_genl_cmd_doit, + }, + { + .cmd = THERMAL_GENL_CMD_THRESHOLD_ADD, + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, + .doit = thermal_genl_cmd_doit, + }, + { + .cmd = THERMAL_GENL_CMD_THRESHOLD_DELETE, + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, + .doit = thermal_genl_cmd_doit, + }, + { + .cmd = THERMAL_GENL_CMD_THRESHOLD_FLUSH, + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, + .doit = thermal_genl_cmd_doit, + }, }; static struct genl_family thermal_genl_family __ro_after_init = {
@@ -703,7 +930,7 @@ static struct genl_family thermal_genl_family __ro_after_init = { .unbind = thermal_genl_unbind, .small_ops = thermal_genl_ops, .n_small_ops = ARRAY_SIZE(thermal_genl_ops), - .resv_start_op = THERMAL_GENL_CMD_CDEV_GET + 1, + .resv_start_op = __THERMAL_GENL_CMD_MAX, .mcgrps = thermal_genl_mcgrps, .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps), };
drivers/thermal/thermal_netlink.h +7 -0
@@ -53,6 +53,13 @@ int thermal_notify_tz_gov_change(const struct thermal_zone_device *tz,int thermal_genl_sampling_temp (int id,int temp);int thermal_genl_cpu_capability_event (int count,struct thermal_genl_cpu_caps *caps); +int thermal_notify_threshold_add (const struct thermal_zone_device *tz, +int temperature,int direction,pid_t pid); +int thermal_notify_threshold_delete (const struct thermal_zone_device *tz, +int temperature,int direction,pid_t pid); +int thermal_notify_threshold_flush (const struct thermal_zone_device *tz,pid_t pid); +int thermal_notify_threshold_down (const struct thermal_zone_device *tz); +int thermal_notify_threshold_up (const struct thermal_zone_device *tz);#else static inline int thermal_netlink_init (void ) {
drivers/thermal/thermal_thresholds.c +21 -15
@@ -31,7 +31,7 @@ int thermal_thresholds_init(struct thermal_zone_device *tz)void thermal_thresholds_exit (struct thermal_zone_device *tz) { -thermal_thresholds_flush (tz); +thermal_thresholds_flush (tz,0 );kfree (tz->thresholds); tz->thresholds =NULL ; }
@@ -110,7 +110,7 @@ static bool thermal_thresholds_handle_dropping(struct thresholds *thresholds, inreturn false; } -void thermal_thresholds_flush (struct thermal_zone_device *tz) +void thermal_thresholds_flush (struct thermal_zone_device *tz,pid_t pid) {struct thresholds *thresholds = tz->thresholds;struct threshold *entry, *tmp;
@@ -122,6 +122,8 @@ void thermal_thresholds_flush(struct thermal_zone_device *tz)kfree (entry); } +thermal_notify_threshold_flush (tz, pid); +__thermal_zone_device_update (tz,THERMAL_THRESHOLD_FLUSHED ); }
@@ -131,7 +133,6 @@ int thermal_thresholds_handle(struct thermal_zone_device *tz, int *low, int *higint temperature = tz->temperature;int last_temperature = tz->last_temperature; -bool notify;lockdep_assert_held (&tz->lock);
@@ -153,21 +154,21 @@ int thermal_thresholds_handle(struct thermal_zone_device *tz, int *low, int *hig* - increased : thresholds are crossed the way up * - decreased : thresholds are crossed the way down */ -if (temperature > last_temperature) - notify =thermal_thresholds_handle_raising (thresholds, temperature, - last_temperature, low, high); -else - notify =thermal_thresholds_handle_dropping (thresholds, temperature, - last_temperature, low, high); - -if (notify) -pr_debug ("A threshold has been crossed the way %s, with a temperature=%d, last_temperature=%d\n" , - temperature > last_temperature ?"up" :"down" , temperature, last_temperature); +if (temperature > last_temperature) { +if (thermal_thresholds_handle_raising (thresholds, temperature, + last_temperature, low, high)) +thermal_notify_threshold_up (tz); + }else { +if (thermal_thresholds_handle_dropping (thresholds, temperature, + last_temperature, low, high)) +thermal_notify_threshold_down (tz); + }return 0 ; } -int thermal_thresholds_add (struct thermal_zone_device *tz,int temperature,int direction) +int thermal_thresholds_add (struct thermal_zone_device *tz, +int temperature,int direction,pid_t pid) {struct thresholds *thresholds = tz->thresholds;struct threshold *t;
@@ -193,12 +194,15 @@ int thermal_thresholds_add(struct thermal_zone_device *tz, int temperature, intlist_sort (NULL , &thresholds->list, __thermal_thresholds_cmp); } +thermal_notify_threshold_add (tz, temperature, direction, pid); +__thermal_zone_device_update (tz,THERMAL_THRESHOLD_ADDED );return 0 ; } -int thermal_thresholds_delete (struct thermal_zone_device *tz,int temperature,int direction) +int thermal_thresholds_delete (struct thermal_zone_device *tz, +int temperature,int direction,pid_t pid) {struct thresholds *thresholds = tz->thresholds;struct threshold *t;
@@ -218,6 +222,8 @@ int thermal_thresholds_delete(struct thermal_zone_device *tz, int temperature, i__thermal_zone_device_update (tz,THERMAL_THRESHOLD_DELETED ); +thermal_notify_threshold_delete (tz, temperature, direction, pid); +return 0 ; }
drivers/thermal/thermal_thresholds.h +6 -9
@@ -1,8 +1,5 @@/* SPDX-License-Identifier: GPL-2.0 */ -#define THERMAL_THRESHOLD_WAY_UP BIT(0) -#define THERMAL_THRESHOLD_WAY_DOWN BIT(1) -struct threshold {int temperature;int direction;
@@ -12,9 +9,9 @@ struct threshold {#ifdef CONFIG_THERMAL_THRESHOLDS int thermal_thresholds_init (struct thermal_zone_device *tz);void thermal_thresholds_exit (struct thermal_zone_device *tz); -void thermal_thresholds_flush (struct thermal_zone_device *tz); -int thermal_thresholds_add (struct thermal_zone_device *tz,int temperature,int direction); -int thermal_thresholds_delete (struct thermal_zone_device *tz,int temperature,int direction); +void thermal_thresholds_flush (struct thermal_zone_device *tz,pid_t pid); +int thermal_thresholds_add (struct thermal_zone_device *tz,int temperature,int direction,pid_t pid); +int thermal_thresholds_delete (struct thermal_zone_device *tz,int temperature,int direction,pid_t pid);int thermal_thresholds_handle (struct thermal_zone_device *tz,int *low,int *high);int thermal_thresholds_for_each (struct thermal_zone_device *tz,int (*cb)(struct threshold *,void *arg),void *arg);
@@ -29,17 +26,17 @@ static inline void thermal_thresholds_exit(struct thermal_zone_device *tz) ; } -static inline void thermal_thresholds_flush (struct thermal_zone_device *tz) +static inline void thermal_thresholds_flush (struct thermal_zone_device *tz,pid_t pid) { ; } -static inline int thermal_thresholds_add (struct thermal_zone_device *tz,int temperature,int direction) +static inline int thermal_thresholds_add (struct thermal_zone_device *tz,int temperature,int direction,pid_t pid) {return 0 ; } -static inline int thermal_thresholds_delete (struct thermal_zone_device *tz,int temperature,int direction) +static inline int thermal_thresholds_delete (struct thermal_zone_device *tz,int temperature,int direction,pid_t pid) {return 0 ; }
include/uapi/linux/thermal.h +23 -7
@@ -3,6 +3,8 @@#define _UAPI_LINUX_THERMAL_H#define THERMAL_NAME_LENGTH 20 +#define THERMAL_THRESHOLD_WAY_UP 0x1 +#define THERMAL_THRESHOLD_WAY_DOWN 0x2enum thermal_device_mode {THERMAL_DEVICE_DISABLED =0 ,
@@ -18,7 +20,7 @@ enum thermal_trip_type {/* Adding event notification support elements */ #define THERMAL_GENL_FAMILY_NAME "thermal" -#define THERMAL_GENL_VERSION 0x01 +#define THERMAL_GENL_VERSION 0x02#define THERMAL_GENL_SAMPLING_GROUP_NAME "sampling"#define THERMAL_GENL_EVENT_GROUP_NAME "event"
@@ -28,6 +30,7 @@ enum thermal_genl_attr {THERMAL_GENL_ATTR_TZ ,THERMAL_GENL_ATTR_TZ_ID ,THERMAL_GENL_ATTR_TZ_TEMP , +THERMAL_GENL_ATTR_TZ_LAST_TEMP ,THERMAL_GENL_ATTR_TZ_TRIP ,THERMAL_GENL_ATTR_TZ_TRIP_ID , THERMAL_GENL_ATTR_TZ_TRIP_TYPE,
@@ -48,6 +51,10 @@ enum thermal_genl_attr {THERMAL_GENL_ATTR_CPU_CAPABILITY_ID ,THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE ,THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY , +THERMAL_GENL_ATTR_THRESHOLD , +THERMAL_GENL_ATTR_THRESHOLD_TEMP , +THERMAL_GENL_ATTR_THRESHOLD_WAY , +THERMAL_GENL_ATTR_THRESHOLD_PID , __THERMAL_GENL_ATTR_MAX, };#define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
@@ -75,6 +82,11 @@ enum thermal_genl_event {THERMAL_GENL_EVENT_CDEV_STATE_UPDATE ,/* Cdev state updated */ THERMAL_GENL_EVENT_TZ_GOV_CHANGE ,/* Governor policy changed */ THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE ,/* CPU capability changed */ +THERMAL_GENL_EVENT_THRESHOLD_ADD ,/* A thresold has been added */ +THERMAL_GENL_EVENT_THRESHOLD_DELETE ,/* A thresold has been deleted */ +THERMAL_GENL_EVENT_THRESHOLD_FLUSH ,/* All thresolds have been deleted */ +THERMAL_GENL_EVENT_THRESHOLD_UP ,/* A thresold has been crossed the way up */ +THERMAL_GENL_EVENT_THRESHOLD_DOWN ,/* A thresold has been crossed the way down */ __THERMAL_GENL_EVENT_MAX, };#define THERMAL_GENL_EVENT_MAX (__THERMAL_GENL_EVENT_MAX - 1)
@@ -82,12 +94,16 @@ enum thermal_genl_event {/* Commands supported by the thermal_genl_family */ enum thermal_genl_cmd {THERMAL_GENL_CMD_UNSPEC , -THERMAL_GENL_CMD_TZ_GET_ID ,/* List of thermal zones id */ -THERMAL_GENL_CMD_TZ_GET_TRIP ,/* List of thermal trips */ -THERMAL_GENL_CMD_TZ_GET_TEMP ,/* Get the thermal zone temperature */ -THERMAL_GENL_CMD_TZ_GET_GOV ,/* Get the thermal zone governor */ -THERMAL_GENL_CMD_TZ_GET_MODE ,/* Get the thermal zone mode */ -THERMAL_GENL_CMD_CDEV_GET ,/* List of cdev id */ +THERMAL_GENL_CMD_TZ_GET_ID ,/* List of thermal zones id */ +THERMAL_GENL_CMD_TZ_GET_TRIP ,/* List of thermal trips */ +THERMAL_GENL_CMD_TZ_GET_TEMP ,/* Get the thermal zone temperature */ +THERMAL_GENL_CMD_TZ_GET_GOV ,/* Get the thermal zone governor */ +THERMAL_GENL_CMD_TZ_GET_MODE ,/* Get the thermal zone mode */ +THERMAL_GENL_CMD_CDEV_GET ,/* List of cdev id */ +THERMAL_GENL_CMD_THRESHOLD_GET ,/* List of thresholds */ +THERMAL_GENL_CMD_THRESHOLD_ADD ,/* Add a threshold */ +THERMAL_GENL_CMD_THRESHOLD_DELETE ,/* Delete a threshold */ +THERMAL_GENL_CMD_THRESHOLD_FLUSH ,/* Flush all the thresholds */ __THERMAL_GENL_CMD_MAX, };#define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
Signature · 1 line
2.43.0
PATCH 5/7 tools/lib/thermal: Make more generic the command encoding function
In reply to Daniel LezcanoThe thermal netlink has been extended with more commands which require an encoding with more information. The generic encoding function puts the thermal zone id with the command name. It is the unique parameters.
The next changes will provide more parameters to the command. Set the scene for those new parameters by making the encoding function more generic.
--- tools/lib/thermal/commands.c | 41 ++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-)
tools/lib/thermal/commands.c +32 -9
@@ -261,8 +261,23 @@ static struct genl_ops thermal_cmd_ops = { .o_ncmds = ARRAY_SIZE(thermal_cmds), }; -static thermal_error_t thermal_genl_auto (struct thermal_handler *th,int id,int cmd, -int flags,void *arg) +struct cmd_param { +int tz_id; +}; + +typedef int (*cmd_cb_t )(struct nl_msg *,struct cmd_param *); + +static int thermal_genl_tz_id_encode (struct nl_msg *msg,struct cmd_param *p) +{ +if (p->tz_id >=0 &&nla_put_u32 (msg,THERMAL_GENL_ATTR_TZ_ID , p->tz_id)) +return -1 ; + +return 0 ; +} + +static thermal_error_t thermal_genl_auto (struct thermal_handler *th,cmd_cb_t cmd_cb, +struct cmd_param *param, +int cmd,int flags,void *arg) {struct nl_msg *msg;void *hdr;
@@ -276,7 +291,7 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, int id, intif (!hdr)return THERMAL_ERROR ; -if (id >=0 &&nla_put_u32 (msg,THERMAL_GENL_ATTR_TZ_ID , id)) +if (cmd_cb &&cmd_cb (msg, param))return THERMAL_ERROR ;if (nl_send_msg (th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
@@ -289,30 +304,38 @@ static thermal_error_t thermal_genl_auto(struct thermal_handler *th, int id, intthermal_error_t thermal_cmd_get_tz (struct thermal_handler *th,struct thermal_zone **tz) { -return thermal_genl_auto (th,-1 ,THERMAL_GENL_CMD_TZ_GET_ID , +return thermal_genl_auto (th,NULL ,NULL ,THERMAL_GENL_CMD_TZ_GET_ID ,NLM_F_DUMP |NLM_F_ACK , tz); }thermal_error_t thermal_cmd_get_cdev (struct thermal_handler *th,struct thermal_cdev **tc) { -return thermal_genl_auto (th,-1 ,THERMAL_GENL_CMD_CDEV_GET , +return thermal_genl_auto (th,NULL ,NULL ,THERMAL_GENL_CMD_CDEV_GET ,NLM_F_DUMP |NLM_F_ACK , tc); }thermal_error_t thermal_cmd_get_trip (struct thermal_handler *th,struct thermal_zone *tz) { -return thermal_genl_auto (th, tz->id,THERMAL_GENL_CMD_TZ_GET_TRIP , -0 , tz); +struct cmd_param p = { .tz_id = tz->id }; + +return thermal_genl_auto (th, thermal_genl_tz_id_encode, &p, +THERMAL_GENL_CMD_TZ_GET_TRIP ,0 , tz); }thermal_error_t thermal_cmd_get_governor (struct thermal_handler *th,struct thermal_zone *tz) { -return thermal_genl_auto (th, tz->id,THERMAL_GENL_CMD_TZ_GET_GOV ,0 , tz); +struct cmd_param p = { .tz_id = tz->id }; + +return thermal_genl_auto (th, thermal_genl_tz_id_encode, &p, +THERMAL_GENL_CMD_TZ_GET_GOV ,0 , tz); }thermal_error_t thermal_cmd_get_temp (struct thermal_handler *th,struct thermal_zone *tz) { -return thermal_genl_auto (th, tz->id,THERMAL_GENL_CMD_TZ_GET_TEMP ,0 , tz); +struct cmd_param p = { .tz_id = tz->id }; + +return thermal_genl_auto (th, thermal_genl_tz_id_encode, &p, +THERMAL_GENL_CMD_TZ_GET_TEMP ,0 , tz); }thermal_error_t thermal_cmd_exit (struct thermal_handler *th)
Signature · 1 line
2.43.0
PATCH 6/7 tools/lib/thermal: Add the threshold netlink ABI
In reply to Daniel LezcanoThe thermal framework supports the thresholds and allows the userspace to create, delete, flush, get the list of the thresholds as well as getting the list of the thresholds set for a specific thermal zone.
Add the netlink abstraction in the thermal library to take full advantage of thresholds for the userspace program.
--- tools/lib/thermal/commands.c | 128 +++++++++++++++++++++++++++- tools/lib/thermal/events.c | 58 ++++++++++--- tools/lib/thermal/include/thermal.h | 40 +++++++++ tools/lib/thermal/libthermal.map | 5 ++ tools/lib/thermal/thermal.c | 17 ++++ tools/thermal/lib/Makefile | 2 +- 6 files changed, 235 insertions(+), 15 deletions(-)
tools/lib/thermal/commands.c +127 -1
@@ -5,6 +5,7 @@#include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <limits.h> #include <thermal.h> #include "thermal_nl.h"
@@ -33,6 +34,11 @@ static struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = { [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 }, [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 }, [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING }, + +/* Thresholds */ + [THERMAL_GENL_ATTR_THRESHOLD] = { .type = NLA_NESTED }, + [THERMAL_GENL_ATTR_THRESHOLD_TEMP] = { .type = NLA_U32 }, + [THERMAL_GENL_ATTR_THRESHOLD_WAY] = { .type = NLA_U32 }, };static int parse_tz_get (struct genl_info *info,struct thermal_zone **tz)
@@ -182,6 +188,38 @@ static int parse_tz_get_gov(struct genl_info *info, struct thermal_zone *tz)return THERMAL_SUCCESS ; } +static int parse_threshold_get (struct genl_info *info,struct thermal_zone *tz) +{ +struct nlattr *attr; +struct thermal_threshold *__tt =NULL ; +size_t size =0 ; +int rem; + +nla_for_each_nested (attr, info->attrs[THERMAL_GENL_ATTR_THRESHOLD ], rem) { + +if (nla_type (attr) ==THERMAL_GENL_ATTR_THRESHOLD_TEMP ) { + + size++; + + __tt =realloc (__tt,sizeof (*__tt) * (size +2 )); +if (!__tt) +return THERMAL_ERROR ; + + __tt[size -1 ].temperature =nla_get_u32 (attr); + } + +if (nla_type (attr) ==THERMAL_GENL_ATTR_THRESHOLD_WAY ) + __tt[size -1 ].direction =nla_get_u32 (attr); + } + +if (__tt) + __tt[size].temperature =INT_MAX ; + + tz->thresholds = __tt; + +return THERMAL_SUCCESS ; +} +static int handle_netlink (struct nl_cache_ops *unused,struct genl_cmd *cmd,struct genl_info *info,void *arg)
@@ -210,6 +248,10 @@ static int handle_netlink(struct nl_cache_ops *unused, ret =parse_tz_get_gov (info, arg);break ; +case THERMAL_GENL_CMD_THRESHOLD_GET : + ret =parse_threshold_get (info, arg); +break ; +default :return THERMAL_ERROR ; }
@@ -253,6 +295,34 @@ static struct genl_cmd thermal_cmds[] = { .c_maxattr = THERMAL_GENL_ATTR_MAX, .c_attr_policy = thermal_genl_policy, }, + { + .c_id = THERMAL_GENL_CMD_THRESHOLD_GET, + .c_name = (char *)"Get thresholds list", + .c_msg_parser = handle_netlink, + .c_maxattr = THERMAL_GENL_ATTR_MAX, + .c_attr_policy = thermal_genl_policy, + }, + { + .c_id = THERMAL_GENL_CMD_THRESHOLD_ADD, + .c_name = (char *)"Add a threshold", + .c_msg_parser = handle_netlink, + .c_maxattr = THERMAL_GENL_ATTR_MAX, + .c_attr_policy = thermal_genl_policy, + }, + { + .c_id = THERMAL_GENL_CMD_THRESHOLD_DELETE, + .c_name = (char *)"Delete a threshold", + .c_msg_parser = handle_netlink, + .c_maxattr = THERMAL_GENL_ATTR_MAX, + .c_attr_policy = thermal_genl_policy, + }, + { + .c_id = THERMAL_GENL_CMD_THRESHOLD_FLUSH, + .c_name = (char *)"Flush the thresholds", + .c_msg_parser = handle_netlink, + .c_maxattr = THERMAL_GENL_ATTR_MAX, + .c_attr_policy = thermal_genl_policy, + }, }; static struct genl_ops thermal_cmd_ops = {
@@ -263,13 +333,29 @@ static struct genl_ops thermal_cmd_ops = {struct cmd_param {int tz_id; +int temp; +int direction; };typedef int (*cmd_cb_t )(struct nl_msg *,struct cmd_param *);static int thermal_genl_tz_id_encode (struct nl_msg *msg,struct cmd_param *p) { -if (p->tz_id >=0 &&nla_put_u32 (msg,THERMAL_GENL_ATTR_TZ_ID , p->tz_id)) +if (nla_put_u32 (msg,THERMAL_GENL_ATTR_TZ_ID , p->tz_id)) +return -1 ; + +return 0 ; +} + +static int thermal_genl_threshold_encode (struct nl_msg *msg,struct cmd_param *p) +{ +if (thermal_genl_tz_id_encode (msg, p)) +return -1 ; + +if (nla_put_u32 (msg,THERMAL_GENL_ATTR_THRESHOLD_TEMP , p->temp)) +return -1 ; + +if (nla_put_u32 (msg,THERMAL_GENL_ATTR_THRESHOLD_WAY , p->direction))return -1 ;return 0 ;
@@ -338,6 +424,46 @@ thermal_error_t thermal_cmd_get_temp(struct thermal_handler *th, struct thermal_ THERMAL_GENL_CMD_TZ_GET_TEMP, 0, tz); } +thermal_error_t thermal_cmd_threshold_get (struct thermal_handler *th, +struct thermal_zone *tz) +{ +struct cmd_param p = { .tz_id = tz->id }; + +return thermal_genl_auto (th, thermal_genl_tz_id_encode, &p, +THERMAL_GENL_CMD_THRESHOLD_GET ,0 , tz); +} + +thermal_error_t thermal_cmd_threshold_add (struct thermal_handler *th, +struct thermal_zone *tz, +int temperature, +int direction) +{ +struct cmd_param p = { .tz_id = tz->id, .temp = temperature, .direction = direction }; + +return thermal_genl_auto (th, thermal_genl_threshold_encode, &p, +THERMAL_GENL_CMD_THRESHOLD_ADD ,0 , tz); +} + +thermal_error_t thermal_cmd_threshold_delete (struct thermal_handler *th, +struct thermal_zone *tz, +int temperature, +int direction) +{ +struct cmd_param p = { .tz_id = tz->id, .temp = temperature, .direction = direction }; + +return thermal_genl_auto (th, thermal_genl_threshold_encode, &p, +THERMAL_GENL_CMD_THRESHOLD_DELETE ,0 , tz); +} + +thermal_error_t thermal_cmd_threshold_flush (struct thermal_handler *th, +struct thermal_zone *tz) +{ +struct cmd_param p = { .tz_id = tz->id }; + +return thermal_genl_auto (th, thermal_genl_tz_id_encode, &p, +THERMAL_GENL_CMD_THRESHOLD_FLUSH ,0 , tz); +} +thermal_error_t thermal_cmd_exit (struct thermal_handler *th) {if (genl_unregister_family (&thermal_cmd_ops))
tools/lib/thermal/events.c +45 -13
70 lines not on this page · raw message
tools/lib/thermal/include/thermal.h +40 -0
@@ -4,11 +4,20 @@#define __LIBTHERMAL_H#include <linux/thermal.h> +#include <sys/types.h> #ifndef LIBTHERMAL_API #define LIBTHERMAL_API __attribute__((visibility("default")))#endif +#ifndef THERMAL_THRESHOLD_WAY_UP +#define THERMAL_THRESHOLD_WAY_UP 0x1 +#endif + +#ifndef THERMAL_THRESHOLD_WAY_DOWN +#define THERMAL_THRESHOLD_WAY_DOWN 0x2 +#endif +#ifdef __cplusplusextern "C" {#endif
@@ -31,6 +40,11 @@ struct thermal_events_ops {int (*cdev_delete)(int cdev_id,void *arg);int (*cdev_update)(int cdev_id,int cur_state,void *arg);int (*gov_change)(int tz_id,const char *gov_name,void *arg); +int (*threshold_add)(int tz_id,int temperature,int direction,pid_t pid,void *arg); +int (*threshold_delete)(int tz_id,int temperature,int direction,pid_t pid,void *arg); +int (*threshold_flush)(int tz_id,pid_t pid,void *arg); +int (*threshold_up)(int tz_id,int temp,int last_temp,void *arg); +int (*threshold_down)(int tz_id,int temp,int last_temp,void *arg); };struct thermal_ops {
@@ -45,12 +59,18 @@ struct thermal_trip {int hyst; }; +struct thermal_threshold { +int temperature; +int direction; +}; +struct thermal_zone {int id;int temp;char name[THERMAL_NAME_LENGTH ];char governor[THERMAL_NAME_LENGTH ];struct thermal_trip *trip; +struct thermal_threshold *thresholds; };struct thermal_cdev {
@@ -74,12 +94,16 @@ typedef int (*cb_tt_t)(struct thermal_trip *, void *);typedef int (*cb_tc_t )(struct thermal_cdev *,void *); +typedef int (*cb_th_t )(struct thermal_threshold *,void *); + LIBTHERMAL_API int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg); LIBTHERMAL_API int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg); LIBTHERMAL_API int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg); +LIBTHERMAL_API int for_each_thermal_threshold(struct thermal_threshold *th, cb_th_t cb, void *arg); + LIBTHERMAL_API struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,const char *name);
@@ -124,6 +148,22 @@ LIBTHERMAL_API thermal_error_t thermal_cmd_get_governor(struct thermal_handler * LIBTHERMAL_API thermal_error_t thermal_cmd_get_temp(struct thermal_handler *th,struct thermal_zone *tz); +LIBTHERMAL_API thermal_error_t thermal_cmd_threshold_get(struct thermal_handler *th, +struct thermal_zone *tz); + +LIBTHERMAL_API thermal_error_t thermal_cmd_threshold_add(struct thermal_handler *th, +struct thermal_zone *tz, +int temperature, +int direction); + +LIBTHERMAL_API thermal_error_t thermal_cmd_threshold_delete(struct thermal_handler *th, +struct thermal_zone *tz, +int temperature, +int direction); + +LIBTHERMAL_API thermal_error_t thermal_cmd_threshold_flush(struct thermal_handler *th, +struct thermal_zone *tz); +/* * Netlink thermal samples */
tools/lib/thermal/libthermal.map +5 -0
@@ -4,6 +4,7 @@ LIBTHERMAL_0.0.1 { for_each_thermal_zone; for_each_thermal_trip; for_each_thermal_cdev; + for_each_thermal_threshold; thermal_zone_find_by_name; thermal_zone_find_by_id; thermal_zone_discover;
@@ -17,6 +18,10 @@ LIBTHERMAL_0.0.1 { thermal_cmd_get_trip; thermal_cmd_get_governor; thermal_cmd_get_temp; + thermal_cmd_threshold_get; + thermal_cmd_threshold_add; + thermal_cmd_threshold_delete; + thermal_cmd_threshold_flush; thermal_sampling_init; thermal_sampling_handle; thermal_sampling_fd;
tools/lib/thermal/thermal.c +17 -0
@@ -1,10 +1,24 @@// SPDX-License-Identifier: LGPL-2.1+ // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]> #include <stdio.h> +#include <limits.h> #include <thermal.h> #include "thermal_nl.h" +int for_each_thermal_threshold (struct thermal_threshold *th,cb_th_t cb,void *arg) +{ +int i, ret =0 ; + +if (!th) +return 0 ; + +for (i =0 ; th[i].temperature !=INT_MAX ; i++) + ret |=cb (&th[i], arg); + +return ret; +} +int for_each_thermal_cdev (struct thermal_cdev *cdev,cb_tc_t cb,void *arg) {int i, ret =0 ;
@@ -80,6 +94,9 @@ static int __thermal_zone_discover(struct thermal_zone *tz, void *th)if (thermal_cmd_get_trip (th, tz) <0 )return -1 ; +if (thermal_cmd_threshold_get (th, tz) <0 ) +return -1 ; +if (thermal_cmd_get_governor (th, tz))return -1 ;
tools/thermal/lib/Makefile +1 -1
@@ -3,7 +3,7 @@ LIBTHERMAL_TOOLS_VERSION = 0 LIBTHERMAL_TOOLS_PATCHLEVEL = 0 -LIBTHERMAL_TOOLS_EXTRAVERSION = 1 +LIBTHERMAL_TOOLS_EXTRAVERSION = 2 MAKEFLAGS += --no-print-directory
Signature · 1 line
2.43.0
PATCH 7/7 tools/thermal/thermal-engine: Take into account the thresholds API
In reply to Daniel LezcanoEnhance the thermal-engine skeleton with the thresholds added in the kernel and use the API exported by the thermal library.
--- tools/thermal/thermal-engine/thermal-engine.c | 109 +++++++++++++++--- 1 file changed, 96 insertions(+), 13 deletions(-)
tools/thermal/thermal-engine/thermal-engine.c +96 -13
@@ -38,6 +38,14 @@ struct thermal_data {struct thermal_handler *th; }; +static int show_threshold (struct thermal_threshold *th,__maybe_unused void *arg) +{ +INFO ("threshold temp=%d, direction=%d\n" , + th->temperature, th->direction); + +return 0 ; +} +static int show_trip (struct thermal_trip *tt,__maybe_unused void *arg) { INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",
@@ -70,6 +78,8 @@ static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)for_each_thermal_trip (tz->trip, show_trip,NULL ); +for_each_thermal_threshold (tz->thresholds, show_threshold,NULL ); +show_temp (tz, arg);show_governor (tz, arg);
@@ -77,6 +87,30 @@ static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)return 0 ; } +static int set_threshold (struct thermal_zone *tz,__maybe_unused void *arg) +{ +struct thermal_handler *th = arg; +int thresholds[] = {43000 ,65000 ,49000 ,55000 ,57000 }; +size_t i; + +INFO ("Setting threshold for thermal zone '%s', id=%d\n" , tz->name, tz->id); + +if (thermal_cmd_threshold_flush (th, tz)) { +ERROR ("Failed to flush all previous thresholds\n" ); +return -1 ; + } + +for (i =0 ; i <sizeof (thresholds) /sizeof (thresholds[0 ]); i++) +if (thermal_cmd_threshold_add (th, tz, thresholds[i], +THERMAL_THRESHOLD_WAY_UP | +THERMAL_THRESHOLD_WAY_DOWN )) { +ERROR ("Failed to set threshold\n" ); +return -1 ; + } + +return 0 ; +} +static int tz_create (const char *name,int tz_id,__maybe_unused void *arg) {INFO ("Thermal zone '%s'/%d created\n" , name, tz_id);
@@ -197,20 +231,66 @@ static int gov_change(int tz_id, const char *name, __maybe_unused void *arg)return 0 ; } +static int threshold_add (int tz_id,int temp,int direction, +pid_t pid,__maybe_unused void *arg) +{ +INFO ("Threshold added by pid=%d, tz_id=%d: temp=%d, direction=%d\n" , + pid, tz_id, temp, direction); + +return 0 ; +} + +static int threshold_delete (int tz_id,int temp,int direction, +pid_t pid,__maybe_unused void *arg) +{ +INFO ("Threshold deleted by pid=%d, tz_id=%d: temp=%d, direction=%d\n" , + pid, tz_id, temp, direction); + +return 0 ; +} + +static int threshold_flush (int tz_id,pid_t pid,__maybe_unused void *arg) +{ +INFO ("Thresholds flushed by pid=%d, tz_id=%d\n" , pid, tz_id); + +return 0 ; +} + +static int threshold_up (int tz_id,int temp,int last_temp,__maybe_unused void *arg) +{ +INFO ("Threshold crossed way up tz_id=%d: temp=%d, last_temp=%d\n" , + tz_id, temp, last_temp); + +return 0 ; +} + +static int threshold_down (int tz_id,int temp,int last_temp,__maybe_unused void *arg) +{ +INFO ("Threshold crossed way down tz_id=%d: temp=%d, last_temp=%d\n" , + tz_id, temp, last_temp); + +return 0 ; +} +static struct thermal_ops ops = { - .events.tz_create = tz_create, - .events.tz_delete = tz_delete, - .events.tz_disable = tz_disable, - .events.tz_enable = tz_enable, - .events.trip_high = trip_high, - .events.trip_low = trip_low, - .events.trip_add = trip_add, - .events.trip_delete = trip_delete, - .events.trip_change = trip_change, - .events.cdev_add = cdev_add, - .events.cdev_delete = cdev_delete, - .events.cdev_update = cdev_update, - .events.gov_change = gov_change + .events.tz_create = tz_create, + .events.tz_delete = tz_delete, + .events.tz_disable = tz_disable, + .events.tz_enable = tz_enable, + .events.trip_high = trip_high, + .events.trip_low = trip_low, + .events.trip_add = trip_add, + .events.trip_delete = trip_delete, + .events.trip_change = trip_change, + .events.cdev_add = cdev_add, + .events.cdev_delete = cdev_delete, + .events.cdev_update = cdev_update, + .events.gov_change = gov_change, + .events.threshold_add = threshold_add, + .events.threshold_delete = threshold_delete, + .events.threshold_flush = threshold_flush, + .events.threshold_up = threshold_up, + .events.threshold_down = threshold_down, };static int thermal_event (__maybe_unused int fd,__maybe_unused void *arg)
@@ -280,6 +360,7 @@ enum {THERMAL_ENGINE_DAEMON_ERROR ,THERMAL_ENGINE_LOG_ERROR ,THERMAL_ENGINE_THERMAL_ERROR , +THERMAL_ENGINE_THRESHOLD_ERROR , THERMAL_ENGINE_MAINLOOP_ERROR, };
@@ -318,6 +399,8 @@ int main(int argc, char *argv[])return THERMAL_ENGINE_THERMAL_ERROR ; } +for_each_thermal_zone (td.tz, set_threshold, td.th); +for_each_thermal_zone (td.tz, show_tz, td.th);if (mainloop_init ()) {
Signature · 1 line
2.43.0