Re: [PATCH 4/8] thermal/core: Update the generic trip points
From: Rafael J. Wysocki
Date: Sun Jun 25 2023 - 16:30:13 EST
On Thu, May 25, 2023 at 4:02 PM Daniel Lezcano
<daniel.lezcano@xxxxxxxxxx> wrote:
>
> At this point, the generic trip points rework allows to create a
> thermal zone with a fixed number of trip points. This usage satisfy
> almost all of the existing drivers.
>
> A few remaining drivers have a mechanism where the firmware updates
> the trip points. But there is no such update mechanism for the generic
> trip points, thus those drivers can not be converted to the generic
> approach.
>
> This patch provides a function 'thermal_zone_trips_update()' allowing
> to change the trip points of a thermal zone.
>
> At the same time, with the logic the trip points array is passed as a
> parameter to the thermal zone at creation time, we make our own
> private trip points array by copying the one passed as parameter.
>
> Note, no code has been found where the trip points update leads to a
> refresh of the trip points in sysfs, so it is very unlikey the number
> of trip points changes. However, for the sake of consistency it would
> be nicer to have the trip points being refreshed in sysfs also, but
> that could be done in a separate set of changes.
>
> Signed-off-by: Daniel Lezcano <daniel.lezcano@xxxxxxxxxx>
> ---
> drivers/thermal/thermal_core.c | 40 ++++++++---------
> drivers/thermal/thermal_core.h | 3 ++
> drivers/thermal/thermal_trip.c | 78 ++++++++++++++++++++++++++++++++++
> include/linux/thermal.h | 4 ++
> 4 files changed, 102 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index afcd4197babd..3688b06401c8 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -1224,32 +1224,11 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
> return ERR_PTR(-EINVAL);
> }
>
> - /*
> - * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
> - * For example, shifting by 32 will result in compiler warning:
> - * warning: right shift count >= width of type [-Wshift-count- overflow]
> - *
> - * Also "mask >> num_trips" will always be true with 32 bit shift.
> - * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
> - * mask >> 32 = 0x80000000
> - * This will result in failure for the below condition.
> - *
> - * Check will be true when the bit 31 of the mask is set.
> - * 32 bit shift will cause overflow of 4 byte integer.
> - */
> - if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
> - pr_err("Incorrect number of thermal trips\n");
> - return ERR_PTR(-EINVAL);
> - }
> -
> if (!ops) {
> pr_err("Thermal zone device ops not defined\n");
> return ERR_PTR(-EINVAL);
> }
>
> - if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips)
> - return ERR_PTR(-EINVAL);
> -
> if (!thermal_class)
> return ERR_PTR(-ENODEV);
>
> @@ -1283,8 +1262,22 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
> tz->ops = ops;
> tz->device.class = thermal_class;
> tz->devdata = devdata;
> - tz->trips = trips;
> - tz->num_trips = num_trips;
> +
> + if (trips) {
> + result = __thermal_zone_trips_update(tz, trips, num_trips, mask);
> + if (result)
> + goto remove_id;
> + } else {
> + /*
> + * Legacy trip point handling
> + */
> + if ((!tz->ops->get_trip_type || !tz->ops->get_trip_temp) && num_trips) {
> + result = -EINVAL;
> + goto remove_id;
> + }
> +
> + tz->num_trips = num_trips;
> + }
Lest I forget, if I'm not mistaken, the above change would break the
int3403 driver that uses int340x_thermal_update_trips() to update trip
points in int3403_notify(), which handles notifications from the
platform firmware, and it updates them through the driver's private
pointer to the trips array used by the core with the assumption that
the core will notice the changes.
So it looks like at least this particular driver would need to be
updated before the $subject patch can be applied.
That said, I think that the ACPI thermal driver won't really need to
access the trips array after passing it to
thermal_zone_device_register_with_trips() and it may create a new
trips table every time the trip points are updated by the platform
firmware, but I'm not convinced about this design.