Skip to contentSkip to conversation

thermal: gov_bang_bang: Prevent cooling devices from getting stuck in the "on" state

PATCH In review Rafael J. Wysocki 5 participants 22 messages Aug 13, 2024 – Aug 16, 2024 Thermal

lore
Share & tools
lore
#0 lore raw

cover thermal: gov_bang_bang: Prevent cooling devices from getting stuck in the "on" state

Hi Everyone,

After changes made in 6.10, the Bang-bang governor has an initialization problem on systems where cooling devices start in the "on" state, but the thermal zone temperature stays below the corresponding trip points.

Namely, the Bang-bang governor only implements a .trip_crossed() callback which only runs when a trip point is crossed. If the zone temperature is always below the trip point, that callback will never be invoked. Now, if a cooling device bound to that trip point starts in the "on" state, the governor has no chance to change its state to "off".

This currently happens in the acerhdf driver, but it may as well happen elsewhere, so I think that it needs to be addressed in the thermal subsystem.

It can be addressed by adding a .manage() callback to the Bang-bang governor, which is done in patch [3/4]. That callback will be invoked every time __thermal_zone_device_update() runs, not just when a trip is crossed, so it can adjust the states of the cooling devices to the thermal zone temperature. However, after running once, it becomes a pure needless overhead because the states of cooling devices only need to be fixed up once (modulo some special situations like system resume).

That's addressed in patch [4/4] which uses governor_data to store the information on whether or not the states of the cooling devices will need to be adjusted.

Patches [1-2/4] are preliminary, but IMV it is better to make these changes separately for clarity, but also in case they turn out to have a functional effect which is not expected.

Overall, this series is a fix candidate for 6.11-rc because the change in behavior addressed by it can be regarded as a regression with respect to 6.9.

Unfortunately, it affects this series:

https://lore.kernel.org/linux-pm/[email protected]/

which will need to be reordered and rebased (slightly), but because I've dropped one broken patch from it already, it will need to be changed anyway.

Thanks!

#1 lore raw

PATCH 1/4 thermal: gov_bang_bang: Call __thermal_cdev_update() directly

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

Instead of clearing the "updated" flag for each cooling device affected by the trip point crossing in bang_bang_control() and walking all thermal instances to run thermal_cdev_update() for all of the affected cooling devices, call __thermal_cdev_update() directly for each of them.

No intentional functional impact.

---
 drivers/thermal/gov_bang_bang.c |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/thermal/gov_bang_bang.c +1 -4
Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================
@@ -71,12 +71,9 @@ static void bang_bang_control(struct the
 		dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
 
 		mutex_lock(&instance->cdev->lock);
-		instance->cdev->updated = false; /* cdev needs update */
+		__thermal_cdev_update(instance->cdev);
 		mutex_unlock(&instance->cdev->lock);
 	}
-
-	list_for_each_entry(instance, &tz->thermal_instances, tz_node)
-		thermal_cdev_update(instance->cdev);
 }
 
 static struct thermal_governor thermal_gov_bang_bang = {
Acked-by on v1 1/4
#2 lore raw

Re: [PATCH v1 1/4] thermal: gov_bang_bang: Call __thermal_cdev_update() directly

Quoted 11 lines · Rafael J. Wysocki

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

Instead of clearing the "updated" flag for each cooling device affected by the trip point crossing in bang_bang_control() and walking all thermal instances to run thermal_cdev_update() for all of the affected cooling devices, call __thermal_cdev_update() directly for each of them.

No intentional functional impact.

Quoted 25 lines
---
  drivers/thermal/gov_bang_bang.c |    5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +1 -4
@@ -71,12 +71,9 @@ static void bang_bang_control(struct the
  		dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
 
  		mutex_lock(&instance->cdev->lock);
-		instance->cdev->updated = false; /* cdev needs update */
+		__thermal_cdev_update(instance->cdev);
  		mutex_unlock(&instance->cdev->lock);
  	}
-
-	list_for_each_entry(instance, &tz->thermal_instances, tz_node)
-		thermal_cdev_update(instance->cdev);
  }
 
  static struct thermal_governor thermal_gov_bang_bang = {
reviewer
#3 lore raw

Re: [PATCH v1 1/4] thermal: gov_bang_bang: Call __thermal_cdev_update() directly

In reply to Rafael J. Wysocki
Quoted 7 lines · Rafael J. Wysocki

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

Instead of clearing the "updated" flag for each cooling device affected by the trip point crossing in bang_bang_control() and walking all thermal instances to run thermal_cdev_update() for all of the affected cooling devices, call __thermal_cdev_update() directly for each of them.

with this change, we may invoke thermal_cdev_set_cur_state() for multiple times instead of one, in one bang_bang_control() run.

So this effectively changes the notifications and statistics.

If this is not a problem, maybe better to mention this change in the changelog?

thanks,
rui

Quoted 32 lines

No intentional functional impact.

---
 drivers/thermal/gov_bang_bang.c |    5 +----

 1 file changed, 1 insertion(+), 4 deletions(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +0 -0
@@ -71,12 +71,9 @@ static void bang_bang_control(struct the
                 dev_dbg(&instance->cdev->device, "target=%ld\n",
 instance->target);


                mutex_lock(&instance->cdev->lock);
-               instance->cdev->updated = false; /* cdev needs update */
+               __thermal_cdev_update(instance->cdev); mutex_unlock(&instance->cdev->lock);
        }
-
-       list_for_each_entry(instance, &tz->thermal_instances, tz_node)
-               thermal_cdev_update(instance->cdev);
 }

 static struct thermal_governor thermal_gov_bang_bang = {

#4 lore raw

Re: [PATCH v1 1/4] thermal: gov_bang_bang: Call __thermal_cdev_update() directly

Quoted 4 lines · Rui
Quoted 2 lines · Rafael J. Wysocki

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

Quoted 8 lines
Quoted 5 lines

Instead of clearing the "updated" flag for each cooling device affected by the trip point crossing in bang_bang_control() and walking all thermal instances to run thermal_cdev_update() for all of the affected cooling devices, call __thermal_cdev_update() directly for each of them.

with this change, we may invoke thermal_cdev_set_cur_state() for multiple times instead of one, in one bang_bang_control() run.

No, this cannot happen AFAICS because one cooling device cannot be bound to the same trip point more than once.

Since bang_bang_control() only checks thermal instances for one trip point, all cooling devices pointed to by them are guaranteed to be different.

Quoted 40 lines

So this effectively changes the notifications and statistics.

If this is not a problem, maybe better to mention this change in the changelog?

thanks,
rui

Quoted 32 lines

No intentional functional impact.

---
 drivers/thermal/gov_bang_bang.c |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +1 -4
@@ -71,12 +71,9 @@ static void bang_bang_control(struct the
                dev_dbg(&instance->cdev->device, "target=%ld\n",
 instance->target);
 
                mutex_lock(&instance->cdev->lock);
-               instance->cdev->updated = false; /* cdev needs update
 */
+               __thermal_cdev_update(instance->cdev);
                mutex_unlock(&instance->cdev->lock);
        }
-
-       list_for_each_entry(instance, &tz->thermal_instances,
 tz_node)
-               thermal_cdev_update(instance->cdev);

}

static struct thermal_governor thermal_gov_bang_bang = {

reviewer Reviewed-by on v1 1/4
#5 lore raw

Re: [PATCH v1 1/4] thermal: gov_bang_bang: Call __thermal_cdev_update() directly

In reply to Rafael J. Wysocki
Quoted 11 lines · Rafael J. Wysocki

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

Instead of clearing the "updated" flag for each cooling device affected by the trip point crossing in bang_bang_control() and walking all thermal instances to run thermal_cdev_update() for all of the affected cooling devices, call __thermal_cdev_update() directly for each of them.

No intentional functional impact.

thanks,
rui

Quoted 28 lines

---
 drivers/thermal/gov_bang_bang.c |    5 +----

 1 file changed, 1 insertion(+), 4 deletions(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +0 -0
@@ -71,12 +71,9 @@ static void bang_bang_control(struct the
                 dev_dbg(&instance->cdev->device, "target=%ld\n",
 instance->target);


                mutex_lock(&instance->cdev->lock);
-               instance->cdev->updated = false; /* cdev needs update */
+               __thermal_cdev_update(instance->cdev); mutex_unlock(&instance->cdev->lock);
        }
-
-       list_for_each_entry(instance, &tz->thermal_instances, tz_node)
-               thermal_cdev_update(instance->cdev);
 }

 static struct thermal_governor thermal_gov_bang_bang = {

#6 lore raw

PATCH 2/4 thermal: gov_bang_bang: Split bang_bang_control()

In reply to Rafael J. Wysocki

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

Move the setting of the thermal instance target state from bang_bang_control() into a separate function that will be also called in a different place going forward.

No intentional functional impact.

---
 drivers/thermal/gov_bang_bang.c |   42 +++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)
drivers/thermal/gov_bang_bang.c +23 -19
Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================
@@ -13,6 +13,27 @@
 
 #include "thermal_core.h"
 
+static void bang_bang_set_instance_target(struct thermal_instance *instance,
+					  unsigned int target)
+{
+	if (instance->target != 0 && instance->target != 1 &&
+	    instance->target != THERMAL_NO_TARGET)
+		pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
+			 instance->target, instance->name);
+
+	/*
+	 * Enable the fan when the trip is crossed on the way up and disable it
+	 * when the trip is crossed on the way down.
+	 */
+	instance->target = target;
+
+	dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
+
+	mutex_lock(&instance->cdev->lock);
+	__thermal_cdev_update(instance->cdev);
+	mutex_unlock(&instance->cdev->lock);
+}
+
 /**
  * bang_bang_control - controls devices associated with the given zone
  * @tz: thermal_zone_device
@@ -54,25 +75,8 @@ static void bang_bang_control(struct the
 		tz->temperature, trip->hysteresis);
 
 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
-		if (instance->trip != trip)
-			continue;
-
-		if (instance->target != 0 && instance->target != 1 &&
-		    instance->target != THERMAL_NO_TARGET)
-			pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
-				 instance->target, instance->name);
-
-		/*
-		 * Enable the fan when the trip is crossed on the way up and
-		 * disable it when the trip is crossed on the way down.
-		 */
-		instance->target = crossed_up;
-
-		dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
-
-		mutex_lock(&instance->cdev->lock);
-		__thermal_cdev_update(instance->cdev);
-		mutex_unlock(&instance->cdev->lock);
+		if (instance->trip == trip)
+			bang_bang_set_instance_target(instance, crossed_up);
 	}
 }
 
