Skip to contentSkip to conversation

thermal/core: Use clamp() helper in the stepwise governor

PATCH Applied Daniel Lezcano 3 participants 7 messages Jun 29, 2022 – Jul 28, 2022 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 thermal/core: Use clamp() helper in the stepwise governor

The code is actually clampling the next cooling device state using the lowest and highest states of the thermal instance.

That code can be replaced by the clamp() macro which does exactly the same. It results in a simpler routine to read.

---
 drivers/thermal/gov_step_wise.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/thermal/gov_step_wise.c +4 -11
index 12acb12aac50..6efbfaf014da 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/thermal.h>
+#include <linux/minmax.h>
 #include <trace/events/thermal.h>
 
 #include "thermal_core.h"
@@ -52,10 +53,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 
 	if (!instance->initialized) {
 		if (throttle) {
-			next_target = (cur_state + 1) >= instance->upper ?
-					instance->upper :
-					((cur_state + 1) < instance->lower ?
-					instance->lower : (cur_state + 1));
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		} else {
 			next_target = THERMAL_NO_TARGET;
 		}
@@ -66,10 +64,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 	switch (trend) {
 	case THERMAL_TREND_RAISING:
 		if (throttle) {
-			next_target = cur_state < instance->upper ?
-				    (cur_state + 1) : instance->upper;
-			if (next_target < instance->lower)
-				next_target = instance->lower;
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		}
 		break;
 	case THERMAL_TREND_RAISE_FULL:
@@ -82,9 +77,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 				next_target = THERMAL_NO_TARGET;
 		} else {
 			if (!throttle) {
-				next_target = cur_state - 1;
-				if (next_target > instance->upper)
-					next_target = instance->upper;
+				next_target = clamp((cur_state - 1), instance->lower, instance->upper);
 			}
 		}
 		break;
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 thermal/core: Remove DROP_FULL and RAISE_FULL

The trends DROP_FULL and RAISE_FULL are not used and were never used in the past AFAICT. Remove these conditions as they seems to not be handled anywhere.

---
 drivers/thermal/gov_step_wise.c | 11 -----------
 include/linux/thermal.h         |  2 --
 2 files changed, 13 deletions(-)
drivers/thermal/gov_step_wise.c +0 -11
index 6efbfaf014da..9729b46d0258 100644
@@ -67,10 +67,6 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		}
 		break;
-	case THERMAL_TREND_RAISE_FULL:
-		if (throttle)
-			next_target = instance->upper;
-		break;
 	case THERMAL_TREND_DROPPING:
 		if (cur_state <= instance->lower) {
 			if (!throttle)
@@ -81,13 +77,6 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 			}
 		}
 		break;
-	case THERMAL_TREND_DROP_FULL:
-		if (cur_state == instance->lower) {
-			if (!throttle)
-				next_target = THERMAL_NO_TARGET;
-		} else
-			next_target = instance->lower;
-		break;
 	default:
 		break;
 	}
include/linux/thermal.h +0 -2
index 522c9180a08d..c8528bb6c01c 100644
@@ -41,8 +41,6 @@ enum thermal_trend {
 	THERMAL_TREND_STABLE, /* temperature is stable */
 	THERMAL_TREND_RAISING, /* temperature is raising */
 	THERMAL_TREND_DROPPING, /* temperature is dropping */
-	THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
-	THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
 };
 
 /* Thermal notification reason */
Signature · 1 line
2.25.1
#2 lore raw

[thermal: thermal/next] thermal/core: Remove DROP_FULL and RAISE_FULL

The following commit has been merged into the thermal/next branch of thermal:

The following commit has been merged into the thermal/next branch of thermal:

Commit-ID:     4102c4042a33c682021683ec26c2dca3fd9d7cc2
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/thermal/linux.git//4102c4042a33c682021683ec26c2dca3fd9d7cc2
Author:        Daniel Lezcano <[email protected]>
AuthorDate:    Wed, 29 Jun 2022 17:10:12 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Thu, 28 Jul 2022 17:29:47 +02:00

thermal/core: Remove DROP_FULL and RAISE_FULL

The trends DROP_FULL and RAISE_FULL are not used and were never used in the past AFAICT. Remove these conditions as they seems to not be handled anywhere.

