From: Jacek Anaszewski <j.anaszewski@samsung.com>
To: linux-leds@vger.kernel.org
Cc: linux-kernel@vger.kernel.org,
Jacek Anaszewski <j.anaszewski@samsung.com>,
Hans de Goede <hdegoede@redhat.com>,
Sakari Ailus <sakari.ailus@linux.intel.com>,
Pavel Machek <pavel@ucw.cz>, Andrew Lunn <andrew@lunn.ch>
Subject: [PATCH v2] leds: Add mutex protection in brightness_show()
Date: Wed, 09 Nov 2016 13:15:25 +0100 [thread overview]
Message-ID: <1478693725-29893-1-git-send-email-j.anaszewski@samsung.com> (raw)
Initially the claim about no need for lock in brightness_show()
was valid as the function was just returning unchanged
LED brightness.
After the addition of led_update_brightness() this is no longer
true, as the function can change the brightness if a LED class
driver implements brightness_get op. It can lead to races between
led_update_brightness() and led_set_brightness().
Signed-off-by: Jacek Anaszewski <j.anaszewski@samsung.com>
Cc: Hans de Goede <hdegoede@redhat.com>
Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Andrew Lunn <andrew@lunn.ch>
---
Changes since v1:
- added led_sysfs_is_disabled() check
- moved sprintf under mutex protection
drivers/leds/led-class.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index b12f861..e472407 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -29,11 +29,23 @@ static ssize_t brightness_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
+ int ret;
- /* no lock needed for this */
- led_update_brightness(led_cdev);
+ mutex_lock(&led_cdev->led_access);
+
+ if (led_sysfs_is_disabled(led_cdev)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ ret = led_update_brightness(led_cdev);
+ if (ret < 0)
+ goto unlock;
- return sprintf(buf, "%u\n", led_cdev->brightness);
+ ret = sprintf(buf, "%u\n", led_cdev->brightness);
+unlock:
+ mutex_unlock(&led_cdev->led_access);
+ return ret;
}
static ssize_t brightness_store(struct device *dev,
--
1.9.1
reply other threads:[~2016-11-09 12:16 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=1478693725-29893-1-git-send-email-j.anaszewski@samsung.com \
--to=j.anaszewski@samsung.com \
--cc=andrew@lunn.ch \
--cc=hdegoede@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=pavel@ucw.cz \
--cc=sakari.ailus@linux.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®