Acked-by on v1 2/4
#7 lore raw

Re: [PATCH v1 2/4] thermal: gov_bang_bang: Split bang_bang_control()

Quoted 9 lines · Rafael J. Wysocki

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

Move the setting of the thermal instance target state from bang_bang_control() into a separate function that will be also called in a different place going forward.

No intentional functional impact.

Quoted 67 lines
---
  drivers/thermal/gov_bang_bang.c |   42 +++++++++++++++++++++-------------------
  1 file changed, 23 insertions(+), 19 deletions(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +23 -19
@@ -13,6 +13,27 @@
 
  #include "thermal_core.h"
 
+static void bang_bang_set_instance_target(struct thermal_instance *instance,
+					  unsigned int target)
+{
+	if (instance->target != 0 && instance->target != 1 &&
+	    instance->target != THERMAL_NO_TARGET)
+		pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
+			 instance->target, instance->name);
+
+	/*
+	 * Enable the fan when the trip is crossed on the way up and disable it
+	 * when the trip is crossed on the way down.
+	 */
+	instance->target = target;
+
+	dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
+
+	mutex_lock(&instance->cdev->lock);
+	__thermal_cdev_update(instance->cdev);
+	mutex_unlock(&instance->cdev->lock);
+}
+
  /**
   * bang_bang_control - controls devices associated with the given zone
   * @tz: thermal_zone_device
@@ -54,25 +75,8 @@ static void bang_bang_control(struct the
  		tz->temperature, trip->hysteresis);
 
  	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
-		if (instance->trip != trip)
-			continue;
-
-		if (instance->target != 0 && instance->target != 1 &&
-		    instance->target != THERMAL_NO_TARGET)
-			pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
-				 instance->target, instance->name);
-
-		/*
-		 * Enable the fan when the trip is crossed on the way up and
-		 * disable it when the trip is crossed on the way down.
-		 */
-		instance->target = crossed_up;
-
-		dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
-
-		mutex_lock(&instance->cdev->lock);
-		__thermal_cdev_update(instance->cdev);
-		mutex_unlock(&instance->cdev->lock);
+		if (instance->trip == trip)
+			bang_bang_set_instance_target(instance, crossed_up);
  	}
  }
 
reviewer Reviewed-by on v1 2/4
#8 lore raw

Re: [PATCH v1 2/4] thermal: gov_bang_bang: Split bang_bang_control()

In reply to Rafael J. Wysocki
Quoted 9 lines · Rafael J. Wysocki

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

Move the setting of the thermal instance target state from bang_bang_control() into a separate function that will be also called in a different place going forward.

No intentional functional impact.

thanks,
rui

Quoted 79 lines

---
 drivers/thermal/gov_bang_bang.c |   42 +++++++++++++++++++++--------
-----------

 1 file changed, 23 insertions(+), 19 deletions(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +0 -0
@@ -13,6 +13,27 @@


 #include "thermal_core.h"

+static void bang_bang_set_instance_target(struct thermal_instance *instance,
+                                         unsigned int target) +{
+       if (instance->target != 0 && instance->target != 1 && +           instance->target != THERMAL_NO_TARGET)
+               pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
+                        instance->target, instance->name); +
+       /*
+        * Enable the fan when the trip is crossed on the way up and disable it
+        * when the trip is crossed on the way down.
+        */
+       instance->target = target;
+
+       dev_dbg(&instance->cdev->device, "target=%ld\n", instance-

Quoted 1 line

target);

drivers/thermal/gov_bang_bang.c +6 -0
+
+       mutex_lock(&instance->cdev->lock);
+       __thermal_cdev_update(instance->cdev);
+       mutex_unlock(&instance->cdev->lock);
+}
+

 /**
  * bang_bang_control - controls devices associated with the given zone
  * @tz: thermal_zone_device

drivers/thermal/gov_bang_bang.c +0 -0
@@ -54,25 +75,8 @@ static void bang_bang_control(struct the
                 tz->temperature, trip->hysteresis);


        list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
-               if (instance->trip != trip)
-                       continue;
-
-               if (instance->target != 0 && instance->target != 1 && -                   instance->target != THERMAL_NO_TARGET) -                       pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n",
-                                instance->target, instance->name); -
-               /*
-                * Enable the fan when the trip is crossed on the way up and
-                * disable it when the trip is crossed on the way down.
-                */
-               instance->target = crossed_up;
-
-               dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
-
-               mutex_lock(&instance->cdev->lock);
-               __thermal_cdev_update(instance->cdev);
-               mutex_unlock(&instance->cdev->lock);
+               if (instance->trip == trip)
+                       bang_bang_set_instance_target(instance, crossed_up);
        }
 }

