Skip to content

Commit 32360bf

Browse files
rockosovlag-linaro
authored andcommitted
leds: Introduce ordered workqueue for LEDs events instead of system_wq
This allows to setup ordered workqueue for LEDs events. This may be useful, because default 'system_wq' does not guarantee execution order of each work_struct, thus for several brightness update requests (for multiple LEDs), real brightness switch could be in random order. Yes, for sysfs-based LEDs we have flush_work() call inside brightness_store() operation, but it's blocking call, so userspace caller can be blocked at a long time, which means LEDs animation stream can be broken. Ordered workqueue has the same behaviour as system_wq + flush_work(), but all scheduled works are async and userspace caller is not blocked, which it better for userspace animation scheduling. Signed-off-by: Alexey Romanov <avromanov@salutedevices.com> Signed-off-by: Dmitry Rokosov <ddrokosov@salutedevices.com> Link: https://lore.kernel.org/r/20240903223936.21292-1-ddrokosov@salutedevices.com [Lee: Couple of style fix-ups] Signed-off-by: Lee Jones <lee@kernel.org>
1 parent 64dd44a commit 32360bf

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

drivers/leds/led-class.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
static DEFINE_MUTEX(leds_lookup_lock);
2626
static LIST_HEAD(leds_lookup_list);
2727

28+
static struct workqueue_struct *leds_wq;
29+
2830
static ssize_t brightness_show(struct device *dev,
2931
struct device_attribute *attr, char *buf)
3032
{
@@ -57,7 +59,6 @@ static ssize_t brightness_store(struct device *dev,
5759
if (state == LED_OFF)
5860
led_trigger_remove(led_cdev);
5961
led_set_brightness(led_cdev, state);
60-
flush_work(&led_cdev->set_brightness_work);
6162

6263
ret = size;
6364
unlock:
@@ -549,6 +550,8 @@ int led_classdev_register_ext(struct device *parent,
549550

550551
led_update_brightness(led_cdev);
551552

553+
led_cdev->wq = leds_wq;
554+
552555
led_init_core(led_cdev);
553556

554557
#ifdef CONFIG_LEDS_TRIGGERS
@@ -667,12 +670,19 @@ EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
667670

668671
static int __init leds_init(void)
669672
{
673+
leds_wq = alloc_ordered_workqueue("leds", 0);
674+
if (!leds_wq) {
675+
pr_err("Failed to create LEDs ordered workqueue\n");
676+
return -ENOMEM;
677+
}
678+
670679
return class_register(&leds_class);
671680
}
672681

673682
static void __exit leds_exit(void)
674683
{
675684
class_unregister(&leds_class);
685+
destroy_workqueue(leds_wq);
676686
}
677687

678688
subsys_initcall(leds_init);

drivers/leds/led-core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on
273273
led_cdev->delayed_delay_on = delay_on;
274274
led_cdev->delayed_delay_off = delay_off;
275275
set_bit(LED_SET_BLINK, &led_cdev->work_flags);
276-
schedule_work(&led_cdev->set_brightness_work);
276+
queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
277277
return;
278278
}
279279

@@ -304,7 +304,7 @@ void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
304304
*/
305305
if (!brightness) {
306306
set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
307-
schedule_work(&led_cdev->set_brightness_work);
307+
queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
308308
} else {
309309
set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
310310
&led_cdev->work_flags);
@@ -340,7 +340,7 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
340340
set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
341341
}
342342

343-
schedule_work(&led_cdev->set_brightness_work);
343+
queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
344344
}
345345
EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
346346

include/linux/leds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ struct led_classdev {
171171
int new_blink_brightness;
172172
void (*flash_resume)(struct led_classdev *led_cdev);
173173

174+
struct workqueue_struct *wq; /* LED workqueue */
174175
struct work_struct set_brightness_work;
175176
int delayed_set_value;
176177
unsigned long delayed_delay_on;

0 commit comments

Comments
 (0)