leds: ledtrig-timer trigger_data allocation fix
PATCH 1/1 leds: ledtrig-timer trigger_data allocation fix
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.
Quoted 1 line
From
9f7a6fc40ce7322145b6adea8771c8d547c151ecMon Sep 17 00:00:00 2001
From: Shuah Khan <[email protected]>
Date: Tue, 17 Apr 2012 16:04:56 -0600
Subject: [PATCH] leds: ledtrig-timer trigger_data allocation fix
--- drivers/leds/ledtrig-timer.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
drivers/leds/ledtrig-timer.c +7 -1
@@ -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 */
Signature · 1 line
1.7.5.4
Quoted 5 lines · On Tue, 17 Apr 2012 17:05:11 -0600 Shuah Khan <[email protected]> wrote:
ledtrig-timer doesn't allocate memory for
trigger_dataand assigns (void *)1 to save state. Fixed it to allocate int instead. Please note that non-nulltrigger_datais 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.
Review
Quoted 2 lines
(quoted diff) +1
-1- 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:
drivers/leds/ledtrig-timer.c renamed from drivers/leds/ledtrig-timer.c~leds-ledtrig-timer-trigger_data-allocation-fix-fix +5 -0
@@ -89,6 +89,11 @@ static void timer_trig_activate(struct lled_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;
_
PATCH 1/1 leds: add new field to led_classdev struct to save activation state
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.
Quoted 1 line
From
3765101a5ffe32edd68a71bafca2d6d262cf2399Mon Sep 17 00:00:00 2001
From: Shuah Khan <[email protected]>
Date: Thu, 19 Apr 2012 20:44:15 -0600
Subject: [PATCH] leds: add new field to led_classdev struct to save
activation state
--- include/linux/leds.h | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
include/linux/leds.h +2 -0
@@ -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 };
Signature · 1 line
1.7.5.4
PATCH 1/1 leds: change existing triggers to use activated flag
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.
Quoted 1 line
From
c0afeb736a5cb23d4212ac5900e2b514d0e75e90Mon Sep 17 00:00:00 2001
From: Shuah Khan <[email protected]>
Date: Fri, 20 Apr 2012 12:41:00 -0600
Subject: [PATCH] leds: change existing triggers to use activated flag
--- 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(-)
drivers/leds/ledtrig-backlight.c +3 -1
@@ -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; } }
drivers/leds/ledtrig-gpio.c +3 -1
@@ -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; } }
drivers/leds/ledtrig-heartbeat.c +3 -1
@@ -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; } }
Signature · 1 line
1.7.5.4
PATCH 1/1 leds: change ledtrig-timer to use activated flag
In reply to Shuah KhanChanged existing timer trigger to use activated flag to set activate successful status in activate routine and check it in deactivate routine to do cleanup.
Quoted 1 line
From
4b7baef37d25b5ba5ed9f561375157e2a717e28fMon Sep 17 00:00:00 2001
From: Shuah Khan <[email protected]>
Date: Fri, 20 Apr 2012 13:07:20 -0600
Subject: [PATCH] leds: change ledtrig-timer to use activated flag
--- drivers/leds/ledtrig-timer.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/leds/ledtrig-timer.c +3 -3
@@ -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 */
Signature · 1 line
1.7.5.4
Re: [PATCH] leds: change ledtrig-timer to use activated flag
Quoted 3 lines · On Fri, 20 Apr 2012 14:17:25 -0600 Shuah Khan <[email protected]> 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?
Re: [PATCH] leds: change ledtrig-timer to use activated flag
Quoted 11 lines · Andrew Morton
Quoted 3 lines · On Fri, 20 Apr 2012 14:17:25 -0600 Shuah Khan <[email protected]> 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_dataallocation fix" as these patches conflict, and I believe that "leds: ledtrig-timertrigger_dataallocation fix" is no longer needed?
That is correct. It is no longer needed.
-- Shuah
Quoted 6 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/