---
 drivers/thermal/gov_step_wise.c | 11 -----------
 include/linux/thermal.h         |  2 --
 2 files changed, 13 deletions(-)
drivers/thermal/gov_step_wise.c +0 -11
index 6efbfaf..9729b46 100644
@@ -67,10 +67,6 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		}
 		break;
-	case THERMAL_TREND_RAISE_FULL:
-		if (throttle)
-			next_target = instance->upper;
-		break;
 	case THERMAL_TREND_DROPPING:
 		if (cur_state <= instance->lower) {
 			if (!throttle)
@@ -81,13 +77,6 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 			}
 		}
 		break;
-	case THERMAL_TREND_DROP_FULL:
-		if (cur_state == instance->lower) {
-			if (!throttle)
-				next_target = THERMAL_NO_TARGET;
-		} else
-			next_target = instance->lower;
-		break;
 	default:
 		break;
 	}
include/linux/thermal.h +0 -2
index 365733b..231bac2 100644
@@ -40,8 +40,6 @@ enum thermal_trend {
 	THERMAL_TREND_STABLE, /* temperature is stable */
 	THERMAL_TREND_RAISING, /* temperature is raising */
 	THERMAL_TREND_DROPPING, /* temperature is dropping */
-	THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
-	THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
 };
 
 /* Thermal notification reason */
#3 lore raw
In reply to Daniel Lezcano
Quoted 8 lines · Daniel Lezcano

The code is actually clampling the next cooling device state using the lowest and highest states of the thermal instance.

That code can be replaced by the clamp() macro which does exactly the same. It results in a simpler routine to read.

---

I'll apply this series if nobody has comments

Quoted 50 lines
  drivers/thermal/gov_step_wise.c | 15 ++++-----------
  1 file changed, 4 insertions(+), 11 deletions(-)
drivers/thermal/gov_step_wise.c +4 -11
index 12acb12aac50..6efbfaf014da 100644
@@ -11,6 +11,7 @@
   */
 
  #include <linux/thermal.h>
+#include <linux/minmax.h>
  #include <trace/events/thermal.h>
 
  #include "thermal_core.h"
