[PATCH v1 1/6] thermal: sysfs: Trigger zone temperature updates on sysfs reads

From: Rafael J. Wysocki
Date: Fri May 10 2024 - 10:21:44 EST


From: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>

Reading the zone temperature via sysfs causes the driver callback to
be invoked, but it does not cause the thermal zone object to be updated.

This is problematic if the zone temperature read via sysfs differs from
the temperature value stored in the thermal zone object as it may cause
the kernel and user space to act against each other in some cases.

For this reason, make temp_show() trigger a zone temperature update if
the temperature returned by thermal_zone_get_temp() is different from
the temperature value stored in the thermal zone object.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>
---
drivers/thermal/thermal_core.c | 2 +-
drivers/thermal/thermal_sysfs.c | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/thermal/thermal_sysfs.c
===================================================================
--- linux-pm.orig/drivers/thermal/thermal_sysfs.c
+++ linux-pm/drivers/thermal/thermal_sysfs.c
@@ -42,6 +42,9 @@ temp_show(struct device *dev, struct dev
if (ret)
return ret;

+ if (temperature != READ_ONCE(tz->temperature))
+ thermal_zone_device_update(tz, THERMAL_EVENT_TEMP_SAMPLE);
+
return sprintf(buf, "%d\n", temperature);
}

Index: linux-pm/drivers/thermal/thermal_core.c
===================================================================
--- linux-pm.orig/drivers/thermal/thermal_core.c
+++ linux-pm/drivers/thermal/thermal_core.c
@@ -429,7 +429,7 @@ static void update_temperature(struct th
}

tz->last_temperature = tz->temperature;
- tz->temperature = temp;
+ WRITE_ONCE(tz->temperature, temp);

trace_thermal_temperature(tz);