#9 lore raw

PATCH 3/4 thermal: gov_bang_bang: Add .manage() callback

In reply to Rafael J. Wysocki

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

After recent changes, the Bang-bang governor may not adjust the initial configuration of cooling devices to the actual situation.

Namely, if a cooling device bound to a certain trip point starts in the "on" state and the thermal zone temperature is below the threshold of that trip point, the trip point may never be crossed on the way up in which case the state of the cooling device will never be adjusted because the thermal core will never invoke the governor's .trip_crossed() callback. [Note that there is no issue if the zone temperature is at the trip threshold or above it to start with because .trip_crossed() will be invoked then to indicate the start of thermal mitigation for the given trip.]

To address this, add a .manage() callback to the Bang-bang governor and use it to ensure that all of the thermal instances managed by the governor have been initialized properly and the states of all of the cooling devices involved have been adjusted to the current zone temperature as appropriate.

Fixes 530c932bdf75 ("thermal: gov_bang_bang: Use .trip_crossed() instead of .throttle()")
Cc 6.10+ <[email protected]> # 6.10+
---
 drivers/thermal/gov_bang_bang.c |   30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
drivers/thermal/gov_bang_bang.c +30 -0
Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================
@@ -26,6 +26,7 @@ static void bang_bang_set_instance_targe
 	 * when the trip is crossed on the way down.
 	 */
 	instance->target = target;
+	instance->initialized = true;
 
 	dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
 
@@ -80,8 +81,37 @@ static void bang_bang_control(struct the
 	}
 }
 
+static void bang_bang_manage(struct thermal_zone_device *tz)
+{
+	const struct thermal_trip_desc *td;
+	struct thermal_instance *instance;
+
+	for_each_trip_desc(tz, td) {
+		const struct thermal_trip *trip = &td->trip;
+
+		if (tz->temperature >= td->threshold ||
+		    trip->temperature == THERMAL_TEMP_INVALID ||
+		    trip->type == THERMAL_TRIP_CRITICAL ||
+		    trip->type == THERMAL_TRIP_HOT)
+			continue;
+
+		/*
+		 * If the initial cooling device state is "on", but the zone
+		 * temperature is not above the trip point, the core will not
+		 * call bang_bang_control() until the zone temperature reaches
+		 * the trip point temperature which may be never.  In those
+		 * cases, set the initial state of the cooling device to 0.
+		 */
+		list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+			if (!instance->initialized && instance->trip == trip)
+				bang_bang_set_instance_target(instance, 0);
+		}
+	}
+}
+
 static struct thermal_governor thermal_gov_bang_bang = {
 	.name		= "bang_bang",
 	.trip_crossed	= bang_bang_control,
+	.manage		= bang_bang_manage,
 };
 THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
Acked-by on v1 3/4
#10 lore raw

Re: [PATCH v1 3/4] thermal: gov_bang_bang: Add .manage() callback

Quoted 21 lines · Rafael J. Wysocki

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

After recent changes, the Bang-bang governor may not adjust the initial configuration of cooling devices to the actual situation.

Namely, if a cooling device bound to a certain trip point starts in the "on" state and the thermal zone temperature is below the threshold of that trip point, the trip point may never be crossed on the way up in which case the state of the cooling device will never be adjusted because the thermal core will never invoke the governor's .trip_crossed() callback. [Note that there is no issue if the zone temperature is at the trip threshold or above it to start with because .trip_crossed() will be invoked then to indicate the start of thermal mitigation for the given trip.]

To address this, add a .manage() callback to the Bang-bang governor and use it to ensure that all of the thermal instances managed by the governor have been initialized properly and the states of all of the cooling devices involved have been adjusted to the current zone temperature as appropriate.

Quoted 4 lines
Fixes 530c932bdf75 ("thermal: gov_bang_bang: Use .trip_crossed() instead of .throttle()")
Cc 6.10+ <[email protected]> # 6.10+
Quoted 57 lines
---
  drivers/thermal/gov_bang_bang.c |   30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +30 -0
@@ -26,6 +26,7 @@ static void bang_bang_set_instance_targe
  	 * when the trip is crossed on the way down.
  	 */
  	instance->target = target;
+	instance->initialized = true;
 
  	dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
 
@@ -80,8 +81,37 @@ static void bang_bang_control(struct the
  	}
  }
 
+static void bang_bang_manage(struct thermal_zone_device *tz)
+{
+	const struct thermal_trip_desc *td;
+	struct thermal_instance *instance;
+
+	for_each_trip_desc(tz, td) {
+		const struct thermal_trip *trip = &td->trip;
+
+		if (tz->temperature >= td->threshold ||
+		    trip->temperature == THERMAL_TEMP_INVALID ||
+		    trip->type == THERMAL_TRIP_CRITICAL ||
+		    trip->type == THERMAL_TRIP_HOT)
+			continue;
+
+		/*
+		 * If the initial cooling device state is "on", but the zone
+		 * temperature is not above the trip point, the core will not
+		 * call bang_bang_control() until the zone temperature reaches
+		 * the trip point temperature which may be never.  In those
+		 * cases, set the initial state of the cooling device to 0.
+		 */
+		list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+			if (!instance->initialized && instance->trip == trip)
+				bang_bang_set_instance_target(instance, 0);
+		}
+	}
+}
+
  static struct thermal_governor thermal_gov_bang_bang = {
  	.name		= "bang_bang",
  	.trip_crossed	= bang_bang_control,
+	.manage		= bang_bang_manage,
  };
  THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
Acked-by on v1 3/4
#11 lore raw

Re: [PATCH v1 3/4] thermal: gov_bang_bang: Add .manage() callback

In reply to Rafael J. Wysocki
Quoted 21 lines · Rafael J. Wysocki

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

After recent changes, the Bang-bang governor may not adjust the initial configuration of cooling devices to the actual situation.

Namely, if a cooling device bound to a certain trip point starts in the "on" state and the thermal zone temperature is below the threshold of that trip point, the trip point may never be crossed on the way up in which case the state of the cooling device will never be adjusted because the thermal core will never invoke the governor's .trip_crossed() callback. [Note that there is no issue if the zone temperature is at the trip threshold or above it to start with because .trip_crossed() will be invoked then to indicate the start of thermal mitigation for the given trip.]

To address this, add a .manage() callback to the Bang-bang governor and use it to ensure that all of the thermal instances managed by the governor have been initialized properly and the states of all of the cooling devices involved have been adjusted to the current zone temperature as appropriate.

Quoted 4 lines
Fixes 530c932bdf75 ("thermal: gov_bang_bang: Use .trip_crossed() instead of .throttle()")
Cc 6.10+ <[email protected]> # 6.10+

oops, previously sent from wrong email address, here again from correct one...

Quoted 57 lines
---
  drivers/thermal/gov_bang_bang.c |   30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +30 -0
