All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] leds: ledtrig-timer trigger_data allocation fix
@ 2012-04-17 23:05 Shuah Khan
  2012-04-17 23:12 ` Andrew Morton
  0 siblings, 1 reply; 7+ messages in thread
From: Shuah Khan @ 2012-04-17 23:05 UTC (permalink / raw)
  To: Andrew Morton; +Cc: shuahkhan, LKML, Richard Purdie

ledtrig-timer doesn't allocate memory for trigger_data and assigns
(void *)1 to save state. Fixed it to allocate int instead. Please 
note that non-null trigger_data is used to key off of to do proper
cleanup in deactivation routine and not used during the life of the
trigger itself.

>From 9f7a6fc40ce7322145b6adea8771c8d547c151ec Mon Sep 17 00:00:00 2001
From: Shuah Khan <shuahkhan@gmail.com>
Date: Tue, 17 Apr 2012 16:04:56 -0600
Subject: [PATCH] leds: ledtrig-timer trigger_data allocation fix


Signed-off-by: Shuah Khan <shuahkhan@gmail.com>
---
 drivers/leds/ledtrig-timer.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/drivers/leds/ledtrig-timer.c b/drivers/leds/ledtrig-timer.c
index b32d5ea..64b6915 100644
--- a/drivers/leds/ledtrig-timer.c
+++ b/drivers/leds/ledtrig-timer.c
@@ -16,6 +16,7 @@
 #include <linux/init.h>
 #include <linux/device.h>
 #include <linux/ctype.h>
+#include <linux/slab.h>
 #include <linux/leds.h>
 #include "leds.h"
 
@@ -88,10 +89,14 @@ static void timer_trig_activate(struct led_classdev *led_cdev)
 	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
 		      &led_cdev->blink_delay_off);
 
-	led_cdev->trigger_data = (void *)1;
+	led_cdev->trigger_data = kzalloc(sizeof(int), GFP_KERNEL);
+	if (!led_cdev->trigger_data)
+		goto err_out_delayoff;
 
 	return;
 
+err_out_delayoff:
+	device_remove_file(led_cdev->dev, &dev_attr_delay_off);
 err_out_delayon:
 	device_remove_file(led_cdev->dev, &dev_attr_delay_on);
 }
@@ -101,6 +106,7 @@ static void timer_trig_deactivate(struct led_classdev *led_cdev)
 	if (led_cdev->trigger_data) {
 		device_remove_file(led_cdev->dev, &dev_attr_delay_on);
 		device_remove_file(led_cdev->dev, &dev_attr_delay_off);
+		kfree(led_cdev->trigger_data);
 	}
 
 	/* Stop blinking */
-- 
1.7.5.4






^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] leds: ledtrig-timer trigger_data allocation fix
  2012-04-17 23:05 [PATCH] leds: ledtrig-timer trigger_data allocation fix Shuah Khan
@ 2012-04-17 23:12 ` Andrew Morton
  2012-04-20  3:48   ` [PATCH] leds: add new field to led_classdev struct to save activation state Shuah Khan
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2012-04-17 23:12 UTC (permalink / raw)
  To: shuahkhan; +Cc: LKML, Richard Purdie

On Tue, 17 Apr 2012 17:05:11 -0600
Shuah Khan <shuahkhan@gmail.com> wrote:

> ledtrig-timer doesn't allocate memory for trigger_data and assigns
> (void *)1 to save state. Fixed it to allocate int instead. Please 
> note that non-null trigger_data is used to key off of to do proper
> cleanup in deactivation routine and not used during the life of the
> trigger itself.

In that case, overloading the value of ->trigger_data in this fashion
is inappropriate and a new field should be added to led_classdev for
this purpose.

> -	led_cdev->trigger_data = (void *)1;
> +	led_cdev->trigger_data = kzalloc(sizeof(int), GFP_KERNEL);

But that's better than what we have now.

It does require a comment though:

--- a/drivers/leds/ledtrig-timer.c~leds-ledtrig-timer-trigger_data-allocation-fix-fix
+++ a/drivers/leds/ledtrig-timer.c
@@ -89,6 +89,11 @@ static void timer_trig_activate(struct l
 	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
 		      &led_cdev->blink_delay_off);
 
+	/*
+	 * Place a dummy allocation at ->trigger_data so that
+	 * timer_trig_deactivate() cleans up correctly.  This data is never
+	 * actually used.
+	 */
 	led_cdev->trigger_data = kzalloc(sizeof(int), GFP_KERNEL);
 	if (!led_cdev->trigger_data)
 		goto err_out_delayoff;
_


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] leds: add new field to led_classdev struct to save activation state
  2012-04-17 23:12 ` Andrew Morton
