[PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq

Dmitry Rokosov posted 1 patch 1 year, 3 months ago
drivers/leds/led-class.c | 12 +++++++++++-
drivers/leds/led-core.c  |  6 +++---
include/linux/leds.h     |  1 +
3 files changed, 15 insertions(+), 4 deletions(-)
[PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Dmitry Rokosov 1 year, 3 months ago
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>
---
Changes v2 since v1 at [1]:
    - replace "leds" with "LEDs" in the log message

Links:
    [1] https://lore.kernel.org/all/20240820155407.32729-1-ddrokosov@salutedevices.com/
---
 drivers/leds/led-class.c | 12 +++++++++++-
 drivers/leds/led-core.c  |  6 +++---
 include/linux/leds.h     |  1 +
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index c66d1bead0a4..b5e28ad54f7f 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -25,6 +25,8 @@
 static DEFINE_MUTEX(leds_lookup_lock);
 static LIST_HEAD(leds_lookup_list);
 
+static struct workqueue_struct *leds_wq;
+
 static ssize_t brightness_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
@@ -57,7 +59,6 @@ static ssize_t brightness_store(struct device *dev,
 	if (state == LED_OFF)
 		led_trigger_remove(led_cdev);
 	led_set_brightness(led_cdev, state);
-	flush_work(&led_cdev->set_brightness_work);
 
 	ret = size;
 unlock:
@@ -548,6 +549,8 @@ int led_classdev_register_ext(struct device *parent,
 
 	led_update_brightness(led_cdev);
 
+	led_cdev->wq = leds_wq;
+
 	led_init_core(led_cdev);
 
 #ifdef CONFIG_LEDS_TRIGGERS
@@ -666,12 +669,19 @@ EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
 
 static int __init leds_init(void)
 {
+	leds_wq = alloc_ordered_workqueue("leds", 0);
+	if (!leds_wq) {
+		pr_err("failed to create LEDs ordered workqueue\n");
+		return -ENOMEM;
+	}
+
 	return class_register(&leds_class);
 }
 
 static void __exit leds_exit(void)
 {
 	class_unregister(&leds_class);
+	destroy_workqueue(leds_wq);
 }
 
 subsys_initcall(leds_init);
diff --git a/drivers/leds/led-core.c b/drivers/leds/led-core.c
index 89c9806cc97f..9769ac49be20 100644
--- a/drivers/leds/led-core.c
+++ b/drivers/leds/led-core.c
@@ -266,7 +266,7 @@ void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on
 		led_cdev->delayed_delay_on = delay_on;
 		led_cdev->delayed_delay_off = delay_off;
 		set_bit(LED_SET_BLINK, &led_cdev->work_flags);
-		schedule_work(&led_cdev->set_brightness_work);
+		queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
 		return;
 	}
 
@@ -297,7 +297,7 @@ void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
 		 */
 		if (!brightness) {
 			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
-			schedule_work(&led_cdev->set_brightness_work);
+			queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
 		} else {
 			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
 				&led_cdev->work_flags);
@@ -333,7 +333,7 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
 		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
 	}
 
-	schedule_work(&led_cdev->set_brightness_work);
+	queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
 }
 EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
 
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 6300313c46b7..7c9f1cb12ab9 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -169,6 +169,7 @@ struct led_classdev {
 	int			 new_blink_brightness;
 	void			(*flash_resume)(struct led_classdev *led_cdev);
 
+	struct workqueue_struct *wq; /* LED workqueue */
 	struct work_struct	set_brightness_work;
 	int			delayed_set_value;
 	unsigned long		delayed_delay_on;
-- 
2.43.0
Re: (subset) [PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Lee Jones 1 year, 1 month ago
On Wed, 04 Sep 2024 01:39:30 +0300, Dmitry Rokosov wrote:
> 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.
> 
> [...]

Applied, thanks!

[1/1] leds: introduce ordered workqueue for leds events instead of system_wq
      commit: 32360bf6a5d4016669c3545e7b0ec939937f5331

--
Lee Jones [李琼斯]

Re: (subset) [PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Lee Jones 1 year, 1 month ago
On Wed, 06 Nov 2024, Lee Jones wrote:

> On Wed, 04 Sep 2024 01:39:30 +0300, Dmitry Rokosov wrote:
> > 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.
> > 
> > [...]
> 
> Applied, thanks!
> 
> [1/1] leds: introduce ordered workqueue for leds events instead of system_wq
>       commit: 32360bf6a5d4016669c3545e7b0ec939937f5331

With a couple of style fix-ups.

-- 
Lee Jones [李琼斯]
Re: [PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Dmitry Rokosov 1 year, 3 months ago
Hello Lee!

What are the next steps? Should I make any changes, or are we waiting
for test results from the mailing list members?

Sorry for the ping.

On Wed, Sep 04, 2024 at 01:39:30AM +0300, Dmitry Rokosov wrote:
> 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>
> ---
> Changes v2 since v1 at [1]:
>     - replace "leds" with "LEDs" in the log message
> 
> Links:
>     [1] https://lore.kernel.org/all/20240820155407.32729-1-ddrokosov@salutedevices.com/
> ---
>  drivers/leds/led-class.c | 12 +++++++++++-
>  drivers/leds/led-core.c  |  6 +++---
>  include/linux/leds.h     |  1 +
>  3 files changed, 15 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
> index c66d1bead0a4..b5e28ad54f7f 100644
> --- a/drivers/leds/led-class.c
> +++ b/drivers/leds/led-class.c
> @@ -25,6 +25,8 @@
>  static DEFINE_MUTEX(leds_lookup_lock);
>  static LIST_HEAD(leds_lookup_list);
>  
> +static struct workqueue_struct *leds_wq;
> +
>  static ssize_t brightness_show(struct device *dev,
>  		struct device_attribute *attr, char *buf)
>  {
> @@ -57,7 +59,6 @@ static ssize_t brightness_store(struct device *dev,
>  	if (state == LED_OFF)
>  		led_trigger_remove(led_cdev);
>  	led_set_brightness(led_cdev, state);
> -	flush_work(&led_cdev->set_brightness_work);
>  
>  	ret = size;
>  unlock:
> @@ -548,6 +549,8 @@ int led_classdev_register_ext(struct device *parent,
>  
>  	led_update_brightness(led_cdev);
>  
> +	led_cdev->wq = leds_wq;
> +
>  	led_init_core(led_cdev);
>  
>  #ifdef CONFIG_LEDS_TRIGGERS
> @@ -666,12 +669,19 @@ EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
>  
>  static int __init leds_init(void)
>  {
> +	leds_wq = alloc_ordered_workqueue("leds", 0);
> +	if (!leds_wq) {
> +		pr_err("failed to create LEDs ordered workqueue\n");
> +		return -ENOMEM;
> +	}
> +
>  	return class_register(&leds_class);
>  }
>  
>  static void __exit leds_exit(void)
>  {
>  	class_unregister(&leds_class);
> +	destroy_workqueue(leds_wq);
>  }
>  
>  subsys_initcall(leds_init);
> diff --git a/drivers/leds/led-core.c b/drivers/leds/led-core.c
> index 89c9806cc97f..9769ac49be20 100644
> --- a/drivers/leds/led-core.c
> +++ b/drivers/leds/led-core.c
> @@ -266,7 +266,7 @@ void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on
>  		led_cdev->delayed_delay_on = delay_on;
>  		led_cdev->delayed_delay_off = delay_off;
>  		set_bit(LED_SET_BLINK, &led_cdev->work_flags);
> -		schedule_work(&led_cdev->set_brightness_work);
> +		queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
>  		return;
>  	}
>  
> @@ -297,7 +297,7 @@ void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
>  		 */
>  		if (!brightness) {
>  			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
> -			schedule_work(&led_cdev->set_brightness_work);
> +			queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
>  		} else {
>  			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
>  				&led_cdev->work_flags);
> @@ -333,7 +333,7 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
>  		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
>  	}
>  
> -	schedule_work(&led_cdev->set_brightness_work);
> +	queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
>  }
>  EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
>  
> diff --git a/include/linux/leds.h b/include/linux/leds.h
> index 6300313c46b7..7c9f1cb12ab9 100644
> --- a/include/linux/leds.h
> +++ b/include/linux/leds.h
> @@ -169,6 +169,7 @@ struct led_classdev {
>  	int			 new_blink_brightness;
>  	void			(*flash_resume)(struct led_classdev *led_cdev);
>  
> +	struct workqueue_struct *wq; /* LED workqueue */
>  	struct work_struct	set_brightness_work;
>  	int			delayed_set_value;
>  	unsigned long		delayed_delay_on;
> -- 
> 2.43.0
> 

-- 
Thank you,
Dmitry
Re: [PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Lee Jones 1 year, 3 months ago
On Mon, 16 Sep 2024, Dmitry Rokosov wrote:

> Hello Lee!
> 
> What are the next steps? Should I make any changes, or are we waiting
> for test results from the mailing list members?

This is an intrusive core change that was submitted during -rc6.

It's going to need some time on the list for people to respond.

> Sorry for the ping.
> 
> On Wed, Sep 04, 2024 at 01:39:30AM +0300, Dmitry Rokosov wrote:
> > 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>
> > ---
> > Changes v2 since v1 at [1]:
> >     - replace "leds" with "LEDs" in the log message
> > 
> > Links:
> >     [1] https://lore.kernel.org/all/20240820155407.32729-1-ddrokosov@salutedevices.com/
> > ---
> >  drivers/leds/led-class.c | 12 +++++++++++-
> >  drivers/leds/led-core.c  |  6 +++---
> >  include/linux/leds.h     |  1 +
> >  3 files changed, 15 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
> > index c66d1bead0a4..b5e28ad54f7f 100644
> > --- a/drivers/leds/led-class.c
> > +++ b/drivers/leds/led-class.c
> > @@ -25,6 +25,8 @@
> >  static DEFINE_MUTEX(leds_lookup_lock);
> >  static LIST_HEAD(leds_lookup_list);
> >  
> > +static struct workqueue_struct *leds_wq;
> > +
> >  static ssize_t brightness_show(struct device *dev,
> >  		struct device_attribute *attr, char *buf)
> >  {
> > @@ -57,7 +59,6 @@ static ssize_t brightness_store(struct device *dev,
> >  	if (state == LED_OFF)
> >  		led_trigger_remove(led_cdev);
> >  	led_set_brightness(led_cdev, state);
> > -	flush_work(&led_cdev->set_brightness_work);
> >  
> >  	ret = size;
> >  unlock:
> > @@ -548,6 +549,8 @@ int led_classdev_register_ext(struct device *parent,
> >  
> >  	led_update_brightness(led_cdev);
> >  
> > +	led_cdev->wq = leds_wq;
> > +
> >  	led_init_core(led_cdev);
> >  
> >  #ifdef CONFIG_LEDS_TRIGGERS
> > @@ -666,12 +669,19 @@ EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
> >  
> >  static int __init leds_init(void)
> >  {
> > +	leds_wq = alloc_ordered_workqueue("leds", 0);
> > +	if (!leds_wq) {
> > +		pr_err("failed to create LEDs ordered workqueue\n");
> > +		return -ENOMEM;
> > +	}
> > +
> >  	return class_register(&leds_class);
> >  }
> >  
> >  static void __exit leds_exit(void)
> >  {
> >  	class_unregister(&leds_class);
> > +	destroy_workqueue(leds_wq);
> >  }
> >  
> >  subsys_initcall(leds_init);
> > diff --git a/drivers/leds/led-core.c b/drivers/leds/led-core.c
> > index 89c9806cc97f..9769ac49be20 100644
> > --- a/drivers/leds/led-core.c
> > +++ b/drivers/leds/led-core.c
> > @@ -266,7 +266,7 @@ void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on
> >  		led_cdev->delayed_delay_on = delay_on;
> >  		led_cdev->delayed_delay_off = delay_off;
> >  		set_bit(LED_SET_BLINK, &led_cdev->work_flags);
> > -		schedule_work(&led_cdev->set_brightness_work);
> > +		queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> >  		return;
> >  	}
> >  
> > @@ -297,7 +297,7 @@ void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
> >  		 */
> >  		if (!brightness) {
> >  			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
> > -			schedule_work(&led_cdev->set_brightness_work);
> > +			queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> >  		} else {
> >  			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
> >  				&led_cdev->work_flags);
> > @@ -333,7 +333,7 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
> >  		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
> >  	}
> >  
> > -	schedule_work(&led_cdev->set_brightness_work);
> > +	queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> >  }
> >  EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
> >  
> > diff --git a/include/linux/leds.h b/include/linux/leds.h
> > index 6300313c46b7..7c9f1cb12ab9 100644
> > --- a/include/linux/leds.h
> > +++ b/include/linux/leds.h
> > @@ -169,6 +169,7 @@ struct led_classdev {
> >  	int			 new_blink_brightness;
> >  	void			(*flash_resume)(struct led_classdev *led_cdev);
> >  
> > +	struct workqueue_struct *wq; /* LED workqueue */
> >  	struct work_struct	set_brightness_work;
> >  	int			delayed_set_value;
> >  	unsigned long		delayed_delay_on;
> > -- 
> > 2.43.0
> > 
> 
> -- 
> Thank you,
> Dmitry

-- 
Lee Jones [李琼斯]
Re: [PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Dmitry Rokosov 1 year, 1 month ago
Hello Lee,

I hope this message finds you well.
I wanted to ask for your thoughts on whether there was enough time for
testing and if we can proceed with merging this patchset.

I appreciate any feedback about it.

On Tue, Sep 17, 2024 at 09:04:12AM +0100, Lee Jones wrote:
> On Mon, 16 Sep 2024, Dmitry Rokosov wrote:
> 
> > Hello Lee!
> > 
> > What are the next steps? Should I make any changes, or are we waiting
> > for test results from the mailing list members?
> 
> This is an intrusive core change that was submitted during -rc6.
> 
> It's going to need some time on the list for people to respond.
> 
> > Sorry for the ping.
> > 
> > On Wed, Sep 04, 2024 at 01:39:30AM +0300, Dmitry Rokosov wrote:
> > > 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>
> > > ---
> > > Changes v2 since v1 at [1]:
> > >     - replace "leds" with "LEDs" in the log message
> > > 
> > > Links:
> > >     [1] https://lore.kernel.org/all/20240820155407.32729-1-ddrokosov@salutedevices.com/
> > > ---
> > >  drivers/leds/led-class.c | 12 +++++++++++-
> > >  drivers/leds/led-core.c  |  6 +++---
> > >  include/linux/leds.h     |  1 +
> > >  3 files changed, 15 insertions(+), 4 deletions(-)
> > > 
> > > diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
> > > index c66d1bead0a4..b5e28ad54f7f 100644
> > > --- a/drivers/leds/led-class.c
> > > +++ b/drivers/leds/led-class.c
> > > @@ -25,6 +25,8 @@
> > >  static DEFINE_MUTEX(leds_lookup_lock);
> > >  static LIST_HEAD(leds_lookup_list);
> > >  
> > > +static struct workqueue_struct *leds_wq;
> > > +
> > >  static ssize_t brightness_show(struct device *dev,
> > >  		struct device_attribute *attr, char *buf)
> > >  {
> > > @@ -57,7 +59,6 @@ static ssize_t brightness_store(struct device *dev,
> > >  	if (state == LED_OFF)
> > >  		led_trigger_remove(led_cdev);
> > >  	led_set_brightness(led_cdev, state);
> > > -	flush_work(&led_cdev->set_brightness_work);
> > >  
> > >  	ret = size;
> > >  unlock:
> > > @@ -548,6 +549,8 @@ int led_classdev_register_ext(struct device *parent,
> > >  
> > >  	led_update_brightness(led_cdev);
> > >  
> > > +	led_cdev->wq = leds_wq;
> > > +
> > >  	led_init_core(led_cdev);
> > >  
> > >  #ifdef CONFIG_LEDS_TRIGGERS
> > > @@ -666,12 +669,19 @@ EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
> > >  
> > >  static int __init leds_init(void)
> > >  {
> > > +	leds_wq = alloc_ordered_workqueue("leds", 0);
> > > +	if (!leds_wq) {
> > > +		pr_err("failed to create LEDs ordered workqueue\n");
> > > +		return -ENOMEM;
> > > +	}
> > > +
> > >  	return class_register(&leds_class);
> > >  }
> > >  
> > >  static void __exit leds_exit(void)
> > >  {
> > >  	class_unregister(&leds_class);
> > > +	destroy_workqueue(leds_wq);
> > >  }
> > >  
> > >  subsys_initcall(leds_init);
> > > diff --git a/drivers/leds/led-core.c b/drivers/leds/led-core.c
> > > index 89c9806cc97f..9769ac49be20 100644
> > > --- a/drivers/leds/led-core.c
> > > +++ b/drivers/leds/led-core.c
> > > @@ -266,7 +266,7 @@ void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on
> > >  		led_cdev->delayed_delay_on = delay_on;
> > >  		led_cdev->delayed_delay_off = delay_off;
> > >  		set_bit(LED_SET_BLINK, &led_cdev->work_flags);
> > > -		schedule_work(&led_cdev->set_brightness_work);
> > > +		queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> > >  		return;
> > >  	}
> > >  
> > > @@ -297,7 +297,7 @@ void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
> > >  		 */
> > >  		if (!brightness) {
> > >  			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
> > > -			schedule_work(&led_cdev->set_brightness_work);
> > > +			queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> > >  		} else {
> > >  			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
> > >  				&led_cdev->work_flags);
> > > @@ -333,7 +333,7 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
> > >  		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
> > >  	}
> > >  
> > > -	schedule_work(&led_cdev->set_brightness_work);
> > > +	queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> > >  }
> > >  EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
> > >  
> > > diff --git a/include/linux/leds.h b/include/linux/leds.h
> > > index 6300313c46b7..7c9f1cb12ab9 100644
> > > --- a/include/linux/leds.h
> > > +++ b/include/linux/leds.h
> > > @@ -169,6 +169,7 @@ struct led_classdev {
> > >  	int			 new_blink_brightness;
> > >  	void			(*flash_resume)(struct led_classdev *led_cdev);
> > >  
> > > +	struct workqueue_struct *wq; /* LED workqueue */
> > >  	struct work_struct	set_brightness_work;
> > >  	int			delayed_set_value;
> > >  	unsigned long		delayed_delay_on;
> > > -- 
> > > 2.43.0
> > > 
> > 
> > -- 
> > Thank you,
> > Dmitry
> 
> -- 
> Lee Jones [李琼斯]

-- 
Thank you,
Dmitry
Re: [PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Dmitry Rokosov 1 year, 1 month ago
Hello Lee,

Kindly reminder.

On Tue, Oct 22, 2024 at 05:22:48PM +0300, Dmitry Rokosov wrote:
> Hello Lee,
> 
> I hope this message finds you well.
> I wanted to ask for your thoughts on whether there was enough time for
> testing and if we can proceed with merging this patchset.
> 
> I appreciate any feedback about it.
> 
> On Tue, Sep 17, 2024 at 09:04:12AM +0100, Lee Jones wrote:
> > On Mon, 16 Sep 2024, Dmitry Rokosov wrote:
> > 
> > > Hello Lee!
> > > 
> > > What are the next steps? Should I make any changes, or are we waiting
> > > for test results from the mailing list members?
> > 
> > This is an intrusive core change that was submitted during -rc6.
> > 
> > It's going to need some time on the list for people to respond.
> > 
> > > Sorry for the ping.
> > > 
> > > On Wed, Sep 04, 2024 at 01:39:30AM +0300, Dmitry Rokosov wrote:
> > > > 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>
> > > > ---
> > > > Changes v2 since v1 at [1]:
> > > >     - replace "leds" with "LEDs" in the log message
> > > > 
> > > > Links:
> > > >     [1] https://lore.kernel.org/all/20240820155407.32729-1-ddrokosov@salutedevices.com/
> > > > ---
> > > >  drivers/leds/led-class.c | 12 +++++++++++-
> > > >  drivers/leds/led-core.c  |  6 +++---
> > > >  include/linux/leds.h     |  1 +
> > > >  3 files changed, 15 insertions(+), 4 deletions(-)
> > > > 
> > > > diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
> > > > index c66d1bead0a4..b5e28ad54f7f 100644
> > > > --- a/drivers/leds/led-class.c
> > > > +++ b/drivers/leds/led-class.c
> > > > @@ -25,6 +25,8 @@
> > > >  static DEFINE_MUTEX(leds_lookup_lock);
> > > >  static LIST_HEAD(leds_lookup_list);
> > > >  
> > > > +static struct workqueue_struct *leds_wq;
> > > > +
> > > >  static ssize_t brightness_show(struct device *dev,
> > > >  		struct device_attribute *attr, char *buf)
> > > >  {
> > > > @@ -57,7 +59,6 @@ static ssize_t brightness_store(struct device *dev,
> > > >  	if (state == LED_OFF)
> > > >  		led_trigger_remove(led_cdev);
> > > >  	led_set_brightness(led_cdev, state);
> > > > -	flush_work(&led_cdev->set_brightness_work);
> > > >  
> > > >  	ret = size;
> > > >  unlock:
> > > > @@ -548,6 +549,8 @@ int led_classdev_register_ext(struct device *parent,
> > > >  
> > > >  	led_update_brightness(led_cdev);
> > > >  
> > > > +	led_cdev->wq = leds_wq;
> > > > +
> > > >  	led_init_core(led_cdev);
> > > >  
> > > >  #ifdef CONFIG_LEDS_TRIGGERS
> > > > @@ -666,12 +669,19 @@ EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
> > > >  
> > > >  static int __init leds_init(void)
> > > >  {
> > > > +	leds_wq = alloc_ordered_workqueue("leds", 0);
> > > > +	if (!leds_wq) {
> > > > +		pr_err("failed to create LEDs ordered workqueue\n");
> > > > +		return -ENOMEM;
> > > > +	}
> > > > +
> > > >  	return class_register(&leds_class);
> > > >  }
> > > >  
> > > >  static void __exit leds_exit(void)
> > > >  {
> > > >  	class_unregister(&leds_class);
> > > > +	destroy_workqueue(leds_wq);
> > > >  }
> > > >  
> > > >  subsys_initcall(leds_init);
> > > > diff --git a/drivers/leds/led-core.c b/drivers/leds/led-core.c
> > > > index 89c9806cc97f..9769ac49be20 100644
> > > > --- a/drivers/leds/led-core.c
> > > > +++ b/drivers/leds/led-core.c
> > > > @@ -266,7 +266,7 @@ void led_blink_set_nosleep(struct led_classdev *led_cdev, unsigned long delay_on
> > > >  		led_cdev->delayed_delay_on = delay_on;
> > > >  		led_cdev->delayed_delay_off = delay_off;
> > > >  		set_bit(LED_SET_BLINK, &led_cdev->work_flags);
> > > > -		schedule_work(&led_cdev->set_brightness_work);
> > > > +		queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> > > >  		return;
> > > >  	}
> > > >  
> > > > @@ -297,7 +297,7 @@ void led_set_brightness(struct led_classdev *led_cdev, unsigned int brightness)
> > > >  		 */
> > > >  		if (!brightness) {
> > > >  			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
> > > > -			schedule_work(&led_cdev->set_brightness_work);
> > > > +			queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> > > >  		} else {
> > > >  			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
> > > >  				&led_cdev->work_flags);
> > > > @@ -333,7 +333,7 @@ void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
> > > >  		set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
> > > >  	}
> > > >  
> > > > -	schedule_work(&led_cdev->set_brightness_work);
> > > > +	queue_work(led_cdev->wq, &led_cdev->set_brightness_work);
> > > >  }
> > > >  EXPORT_SYMBOL_GPL(led_set_brightness_nopm);
> > > >  
> > > > diff --git a/include/linux/leds.h b/include/linux/leds.h
> > > > index 6300313c46b7..7c9f1cb12ab9 100644
> > > > --- a/include/linux/leds.h
> > > > +++ b/include/linux/leds.h
> > > > @@ -169,6 +169,7 @@ struct led_classdev {
> > > >  	int			 new_blink_brightness;
> > > >  	void			(*flash_resume)(struct led_classdev *led_cdev);
> > > >  
> > > > +	struct workqueue_struct *wq; /* LED workqueue */
> > > >  	struct work_struct	set_brightness_work;
> > > >  	int			delayed_set_value;
> > > >  	unsigned long		delayed_delay_on;
> > > > -- 
> > > > 2.43.0

-- 
Thank you,
Dmitry
Re: [PATCH v2] leds: introduce ordered workqueue for leds events instead of system_wq
Posted by Lee Jones 1 year, 1 month ago
On Tue, 29 Oct 2024, Dmitry Rokosov wrote:

> Hello Lee,
> 
> Kindly reminder.

It's on the list.  There is no need to contentless pings.

-- 
Lee Jones [李琼斯]