@@ -26,6 +26,7 @@ static void bang_bang_set_instance_targe
  	 * when the trip is crossed on the way down.
  	 */
  	instance->target = target;
+	instance->initialized = true;
 
  	dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
 
@@ -80,8 +81,37 @@ static void bang_bang_control(struct the
  	}
  }
 
+static void bang_bang_manage(struct thermal_zone_device *tz)
+{
+	const struct thermal_trip_desc *td;
+	struct thermal_instance *instance;
+
+	for_each_trip_desc(tz, td) {
+		const struct thermal_trip *trip = &td->trip;
+
+		if (tz->temperature >= td->threshold ||
+		    trip->temperature == THERMAL_TEMP_INVALID ||
+		    trip->type == THERMAL_TRIP_CRITICAL ||
+		    trip->type == THERMAL_TRIP_HOT)
+			continue;
+
+		/*
+		 * If the initial cooling device state is "on", but the zone
+		 * temperature is not above the trip point, the core will not
+		 * call bang_bang_control() until the zone temperature reaches
+		 * the trip point temperature which may be never.  In those
+		 * cases, set the initial state of the cooling device to 0.
+		 */
+		list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+			if (!instance->initialized && instance->trip == trip)
+				bang_bang_set_instance_target(instance, 0);
+		}
+	}
+}
+
  static struct thermal_governor thermal_gov_bang_bang = {
  	.name		= "bang_bang",
  	.trip_crossed	= bang_bang_control,
+	.manage		= bang_bang_manage,
  };
  THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
#12 lore raw

Re: [PATCH v1 3/4] thermal: gov_bang_bang: Add .manage() callback

Quoted 23 lines · Peter Kästle
Quoted 21 lines · Rafael J. Wysocki

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

After recent changes, the Bang-bang governor may not adjust the initial configuration of cooling devices to the actual situation.

Namely, if a cooling device bound to a certain trip point starts in the "on" state and the thermal zone temperature is below the threshold of that trip point, the trip point may never be crossed on the way up in which case the state of the cooling device will never be adjusted because the thermal core will never invoke the governor's .trip_crossed() callback. [Note that there is no issue if the zone temperature is at the trip threshold or above it to start with because .trip_crossed() will be invoked then to indicate the start of thermal mitigation for the given trip.]

To address this, add a .manage() callback to the Bang-bang governor and use it to ensure that all of the thermal instances managed by the governor have been initialized properly and the states of all of the cooling devices involved have been adjusted to the current zone temperature as appropriate.

Quoted 7 lines
Quoted 4 lines
Fixes 530c932bdf75 ("thermal: gov_bang_bang: Use .trip_crossed() instead of .throttle()")
Cc 6.10+ <[email protected]> # 6.10+

oops, previously sent from wrong email address, here again from correct one...

No worries and thanks for the ACKs!

I gather that they mean that the changes work for you.

Quoted 58 lines
Quoted 57 lines
---
  drivers/thermal/gov_bang_bang.c |   30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +30 -0
@@ -26,6 +26,7 @@ static void bang_bang_set_instance_targe
       * when the trip is crossed on the way down.
       */
      instance->target = target;
+     instance->initialized = true;
 
      dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
 
@@ -80,8 +81,37 @@ static void bang_bang_control(struct the
      }
  }
 
+static void bang_bang_manage(struct thermal_zone_device *tz)
+{
+     const struct thermal_trip_desc *td;
+     struct thermal_instance *instance;
+
+     for_each_trip_desc(tz, td) {
+             const struct thermal_trip *trip = &td->trip;
+
+             if (tz->temperature >= td->threshold ||
+                 trip->temperature == THERMAL_TEMP_INVALID ||
+                 trip->type == THERMAL_TRIP_CRITICAL ||
+                 trip->type == THERMAL_TRIP_HOT)
+                     continue;
+
+             /*
+              * If the initial cooling device state is "on", but the zone
+              * temperature is not above the trip point, the core will not
+              * call bang_bang_control() until the zone temperature reaches
+              * the trip point temperature which may be never.  In those
+              * cases, set the initial state of the cooling device to 0.
+              */
+             list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+                     if (!instance->initialized && instance->trip == trip)
+                             bang_bang_set_instance_target(instance, 0);
+             }
+     }
+}
+
  static struct thermal_governor thermal_gov_bang_bang = {
      .name           = "bang_bang",
      .trip_crossed   = bang_bang_control,
+     .manage         = bang_bang_manage,
  };
  THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
#13 lore raw

Re: [PATCH v1 3/4] thermal: gov_bang_bang: Add .manage() callback

Hi Rafael,

Quoted 29 lines · Rafael J. Wysocki
Quoted 28 lines · Peter Kästle
Quoted 25 lines · Rafael J. Wysocki

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

After recent changes, the Bang-bang governor may not adjust the initial configuration of cooling devices to the actual situation.

Namely, if a cooling device bound to a certain trip point starts in the "on" state and the thermal zone temperature is below the threshold of that trip point, the trip point may never be crossed on the way up in which case the state of the cooling device will never be adjusted because the thermal core will never invoke the governor's .trip_crossed() callback. [Note that there is no issue if the zone temperature is at the trip threshold or above it to start with because .trip_crossed() will be invoked then to indicate the start of thermal mitigation for the given trip.]

To address this, add a .manage() callback to the Bang-bang governor and use it to ensure that all of the thermal instances managed by the governor have been initialized properly and the states of all of the cooling devices involved have been adjusted to the current zone temperature as appropriate.

Fixes 530c932bdf75 ("thermal: gov_bang_bang: Use .trip_crossed() instead of .throttle()")
Cc 6.10+ <[email protected]> # 6.10+
Quoted 6 lines
Quoted 2 lines

oops, previously sent from wrong email address, here again from correct one...

No worries and thanks for the ACKs!

I gather that they mean that the changes work for you.

Yes, successfully tested all my use cases and reviewed the code. Thanks. Still working on the acerhdf cleanup, but this will be another patch series.

Quoted 59 lines
Quoted 59 lines
Quoted 58 lines
---
   drivers/thermal/gov_bang_bang.c |   30 ++++++++++++++++++++++++++++++
   1 file changed, 30 insertions(+)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +30 -0
@@ -26,6 +26,7 @@ static void bang_bang_set_instance_targe
        * when the trip is crossed on the way down.
        */
       instance->target = target;
+     instance->initialized = true;
 
       dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target);
 
@@ -80,8 +81,37 @@ static void bang_bang_control(struct the
       }
   }
 
+static void bang_bang_manage(struct thermal_zone_device *tz)
+{
+     const struct thermal_trip_desc *td;
+     struct thermal_instance *instance;
+
+     for_each_trip_desc(tz, td) {
+             const struct thermal_trip *trip = &td->trip;
+
+             if (tz->temperature >= td->threshold ||
+                 trip->temperature == THERMAL_TEMP_INVALID ||
+                 trip->type == THERMAL_TRIP_CRITICAL ||
+                 trip->type == THERMAL_TRIP_HOT)
+                     continue;
+
+             /*
+              * If the initial cooling device state is "on", but the zone
+              * temperature is not above the trip point, the core will not
+              * call bang_bang_control() until the zone temperature reaches
+              * the trip point temperature which may be never.  In those
+              * cases, set the initial state of the cooling device to 0.
+              */
+             list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+                     if (!instance->initialized && instance->trip == trip)
+                             bang_bang_set_instance_target(instance, 0);
+             }
+     }
+}
+
   static struct thermal_governor thermal_gov_bang_bang = {
       .name           = "bang_bang",
       .trip_crossed   = bang_bang_control,
+     .manage         = bang_bang_manage,
   };
   THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
