thermal/core: Protect sysfs accesses to thermal operations with thermal zone mutex

Protect access to thermal operations against thermal zone removal by
acquiring the thermal zone device mutex. After acquiring the mutex, check
if the thermal zone device is registered and abort the operation if not.

With this change, we can call __thermal_zone_device_update() instead of
thermal_zone_device_update() from trip_point_temp_store() and from
emul_temp_store(). Similar, we can call __thermal_zone_set_trips() instead
of thermal_zone_set_trips() from trip_point_hyst_store().

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
This commit is contained in:
Guenter Roeck 2022-11-10 07:24:58 -08:00 committed by Rafael J. Wysocki
parent ea37bec51f
commit 05eeee2b51
3 changed files with 70 additions and 17 deletions

View File

@ -403,8 +403,8 @@ static void thermal_zone_device_init(struct thermal_zone_device *tz)
pos->initialized = false; pos->initialized = false;
} }
static void __thermal_zone_device_update(struct thermal_zone_device *tz, void __thermal_zone_device_update(struct thermal_zone_device *tz,
enum thermal_notify_event event) enum thermal_notify_event event)
{ {
int count; int count;

View File

@ -109,6 +109,8 @@ int thermal_register_governor(struct thermal_governor *);
void thermal_unregister_governor(struct thermal_governor *); void thermal_unregister_governor(struct thermal_governor *);
int thermal_zone_device_set_policy(struct thermal_zone_device *, char *); int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
int thermal_build_list_of_policies(char *buf); int thermal_build_list_of_policies(char *buf);
void __thermal_zone_device_update(struct thermal_zone_device *tz,
enum thermal_notify_event event);
/* Helpers */ /* Helpers */
void thermal_zone_set_trips(struct thermal_zone_device *tz); void thermal_zone_set_trips(struct thermal_zone_device *tz);

View File

@ -92,7 +92,14 @@ trip_point_type_show(struct device *dev, struct device_attribute *attr,
if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1) if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
return -EINVAL; return -EINVAL;
result = tz->ops->get_trip_type(tz, trip, &type); mutex_lock(&tz->lock);
if (device_is_registered(dev))
result = tz->ops->get_trip_type(tz, trip, &type);
else
result = -ENODEV;
mutex_unlock(&tz->lock);
if (result) if (result)
return result; return result;
@ -128,10 +135,17 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
if (kstrtoint(buf, 10, &temperature)) if (kstrtoint(buf, 10, &temperature))
return -EINVAL; return -EINVAL;
mutex_lock(&tz->lock);
if (!device_is_registered(dev)) {
ret = -ENODEV;
goto unlock;
}
if (tz->ops->set_trip_temp) { if (tz->ops->set_trip_temp) {
ret = tz->ops->set_trip_temp(tz, trip, temperature); ret = tz->ops->set_trip_temp(tz, trip, temperature);
if (ret) if (ret)
return ret; goto unlock;
} }
if (tz->trips) if (tz->trips)
@ -140,16 +154,22 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
if (tz->ops->get_trip_hyst) { if (tz->ops->get_trip_hyst) {
ret = tz->ops->get_trip_hyst(tz, trip, &hyst); ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
if (ret) if (ret)
return ret; goto unlock;
} }
ret = tz->ops->get_trip_type(tz, trip, &type); ret = tz->ops->get_trip_type(tz, trip, &type);
if (ret) if (ret)
return ret; goto unlock;
thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst); thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
unlock:
mutex_unlock(&tz->lock);
if (ret)
return ret;
return count; return count;
} }
@ -168,7 +188,14 @@ trip_point_temp_show(struct device *dev, struct device_attribute *attr,
if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1) if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
return -EINVAL; return -EINVAL;
ret = tz->ops->get_trip_temp(tz, trip, &temperature); mutex_lock(&tz->lock);
if (device_is_registered(dev))
ret = tz->ops->get_trip_temp(tz, trip, &temperature);
else
ret = -ENODEV;
mutex_unlock(&tz->lock);
if (ret) if (ret)
return ret; return ret;
@ -193,6 +220,13 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
if (kstrtoint(buf, 10, &temperature)) if (kstrtoint(buf, 10, &temperature))
return -EINVAL; return -EINVAL;
mutex_lock(&tz->lock);
if (!device_is_registered(dev)) {
ret = -ENODEV;
goto unlock;
}
/* /*
* We are not doing any check on the 'temperature' value * We are not doing any check on the 'temperature' value
* here. The driver implementing 'set_trip_hyst' has to * here. The driver implementing 'set_trip_hyst' has to
@ -201,7 +235,10 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
ret = tz->ops->set_trip_hyst(tz, trip, temperature); ret = tz->ops->set_trip_hyst(tz, trip, temperature);
if (!ret) if (!ret)
thermal_zone_set_trips(tz); __thermal_zone_set_trips(tz);
unlock:
mutex_unlock(&tz->lock);
return ret ? ret : count; return ret ? ret : count;
} }
@ -220,7 +257,14 @@ trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1) if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
return -EINVAL; return -EINVAL;
ret = tz->ops->get_trip_hyst(tz, trip, &temperature); mutex_lock(&tz->lock);
if (device_is_registered(dev))
ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
else
ret = -ENODEV;
mutex_unlock(&tz->lock);
return ret ? ret : sprintf(buf, "%d\n", temperature); return ret ? ret : sprintf(buf, "%d\n", temperature);
} }
@ -269,16 +313,23 @@ emul_temp_store(struct device *dev, struct device_attribute *attr,
if (kstrtoint(buf, 10, &temperature)) if (kstrtoint(buf, 10, &temperature))
return -EINVAL; return -EINVAL;
if (!tz->ops->set_emul_temp) { mutex_lock(&tz->lock);
mutex_lock(&tz->lock);
tz->emul_temperature = temperature; if (!device_is_registered(dev)) {
mutex_unlock(&tz->lock); ret = -ENODEV;
} else { goto unlock;
ret = tz->ops->set_emul_temp(tz, temperature);
} }
if (!tz->ops->set_emul_temp)
tz->emul_temperature = temperature;
else
ret = tz->ops->set_emul_temp(tz, temperature);
if (!ret) if (!ret)
thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
unlock:
mutex_unlock(&tz->lock);
return ret ? ret : count; return ret ? ret : count;
} }