leds: class: improve led_remove_lookup()
v2 cover leds: class: improve led_remove_lookup()
This mini-series targets led_remove_lookup() to be more handy for the optional resources. The first patch makes it no-op for NULL pointer and the second one makes it idempotent.
Changelog v2:
- added patch 2
Andy Shevchenko (2):
leds: class: Make led_remove_lookup() NULL-aware
leds: class: Reinitialise list after dropping from lookup table
drivers/leds/led-class.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
2.50.1
PATCH v2 1/2 leds: class: Make led_remove_lookup() NULL-aware
It is a usual pattern in the kernel to make releasing functions be NULL-aware so they become a no-op. This helps reducing unneeded checks in the code where the given resource is optional.
--- drivers/leds/led-class.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/leds/led-class.c +3 -0
@@ -421,6 +421,9 @@ EXPORT_SYMBOL_GPL(led_add_lookup);*/ void led_remove_lookup (struct led_lookup_data *led_lookup) { +if (!led_lookup) +return ; +mutex_lock (&leds_lookup_lock);list_del (&led_lookup->list);mutex_unlock (&leds_lookup_lock);
Signature · 1 line
2.50.1
PATCH v2 2/2 leds: class: Reinitialise list after dropping from lookup table
In reply to Andy ShevchenkoCurrently the lookup table just removes the list entry and leaves the stale pointers in it. If the lookup is embedded in some data structure, the pointer to the lookup entry can't be NULL (always valid), but calling led_remove_lookup() on it twice will lead to the wrong behaviour. To avoid that the user has to track the state itself. With this change in place, the user may drop that approach and use something like
probe:
INIT_LIST_HEAD(&lookup.list); if (LED lookup is required) led_add_lookup(&lookup);
remove:
led_remove_lookup(&lookup);
without any additional tracking kept over the device lifetime.
--- drivers/leds/led-class.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/led-class.c +1 -1
@@ -425,7 +425,7 @@ void led_remove_lookup(struct led_lookup_data *led_lookup)return ;mutex_lock (&leds_lookup_lock); -list_del (&led_lookup->list); +list_del_init (&led_lookup->list);mutex_unlock (&leds_lookup_lock); }EXPORT_SYMBOL_GPL (led_remove_lookup);
Signature · 1 line
2.50.1
Lee Jones
maintainerRe: [PATCH v2 2/2] leds: class: Reinitialise list after dropping from lookup table
Quoted 8 lines · Andy Shevchenko
Currently the lookup table just removes the list entry and leaves the stale pointers in it. If the lookup is embedded in some data structure, the pointer to the lookup entry can't be NULL (always valid), but calling
led_remove_lookup()on it twice will lead to the wrong behaviour. To avoid that the user has to track the state itself. With this change in place, the user may drop that approach and use something like
Quoted 7 lines
probe:
INIT_LIST_HEAD(&lookup.list); if (LED lookup is required) led_add_lookup(&lookup);remove:
led_remove_lookup(&lookup);
How do we feel about a devm_led_add_lookup()?
Quoted 23 lines
without any additional tracking kept over the device lifetime.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/leds/led-class.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/leds/led-class.c +1
-1@@ -425,7 +425,7 @@ void led_remove_lookup(struct led_lookup_data *led_lookup)return ;mutex_lock (&leds_lookup_lock); -list_del (&led_lookup->list); +list_del_init (&led_lookup->list);mutex_unlock (&leds_lookup_lock); }EXPORT_SYMBOL_GPL (led_remove_lookup);Signature · 1 line
2.50.1
Signature · 1 line
Lee Jones [李琼斯]
Re: [PATCH v2 2/2] leds: class: Reinitialise list after dropping from lookup table
Quoted 15 lines · Lee Jones
Quoted 13 lines · Andy Shevchenko
Currently the lookup table just removes the list entry and leaves the stale pointers in it. If the lookup is embedded in some data structure, the pointer to the lookup entry can't be NULL (always valid), but calling
led_remove_lookup()on it twice will lead to the wrong behaviour. To avoid that the user has to track the state itself. With this change in place, the user may drop that approach and use something likeprobe:
INIT_LIST_HEAD(&lookup.list); if (LED lookup is required) led_add_lookup(&lookup);
Quoted 4 lines
Quoted 2 lines
remove:
led_remove_lookup(&lookup);How do we feel about a
devm_led_add_lookup()?
No strong opinion. If we have that already, great! This patch won't be needed. If not yet, we may add one, but briefly looking at the kernel I have not found any other framework doing that.
In any case the user that would need this patch has been already modified the way that lookup is always there, effectively means we may postpone or even drop this patch for now.
Quoted 1 line
Quoted 1 line
without any additional tracking kept over the device lifetime.
Signature · 2 lines
With Best Regards, Andy Shevchenko