[PATCH v2 09/12] thermal: core: Update thermal zones after cooling device binding

Rafael J. Wysocki posted 12 patches 1 month, 3 weeks ago
[PATCH v2 09/12] thermal: core: Update thermal zones after cooling device binding
Posted by Rafael J. Wysocki 1 month, 3 weeks ago
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

If a new cooling device is registered and it is bound to at least one
trip point in a given thermal zone, that thermal zone needs to be
updated via __thermal_zone_device_update().

Instead of doing this with the help of the need_update atomic field in
struct thermal_zone_device, which is not particularly straightforward,
make __thermal_zone_cdev_bind() return a bool value indicating whether
or not the given thermal zone needs to be updated because a new cooling
device has been bound to it and update thermal_zone_cdev_bind() to
call __thermal_zone_device_update() when this value is "true".

No intentional functional impact.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---

This is a new iteration of

https://lore.kernel.org/linux-pm/3603909.iIbC2pHGDl@rjwysocki.net/

v1 -> v2: Rebase.

---
 drivers/thermal/thermal_core.c |   23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

Index: linux-pm/drivers/thermal/thermal_core.c
===================================================================
--- linux-pm.orig/drivers/thermal/thermal_core.c
+++ linux-pm/drivers/thermal/thermal_core.c
@@ -933,13 +933,14 @@ void print_bind_err_msg(struct thermal_z
 		cdev->type, thermal_zone_trip_id(tz, trip), ret);
 }
 
-static void __thermal_zone_cdev_bind(struct thermal_zone_device *tz,
+static bool __thermal_zone_cdev_bind(struct thermal_zone_device *tz,
 				     struct thermal_cooling_device *cdev)
 {
 	struct thermal_trip_desc *td;
+	bool update_tz = false;
 
 	if (!tz->ops.should_bind)
-		return;
+		return false;
 
 	for_each_trip_desc(tz, td) {
 		struct thermal_trip *trip = &td->trip;
@@ -954,9 +955,15 @@ static void __thermal_zone_cdev_bind(str
 			continue;
 
 		ret = thermal_bind_cdev_to_trip(tz, trip, cdev, &c);
-		if (ret)
+		if (ret) {
 			print_bind_err_msg(tz, trip, cdev, ret);
+			continue;
+		}
+
+		update_tz = true;
 	}
+
+	return update_tz;
 }
 
 static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
@@ -964,7 +971,8 @@ static void thermal_zone_cdev_bind(struc
 {
 	mutex_lock(&tz->lock);
 
-	__thermal_zone_cdev_bind(tz, cdev);
+	if (__thermal_zone_cdev_bind(tz, cdev))
+		__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
 
 	mutex_unlock(&tz->lock);
 }
@@ -991,7 +999,7 @@ __thermal_cooling_device_register(struct
 				  const struct thermal_cooling_device_ops *ops)
 {
 	struct thermal_cooling_device *cdev;
-	struct thermal_zone_device *pos = NULL;
+	struct thermal_zone_device *pos;
 	unsigned long current_state;
 	int id, ret;
 
@@ -1067,11 +1075,6 @@ __thermal_cooling_device_register(struct
 	list_for_each_entry(pos, &thermal_tz_list, node)
 		thermal_zone_cdev_bind(pos, cdev);
 
-	list_for_each_entry(pos, &thermal_tz_list, node)
-		if (atomic_cmpxchg(&pos->need_update, 1, 0))
-			thermal_zone_device_update(pos,
-						   THERMAL_EVENT_UNSPECIFIED);
-
 	mutex_unlock(&thermal_list_lock);
 
 	return cdev;
Re: [PATCH v2 09/12] thermal: core: Update thermal zones after cooling device binding
Posted by Lukasz Luba 1 month, 1 week ago

On 10/4/24 20:33, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> If a new cooling device is registered and it is bound to at least one
> trip point in a given thermal zone, that thermal zone needs to be
> updated via __thermal_zone_device_update().
> 
> Instead of doing this with the help of the need_update atomic field in
> struct thermal_zone_device, which is not particularly straightforward,
> make __thermal_zone_cdev_bind() return a bool value indicating whether
> or not the given thermal zone needs to be updated because a new cooling
> device has been bound to it and update thermal_zone_cdev_bind() to
> call __thermal_zone_device_update() when this value is "true".
> 
> No intentional functional impact.
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
> 
> This is a new iteration of
> 
> https://lore.kernel.org/linux-pm/3603909.iIbC2pHGDl@rjwysocki.net/
> 
> v1 -> v2: Rebase.
> 
> ---
>   drivers/thermal/thermal_core.c |   23 +++++++++++++----------
>   1 file changed, 13 insertions(+), 10 deletions(-)
> 
> Index: linux-pm/drivers/thermal/thermal_core.c
> ===================================================================
> --- linux-pm.orig/drivers/thermal/thermal_core.c
> +++ linux-pm/drivers/thermal/thermal_core.c
> @@ -933,13 +933,14 @@ void print_bind_err_msg(struct thermal_z
>   		cdev->type, thermal_zone_trip_id(tz, trip), ret);
>   }
>   
> -static void __thermal_zone_cdev_bind(struct thermal_zone_device *tz,
> +static bool __thermal_zone_cdev_bind(struct thermal_zone_device *tz,
>   				     struct thermal_cooling_device *cdev)
>   {
>   	struct thermal_trip_desc *td;
> +	bool update_tz = false;
>   
>   	if (!tz->ops.should_bind)
> -		return;
> +		return false;
>   
>   	for_each_trip_desc(tz, td) {
>   		struct thermal_trip *trip = &td->trip;
> @@ -954,9 +955,15 @@ static void __thermal_zone_cdev_bind(str
>   			continue;
>   
>   		ret = thermal_bind_cdev_to_trip(tz, trip, cdev, &c);
> -		if (ret)
> +		if (ret) {
>   			print_bind_err_msg(tz, trip, cdev, ret);
> +			continue;
> +		}
> +
> +		update_tz = true;
>   	}
> +
> +	return update_tz;
>   }
>   
>   static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
> @@ -964,7 +971,8 @@ static void thermal_zone_cdev_bind(struc
>   {
>   	mutex_lock(&tz->lock);
>   
> -	__thermal_zone_cdev_bind(tz, cdev);
> +	if (__thermal_zone_cdev_bind(tz, cdev))
> +		__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
>   
>   	mutex_unlock(&tz->lock);
>   }
> @@ -991,7 +999,7 @@ __thermal_cooling_device_register(struct
>   				  const struct thermal_cooling_device_ops *ops)
>   {
>   	struct thermal_cooling_device *cdev;
> -	struct thermal_zone_device *pos = NULL;
> +	struct thermal_zone_device *pos;
>   	unsigned long current_state;
>   	int id, ret;
>   
> @@ -1067,11 +1075,6 @@ __thermal_cooling_device_register(struct
>   	list_for_each_entry(pos, &thermal_tz_list, node)
>   		thermal_zone_cdev_bind(pos, cdev);
>   
> -	list_for_each_entry(pos, &thermal_tz_list, node)
> -		if (atomic_cmpxchg(&pos->need_update, 1, 0))
> -			thermal_zone_device_update(pos,
> -						   THERMAL_EVENT_UNSPECIFIED);
> -
>   	mutex_unlock(&thermal_list_lock);
>   
>   	return cdev;
> 
> 
> 

Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>