reviewer Reviewed-by on v1 3/4
#14 lore raw

Re: [PATCH v1 3/4] thermal: gov_bang_bang: Add .manage() callback

In reply to Rafael J. Wysocki
Quoted 23 lines · Rafael J. Wysocki

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

After recent changes, the Bang-bang governor may not adjust the initial configuration of cooling devices to the actual situation.

Namely, if a cooling device bound to a certain trip point starts in the "on" state and the thermal zone temperature is below the threshold
of that trip point, the trip point may never be crossed on the way up in which case the state of the cooling device will never be adjusted because the thermal core will never invoke the governor's .trip_crossed() callback.  [Note that there is no issue if the zone temperature is at the trip threshold or above it to start with because
.trip_crossed() will be invoked then to indicate the start of thermal mitigation for the given trip.]

To address this, add a .manage() callback to the Bang-bang governor and use it to ensure that all of the thermal instances managed by the governor have been initialized properly and the states of all of the cooling devices involved have been adjusted to the current zone temperature as appropriate.

Quoted 6 lines
Fixes 530c932bdf75 ("thermal: gov_bang_bang: Use .trip_crossed()

instead of .throttle()")
Link:
https://lore.kernel.org/linux-pm/[email protected]/

thanks,
rui

Quoted 67 lines

---
 drivers/thermal/gov_bang_bang.c |   30
++++++++++++++++++++++++++++++

 1 file changed, 30 insertions(+)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +1 -0
@@ -26,6 +26,7 @@ static void bang_bang_set_instance_targe
          * when the trip is crossed on the way down.
          */
         instance->target = target;
+       instance->initialized = true;


        dev_dbg(&instance->cdev->device, "target=%ld\n", instance-

Quoted 1 line

target);

drivers/thermal/gov_bang_bang.c +0 -0
@@ -80,8 +81,37 @@ static void bang_bang_control(struct the
         }
  }

drivers/thermal/gov_bang_bang.c +16 -0
+static void bang_bang_manage(struct thermal_zone_device *tz)
+{
+       const struct thermal_trip_desc *td;
+       struct thermal_instance *instance;
+
+       for_each_trip_desc(tz, td) {
+               const struct thermal_trip *trip = &td->trip;
+
+               if (tz->temperature >= td->threshold ||
+                   trip->temperature == THERMAL_TEMP_INVALID ||
+                   trip->type == THERMAL_TRIP_CRITICAL ||
+                   trip->type == THERMAL_TRIP_HOT)
+                       continue;
+
+               /*
+                * If the initial cooling device state is "on", but

the zone
+                * temperature is not above the trip point, the core will not
+                * call bang_bang_control() until the zone temperature reaches
+                * the trip point temperature which may be never.  In those
+                * cases, set the initial state of the cooling device to 0.
+                */
+               list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+                       if (!instance->initialized && instance->trip == trip)
+                               bang_bang_set_instance_target(instanc e, 0);
+               }
+       }

drivers/thermal/gov_bang_bang.c +2 -0
+}
+

 static struct thermal_governor thermal_gov_bang_bang = { .name           = "bang_bang", .trip_crossed   = bang_bang_control,
+       .manage         = bang_bang_manage,
 }; THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);

#15 lore raw

PATCH 4/4 thermal: gov_bang_bang: Use governor_data to reduce overhead

In reply to Rafael J. Wysocki

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

After running once, the for_each_trip_desc() loop in bang_bang_manage() is pure needless overhead because it is not going to make any changes unless a new cooling device has been bound to one of the trips in the thermal zone or the system is resuming from sleep.

For this reason, make bang_bang_manage() set governor_data for the thermal zone and check it upfront to decide whether or not it needs to do anything.

However, governor_data needs to be reset in some cases to let bang_bang_manage() know that it should walk the trips again, so add an .update_tz() callback to the governor and make the core additionally invoke it during system resume.

To avoid affecting the other users of that callback unnecessarily, add a special notification reason for system resume, THERMAL_TZ_RESUME, and also pass it to __thermal_zone_device_update() called during system resume for consistency.

---
 drivers/thermal/gov_bang_bang.c |   18 ++++++++++++++++++
 drivers/thermal/thermal_core.c  |    3 ++-
 include/linux/thermal.h         |    1 +
 3 files changed, 21 insertions(+), 1 deletion(-)
drivers/thermal/gov_bang_bang.c +18 -0
Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================
@@ -86,6 +86,10 @@ static void bang_bang_manage(struct ther
 	const struct thermal_trip_desc *td;
 	struct thermal_instance *instance;
 
+	/* If the code below has run already, nothing needs to be done. */
+	if (tz->governor_data)
+		return;
+
 	for_each_trip_desc(tz, td) {
 		const struct thermal_trip *trip = &td->trip;
 
@@ -107,11 +111,25 @@ static void bang_bang_manage(struct ther
 				bang_bang_set_instance_target(instance, 0);
 		}
 	}
+
+	tz->governor_data = (void *)true;
+}
+
+static void bang_bang_update_tz(struct thermal_zone_device *tz,
+				enum thermal_notify_event reason)
+{
+	/*
+	 * Let bang_bang_manage() know that it needs to walk trips after binding
+	 * a new cdev and after system resume.
+	 */
+	if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
+		tz->governor_data = NULL;
 }
 
 static struct thermal_governor thermal_gov_bang_bang = {
 	.name		= "bang_bang",
 	.trip_crossed	= bang_bang_control,
 	.manage		= bang_bang_manage,
+	.update_tz	= bang_bang_update_tz,
 };
 THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
drivers/thermal/thermal_core.c +2 -1
Index: linux-pm/drivers/thermal/thermal_core.c
===================================================================
@@ -1692,7 +1692,8 @@ static void thermal_zone_device_resume(s
 
 	thermal_debug_tz_resume(tz);
 	thermal_zone_device_init(tz);
-	__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
+	thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
+	__thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
 
 	complete(&tz->resume);
 	tz->resuming = false;
include/linux/thermal.h +1 -0
Index: linux-pm/include/linux/thermal.h
===================================================================
@@ -55,6 +55,7 @@ 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_TZ_RESUME, /* Thermal zone is resuming after system sleep */
 };
 
 /**
Acked-by on v1 4/4
#16 lore raw

Re: [PATCH v1 4/4] thermal: gov_bang_bang: Use governor_data to reduce overhead

Quoted 16 lines · Rafael J. Wysocki

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

After running once, the for_each_trip_desc() loop in bang_bang_manage() is pure needless overhead because it is not going to make any changes unless a new cooling device has been bound to one of the trips in the thermal zone or the system is resuming from sleep.

For this reason, make bang_bang_manage() set governor_data for the thermal zone and check it upfront to decide whether or not it needs to do anything.

However, governor_data needs to be reset in some cases to let bang_bang_manage() know that it should walk the trips again, so add an .update_tz() callback to the governor and make the core additionally invoke it during system resume.

Quoted 6 lines

To avoid affecting the other users of that callback unnecessarily, add a special notification reason for system resume, THERMAL_TZ_RESUME, and also pass it to __thermal_zone_device_update() called during system resume for consistency.

Quoted 76 lines
---
  drivers/thermal/gov_bang_bang.c |   18 ++++++++++++++++++
  drivers/thermal/thermal_core.c  |    3 ++-
  include/linux/thermal.h         |    1 +
  3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +18 -0
@@ -86,6 +86,10 @@ static void bang_bang_manage(struct ther
  	const struct thermal_trip_desc *td;
  	struct thermal_instance *instance;
 
+	/* If the code below has run already, nothing needs to be done. */
+	if (tz->governor_data)
+		return;
+
  	for_each_trip_desc(tz, td) {
  		const struct thermal_trip *trip = &td->trip;
 
@@ -107,11 +111,25 @@ static void bang_bang_manage(struct ther
  				bang_bang_set_instance_target(instance, 0);
  		}
  	}