@ 2012-04-20  3:48   ` Shuah Khan
  2012-04-20 20:14     ` [PATCH ] leds: change existing triggers to use activated flag Shuah Khan
  2012-04-20 20:17     ` [PATCH] leds: change ledtrig-timer " Shuah Khan
  0 siblings, 2 replies; 7+ messages in thread
From: Shuah Khan @ 2012-04-20  3:48 UTC (permalink / raw)
  To: Andrew Morton; +Cc: shuahkhan, LKML, Richard Purdie

This patch adds a new field to led_classdev to save activattion state
after activate routine is successful. This saved state is used in 
deactivate routine to do cleanup such as removing device files, and
free memory allocated during activation. Currently trigger_data not being
null is used for this purpose.

Existing triggers will need changes to use this new field.

>From 3765101a5ffe32edd68a71bafca2d6d262cf2399 Mon Sep 17 00:00:00 2001
From: Shuah Khan <shuahkhan@gmail.com>
Date: Thu, 19 Apr 2012 20:44:15 -0600
Subject: [PATCH] leds: add new field to led_classdev struct to save
 activation state


Signed-off-by: Shuah Khan <shuahkhan@gmail.com>
---
 include/linux/leds.h |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/include/linux/leds.h b/include/linux/leds.h
index 5884def..39eee41 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -73,6 +73,8 @@ struct led_classdev {
 	struct led_trigger	*trigger;
 	struct list_head	 trig_list;
 	void			*trigger_data;
+	/* true if activated - deactivate routine uses it to do cleanup */
+	bool			activated;
 #endif
 };
 
-- 
1.7.5.4




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH ] leds: change existing triggers to use activated flag
  2012-04-20  3:48   ` [PATCH] leds: add new field to led_classdev struct to save activation state Shuah Khan
@ 2012-04-20 20:14     ` Shuah Khan
  2012-04-20 20:17     ` [PATCH] leds: change ledtrig-timer " Shuah Khan
  1 sibling, 0 replies; 7+ messages in thread
From: Shuah Khan @ 2012-04-20 20:14 UTC (permalink / raw)
  To: Andrew Morton; +Cc: shuahkhan, LKML, Richard Purdie

Changed existing triggers backlight, gpio, and heartbeat to use 
activated flag to set activate successful status in their activate
routines and check it in their deactivate routines to do cleanup.

>From c0afeb736a5cb23d4212ac5900e2b514d0e75e90 Mon Sep 17 00:00:00 2001
From: Shuah Khan <shuahkhan@gmail.com>
Date: Fri, 20 Apr 2012 12:41:00 -0600
Subject: [PATCH] leds: change existing triggers to use activated flag


Signed-off-by: Shuah Khan <shuahkhan@gmail.com>
---
 drivers/leds/ledtrig-backlight.c |    4 +++-
 drivers/leds/ledtrig-gpio.c      |    4 +++-
 drivers/leds/ledtrig-heartbeat.c |    4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/leds/ledtrig-backlight.c b/drivers/leds/ledtrig-backlight.c
