Re: [PATCH v4 1/2] leds: triggers: Add led_trigger_for_each_led()
From: Greg KH
Date: Sat Oct 31 2026 - 06:23:43 EST
On Fri, Oct 30, 2026 at 09:51:31PM +0800, Nikolay Plastinin via B4 Relay wrote:
> From: Nikolay Plastinin <plaztininikolai@xxxxxxxxx>
>
> led_trigger_event() sets one brightness for all LEDs using a trigger.
> A trigger which restores a different brightness per LED needs to iterate
> over the attached LEDs instead.
>
> Add led_trigger_for_each_led(), a wrapper around list_for_each_entry_rcu(),
> so triggers can perform this iteration without spelling out the list
> members. Document that callers must hold the RCU read lock throughout
> the iteration and must not sleep while accessing the LEDs.
>
> Assisted-by: LLM
> Signed-off-by: Nikolay Plastinin <plaztininikolai@xxxxxxxxx>
> ---
> include/linux/leds.h | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/include/linux/leds.h b/include/linux/leds.h
> index a515f075c..675c58661 100644
> --- a/include/linux/leds.h
> +++ b/include/linux/leds.h
> @@ -11,6 +11,7 @@
> #include <dt-bindings/leds/common.h>
> #include <linux/device.h>
> #include <linux/mutex.h>
> +#include <linux/rculist.h>
> #include <linux/rwsem.h>
> #include <linux/spinlock.h>
> #include <linux/timer.h>
> @@ -502,6 +503,17 @@ struct led_trigger {
> const struct attribute_group **groups;
> };
>
> +/**
> + * led_trigger_for_each_led - iterate over the LEDs attached to a trigger
> + * @pos: struct led_classdev pointer used as the loop cursor
> + * @trig: trigger whose LEDs are iterated
> + *
> + * The caller must hold the RCU read lock throughout the iteration and must
> + * not sleep while accessing the LEDs.
> + */
> +#define led_trigger_for_each_led(pos, trig) \
> + list_for_each_entry_rcu(pos, &(trig)->led_cdevs, trig_list)
> +
No need for this when you are only using it in one place, AND you are
always going to forget to grab the rcu lock. Just "open code" it
please, that way driver authors don't get confused and use this
accidentally in the future.
thanks,
greg k-h