Skip to contentSkip to conversation

thermal: gov_step_wise: Two cleanups and small adjustment

PATCH In review Rafael J. Wysocki 2 participants 8 messages Aug 25, 2025 – Sep 4, 2025 Thermal

lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

v2 cover thermal: gov_step_wise: Two cleanups and small adjustment

Hi All,

This series makes a straightforward code cleanup in the Step-wise thermal governor, clarifies a slightly ambiguous comment in it, and adjusts its behavior to be consistent with another throttling logic description comment and possibly reduce undesirable performance oscillations between high and low levels due to cooling in some cases.

Thanks!

author maintainer
#1 lore raw

PATCH v2 1/3 thermal: gov_step_wise: Clean up local variable initialization

From: Rafael J. Wysocki <[email protected]>

Make the initialization of local variable throttle in thermal_zone_trip_update() more straightforward.

No intentional functional impact.

---
 drivers/thermal/gov_step_wise.c |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/thermal/gov_step_wise.c +2 -4
@@ -69,16 +69,14 @@
 				     const struct thermal_trip_desc *td,
 				     int trip_threshold)
 {
+	bool throttle = tz->temperature >= trip_threshold;
 	const struct thermal_trip *trip = &td->trip;
 	enum thermal_trend trend = get_tz_trend(tz, trip);
 	int trip_id = thermal_zone_trip_id(tz, trip);
 	struct thermal_instance *instance;
-	bool throttle = false;
 
-	if (tz->temperature >= trip_threshold) {
-		throttle = true;
+	if (throttle)
 		trace_thermal_zone_trip(tz, trip_id, trip->type);
-	}
 
 	dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
 		trip_id, trip->type, trip_threshold, trend, throttle);
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] thermal: gov_step_wise: Clean up local variable initialization

Quoted 26 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Make the initialization of local variable throttle in thermal_zone_trip_update() more straightforward.

No intentional functional impact.

---
  drivers/thermal/gov_step_wise.c |    6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
drivers/thermal/gov_step_wise.c +1 -0
@@ -69,16 +69,14 @@
  				     const struct thermal_trip_desc *td,
  				     int trip_threshold)
  {
+	bool throttle = tz->temperature >= trip_threshold;
  	const struct thermal_trip *trip = &td->trip;
  	enum thermal_trend trend = get_tz_trend(tz, trip);
  	int trip_id = thermal_zone_trip_id(tz, trip);
  	struct thermal_instance *instance;

drivers/thermal/gov_step_wise.c line 82

Quoted 10 lines
drivers/thermal/gov_step_wise.c +1 -4
-	bool throttle = false;
 
-	if (tz->temperature >= trip_threshold) {
-		throttle = true;
+	if (throttle)
  		trace_thermal_zone_trip(tz, trip_id, trip->type);
-	}
 
  	dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
  		trip_id, trip->type, trip_threshold, trend, throttle);

LGTM,

author maintainer
#3 lore raw

PATCH v2 2/3 thermal: gov_step_wise: Clarify cooling logic description comment

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The cooling logic description comment next to the get_target_state() definition is slightly ambiguous in what it means by "lower cooling state", so clarify that by replacing the ambuguous phrase with "the minimum applicable cooling state".

No functional impact.

---
 drivers/thermal/gov_step_wise.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/thermal/gov_step_wise.c +2 -2
@@ -23,8 +23,8 @@
  *    b. if the trend is THERMAL_TREND_DROPPING, do nothing
  * If the temperature is lower than a trip point,
  *    a. if the trend is THERMAL_TREND_RAISING, do nothing
- *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
- *       state for this trip point, if the cooling state already
+ *    b. if the trend is THERMAL_TREND_DROPPING, use the minimum applicable
+ *       cooling state for this trip point, or if the cooling state already
  *       equals lower limit, deactivate the thermal instance
  */
 static unsigned long get_target_state(struct thermal_instance *instance,
reviewer Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] thermal: gov_step_wise: Clarify cooling logic description comment

Quoted 20 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The cooling logic description comment next to the get_target_state() definition is slightly ambiguous in what it means by "lower cooling state", so clarify that by replacing the ambuguous phrase with "the minimum applicable cooling state".

No functional impact.

---
  drivers/thermal/gov_step_wise.c |    4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

drivers/thermal/gov_step_wise.c line 30

