From: Armin Wolf <W_Armin@gmx.de>
To: lee@kernel.org, pavel@kernel.org
Cc: linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org, i@rong.moe
Subject: [PATCH v2 05/10] leds: class: Fix race condition when creating the device
Date: Fri, 20 Nov 2026 21:35:17 +0100 [thread overview]
Message-ID: <20261120204246.6645-6-W_Armin@gmx.de> (raw)
In-Reply-To: <20261120204246.6645-1-W_Armin@gmx.de>
Userspace applications can access the sysfs attributes as soon as
device_create_with_groups() has returned. Two of those sysfs
attributes ("brightness_hw_changed" and "max_brightness") access
fields that are not properly initialized at this point, with only
"max_brightness" guarding against this by taking the LED access lock.
Fix the race condition affecting "brightness_hw_changed" by creating
the device after the associated fields have been properly initialized.
This also allows us to drop the LED access lock inside
max_brightness_show(), as the maximum brightness does not change after
initialization.
Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
drivers/leds/led-class.c | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)
diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 00532437cc45..fa9803e2ae6d 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -81,13 +81,8 @@ static ssize_t max_brightness_show(struct device *dev, const struct device_attri
char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
- unsigned int max_brightness;
- mutex_lock(&led_cdev->led_access);
- max_brightness = led_cdev->max_brightness;
- mutex_unlock(&led_cdev->led_access);
-
- return sysfs_emit(buf, "%u\n", max_brightness);
+ return sysfs_emit(buf, "%u\n", led_cdev->max_brightness);
}
static const DEVICE_ATTR_RO(max_brightness);
@@ -555,6 +550,18 @@ int led_classdev_register_ext(struct device *parent,
init_rwsem(&led_cdev->trigger_lock);
#endif
mutex_init(&led_cdev->led_access);
+ led_cdev->work_flags = 0;
+#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
+ led_cdev->brightness_hw_changed = -1;
+#endif
+#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED
+ if (led_cdev->flags & LED_TRIG_HW_CHANGED)
+ INIT_WORK(&led_cdev->trigger_hw_changed_work,
+ led_trigger_hw_control_changed_worker);
+#endif
+ if (!led_cdev->max_brightness)
+ led_cdev->max_brightness = LED_FULL;
+
mutex_lock(&led_cdev->led_access);
led_cdev->dev = device_create_with_groups(&leds_class, parent, 0,
led_cdev, led_cdev->groups, "%s", final_name);
@@ -562,6 +569,7 @@ int led_classdev_register_ext(struct device *parent,
mutex_unlock(&led_cdev->led_access);
return PTR_ERR(led_cdev->dev);
}
+
if (init_data && init_data->fwnode)
device_set_node(led_cdev->dev, init_data->fwnode);
@@ -576,18 +584,6 @@ int led_classdev_register_ext(struct device *parent,
}
}
- led_cdev->work_flags = 0;
-#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
- led_cdev->brightness_hw_changed = -1;
-#endif
-#ifdef CONFIG_LEDS_TRIGGERS_HW_CHANGED
- if (led_cdev->flags & LED_TRIG_HW_CHANGED)
- INIT_WORK(&led_cdev->trigger_hw_changed_work,
- led_trigger_hw_control_changed_worker);
-#endif
- if (!led_cdev->max_brightness)
- led_cdev->max_brightness = LED_FULL;
-
led_update_brightness(led_cdev);
led_cdev->wq = leds_wq;
--
2.56.1
next prev parent reply other threads:[~2026-11-20 20:43 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 20:35 [PATCH v2 00/10] leds: class: Support custom sysfs groups on multicolor and flash LEDs Armin Wolf
2026-11-20 20:35 ` [PATCH v2 01/10] leds: class: Constify sysfs attributes Armin Wolf
2026-11-20 20:35 ` [PATCH v2 02/10] leds: multicolor: " Armin Wolf
2026-11-20 20:35 ` [PATCH v2 03/10] leds: flash: " Armin Wolf
2026-11-20 20:35 ` [PATCH v2 04/10] leds: class: Create "brightness_hw_changed" attribute atomically Armin Wolf
2026-11-20 20:35 ` Armin Wolf [this message]
2026-11-20 20:35 ` [PATCH v2 06/10] leds: class: Access brightness_hw_changed exactly once Armin Wolf
2026-11-20 20:35 ` [PATCH v2 07/10] leds: class: Use correct type for brightness_hw_changed field Armin Wolf
2026-11-20 20:35 ` [PATCH v2 08/10] leds: class: Split led_classdev_register_ext() Armin Wolf
2026-11-20 20:35 ` [PATCH v2 09/10] leds: Support custom sysfs groups on multicolor and flash LEDs Armin Wolf
2026-11-20 20:35 ` [PATCH v2 10/10] leds: multicolor: Adjust maximum subled intensity during initialization Armin Wolf
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=20261120204246.6645-6-W_Armin@gmx.de \
--to=w_armin@gmx.de \
--cc=i@rong.moe \
--cc=lee@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=pavel@kernel.org \
/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®