+
+	tz->governor_data = (void *)true;
+}
+
+static void bang_bang_update_tz(struct thermal_zone_device *tz,
+				enum thermal_notify_event reason)
+{
+	/*
+	 * Let bang_bang_manage() know that it needs to walk trips after binding
+	 * a new cdev and after system resume.
+	 */
+	if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
+		tz->governor_data = NULL;
  }
 
  static struct thermal_governor thermal_gov_bang_bang = {
  	.name		= "bang_bang",
  	.trip_crossed	= bang_bang_control,
  	.manage		= bang_bang_manage,
+	.update_tz	= bang_bang_update_tz,
  };
  THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);

Index: linux-pm/drivers/thermal/thermal_core.c
===================================================================

drivers/thermal/thermal_core.c +2 -1
@@ -1692,7 +1692,8 @@ static void thermal_zone_device_resume(s
 
  	thermal_debug_tz_resume(tz);
  	thermal_zone_device_init(tz);
-	__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
+	thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
+	__thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
 
  	complete(&tz->resume);
  	tz->resuming = false;

Index: linux-pm/include/linux/thermal.h
===================================================================

include/linux/thermal.h +1 -0
@@ -55,6 +55,7 @@ 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_TZ_RESUME, /* Thermal zone is resuming after system sleep */
  };
 
  /**
reviewer
#17 lore raw

Re: [PATCH v1 4/4] thermal: gov_bang_bang: Use governor_data to reduce overhead

In reply to Rafael J. Wysocki
Quoted 63 lines · Rafael J. Wysocki

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

After running once, the for_each_trip_desc() loop in bang_bang_manage() is pure needless overhead because it is not going to
make any changes unless a new cooling device has been bound to one of the trips in the thermal zone or the system is resuming from sleep.

For this reason, make bang_bang_manage() set governor_data for the thermal zone and check it upfront to decide whether or not it needs to
do anything.

However, governor_data needs to be reset in some cases to let bang_bang_manage() know that it should walk the trips again, so add an
.update_tz() callback to the governor and make the core additionally invoke it during system resume.

To avoid affecting the other users of that callback unnecessarily, add
a special notification reason for system resume, THERMAL_TZ_RESUME, and
also pass it to __thermal_zone_device_update() called during system resume for consistency.

---
 drivers/thermal/gov_bang_bang.c |   18 ++++++++++++++++++ drivers/thermal/thermal_core.c  |    3 ++- include/linux/thermal.h         |    1 +

 3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +0 -0
@@ -86,6 +86,10 @@ static void bang_bang_manage(struct ther
         const struct thermal_trip_desc *td;
         struct thermal_instance *instance;


+       /* If the code below has run already, nothing needs to be done. */
+       if (tz->governor_data)
+               return;
+
        for_each_trip_desc(tz, td) {
                const struct thermal_trip *trip = &td->trip;

drivers/thermal/gov_bang_bang.c +0 -0
@@ -107,11 +111,25 @@ static void bang_bang_manage(struct ther

                                bang_bang_set_instance_target(instanc e, 0);
                }
        }

drivers/thermal/gov_bang_bang.c +9 -0
+
+       tz->governor_data = (void *)true;
+}
+
+static void bang_bang_update_tz(struct thermal_zone_device *tz,
+                               enum thermal_notify_event reason)
+{
+       /*
+        * Let bang_bang_manage() know that it needs to walk trips
Quoted 7 lines

after binding
+        * a new cdev and after system resume. +        */
+       if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
+               tz->governor_data = NULL;
 }

can we do the cdev initialization for BIND_CDEV and RESUME notification in .update_tz() directly?

Then we don't need .manage() callback. This makes more sense to me because bang_bang governor cares about trip point crossing only.

thanks,
rui

Quoted 41 lines


 static struct thermal_governor thermal_gov_bang_bang = { .name           = "bang_bang", .trip_crossed   = bang_bang_control, .manage         = bang_bang_manage,
+       .update_tz      = bang_bang_update_tz, }; THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
Index: linux-pm/drivers/thermal/thermal_core.c
===================================================================

drivers/thermal/thermal_core.c +0 -0
@@ -1692,7 +1692,8 @@ static void thermal_zone_device_resume(s


        thermal_debug_tz_resume(tz); thermal_zone_device_init(tz);
-       __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); +       thermal_governor_update_tz(tz, THERMAL_TZ_RESUME); +       __thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
        complete(&tz->resume);
        tz->resuming = false;
Index: linux-pm/include/linux/thermal.h
===================================================================

include/linux/thermal.h +0 -0
@@ -55,6 +55,7 @@ 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_TZ_RESUME, /* Thermal zone is resuming after system sleep */
 };

 /**

#18 lore raw

Re: [PATCH v1 4/4] thermal: gov_bang_bang: Use governor_data to reduce overhead

Quoted 67 lines · Rui
Quoted 65 lines · Rafael J. Wysocki

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

After running once, the for_each_trip_desc() loop in bang_bang_manage() is pure needless overhead because it is not going to
make any changes unless a new cooling device has been bound to one of the trips in the thermal zone or the system is resuming from sleep.

For this reason, make bang_bang_manage() set governor_data for the thermal zone and check it upfront to decide whether or not it needs to
do anything.

However, governor_data needs to be reset in some cases to let bang_bang_manage() know that it should walk the trips again, so add an
.update_tz() callback to the governor and make the core additionally invoke it during system resume.

To avoid affecting the other users of that callback unnecessarily, add
a special notification reason for system resume, THERMAL_TZ_RESUME, and
also pass it to __thermal_zone_device_update() called during system resume for consistency.

---
 drivers/thermal/gov_bang_bang.c |   18 ++++++++++++++++++
 drivers/thermal/thermal_core.c  |    3 ++-
 include/linux/thermal.h         |    1 +
 3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
===================================================================

drivers/thermal/gov_bang_bang.c +1 -0
@@ -86,6 +86,10 @@ static void bang_bang_manage(struct ther
        const struct thermal_trip_desc *td;
        struct thermal_instance *instance;
 
+       /* If the code below has run already, nothing needs to be

done. */

drivers/thermal/gov_bang_bang.c +12 -0
+       if (tz->governor_data)
+               return;
+
        for_each_trip_desc(tz, td) {
                const struct thermal_trip *trip = &td->trip;
@@ -107,11 +111,25 @@ static void bang_bang_manage(struct ther
                                bang_bang_set_instance_target(instanc
 e, 0);
                }
        }
+
+       tz->governor_data = (void *)true;
+}
+
+static void bang_bang_update_tz(struct thermal_zone_device *tz,
+                               enum thermal_notify_event reason)
+{
+       /*
+        * Let bang_bang_manage() know that it needs to walk trips

after binding
+ * a new cdev and after system resume.

Quoted 8 lines
Quoted 5 lines
drivers/thermal/gov_bang_bang.c +2 -0
+        */
+       if (reason == THERMAL_TZ_BIND_CDEV || reason ==

THERMAL_TZ_RESUME)
+ tz->governor_data = NULL;
}

can we do the cdev initialization for BIND_CDEV and RESUME notification in .update_tz() directly?

That would be viable if the zone temperature was known at the time .update_tz() runs, but it isn't. See this message:

https://lore.kernel.org/linux-pm/CAJZ5v0ji_7Z-24iCO_Xxu4Zm4jgVFmR9jVp8QNiCOxzV9gqSnA@mail.gmail.com/

As long as the zone temperature is not known, it is not clear which way to initialize the cooling devices.

Interestingly enough, the zone temperature is first checked by the core when the zone is enabled and not when it is registered.

Quoted 2 lines

Then we don't need .manage() callback. This makes more sense to me because bang_bang governor cares about trip point crossing only.

That's true, but this is all about a corner case in which no trip points are crossed and the cooling devices need to be initialized properly regardless.

Quoted 41 lines
Quoted 40 lines

static struct thermal_governor thermal_gov_bang_bang = {

        .name           = "bang_bang",
        .trip_crossed   = bang_bang_control,
        .manage         = bang_bang_manage,
+       .update_tz      = bang_bang_update_tz,

};
THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
Index: linux-pm/drivers/thermal/thermal_core.c
===================================================================

drivers/thermal/thermal_core.c +2 -1
@@ -1692,7 +1692,8 @@ static void thermal_zone_device_resume(s
 
        thermal_debug_tz_resume(tz);
        thermal_zone_device_init(tz);
-       __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
+       thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
+       __thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
 
        complete(&tz->resume);
        tz->resuming = false;

Index: linux-pm/include/linux/thermal.h
===================================================================

include/linux/thermal.h +0 -0
@@ -55,6 +55,7 @@ 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_TZ_RESUME, /* Thermal zone is resuming after system
sleep */
};

/**

reviewer
#19 lore raw

Re: [PATCH v1 4/4] thermal: gov_bang_bang: Use governor_data to reduce overhead

Quoted 92 lines · Rafael J. Wysocki
Quoted 81 lines · Rui
Quoted 79 lines · Rafael J. Wysocki

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

After running once, the for_each_trip_desc() loop in bang_bang_manage() is pure needless overhead because it is not going
to
make any changes unless a new cooling device has been bound to one of
the trips in the thermal zone or the system is resuming from sleep.

For this reason, make bang_bang_manage() set governor_data for the
thermal zone and check it upfront to decide whether or not it needs
to
do anything.

However, governor_data needs to be reset in some cases to let bang_bang_manage() know that it should walk the trips again, so add
an
.update_tz() callback to the governor and make the core additionally
invoke it during system resume.

To avoid affecting the other users of that callback unnecessarily,
add
a special notification reason for system resume, THERMAL_TZ_RESUME,
and
also pass it to __thermal_zone_device_update() called during system
resume for consistency.

---
 drivers/thermal/gov_bang_bang.c |   18 ++++++++++++++++++ drivers/thermal/thermal_core.c  |    3 ++- include/linux/thermal.h         |    1 +

 3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
=================================================================
==

drivers/thermal/gov_bang_bang.c +1 -0
@@ -86,6 +86,10 @@ static void bang_bang_manage(struct ther
         const struct thermal_trip_desc *td;
         struct thermal_instance *instance;
 
+       /* If the code below has run already, nothing needs to be

done. */
+       if (tz->governor_data)
+               return;
+
        for_each_trip_desc(tz, td) {
                const struct thermal_trip *trip = &td->trip;

drivers/thermal/gov_bang_bang.c +0 -0
@@ -107,11 +111,25 @@ static void bang_bang_manage(struct ther


bang_bang_set_instance_target(instanc
e, 0);
                }
        }

drivers/thermal/gov_bang_bang.c +9 -0
+
+       tz->governor_data = (void *)true;
+}
+
+static void bang_bang_update_tz(struct thermal_zone_device *tz,
+                               enum thermal_notify_event reason)
+{
+       /*
+        * Let bang_bang_manage() know that it needs to walk

trips
after binding
+        * a new cdev and after system resume.
+        */

Quoted 8 lines
Quoted 4 lines

+       if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
+               tz->governor_data = NULL;
 }

can we do the cdev initialization for BIND_CDEV and RESUME notification
in .update_tz() directly?

Quoted 7 lines

That would be viable if the zone temperature was known at the time .update_tz() runs, but it isn't.  See this message:

https://lore.kernel.org/linux-pm/CAJZ5v0ji_7Z-24iCO_Xxu4Zm4jgVFmR9jVp8QNiCOxzV9gqSnA@mail.gmail.com/

As long as the zone temperature is not known, it is not clear which way to initialize the cooling devices.

right. Then the patch LGTM.

BTW, what do you think if we add handling for first temperature read in handle_thermal_trip(), say, some draft code like below,

if (tz->last_temperature == THERMAL_TEMP_INIT) {
	if (tz->temperature < trip->temperature)
		list_add(&td->notify_list_node, way_down_list);
	else
		list_add_tail(&td->notify_list_node, way_up_list);
}

This should handle both the init and the resume case.

thanks,
rui

Quoted 59 lines

Interestingly enough, the zone temperature is first checked by the core when the zone is enabled and not when it is registered.

Quoted 2 lines

Then we don't need .manage() callback. This makes more sense to me because bang_bang governor cares about trip point crossing only.

That's true, but this is all about a corner case in which no trip points are crossed and the cooling devices need to be initialized properly regardless.

Quoted 47 lines
Quoted 46 lines

 static struct thermal_governor thermal_gov_bang_bang = { .name           = "bang_bang", .trip_crossed   = bang_bang_control, .manage         = bang_bang_manage,
+       .update_tz      = bang_bang_update_tz, }; THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
Index: linux-pm/drivers/thermal/thermal_core.c
=================================================================
==

