]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
thermal: sysfs: Add sanity checks for trip temperature and hysteresis
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>
Mon, 26 Aug 2024 16:21:59 +0000 (18:21 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 4 Oct 2024 14:38:53 +0000 (16:38 +0200)
[ Upstream commit 874b6476fa888e79e41daf7653384c8d70927b90 ]

Add sanity checks for new trip temperature and hysteresis values to
trip_point_temp_store() and trip_point_hyst_store() to prevent trip
point threshold from falling below THERMAL_TEMP_INVALID.

However, still allow user space to pass THERMAL_TEMP_INVALID as the
new trip temperature value to invalidate the trip if necessary.

Also allow the hysteresis to be updated when the temperature is invalid
to allow user space to avoid having to adjust hysteresis after a valid
temperature has been set, but in that case just change the value and do
nothing else.

Fixes: be0a3600aa1e ("thermal: sysfs: Rework the handling of trip point updates")
Cc: 6.8+ <stable@vger.kernel.org> # 6.8+
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>
Link: https://patch.msgid.link/12528772.O9o76ZdvQC@rjwysocki.net
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/thermal/thermal_sysfs.c

index 2709455486776c2f70057e5a0a8c7df61f9d77c6..d628dd67be5ccf700e1fe6ffc8975d84ae7f5c9e 100644 (file)
@@ -111,18 +111,26 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
 
        mutex_lock(&tz->lock);
 
-       if (temp != trip->temperature) {
-               if (tz->ops.set_trip_temp) {
-                       ret = tz->ops.set_trip_temp(tz, trip, temp);
-                       if (ret)
-                               goto unlock;
-               }
+       if (temp == trip->temperature)
+               goto unlock;
 
-               thermal_zone_set_trip_temp(tz, trip, temp);
+       /* Arrange the condition to avoid integer overflows. */
+       if (temp != THERMAL_TEMP_INVALID &&
+           temp <= trip->hysteresis + THERMAL_TEMP_INVALID) {
+               ret = -EINVAL;
+               goto unlock;
+       }
 
-               __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
+       if (tz->ops.set_trip_temp) {
+               ret = tz->ops.set_trip_temp(tz, trip, temp);
+               if (ret)
+                       goto unlock;
        }
 
+       thermal_zone_set_trip_temp(tz, trip, temp);
+
+       __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
+
 unlock:
        mutex_unlock(&tz->lock);
 
@@ -152,15 +160,33 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
 
        mutex_lock(&tz->lock);
 
-       if (hyst != trip->hysteresis) {
-               thermal_zone_set_trip_hyst(tz, trip, hyst);
+       if (hyst == trip->hysteresis)
+               goto unlock;
+
+       /*
+        * Allow the hysteresis to be updated when the temperature is invalid
+        * to allow user space to avoid having to adjust hysteresis after a
+        * valid temperature has been set, but in that case just change the
+        * value and do nothing else.
+        */
+       if (trip->temperature == THERMAL_TEMP_INVALID) {
+               WRITE_ONCE(trip->hysteresis, hyst);
+               goto unlock;
+       }
 
-               __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
+       if (trip->temperature - hyst <= THERMAL_TEMP_INVALID) {
+               ret = -EINVAL;
+               goto unlock;
        }
 
+       thermal_zone_set_trip_hyst(tz, trip, hyst);
+
+       __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
+
+unlock:
        mutex_unlock(&tz->lock);
 
-       return count;
+       return ret ? ret : count;
 }
 
 static ssize_t