index 2b513a2..e272686 100644
--- a/drivers/leds/ledtrig-backlight.c
+++ b/drivers/leds/ledtrig-backlight.c
@@ -120,6 +120,7 @@ static void bl_trig_activate(struct led_classdev *led)
 	ret = fb_register_client(&n->notifier);
 	if (ret)
 		dev_err(led->dev, "unable to register backlight trigger\n");
+	led->activated = true;
 
 	return;
 
@@ -133,10 +134,11 @@ static void bl_trig_deactivate(struct led_classdev *led)
 	struct bl_trig_notifier *n =
 		(struct bl_trig_notifier *) led->trigger_data;
 
-	if (n) {
+	if (led->activated) {
 		device_remove_file(led->dev, &dev_attr_inverted);
 		fb_unregister_client(&n->notifier);
 		kfree(n);
+		led->activated = false;
 	}
 }
 
diff --git a/drivers/leds/ledtrig-gpio.c b/drivers/leds/ledtrig-gpio.c
index ecc4bf3..f057c10 100644
--- a/drivers/leds/ledtrig-gpio.c
+++ b/drivers/leds/ledtrig-gpio.c
@@ -200,6 +200,7 @@ static void gpio_trig_activate(struct led_classdev *led)
 	gpio_data->led = led;
 	led->trigger_data = gpio_data;
 	INIT_WORK(&gpio_data->work, gpio_trig_work);
+	led->activated = true;
 
 	return;
 
@@ -217,7 +218,7 @@ static void gpio_trig_deactivate(struct led_classdev *led)
 {
 	struct gpio_trig_data *gpio_data = led->trigger_data;
 
-	if (gpio_data) {
+	if (led->activated) {
 		device_remove_file(led->dev, &dev_attr_gpio);
 		device_remove_file(led->dev, &dev_attr_inverted);
 		device_remove_file(led->dev, &dev_attr_desired_brightness);
@@ -225,6 +226,7 @@ static void gpio_trig_deactivate(struct led_classdev *led)
 		if (gpio_data->gpio != 0)
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
 		kfree(gpio_data);
+		led->activated = false;
 	}
 }
 
diff --git a/drivers/leds/ledtrig-heartbeat.c b/drivers/leds/ledtrig-heartbeat.c
index 759c0bb..1aacf4c 100644
--- a/drivers/leds/ledtrig-heartbeat.c
+++ b/drivers/leds/ledtrig-heartbeat.c
@@ -83,15 +83,17 @@ static void heartbeat_trig_activate(struct led_classdev *led_cdev)
 		    led_heartbeat_function, (unsigned long) led_cdev);
 	heartbeat_data->phase = 0;
 	led_heartbeat_function(heartbeat_data->timer.data);
+	led_cdev->activated = true;
 }
 
 static void heartbeat_trig_deactivate(struct led_classdev *led_cdev)
 {
 	struct heartbeat_trig_data *heartbeat_data = led_cdev->trigger_data;
 
-	if (heartbeat_data) {
+	if (led_cdev->activated) {
 		del_timer_sync(&heartbeat_data->timer);
 		kfree(heartbeat_data);
+		led_cdev->activated = false;
 	}
 }
 
-- 
1.7.5.4




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH] leds: change ledtrig-timer to use activated flag
  2012-04-20  3:48   ` [PATCH] leds: add new field to led_classdev struct to save activation state Shuah Khan
  2012-04-20 20:14     ` [PATCH ] leds: change existing triggers to use activated flag Shuah Khan
@ 2012-04-20 20:17     ` Shuah Khan
  2012-04-20 21:03       ` Andrew Morton
  1 sibling, 1 reply; 7+ messages in thread
From: Shuah Khan @ 2012-04-20 20:17 UTC (permalink / raw)
  To: Andrew Morton; +Cc: shuahkhan, LKML, Richard Purdie

Changed existing timer trigger to use activated flag to set activate 
successful status in activate routine and check it in deactivate 
routine to do cleanup.

