From: Guenter Roeck <linux@roeck-us.net>
To: "Rafael J . Wysocki" <rafael@kernel.org>
Cc: Daniel Lezcano <daniel.lezcano@linaro.org>,
Amit Kucheria <amitk@kernel.org>, Zhang Rui <rui.zhang@intel.com>,
linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH 7/9] thermal/core: Protect sysfs accesses to thermal operations with thermal zone mutex
Date: Mon, 17 Oct 2022 06:09:08 -0700 [thread overview]
Message-ID: <20221017130910.2307118-8-linux@roeck-us.net> (raw)
In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net>
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>
---
drivers/thermal/thermal_core.c | 4 +-
drivers/thermal/thermal_core.h | 2 +
drivers/thermal/thermal_sysfs.c | 77 ++++++++++++++++++++++++++++-----
3 files changed, 69 insertions(+), 14 deletions(-)
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 9facd9c5b70f..b8e3b262b2bd 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -403,8 +403,8 @@ static void thermal_zone_device_init(struct thermal_zone_device *tz)
pos->initialized = false;
}
-static void __thermal_zone_device_update(struct thermal_zone_device *tz,
- enum thermal_notify_event event)
+void __thermal_zone_device_update(struct thermal_zone_device *tz,
+ enum thermal_notify_event event)
{
int count;
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 1571917bd3c8..7b51b9a22e7e 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -109,6 +109,8 @@ int thermal_register_governor(struct thermal_governor *);
void thermal_unregister_governor(struct thermal_governor *);
int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
int thermal_build_list_of_policies(char *buf);
+void __thermal_zone_device_update(struct thermal_zone_device *tz,
+ enum thermal_notify_event event);
/* Helpers */
void thermal_zone_set_trips(struct thermal_zone_device *tz);
diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index ec495c7dff03..68607e6070e8 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -92,7 +92,15 @@ trip_point_type_show(struct device *dev, struct device_attribute *attr,
if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
return -EINVAL;
+ mutex_lock(&tz->lock);
+
+ if (!device_is_registered(dev)) {
+ mutex_unlock(&tz->lock);
+ return -ENODEV;
+ }
+
result = tz->ops->get_trip_type(tz, trip, &type);
+ mutex_unlock(&tz->lock);
if (result)
return result;
@@ -128,10 +136,17 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
if (kstrtoint(buf, 10, &temperature))
return -EINVAL;
+ mutex_lock(&tz->lock);
+
+ if (!device_is_registered(dev)) {
+ ret = -ENODEV;
+ goto unlock;
+ }
+
if (tz->ops->set_trip_temp) {
ret = tz->ops->set_trip_temp(tz, trip, temperature);
if (ret)
- return ret;
+ goto unlock;
}
if (tz->trips)
@@ -140,18 +155,21 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
if (tz->ops->get_trip_hyst) {
ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
if (ret)
- return ret;
+ goto unlock;
}
ret = tz->ops->get_trip_type(tz, trip, &type);
if (ret)
- return ret;
+ goto unlock;
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);
- return count;
+unlock:
+ mutex_unlock(&tz->lock);
+
+ return ret ? ret : count;
}
static ssize_t
@@ -168,12 +186,19 @@ trip_point_temp_show(struct device *dev, struct device_attribute *attr,
if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
return -EINVAL;
+ mutex_lock(&tz->lock);
+
+ if (!device_is_registered(dev)) {
+ ret = -ENODEV;
+ goto unlock;
+ }
+
ret = tz->ops->get_trip_temp(tz, trip, &temperature);
- if (ret)
- return ret;
+unlock:
+ mutex_unlock(&tz->lock);
- return sprintf(buf, "%d\n", temperature);
+ return ret ? ret : sprintf(buf, "%d\n", temperature);
}
static ssize_t
@@ -193,6 +218,13 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
if (kstrtoint(buf, 10, &temperature))
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
* here. The driver implementing 'set_trip_hyst' has to
@@ -201,7 +233,10 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
ret = tz->ops->set_trip_hyst(tz, trip, temperature);
if (!ret)
- thermal_zone_set_trips(tz);
+ __thermal_zone_set_trips(tz);
+
+unlock:
+ mutex_unlock(&tz->lock);
return ret ? ret : count;
}
@@ -220,8 +255,18 @@ trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
return -EINVAL;
+ mutex_lock(&tz->lock);
+
+ if (!device_is_registered(dev)) {
+ ret = -ENODEV;
+ goto unlock;
+ }
+
ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
+unlock:
+ mutex_unlock(&tz->lock);
+
return ret ? ret : sprintf(buf, "%d\n", temperature);
}
@@ -269,16 +314,24 @@ emul_temp_store(struct device *dev, struct device_attribute *attr,
if (kstrtoint(buf, 10, &temperature))
return -EINVAL;
+ mutex_lock(&tz->lock);
+
+ if (!device_is_registered(dev)) {
+ ret = -ENODEV;
+ goto unlock;
+ }
+
if (!tz->ops->set_emul_temp) {
- mutex_lock(&tz->lock);
tz->emul_temperature = temperature;
- mutex_unlock(&tz->lock);
} else {
ret = tz->ops->set_emul_temp(tz, temperature);
}
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;
}
--
2.36.2
next prev parent reply other threads:[~2022-10-17 13:10 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-17 13:09 [PATCH 0/9] thermal/core: Protect thermal device operations against removal Guenter Roeck
2022-10-17 13:09 ` [PATCH 1/9] thermal/core: Destroy thermal zone device mutex in release function Guenter Roeck
2022-10-17 13:09 ` [PATCH 2/9] thermal/core: Delete device under thermal device zone lock Guenter Roeck
2022-10-17 13:09 ` [PATCH 3/9] thermal/core: Ensure that thermal device is registered in thermal_zone_get_temp Guenter Roeck
2022-11-09 19:07 ` Rafael J. Wysocki
2022-11-10 14:13 ` Guenter Roeck
2022-10-17 13:09 ` [PATCH 4/9] thermal/core: Move parameter validation from __thermal_zone_get_temp to thermal_zone_get_temp Guenter Roeck
2022-11-09 19:12 ` Rafael J. Wysocki
2022-10-17 13:09 ` [PATCH 5/9] thermal/core: Introduce locked version of thermal_zone_device_update Guenter Roeck
2022-11-09 19:15 ` Rafael J. Wysocki
2022-11-10 0:25 ` Guenter Roeck
2022-11-10 13:01 ` Rafael J. Wysocki
2022-11-10 14:11 ` Guenter Roeck
2022-11-10 14:14 ` Rafael J. Wysocki
2022-10-17 13:09 ` [PATCH 6/9] thermal/core: Protect hwmon accesses to thermal operations with thermal zone mutex Guenter Roeck
2022-11-09 19:19 ` Rafael J. Wysocki
2022-11-10 14:21 ` Guenter Roeck
2022-11-10 14:24 ` Rafael J. Wysocki
2022-10-17 13:09 ` Guenter Roeck [this message]
2022-11-09 19:26 ` [PATCH 7/9] thermal/core: Protect sysfs " Rafael J. Wysocki
2022-10-17 13:09 ` [PATCH 8/9] thermal/core: Remove thermal_zone_set_trips() Guenter Roeck
2022-10-17 13:09 ` [PATCH 9/9] thermal/core: Protect thermal device operations against thermal device removal Guenter Roeck
2022-11-02 18:50 ` [PATCH 0/9] thermal/core: Protect thermal device operations against removal Guenter Roeck
2022-11-02 18:55 ` Daniel Lezcano
2022-11-09 19:30 ` Rafael J. Wysocki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221017130910.2307118-8-linux@roeck-us.net \
--to=linux@roeck-us.net \
--cc=amitk@kernel.org \
--cc=daniel.lezcano@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=rui.zhang@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®