Quoted 10 lines
drivers/thermal/gov_step_wise.c +2 -2
@@ -23,8 +23,8 @@
   *    b. if the trend is THERMAL_TREND_DROPPING, do nothing
   * If the temperature is lower than a trip point,
   *    a. if the trend is THERMAL_TREND_RAISING, do nothing
- *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
- *       state for this trip point, if the cooling state already
+ *    b. if the trend is THERMAL_TREND_DROPPING, use the minimum applicable
+ *       cooling state for this trip point, or if the cooling state already
   *       equals lower limit, deactivate the thermal instance
   */
  static unsigned long get_target_state(struct thermal_instance *instance,
author maintainer
#5 lore raw

PATCH v2 3/3 thermal: gov_step_wise: Allow cooling level to be reduced earlier

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The current behavior of the Step-wise thermal governor is to increase the cooling level one step at a time after trip point threshold passing by thermal zone temperature until the temperature stops to rise and then do nothing until it falls down below the (possibly new) trip point threshold, at which point the cooling level is reduced straight to the applicable minimum.

While this generally works, it is not in agreement with the throttling logic description comment in step_wise_manage() any more after some relatively recent changes, and in the case of passive cooling, it may lead to undesirable performance oscillations between high and low levels.

For this reason, modify the governor's cooling device state selection function, get_target_state(), to reduce cooling by one level even if the temperature is still above the thermal zone threshold, but the temperature has started to fall down. However, ensure that the cooling level will remain above the applicable minimum in that case to pull the zone temperature further down, possibly until it falls below the trip threshold (which may now be equal to the low temperature of the trip).

Doing so should help higher performance to be restored earlier in some cases which is desirable especially for passive trip points with relatively high hysteresis values.

---
 drivers/thermal/gov_step_wise.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/thermal/gov_step_wise.c +14 -1
@@ -20,7 +20,9 @@
  * If the temperature is higher than a trip point,
  *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
  *       state for this trip point
- *    b. if the trend is THERMAL_TREND_DROPPING, do nothing
+ *    b. if the trend is THERMAL_TREND_DROPPING, use a lower cooling state
+ *       for this trip point, but keep the cooling state above the applicable
+ *       minimum
  * If the temperature is lower than a trip point,
  *    a. if the trend is THERMAL_TREND_RAISING, do nothing
  *    b. if the trend is THERMAL_TREND_DROPPING, use the minimum applicable
@@ -51,6 +53,17 @@
 	if (throttle) {
 		if (trend == THERMAL_TREND_RAISING)
 			return clamp(cur_state + 1, instance->lower, instance->upper);
+
+		/*
+		 * If the zone temperature is falling, the cooling level can
+		 * be reduced, but it should still be above the lower state of
+		 * the given thermal instance to pull the temperature further
+		 * down.
+		 */
+		if (trend == THERMAL_TREND_DROPPING)
+			return clamp(cur_state - 1,
+				     min(instance->lower + 1, instance->upper),
+				     instance->upper);
 	} else if (trend == THERMAL_TREND_DROPPING) {
 		if (cur_state <= instance->lower)
 			return THERMAL_NO_TARGET;
reviewer Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] thermal: gov_step_wise: Allow cooling level to be reduced earlier

Quoted 8 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The current behavior of the Step-wise thermal governor is to increase the cooling level one step at a time after trip point threshold passing by thermal zone temperature until the temperature stops to rise and then do nothing until it falls down below the (possibly new) trip point threshold, at which point the cooling level is reduced straight to the applicable minimum.

Quite long single sentence to describe these stuff...

Quoted 49 lines

While this generally works, it is not in agreement with the throttling logic description comment in step_wise_manage() any more after some relatively recent changes, and in the case of passive cooling, it may lead to undesirable performance oscillations between high and low levels.

For this reason, modify the governor's cooling device state selection function, get_target_state(), to reduce cooling by one level even if the temperature is still above the thermal zone threshold, but the temperature has started to fall down. However, ensure that the cooling level will remain above the applicable minimum in that case to pull the zone temperature further down, possibly until it falls below the trip threshold (which may now be equal to the low temperature of the trip).

Doing so should help higher performance to be restored earlier in some cases which is desirable especially for passive trip points with relatively high hysteresis values.

---
  drivers/thermal/gov_step_wise.c |   15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
drivers/thermal/gov_step_wise.c +7 -1
@@ -20,7 +20,9 @@
   * If the temperature is higher than a trip point,
   *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
   *       state for this trip point
- *    b. if the trend is THERMAL_TREND_DROPPING, do nothing
+ *    b. if the trend is THERMAL_TREND_DROPPING, use a lower cooling state
+ *       for this trip point, but keep the cooling state above the applicable
+ *       minimum
   * If the temperature is lower than a trip point,
   *    a. if the trend is THERMAL_TREND_RAISING, do nothing
   *    b. if the trend is THERMAL_TREND_DROPPING, use the minimum applicable
@@ -51,6 +53,17 @@
  	if (throttle) {
  		if (trend == THERMAL_TREND_RAISING)
  			return clamp(cur_state + 1, instance->lower, instance->upper);
+
+		/*
+		 * If the zone temperature is falling, the cooling level can
+		 * be reduced, but it should still be above the lower state of

drivers/thermal/gov_step_wise.c line 69

Quoted 10 lines
drivers/thermal/gov_step_wise.c +7 -0
+		 * the given thermal instance to pull the temperature further
+		 * down.
+		 */
+		if (trend == THERMAL_TREND_DROPPING)
+			return clamp(cur_state - 1,
+				     min(instance->lower + 1, instance->upper),
+				     instance->upper);
  	} else if (trend == THERMAL_TREND_DROPPING) {
  		if (cur_state <= instance->lower)
  			return THERMAL_NO_TARGET;

That make sense

author maintainer
#7 lore raw

Re: [PATCH v2 3/3] thermal: gov_step_wise: Allow cooling level to be reduced earlier

Quoted 6 lines · Lukasz Luba
Quoted 2 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Quoted 8 lines
Quoted 6 lines

The current behavior of the Step-wise thermal governor is to increase the cooling level one step at a time after trip point threshold passing by thermal zone temperature until the temperature stops to rise and then do nothing until it falls down below the (possibly new) trip point threshold, at which point the cooling level is reduced straight to the applicable minimum.

Quite long single sentence to describe these stuff...

Yes, it is long. I'll try to rearrange it when applying.

Quoted 63 lines
Quoted 49 lines

While this generally works, it is not in agreement with the throttling logic description comment in step_wise_manage() any more after some relatively recent changes, and in the case of passive cooling, it may lead to undesirable performance oscillations between high and low levels.

For this reason, modify the governor's cooling device state selection function, get_target_state(), to reduce cooling by one level even if the temperature is still above the thermal zone threshold, but the temperature has started to fall down. However, ensure that the cooling level will remain above the applicable minimum in that case to pull the zone temperature further down, possibly until it falls below the trip threshold (which may now be equal to the low temperature of the trip).

Doing so should help higher performance to be restored earlier in some cases which is desirable especially for passive trip points with relatively high hysteresis values.

---
  drivers/thermal/gov_step_wise.c |   15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
drivers/thermal/gov_step_wise.c +7 -1
@@ -20,7 +20,9 @@
   * If the temperature is higher than a trip point,
   *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
   *       state for this trip point
- *    b. if the trend is THERMAL_TREND_DROPPING, do nothing
+ *    b. if the trend is THERMAL_TREND_DROPPING, use a lower cooling state
+ *       for this trip point, but keep the cooling state above the applicable
+ *       minimum
   * If the temperature is lower than a trip point,
   *    a. if the trend is THERMAL_TREND_RAISING, do nothing
   *    b. if the trend is THERMAL_TREND_DROPPING, use the minimum applicable
@@ -51,6 +53,17 @@
      if (throttle) {
              if (trend == THERMAL_TREND_RAISING)
                      return clamp(cur_state + 1, instance->lower, instance->upper);
+
+             /*
+              * If the zone temperature is falling, the cooling level can
+              * be reduced, but it should still be above the lower state of

drivers/thermal/gov_step_wise.c line 69

Quoted 10 lines
drivers/thermal/gov_step_wise.c +7 -0
+              * the given thermal instance to pull the temperature further
+              * down.
+              */
+             if (trend == THERMAL_TREND_DROPPING)
+                     return clamp(cur_state - 1,
+                                  min(instance->lower + 1, instance->upper),
+                                  instance->upper);
      } else if (trend == THERMAL_TREND_DROPPING) {
              if (cur_state <= instance->lower)
                      return THERMAL_NO_TARGET;

That make sense