drivers/thermal/thermal_core.c +2 -1
@@ -1692,7 +1692,8 @@ static void thermal_zone_device_resume(s
 
         thermal_debug_tz_resume(tz);
         thermal_zone_device_init(tz);
-       __thermal_zone_device_update(tz,
 THERMAL_EVENT_UNSPECIFIED);
+       thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
+       __thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
 
         complete(&tz->resume);

        tz->resuming = false;
Index: linux-pm/include/linux/thermal.h
=================================================================
==

include/linux/thermal.h +0 -0
@@ -55,6 +55,7 @@ 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_TZ_RESUME, /* Thermal zone is resuming after system
sleep */
 };

 /**

#20 lore raw

Re: [PATCH v1 4/4] thermal: gov_bang_bang: Use governor_data to reduce overhead

Quoted 97 lines · Rui
Quoted 95 lines · Rafael J. Wysocki
Quoted 81 lines · Rui
Quoted 79 lines · Rafael J. Wysocki

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

After running once, the for_each_trip_desc() loop in bang_bang_manage() is pure needless overhead because it is not going
to
make any changes unless a new cooling device has been bound to one of
the trips in the thermal zone or the system is resuming from sleep.

For this reason, make bang_bang_manage() set governor_data for the
thermal zone and check it upfront to decide whether or not it needs
to
do anything.

However, governor_data needs to be reset in some cases to let bang_bang_manage() know that it should walk the trips again, so add
an
.update_tz() callback to the governor and make the core additionally
invoke it during system resume.

To avoid affecting the other users of that callback unnecessarily,
add
a special notification reason for system resume, THERMAL_TZ_RESUME,
and
also pass it to __thermal_zone_device_update() called during system
resume for consistency.

---
 drivers/thermal/gov_bang_bang.c |   18 ++++++++++++++++++
 drivers/thermal/thermal_core.c  |    3 ++-
 include/linux/thermal.h         |    1 +
 3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
=================================================================
==

drivers/thermal/gov_bang_bang.c +1 -0
@@ -86,6 +86,10 @@ static void bang_bang_manage(struct ther
        const struct thermal_trip_desc *td;
        struct thermal_instance *instance;
 
+       /* If the code below has run already, nothing needs to be

done. */

drivers/thermal/gov_bang_bang.c +3 -0
+       if (tz->governor_data)
+               return;
+
        for_each_trip_desc(tz, td) {
                const struct thermal_trip *trip = &td->trip;
@@ -107,11 +111,25 @@ static void bang_bang_manage(struct ther
 

bang_bang_set_instance_target(instanc

e, 0);
                }
        }
drivers/thermal/gov_bang_bang.c +9 -0
+
+       tz->governor_data = (void *)true;
+}
+
+static void bang_bang_update_tz(struct thermal_zone_device *tz,
+                               enum thermal_notify_event reason)
+{
+       /*
+        * Let bang_bang_manage() know that it needs to walk

trips
after binding

drivers/thermal/gov_bang_bang.c +2 -0
+        * a new cdev and after system resume.
+        */
Quoted 8 lines
Quoted 4 lines

+ if (reason == THERMAL_TZ_BIND_CDEV || reason ==
THERMAL_TZ_RESUME)
+ tz->governor_data = NULL;
}

can we do the cdev initialization for BIND_CDEV and RESUME notification
in .update_tz() directly?

That would be viable if the zone temperature was known at the time .update_tz() runs, but it isn't. See this message:

Quoted 6 lines
Quoted 4 lines

https://lore.kernel.org/linux-pm/CAJZ5v0ji_7Z-24iCO_Xxu4Zm4jgVFmR9jVp8QNiCOxzV9gqSnA@mail.gmail.com/

As long as the zone temperature is not known, it is not clear which way to initialize the cooling devices.

right. Then the patch LGTM.

Great!

Quoted 11 lines

BTW, what do you think if we add handling for first temperature read in handle_thermal_trip(), say, some draft code like below,

if (tz->last_temperature == THERMAL_TEMP_INIT) {
        if (tz->temperature < trip->temperature)
                list_add(&td->notify_list_node, way_down_list);
        else
                list_add_tail(&td->notify_list_node, way_up_list);
}

This should handle both the init and the resume case.

I have considered doing something similar, but there are quite a few arguments against it.

First, it would cause spurious notifications to be sent to user space. Second, the debug code would need to be modified to take this case into account explicitly. Moreover, this would be extra overhead for the other governors.

IMV it's better to limit the impact to the Bang-bang governor where the problem is.

reviewer Reviewed-by
#21 lore raw

Re: [PATCH v1 4/4] thermal: gov_bang_bang: Use governor_data to reduce overhead

Quoted 135 lines · Rafael J. Wysocki
Quoted 107 lines · Rui
Quoted 105 lines · Rafael J. Wysocki
Quoted 90 lines · Rui
Quoted 88 lines · Rafael J. Wysocki

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

After running once, the for_each_trip_desc() loop in bang_bang_manage() is pure needless overhead because it is not
going
to
make any changes unless a new cooling device has been bound to
one of
the trips in the thermal zone or the system is resuming from sleep.

For this reason, make bang_bang_manage() set governor_data for
the
thermal zone and check it upfront to decide whether or not it needs
to
do anything.

However, governor_data needs to be reset in some cases to let bang_bang_manage() know that it should walk the trips again, so
add
an
.update_tz() callback to the governor and make the core additionally
invoke it during system resume.

To avoid affecting the other users of that callback unnecessarily,
add
a special notification reason for system resume, THERMAL_TZ_RESUME,
and
also pass it to __thermal_zone_device_update() called during system
resume for consistency.

---
 drivers/thermal/gov_bang_bang.c |   18 ++++++++++++++++++ drivers/thermal/thermal_core.c  |    3 ++- include/linux/thermal.h         |    1 +

 3 files changed, 21 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/thermal/gov_bang_bang.c
=============================================================
====
==

drivers/thermal/gov_bang_bang.c +1 -0
@@ -86,6 +86,10 @@ static void bang_bang_manage(struct ther
         const struct thermal_trip_desc *td;
         struct thermal_instance *instance;
 
+       /* If the code below has run already, nothing needs

to be
done. */
+       if (tz->governor_data)
+               return;
+
        for_each_trip_desc(tz, td) {
                const struct thermal_trip *trip = &td->trip;

drivers/thermal/gov_bang_bang.c +0 -0
@@ -107,11 +111,25 @@ static void bang_bang_manage(struct

ther

bang_bang_set_instance_target(instanc
e, 0);
                }
        }

drivers/thermal/gov_bang_bang.c +5 -0
+
+       tz->governor_data = (void *)true;
+}
+
+static void bang_bang_update_tz(struct thermal_zone_device

*tz,
+                               enum thermal_notify_event reason)
+{
+       /*
+        * Let bang_bang_manage() know that it needs to walk trips
after binding
+        * a new cdev and after system resume.
+        */

Quoted 8 lines
Quoted 4 lines

+       if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
+               tz->governor_data = NULL;
 }

can we do the cdev initialization for BIND_CDEV and RESUME notification
in .update_tz() directly?

That would be viable if the zone temperature was known at the time
.update_tz() runs, but it isn't.  See this message:

Quoted 7 lines
Quoted 5 lines

https://lore.kernel.org/linux-pm/CAJZ5v0ji_7Z-24iCO_Xxu4Zm4jgVFmR9jVp8QNiCOxzV9gqSnA@mail.gmail.com/

As long as the zone temperature is not known, it is not clear which
way to initialize the cooling devices.

right. Then the patch LGTM.

Great!

Quoted 4 lines

BTW, what do you think if we add handling for first temperature read in
handle_thermal_trip(), say, some draft code like below,

Quoted 8 lines

if (tz->last_temperature == THERMAL_TEMP_INIT) {
        if (tz->temperature < trip->temperature) list_add(&td->notify_list_node, way_down_list); else
                list_add_tail(&td->notify_list_node, way_up_list); }

This should handle both the init and the resume case.

I have considered doing something similar, but there are quite a few arguments against it.

Quoted 8 lines

First, it would cause spurious notifications to be sent to user space.
Second, the debug code would need to be modified to take this case into account explicitly.  Moreover, this would be extra overhead for the other governors.

IMV it's better to limit the impact to the Bang-bang governor where the problem is.

Agreed.

thanks,
rui