leds: trigger: Call synchronize_rcu() before calling trig->activate()
authorHans de Goede <hdegoede@redhat.com>
Fri, 31 May 2024 12:01:24 +0000 (14:01 +0200)
committerLee Jones <lee@kernel.org>
Fri, 21 Jun 2024 10:57:10 +0000 (11:57 +0100)
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 LED's 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.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Link: https://lore.kernel.org/r/20240531120124.75662-1-hdegoede@redhat.com
Signed-off-by: Lee Jones <lee@kernel.org>
drivers/leds/led-triggers.c

index 4d1a506a772c5e38fdc8b00c27901051b034461f..59deadb8633589b32631d1deb8ae88988d75bbb1 100644 (file)
@@ -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();
+
                ret = 0;
                if (trig->activate)
                        ret = trig->activate(led_cdev);