@@ -52,10 +53,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 
  	if (!instance->initialized) {
  		if (throttle) {
-			next_target = (cur_state + 1) >= instance->upper ?
-					instance->upper :
-					((cur_state + 1) < instance->lower ?
-					instance->lower : (cur_state + 1));
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
  		} else {
  			next_target = THERMAL_NO_TARGET;
  		}
@@ -66,10 +64,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
  	switch (trend) {
  	case THERMAL_TREND_RAISING:
  		if (throttle) {
-			next_target = cur_state < instance->upper ?
-				    (cur_state + 1) : instance->upper;
-			if (next_target < instance->lower)
-				next_target = instance->lower;
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
  		}
  		break;
  	case THERMAL_TREND_RAISE_FULL:
@@ -82,9 +77,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
  				next_target = THERMAL_NO_TARGET;
  		} else {
  			if (!throttle) {
-				next_target = cur_state - 1;
-				if (next_target > instance->upper)
-					next_target = instance->upper;
+				next_target = clamp((cur_state - 1), instance->lower, instance->upper);
  			}
  		}
  		break;
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
#4 lore raw
Quoted 5 lines · Daniel Lezcano
Quoted 3 lines · Daniel Lezcano

The code is actually clampling the next cooling device state using the lowest and highest states of the thermal instance.

Quoted 7 lines
Quoted 5 lines

That code can be replaced by the clamp() macro which does exactly the same. It results in a simpler routine to read.

---

I'll apply this series if nobody has comments

Sounds good to me.

Please feel free to add ACKs from me to the patches.

Quoted 59 lines
Quoted 41 lines
  drivers/thermal/gov_step_wise.c | 15 ++++-----------
  1 file changed, 4 insertions(+), 11 deletions(-)
drivers/thermal/gov_step_wise.c +3 -8
index 12acb12aac50..6efbfaf014da 100644
@@ -11,6 +11,7 @@
   */
 
  #include <linux/thermal.h>
+#include <linux/minmax.h>
  #include <trace/events/thermal.h>
 
  #include "thermal_core.h"
@@ -52,10 +53,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 
      if (!instance->initialized) {
              if (throttle) {
-                     next_target = (cur_state + 1) >= instance->upper ?
-                                     instance->upper :
-                                     ((cur_state + 1) < instance->lower ?
-                                     instance->lower : (cur_state + 1));
+                     next_target = clamp((cur_state + 1), instance->lower, instance->upper);
              } else {
                      next_target = THERMAL_NO_TARGET;
              }
@@ -66,10 +64,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
      switch (trend) {
      case THERMAL_TREND_RAISING:
              if (throttle) {
-                     next_target = cur_state < instance->upper ?
-                                 (cur_state + 1) : instance->upper;
-                     if (next_target < instance->lower)
-                             next_target = instance->lower;
+                     next_target = clamp((cur_state + 1), instance->lower, instance->upper);
              }
              break;
      case THERMAL_TREND_RAISE_FULL:

drivers/thermal/gov_step_wise.c static unsigned long get_target_state(struct thermal_instance *instance, · line 83

Quoted 10 lines
drivers/thermal/gov_step_wise.c +1 -3
@@ -82,9 +77,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
                              next_target = THERMAL_NO_TARGET;
              } else {
                      if (!throttle) {
-                             next_target = cur_state - 1;
-                             if (next_target > instance->upper)
-                                     next_target = instance->upper;
+                             next_target = clamp((cur_state - 1), instance->lower, instance->upper);
                      }
              }
              break;

--
<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

#5 lore raw
Quoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Daniel Lezcano
Quoted 6 lines · Daniel Lezcano

The code is actually clampling the next cooling device state using the lowest and highest states of the thermal instance.

That code can be replaced by the clamp() macro which does exactly the same. It results in a simpler routine to read.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

---

I'll apply this series if nobody has comments

Sounds good to me.

Please feel free to add ACKs from me to the patches.

Sure, thanks!

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
#6 lore raw
In reply to Daniel Lezcano
The following commit has been merged into the thermal/next branch of thermal:

The following commit has been merged into the thermal/next branch of thermal:

Commit-ID:     25bff3ed9a8aaee45c3b554cca58673f1bea1bdc
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/thermal/linux.git//25bff3ed9a8aaee45c3b554cca58673f1bea1bdc
Author:        Daniel Lezcano <[email protected]>
AuthorDate:    Wed, 29 Jun 2022 17:10:11 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Thu, 28 Jul 2022 17:29:47 +02:00

thermal/core: Use clamp() helper in the stepwise governor

The code is actually clampling the next cooling device state using the lowest and highest states of the thermal instance.

That code can be replaced by the clamp() macro which does exactly the same. It results in a simpler routine to read.

---
 drivers/thermal/gov_step_wise.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/thermal/gov_step_wise.c +4 -11
index 12acb12..6efbfaf 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/thermal.h>
+#include <linux/minmax.h>
 #include <trace/events/thermal.h>
 
 #include "thermal_core.h"
@@ -52,10 +53,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 
 	if (!instance->initialized) {
 		if (throttle) {
-			next_target = (cur_state + 1) >= instance->upper ?
-					instance->upper :
-					((cur_state + 1) < instance->lower ?
-					instance->lower : (cur_state + 1));
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		} else {
 			next_target = THERMAL_NO_TARGET;
 		}
@@ -66,10 +64,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 	switch (trend) {
 	case THERMAL_TREND_RAISING:
 		if (throttle) {
-			next_target = cur_state < instance->upper ?
-				    (cur_state + 1) : instance->upper;
-			if (next_target < instance->lower)
-				next_target = instance->lower;
+			next_target = clamp((cur_state + 1), instance->lower, instance->upper);
 		}
 		break;
 	case THERMAL_TREND_RAISE_FULL:
@@ -82,9 +77,7 @@ static unsigned long get_target_state(struct thermal_instance *instance,
 				next_target = THERMAL_NO_TARGET;
 		} else {
 			if (!throttle) {
-				next_target = cur_state - 1;
-				if (next_target > instance->upper)
-					next_target = instance->upper;
+				next_target = clamp((cur_state - 1), instance->lower, instance->upper);
 			}
 		}
 		break;