Skip to contentSkip to conversation

leds: trigger: Call synchronize_rcu() before calling trig->activate()

PATCH Applied Hans de Goede 2 participants 3 messages May 7, 2024 – May 31, 2024 LED

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: trigger: Call synchronize_rcu() before calling trig->activate()

Some triggers call led_trigger_event() from their activate() callback to initialize the brightness of the LED for which the trigger is being activated.

In order for the LEDs initial state to be set correctly this requires that the led_trigger_event() call uses the new version of trigger->led_cdevs, which has the new LED.

AFAICT led_trigger_event() will always use the new version when it is running on the same CPU as where the list_add_tail_rcu() call was made, which is why the missing synchronize_rcu() has not lead to bug reports. But if activate() is pre-empted, sleeps or uses a worker then the led_trigger_event() call may run on another CPU which may still use the old trigger->led_cdevs list.

Add a synchronize_rcu() call to ensure that any led_trigger_event() calls done from activate() always use the new list.

Triggers using led_trigger_event() from their activate() callback are:
net/bluetooth/leds.c, net/rfkill/core.c and drivers/tty/vt/keyboard.c.

---
 drivers/leds/led-triggers.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/leds/led-triggers.c +7 -0
index 6d535a7fd075..018fe1eed4db 100644
@@ -194,6 +194,13 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
 		spin_unlock(&trig->leddev_list_lock);
 		led_cdev->trigger = trig;
 
+		/*
+		 * Some activate() calls use led_trigger_event() to initialize
+		 * the brightness of the LED for which the trigger is being set.
+		 * Ensure the led_cdev is visible on trig->led_cdevs for this.
+		 */
+		synchronize_rcu();
+
 		if (trig->activate)
 			ret = trig->activate(led_cdev);
 		else
Signature · 1 line
2.44.0
maintainer
#1 lore raw
Quoted 18 lines · Hans de Goede

Some triggers call led_trigger_event() from their activate() callback to initialize the brightness of the LED for which the trigger is being activated.

In order for the LEDs initial state to be set correctly this requires that the led_trigger_event() call uses the new version of trigger->led_cdevs, which has the new LED.

AFAICT led_trigger_event() will always use the new version when it is running on the same CPU as where the list_add_tail_rcu() call was made, which is why the missing synchronize_rcu() has not lead to bug reports. But if activate() is pre-empted, sleeps or uses a worker then the led_trigger_event() call may run on another CPU which may still use the old trigger->led_cdevs list.

Add a synchronize_rcu() call to ensure that any led_trigger_event() calls done from activate() always use the new list.

Quoted 7 lines

Triggers using led_trigger_event() from their activate() callback are:
net/bluetooth/leds.c, net/rfkill/core.c and drivers/tty/vt/keyboard.c.

---
 drivers/leds/led-triggers.c | 7 +++++++
 1 file changed, 7 insertions(+)

Looks like there have been a few changes to led_trigger_set() since this was authored. Please rebase and resubmit. Thanks.

Signature · 1 line
Lee Jones [李琼斯]
maintainer Applied
#2 lore raw

Re: (subset) [PATCH] leds: trigger: Call synchronize_rcu() before calling trig->activate()

In reply to Hans de Goede
Quoted 9 lines · Hans de Goede

Some triggers call led_trigger_event() from their activate() callback to initialize the brightness of the LED for which the trigger is being activated.

In order for the LEDs initial state to be set correctly this requires that the led_trigger_event() call uses the new version of trigger->led_cdevs, which has the new LED.

[...]

Applied, thanks!

[1/1] leds: trigger: Call synchronize_rcu() before calling trig->activate()

      commit: 47b8a4059f8a8b2407ab22fad8775842c54e59c6

--
Lee Jones [李琼斯]