Skip to content

Commit cf3986f

Browse files
nfrapradorafaeljw
authored andcommitted
thermal: core: Don't update trip points inside the hysteresis range
When searching for the trip points that need to be set, the nearest higher trip point's temperature is used for the high trip, while the nearest lower trip point's temperature minus the hysteresis is used for the low trip. The issue with this logic is that when the current temperature is inside a trip point's hysteresis range, both high and low trips will come from the same trip point. As a consequence instability can still occur like this: * the temperature rises slightly and enters the hysteresis range of a trip point * polling happens and updates the trip points to the hysteresis range * the temperature falls slightly, exiting the hysteresis range, crossing the trip point and triggering an IRQ, the trip points are updated * repeat So even though the current hysteresis implementation prevents instability from happening due to IRQs triggering on the same temperature value, both ways, it doesn't prevent it from happening due to an IRQ on one way and polling on the other. To properly implement a hysteresis behavior, when inside the hysteresis range, don't update the trip points. This way, the previously set trip points will stay in effect, which will in a way remember the previous state (if the temperature signal came from above or below the range) and therefore have the right trip point already set. The exception is if there was no previous trip point set, in which case a previous state doesn't exist, and so it's sensible to allow the hysteresis range as trip points. The following logs show the current behavior when running on a real machine: [ 202.524658] thermal thermal_zone0: new temperature boundaries: -2147483647 < x < 40000 203.562817: thermal_temperature: thermal_zone=vpu0-thermal id=0 temp_prev=36986 temp=37979 [ 203.562845] thermal thermal_zone0: new temperature boundaries: 37000 < x < 40000 204.176059: thermal_temperature: thermal_zone=vpu0-thermal id=0 temp_prev=37979 temp=40028 [ 204.176089] thermal thermal_zone0: new temperature boundaries: 37000 < x < 100000 205.226813: thermal_temperature: thermal_zone=vpu0-thermal id=0 temp_prev=40028 temp=38652 [ 205.226842] thermal thermal_zone0: new temperature boundaries: 37000 < x < 40000 And with this patch applied: [ 184.933415] thermal thermal_zone0: new temperature boundaries: -2147483647 < x < 40000 185.981182: thermal_temperature: thermal_zone=vpu0-thermal id=0 temp_prev=36986 temp=37872 186.744685: thermal_temperature: thermal_zone=vpu0-thermal id=0 temp_prev=37872 temp=40058 [ 186.744716] thermal thermal_zone0: new temperature boundaries: 37000 < x < 100000 187.773284: thermal_temperature: thermal_zone=vpu0-thermal id=0 temp_prev=40058 temp=38698 Fixes: 060c034 ("thermal: Add support for hardware-tracked trip points") Signed-off-by: Nícolas F. R. A. Prado <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Co-developed-by: Rafael J. Wysocki <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 8c35b1f commit cf3986f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

drivers/thermal/thermal_trip.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
6565
{
6666
struct thermal_trip trip;
6767
int low = -INT_MAX, high = INT_MAX;
68+
bool same_trip = false;
6869
int i, ret;
6970

7071
lockdep_assert_held(&tz->lock);
@@ -73,6 +74,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
7374
return;
7475

7576
for (i = 0; i < tz->num_trips; i++) {
77+
bool low_set = false;
7678
int trip_low;
7779

7880
ret = __thermal_zone_get_trip(tz, i , &trip);
@@ -81,18 +83,31 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
8183

8284
trip_low = trip.temperature - trip.hysteresis;
8385

84-
if (trip_low < tz->temperature && trip_low > low)
86+
if (trip_low < tz->temperature && trip_low > low) {
8587
low = trip_low;
88+
low_set = true;
89+
same_trip = false;
90+
}
8691

8792
if (trip.temperature > tz->temperature &&
88-
trip.temperature < high)
93+
trip.temperature < high) {
8994
high = trip.temperature;
95+
same_trip = low_set;
96+
}
9097
}
9198

9299
/* No need to change trip points */
93100
if (tz->prev_low_trip == low && tz->prev_high_trip == high)
94101
return;
95102

103+
/*
104+
* If "high" and "low" are the same, skip the change unless this is the
105+
* first time.
106+
*/
107+
if (same_trip && (tz->prev_low_trip != -INT_MAX ||
108+
tz->prev_high_trip != INT_MAX))
109+
return;
110+
96111
tz->prev_low_trip = low;
97112
tz->prev_high_trip = high;
98113

0 commit comments

Comments
 (0)