[PATCH v2 6/8] thermal/sysfs: Update instance->weight under tz lock

From: Lukasz Luba
Date: Tue Dec 12 2023 - 08:48:26 EST


The user-space can change thermal instance weight value while the
throtte() callback is running for a governor. The IPA governor uses the
'weight' value for power calculation and also keeps it in 'total_weight'.
Therefore, the 'weight' value must not change during the throttle()
callback. Use 'tz->lock' mutex which also guards the throttle() to make
the update value safe.

Signed-off-by: Lukasz Luba <lukasz.luba@xxxxxxx>
---
drivers/thermal/thermal_sysfs.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index eef40d4f3063..df85df7d4a88 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -961,6 +961,7 @@ ssize_t weight_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct thermal_instance *instance;
+ struct thermal_zone_device *tz;
int ret, weight;

ret = kstrtoint(buf, 0, &weight);
@@ -968,7 +969,12 @@ ssize_t weight_store(struct device *dev, struct device_attribute *attr,
return ret;

instance = container_of(attr, struct thermal_instance, weight_attr);
+ tz = instance->tz;
+
+ /* Don't race with governors using the 'weight' value */
+ mutex_lock(&tz->lock);
instance->weight = weight;
+ mutex_unlock(&tz->lock);

return count;
}
--
2.25.1