>From 4b7baef37d25b5ba5ed9f561375157e2a717e28f Mon Sep 17 00:00:00 2001
From: Shuah Khan <shuahkhan@gmail.com>
Date: Fri, 20 Apr 2012 13:07:20 -0600
Subject: [PATCH] leds: change ledtrig-timer to use activated flag


Signed-off-by: Shuah Khan <shuahkhan@gmail.com>
---
 drivers/leds/ledtrig-timer.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/leds/ledtrig-timer.c b/drivers/leds/ledtrig-timer.c
index 328c64c..e20d1db 100644
--- a/drivers/leds/ledtrig-timer.c
+++ b/drivers/leds/ledtrig-timer.c
@@ -95,8 +95,7 @@ static void timer_trig_activate(struct led_classdev *led_cdev)
 
 	led_blink_set(led_cdev, &led_cdev->blink_delay_on,
 		      &led_cdev->blink_delay_off);
-
-	led_cdev->trigger_data = (void *)1;
+	led_cdev->activated = true;
 
 	return;
 
@@ -106,9 +105,10 @@ err_out_delayon:
 
 static void timer_trig_deactivate(struct led_classdev *led_cdev)
 {
-	if (led_cdev->trigger_data) {
+	if (led_cdev->activated) {
 		device_remove_file(led_cdev->dev, &dev_attr_delay_on);
 		device_remove_file(led_cdev->dev, &dev_attr_delay_off);
+		led_cdev->activated = false;
 	}
 
 	/* Stop blinking */
-- 
1.7.5.4




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] leds: change ledtrig-timer to use activated flag
  2012-04-20 20:17     ` [PATCH] leds: change ledtrig-timer " Shuah Khan
@ 2012-04-20 21:03       ` Andrew Morton
  2012-04-20 21:16         ` Shuah Khan
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2012-04-20 21:03 UTC (permalink / raw)
  To: shuahkhan; +Cc: LKML, Richard Purdie

On Fri, 20 Apr 2012 14:17:25 -0600
Shuah Khan <shuahkhan@gmail.com> wrote:

> Changed existing timer trigger to use activated flag to set activate 
> successful status in activate routine and check it in deactivate 
> routine to do cleanup.

Thanks.  I dropped "leds: ledtrig-timer trigger_data allocation fix" as
these patches conflict, and I believe that "leds: ledtrig-timer
trigger_data allocation fix" is no longer needed?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] leds: change ledtrig-timer to use activated flag
  2012-04-20 21:03       ` Andrew Morton
@ 2012-04-20 21:16         ` Shuah Khan
  0 siblings, 0 replies; 7+ messages in thread
From: Shuah Khan @ 2012-04-20 21:16 UTC (permalink / raw)
  To: Andrew Morton; +Cc: shuahkhan, LKML, Richard Purdie

On Fri, 2012-04-20 at 14:03 -0700, Andrew Morton wrote:
> On Fri, 20 Apr 2012 14:17:25 -0600
> Shuah Khan <shuahkhan@gmail.com> wrote:
> 
> > Changed existing timer trigger to use activated flag to set activate 
> > successful status in activate routine and check it in deactivate 
> > routine to do cleanup.
> 
> Thanks.  I dropped "leds: ledtrig-timer trigger_data allocation fix" as
> these patches conflict, and I believe that "leds: ledtrig-timer
> trigger_data allocation fix" is no longer needed?
> 

That is correct. It is no longer needed.

-- Shuah
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
> 



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-04-20 21:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-17 23:05 [PATCH] leds: ledtrig-timer trigger_data allocation fix Shuah Khan
2012-04-17 23:12 ` Andrew Morton
2012-04-20  3:48   ` [PATCH] leds: add new field to led_classdev struct to save activation state Shuah Khan
2012-04-20 20:14     ` [PATCH ] leds: change existing triggers to use activated flag Shuah Khan
2012-04-20 20:17     ` [PATCH] leds: change ledtrig-timer " Shuah Khan
2012-04-20 21:03       ` Andrew Morton
2012-04-20 21:16         ` Shuah Khan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.