leds: fix regression in usbport led trigger
Hi Christian,
Thank you for the patch.
Quoted 28 lines · Christian Lamparter
The patch "usb: simplify usbport trigger" together with "leds: triggers: add device attribute support" caused an regression for the usbport trigger. it will no longer enumerate any active usb hub ports under the "ports" directory in the sysfs class directory, if the usb host drivers are fully initialized before the usbport trigger was loaded.
The reason is that the usbport driver tries to register the sysfs entries during the
activate()callback. And this will fail with -2 / ENOENT because the patch "leds: triggers: add device attribute support" made it so that the sysfs "ports" group was only being added after theactivate()callback succeeded.This version of the patch moves the
device_add_groups()in front of the call to the trigger'sactivate()function in order to solve the problem.Cc Greg Kroah-Hartman <[email protected]>Cc Uwe Kleine-König <[email protected]>Cc Rafał Miłecki <[email protected]>Fixes 6f7b0bad8839 ("usb: simplify usbport trigger")Signed-off-by Christian Lamparter <[email protected]>---
This version of the patch ... of which will be many more?!
Quoted 11 lines
yeah,
device_add_groups()in front ofactivate()works for the usbport driver since it dynamically registers the entries in "ports". However, if a trigger has a static list of entities this can get more complicated, since I think the sysfs entries will now be available before theactivate()call even started and this can end up badly. So, is there any better approach? Introduce a "post_activate()" callback? Or use the event system and make usbport trigger on theKOBJ_CHANGE? use a (delayed)work_structin usbport to register the ports at a slightly later time? etc...
post_activate() is an option.
Otherwise, with your patch, in order to prevent access to the sysfs interface before the trigger is initialized, we would have to implement is_visible op for each struct attribute_group in the triggers with static attrs, and check led_cdev->activated there
(it is currently unused and scheduled for removal, but we would have to set it after calling activate() in LED trigger core).
It would require more effort but would allow to avoid the addition of a new op. I'd also remove "activated" property from struct led_classdev and made it a flag in leds.h:
+#define LED_TRIGGER_ACTIVATED BIT(24)
Best regards,
Jacek Anaszewski
Quoted 59 lines
(Note: I'm hitting this on 4.19 too, so whatever the real fix will look like it should be backported)
--- drivers/leds/led-triggers.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)drivers/leds/led-triggers.c +10
-9@@ -134,6 +134,12 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)led_set_brightness (led_cdev,LED_OFF ); }if (trig) { + ret =device_add_groups (led_cdev->dev, trig->groups); +if (ret) { +dev_err (led_cdev->dev,"Failed to add trigger attributes\n" ); + goto err_add_groups; + } +write_lock_irqsave (&trig->leddev_list_lock, flags);list_add_tail (&led_cdev->trig_list, &trig->led_cdevs);write_unlock_irqrestore (&trig->leddev_list_lock, flags);@@ -146,12 +152,6 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)if (ret) goto err_activate; - - ret =device_add_groups (led_cdev->dev, trig->groups); -if (ret) { -dev_err (led_cdev->dev,"Failed to add trigger attributes\n" ); - goto err_add_groups; - } }if (event) {@@ -165,17 +165,18 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)return 0 ; -err_add_groups: - +err_activate: +device_remove_groups (led_cdev->dev, trig->groups);if (trig->deactivate) trig->deactivate (led_cdev); -err_activate: led_cdev->trigger =NULL ; led_cdev->trigger_data =NULL ;write_lock_irqsave (&led_cdev->trigger->leddev_list_lock, flags);list_del (&led_cdev->trig_list);write_unlock_irqrestore (&led_cdev->trigger->leddev_list_lock, flags); + +err_add_groups:led_set_brightness (led_cdev,LED_OFF );return ret;