[PATCH v3 1/2] leds: triggers: Add led_trigger_for_each_led()

From: Nikolay Plastinin via B4 Relay

Date: Thu Oct 15 2026 - 15:15:38 EST


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)
+
/*
* Currently the attributes in struct led_trigger::groups are added directly to
* the LED device. As this might change in the future, the following

--
2.56.0