2018-11-21 21:55:12

by Thara Gopinath

[permalink] [raw]
Subject: [PATCH] thermal: Fix locking in cooling device sysfs update cur_state

Sysfs interface to update cooling device cur_state does not
currently hold cooling device lock, leading to stale values
in cur_state especially if getting updated simultanelously
from user space and thermal framework. Adding the proper locking
code fixes this issue.

Signed-off-by: Thara Gopinath <[email protected]>
---
drivers/thermal/thermal_sysfs.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index 2241cea..1f0c1e2 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -712,10 +712,14 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
if ((long)state < 0)
return -EINVAL;

+ mutex_lock(&cdev->lock);
result = cdev->ops->set_cur_state(cdev, state);
- if (result)
+ if (result) {
+ mutex_unlock(&cdev->lock);
return result;
+ }
thermal_cooling_device_stats_update(cdev, state);
+ mutex_unlock(&cdev->lock);
return count;
}

--
2.1.4



2018-11-22 08:36:48

by Daniel Lezcano

[permalink] [raw]
Subject: Re: [PATCH] thermal: Fix locking in cooling device sysfs update cur_state

On 21/11/2018 20:01, Thara Gopinath wrote:
> Sysfs interface to update cooling device cur_state does not
> currently hold cooling device lock, leading to stale values
> in cur_state especially if getting updated simultanelously
> from user space and thermal framework. Adding the proper locking
> code fixes this issue.
>
> Signed-off-by: Thara Gopinath <[email protected]>
> ---
> drivers/thermal/thermal_sysfs.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
> index 2241cea..1f0c1e2 100644
> --- a/drivers/thermal/thermal_sysfs.c
> +++ b/drivers/thermal/thermal_sysfs.c
> @@ -712,10 +712,14 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
> if ((long)state < 0)
> return -EINVAL;
>
> + mutex_lock(&cdev->lock);
> result = cdev->ops->set_cur_state(cdev, state);
> - if (result)
> + if (result) {
> + mutex_unlock(&cdev->lock);
> return result;
> + }
> thermal_cooling_device_stats_update(cdev, state);
> + mutex_unlock(&cdev->lock);

I suggest to invert the condition:

mutex_lock(&cdev->lock);
result = cdev->ops->set_cur_state(cdev, state);
if (!result)
thermal_cooling_device_stats_update(cdev, state);
mutex_unlock(&cdev->lock);

return result ? result : count;


The same problem is there for the cur_state_show() callback.

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog