Skip to contentSkip to conversation

leds: class: improve led_remove_lookup()

PATCH In review Andy Shevchenko 2 participants 5 messages Mar 31 – Apr 9 LED

lore
Share & tools
lore
#0 lore raw

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

v1: [email protected]

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
#1 lore raw

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
index cad55e2beafa..b53ebe3a0faa 100644
@@ -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
#2 lore raw

PATCH v2 2/2 leds: class: Reinitialise list after dropping from lookup table

In reply to 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

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
index b53ebe3a0faa..424c07e0ecce 100644
@@ -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
maintainer
#3 lore raw

Re: [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.

---
 drivers/leds/led-class.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/led-class.c +1 -1
index b53ebe3a0faa..424c07e0ecce 100644
@@ -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 [李琼斯]
#4 lore raw

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 like

probe:

	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