led: simplify led_trigger_register_simple
Subsystems (5)
PATCH 1/1 led: simplify led_trigger_register_simple
From: Felipe Balbi <[email protected]>
We can make led_trigger_register_simple by returning a struct led_trigger *, instead of passing a struct led_trigger ** as a parameter and changing it inside the function.
---
Note that I could only test ide, power_supply and mmc changes with my laptop. The other changes weren't tested.
drivers/leds/led-triggers.c | 25 ++++++++++++++--------- drivers/leds/ledtrig-ide-disk.c | 6 ++++- drivers/mmc/core/host.c | 4 ++- drivers/mtd/nand/nand_base.c | 5 +++- drivers/power/power_supply_leds.c | 36 ++++++++++++++++++++++++++++------ drivers/staging/at76_usb/at76_usb.c | 6 ++++- include/linux/leds.h | 5 +-- 7 files changed, 63 insertions(+), 24 deletions(-)
drivers/leds/led-triggers.c +15 -10
@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/kernel.h> +#include <linux/err.h> #include <linux/init.h> #include <linux/list.h> #include <linux/spinlock.h>
@@ -222,24 +223,28 @@ void led_trigger_event(struct led_trigger *trigger, }EXPORT_SYMBOL_GPL (led_trigger_event); -void led_trigger_register_simple (const char *name,struct led_trigger **tp) +struct led_trigger *led_trigger_register_simple (const char *name) {struct led_trigger *trigger;int err; trigger =kzalloc (sizeof (struct led_trigger ),GFP_KERNEL ); - -if (trigger) { - trigger->name = name; - err =led_trigger_register (trigger); -if (err <0 ) -printk (KERN_WARNING "LED trigger %s failed to register" -" (%d)\n" , name, err); - }else +if (!trigger) {printk (KERN_WARNING "LED trigger %s failed to register" " (no memory)\n" , name); +return ERR_PTR (-ENOMEM ); + } + + trigger->name = name; + err =led_trigger_register (trigger); +if (err <0 ) { +printk (KERN_WARNING "LED trigger %s failed to register" +" (%d)\n" , name, err); +kfree (trigger); +return ERR_PTR (err); + } - *tp = trigger; +return trigger; }EXPORT_SYMBOL_GPL (led_trigger_register_simple);
drivers/leds/ledtrig-ide-disk.c +5 -1
@@ -14,6 +14,7 @@#include <linux/module.h> #include <linux/jiffies.h> #include <linux/kernel.h> +#include <linux/err.h> #include <linux/init.h> #include <linux/timer.h> #include <linux/leds.h>
@@ -46,7 +47,10 @@ static void ledtrig_ide_timerfunc(unsigned long data)static int __init ledtrig_ide_init (void ) { -led_trigger_register_simple ("ide-disk" , &ledtrig_ide); + ledtrig_ide =led_trigger_register_simple ("ide-disk" ); +if (IS_ERR (ledtrig_ide)) +return PTR_ERR (ledtrig_ide); +return 0 ; }
drivers/mmc/core/host.c +3 -1
@@ -120,7 +120,9 @@ int mmc_add_host(struct mmc_host *host)WARN_ON ((host->caps &MMC_CAP_SDIO_IRQ ) && !host->ops->enable_sdio_irq); -led_trigger_register_simple (dev_name (&host->class_dev), &host->led); + host->led =led_trigger_register_simple (dev_name (&host->class_dev)); +if (IS_ERR (host->led)) +return PTR_ERR (host->led); err =device_add (&host->class_dev);if (err)
drivers/mtd/nand/nand_base.c +4 -1
@@ -2781,7 +2781,10 @@ EXPORT_SYMBOL_GPL(nand_release);static int __init nand_base_init (void ) { -led_trigger_register_simple ("nand-disk" , &nand_led_trigger); + nand_led_trigger =led_trigger_register_simple ("nand-disk" ); +if (IS_ERR (nand_led_trigger)) +return PTR_ERR (nand_led_trigger); +return 0 ; }
drivers/power/power_supply_leds.c +29 -7
@@ -11,6 +11,7 @@*/ #include <linux/kernel.h> +#include <linux/err.h> #include <linux/power_supply.h> #include "power_supply.h"
@@ -63,15 +64,32 @@ static int power_supply_create_bat_triggers(struct power_supply *psy)if (!psy->full_trig_name) goto full_failed; -led_trigger_register_simple (psy->charging_full_trig_name, - &psy->charging_full_trig); -led_trigger_register_simple (psy->charging_trig_name, - &psy->charging_trig); -led_trigger_register_simple (psy->full_trig_name, - &psy->full_trig); + psy->charging_full_trig =led_trigger_register_simple ( + psy->charging_full_trig_name); +if (IS_ERR (psy->charging_full_trig)) { + rc =PTR_ERR (psy->charging_full_trig); + goto full_failed; + } + + psy->charging_trig =led_trigger_register_simple ( + psy->charging_trig_name); +if (IS_ERR (psy->charging_trig)) { + rc =PTR_ERR (psy->charging_full_trig); + goto charging_trig_failed; + } + + psy->full_trig =led_trigger_register_simple (psy->full_trig_name); +if (IS_ERR (psy->full_trig)) { + rc =PTR_ERR (psy->charging_full_trig); + goto full_trig_failed; + } goto success; +full_trig_failed: +led_trigger_unregister_simple (psy->charging_trig); +charging_trig_failed: +led_trigger_unregister_simple (psy->charging_full_trig); full_failed:kfree (psy->charging_trig_name); charging_failed:
@@ -117,7 +135,11 @@ static int power_supply_create_gen_triggers(struct power_supply *psy)if (!psy->online_trig_name) goto online_failed; -led_trigger_register_simple (psy->online_trig_name, &psy->online_trig); + psy->online_trig =led_trigger_register_simple (psy->online_trig_name); +if (IS_ERR (psy->online_trig)) { + rc =PTR_ERR (psy->online_trig); + goto online_failed; + } goto success;
drivers/staging/at76_usb/at76_usb.c +5 -1
@@ -21,6 +21,7 @@#include <linux/init.h> #include <linux/kernel.h> #include <linux/sched.h> +#include <linux/err.h> #include <linux/errno.h> #include <linux/slab.h> #include <linux/module.h>
@@ -5528,7 +5529,10 @@ static int __init at76_mod_init(void) printk(KERN_ERR DRIVER_NAME ": usb_register failed (status %d)\n", result); -led_trigger_register_simple ("at76_usb-tx" , &ledtrig_tx); + ledtrig_tx =led_trigger_register_simple ("at76_usb-tx" ); +if (IS_ERR (ledtrig_tx)) + result =PTR_ERR (ledtrig_tx); +return result; }
include/linux/leds.h +2 -3
@@ -94,8 +94,7 @@ extern void led_trigger_unregister(struct led_trigger *trigger);/* Registration functions for simple triggers */ #define DEFINE_LED_TRIGGER (x) static struct led_trigger *x;#define DEFINE_LED_TRIGGER_GLOBAL (x) struct led_trigger *x; -extern void led_trigger_register_simple (const char *name, -struct led_trigger **trigger); +extern struct led_trigger *led_trigger_register_simple (const char *name);extern void led_trigger_unregister_simple (struct led_trigger *trigger);extern void led_trigger_event (struct led_trigger *trigger,enum led_brightness event);
@@ -105,7 +104,7 @@ extern void led_trigger_event(struct led_trigger *trigger,/* Triggers aren't active - null macros */ #define DEFINE_LED_TRIGGER (x)#define DEFINE_LED_TRIGGER_GLOBAL (x) -#define led_trigger_register_simple (x, y) do {} while(0) +#define led_trigger_register_simple (x) NULL#define led_trigger_unregister_simple (x) do {} while(0)#define led_trigger_event (x, y) do {} while(0)
Signature · 1 line
1.6.0.4.617.g2baf1
Quoted 16 lines · Felipe Balbi
From: Felipe Balbi <[email protected]>
We can make
led_trigger_register_simpleby returning a structled_trigger*, instead of passing a structled_trigger** as a parameter and changing it inside the function.Cc Anton Vorontsov <[email protected]>Cc David Woodhouse <[email protected]>Cc Greg KH <[email protected]>Cc Pierre Ossman <[email protected]>Cc Richard Purdie <[email protected]>Signed-off-by Felipe Balbi <[email protected]>---
Note that I could only test ide,power_supplyand mmc changes with my laptop. The other changes weren't tested.
Quoted 5 lines
drivers/leds/led-triggers.c | 25 ++++++++++++++--------- drivers/leds/ledtrig-ide-disk.c | 6 ++++- drivers/mmc/core/host.c | 4 ++- drivers/mtd/nand/nand_base.c | 5 +++- drivers/power/power_supply_leds.c | 36 ++++++++++++++++++++++++++++------
For power supply bits:
Thanks.
Quoted 5 lines · Felipe Balbi
From: Felipe Balbi <[email protected]>
We can make
led_trigger_register_simpleby returning a structled_trigger*, instead of passing a structled_trigger** as a parameter and changing it inside the function.
This misses the whole point that it was intentionally written that way.
Quoted 11 lines
Note that I could only test ide,
power_supplyand mmc changes with my laptop. The other changes weren't tested.drivers/leds/led-triggers.c | 25 ++++++++++++++--------- drivers/leds/ledtrig-ide-disk.c | 6 ++++- drivers/mmc/core/host.c | 4 ++- drivers/mtd/nand/nand_base.c | 5 +++- drivers/power/power_supply_leds.c | 36 ++++++++++++++++++++++++++++------ drivers/staging/at76_usb/at76_usb.c | 6 ++++- include/linux/leds.h | 5 +-- 7 files changed, 63 insertions(+), 24 deletions(-)
The simple triggers were designed to cause minimum interference to the usually external subsystem code they were added into. As an example this meant things like errors were just handled gracefully with a printk warning and did not take down the whole subsystem. I therefore don't regard this patch as a simplification, more a complication.
Regards,
Richard
Quoted 5 lines · Richard Purdie
The simple triggers were designed to cause minimum interference to the usually external subsystem code they were added into. As an example this meant things like errors were just handled gracefully with a printk warning and did not take down the whole subsystem. I therefore don't regard this patch as a simplification, more a complication.
That's a matter of changing the return ERR_PTR(err); back to a printk.
Signature · 1 line
balbi
Quoted 8 lines · Felipe Balbi
Quoted 5 lines · Richard Purdie
The simple triggers were designed to cause minimum interference to the usually external subsystem code they were added into. As an example this meant things like errors were just handled gracefully with a printk warning and did not take down the whole subsystem. I therefore don't regard this patch as a simplification, more a complication.
That's a matter of changing the return
ERR_PTR(err); back to a printk.
And here you are. I still think we should at least kfree(trigger) in case of error, though.
======= cut here ===========
Quoted 1 line
From
4a96a53109f1edc3277ffb2d22641330930e60cdMon Sep 17 00:00:00 2001
From: Felipe Balbi <[email protected]>
Date: Thu, 13 Nov 2008 03:28:22 +0200
Subject: [PATCH] led: simplify led_trigger_register_simple
We can make led_trigger_register_simple by returning a struct led_trigger *, instead of passing a struct led_trigger ** as a parameter and changing it inside the function.
--- drivers/leds/led-triggers.c | 4 ++-- drivers/leds/ledtrig-ide-disk.c | 2 +- drivers/mmc/core/host.c | 2 +- drivers/mtd/nand/nand_base.c | 2 +- drivers/power/power_supply_leds.c | 13 ++++++------- drivers/staging/at76_usb/at76_usb.c | 2 +- include/linux/leds.h | 5 ++--- 7 files changed, 14 insertions(+), 16 deletions(-)
drivers/leds/led-triggers.c +2 -2
@@ -222,7 +222,7 @@ void led_trigger_event(struct led_trigger *trigger, }EXPORT_SYMBOL_GPL (led_trigger_event); -void led_trigger_register_simple (const char *name,struct led_trigger **tp) +struct led_trigger *led_trigger_register_simple (const char *name) {struct led_trigger *trigger;int err;
@@ -239,7 +239,7 @@ void led_trigger_register_simple(const char *name, struct led_trigger **tp)printk (KERN_WARNING "LED trigger %s failed to register" " (no memory)\n" , name); - *tp = trigger; +return trigger; }EXPORT_SYMBOL_GPL (led_trigger_register_simple);
drivers/leds/ledtrig-ide-disk.c +1 -1
@@ -46,7 +46,7 @@ static void ledtrig_ide_timerfunc(unsigned long data)static int __init ledtrig_ide_init (void ) { -led_trigger_register_simple ("ide-disk" , &ledtrig_ide); + ledtrig_ide =led_trigger_register_simple ("ide-disk" );return 0 ; }
drivers/mmc/core/host.c +1 -1
@@ -120,7 +120,7 @@ int mmc_add_host(struct mmc_host *host)WARN_ON ((host->caps &MMC_CAP_SDIO_IRQ ) && !host->ops->enable_sdio_irq); -led_trigger_register_simple (dev_name (&host->class_dev), &host->led); + host->led =led_trigger_register_simple (dev_name (&host->class_dev)); err =device_add (&host->class_dev);if (err)
drivers/mtd/nand/nand_base.c +1 -1
@@ -2781,7 +2781,7 @@ EXPORT_SYMBOL_GPL(nand_release);static int __init nand_base_init (void ) { -led_trigger_register_simple ("nand-disk" , &nand_led_trigger); + nand_led_trigger =led_trigger_register_simple ("nand-disk" );return 0 ; }
drivers/power/power_supply_leds.c +6 -7
@@ -63,12 +63,11 @@ static int power_supply_create_bat_triggers(struct power_supply *psy)if (!psy->full_trig_name) goto full_failed; -led_trigger_register_simple (psy->charging_full_trig_name, - &psy->charging_full_trig); -led_trigger_register_simple (psy->charging_trig_name, - &psy->charging_trig); -led_trigger_register_simple (psy->full_trig_name, - &psy->full_trig); + psy->charging_full_trig =led_trigger_register_simple ( + psy->charging_full_trig_name); + psy->charging_trig =led_trigger_register_simple ( + psy->charging_trig_name); + psy->full_trig =led_trigger_register_simple (psy->full_trig_name); goto success;
@@ -117,7 +116,7 @@ static int power_supply_create_gen_triggers(struct power_supply *psy)if (!psy->online_trig_name) goto online_failed; -led_trigger_register_simple (psy->online_trig_name, &psy->online_trig); + psy->online_trig =led_trigger_register_simple (psy->online_trig_name); goto success;
drivers/staging/at76_usb/at76_usb.c +1 -1
@@ -5528,7 +5528,7 @@ static int __init at76_mod_init(void) printk(KERN_ERR DRIVER_NAME ": usb_register failed (status %d)\n", result); -led_trigger_register_simple ("at76_usb-tx" , &ledtrig_tx); + ledtrig_tx =led_trigger_register_simple ("at76_usb-tx" );return result; }
include/linux/leds.h +2 -3
@@ -94,8 +94,7 @@ extern void led_trigger_unregister(struct led_trigger *trigger);/* Registration functions for simple triggers */ #define DEFINE_LED_TRIGGER (x) static struct led_trigger *x;#define DEFINE_LED_TRIGGER_GLOBAL (x) struct led_trigger *x; -extern void led_trigger_register_simple (const char *name, -struct led_trigger **trigger); +extern struct led_trigger *led_trigger_register_simple (const char *name);extern void led_trigger_unregister_simple (struct led_trigger *trigger);extern void led_trigger_event (struct led_trigger *trigger,enum led_brightness event);
@@ -105,7 +104,7 @@ extern void led_trigger_event(struct led_trigger *trigger,/* Triggers aren't active - null macros */ #define DEFINE_LED_TRIGGER (x)#define DEFINE_LED_TRIGGER_GLOBAL (x) -#define led_trigger_register_simple (x, y) do {} while(0) +#define led_trigger_register_simple (x) NULL#define led_trigger_unregister_simple (x) do {} while(0)#define led_trigger_event (x, y) do {} while(0)
Signature · 4 lines
1.6.0.4.617.g2baf1 -- balbi
Quoted 8 lines · ext Felipe Balbi
Quoted 7 lines · Felipe Balbi
Quoted 5 lines · Richard Purdie
The simple triggers were designed to cause minimum interference to the usually external subsystem code they were added into. As an example this meant things like errors were just handled gracefully with a printk warning and did not take down the whole subsystem. I therefore don't regard this patch as a simplification, more a complication.
Quoted 4 lines
Quoted 1 line
That's a matter of changing the return
ERR_PTR(err); back to a printk.And here you are. I still think we should at least kfree(trigger) in case of error, though.
Any comments here ??
Quoted 160 lines
======= cut here ===========
Quoted 1 line
From
4a96a53109f1edc3277ffb2d22641330930e60cdMon Sep 17 00:00:00 2001From: Felipe Balbi <[email protected]>
Date: Thu, 13 Nov 2008 03:28:22 +0200
Subject: [PATCH] led: simplifyled_trigger_register_simpleWe can make
led_trigger_register_simpleby returning a structled_trigger*, instead of passing a structled_trigger** as a parameter and changing it inside the function.Cc Anton Vorontsov <[email protected]>Cc David Woodhouse <[email protected]>Cc Greg KH <[email protected]>Cc Pierre Ossman <[email protected]>Cc Richard Purdie <[email protected]>Signed-off-by Felipe Balbi <[email protected]>--- drivers/leds/led-triggers.c | 4 ++-- drivers/leds/ledtrig-ide-disk.c | 2 +- drivers/mmc/core/host.c | 2 +- drivers/mtd/nand/nand_base.c | 2 +- drivers/power/power_supply_leds.c | 13 ++++++------- drivers/staging/at76_usb/at76_usb.c | 2 +- include/linux/leds.h | 5 ++--- 7 files changed, 14 insertions(+), 16 deletions(-)drivers/leds/led-triggers.c +2
-2@@ -222,7 +222,7 @@ void led_trigger_event(struct led_trigger *trigger, }EXPORT_SYMBOL_GPL (led_trigger_event); -void led_trigger_register_simple (const char *name,struct led_trigger **tp) +struct led_trigger *led_trigger_register_simple (const char *name) {struct led_trigger *trigger;int err;@@ -239,7 +239,7 @@ void led_trigger_register_simple(const char *name, struct led_trigger **tp)printk (KERN_WARNING "LED trigger %s failed to register" " (no memory)\n" , name); - *tp = trigger; +return trigger; }EXPORT_SYMBOL_GPL (led_trigger_register_simple);drivers/leds/ledtrig-ide-disk.c +1
-1@@ -46,7 +46,7 @@ static void ledtrig_ide_timerfunc(unsigned long data)static int __init ledtrig_ide_init (void ) { -led_trigger_register_simple ("ide-disk" , &ledtrig_ide); + ledtrig_ide =led_trigger_register_simple ("ide-disk" );return 0 ; }drivers/mmc/core/host.c +1
-1@@ -120,7 +120,7 @@ int mmc_add_host(struct mmc_host *host)WARN_ON ((host->caps &MMC_CAP_SDIO_IRQ ) && !host->ops->enable_sdio_irq); -led_trigger_register_simple (dev_name (&host->class_dev), &host->led); + host->led =led_trigger_register_simple (dev_name (&host->class_dev)); err =device_add (&host->class_dev);if (err)drivers/mtd/nand/nand_base.c +1
-1@@ -2781,7 +2781,7 @@ EXPORT_SYMBOL_GPL(nand_release);static int __init nand_base_init (void ) { -led_trigger_register_simple ("nand-disk" , &nand_led_trigger); + nand_led_trigger =led_trigger_register_simple ("nand-disk" );return 0 ; }drivers/power/power_supply_leds.c +6
-7@@ -63,12 +63,11 @@ static int power_supply_create_bat_triggers(struct power_supply *psy)if (!psy->full_trig_name) goto full_failed; -led_trigger_register_simple (psy->charging_full_trig_name, - &psy->charging_full_trig); -led_trigger_register_simple (psy->charging_trig_name, - &psy->charging_trig); -led_trigger_register_simple (psy->full_trig_name, - &psy->full_trig); + psy->charging_full_trig =led_trigger_register_simple ( + psy->charging_full_trig_name); + psy->charging_trig =led_trigger_register_simple ( + psy->charging_trig_name); + psy->full_trig =led_trigger_register_simple (psy->full_trig_name); goto success;@@ -117,7 +116,7 @@ static int power_supply_create_gen_triggers(struct power_supply *psy)if (!psy->online_trig_name) goto online_failed; -led_trigger_register_simple (psy->online_trig_name, &psy->online_trig); + psy->online_trig =led_trigger_register_simple (psy->online_trig_name); goto success;drivers/staging/at76_usb/at76_usb.c +1
-1@@ -5528,7 +5528,7 @@ static int __init at76_mod_init(void) printk(KERN_ERR DRIVER_NAME ": usb_register failed (status %d)\n", result); -led_trigger_register_simple ("at76_usb-tx" , &ledtrig_tx); + ledtrig_tx =led_trigger_register_simple ("at76_usb-tx" );return result; }include/linux/leds.h +2
-3@@ -94,8 +94,7 @@ extern void led_trigger_unregister(struct led_trigger *trigger);/* Registration functions for simple triggers */ #define DEFINE_LED_TRIGGER (x) static struct led_trigger *x;#define DEFINE_LED_TRIGGER_GLOBAL (x) struct led_trigger *x; -extern void led_trigger_register_simple (const char *name, -struct led_trigger **trigger); +extern struct led_trigger *led_trigger_register_simple (const char *name);extern void led_trigger_unregister_simple (struct led_trigger *trigger);extern void led_trigger_event (struct led_trigger *trigger,enum led_brightness event);@@ -105,7 +104,7 @@ extern void led_trigger_event(struct led_trigger *trigger,/* Triggers aren't active - null macros */ #define DEFINE_LED_TRIGGER (x)#define DEFINE_LED_TRIGGER_GLOBAL (x) -#define led_trigger_register_simple (x, y) do {} while(0) +#define led_trigger_register_simple (x) NULL#define led_trigger_unregister_simple (x) do {} while(0)#define led_trigger_event (x, y) do {} while(0)Signature · 4 lines
1.6.0.4.617.g2baf1 -- balbi
Signature · 1 line
balbi
Quoted 8 lines · Felipe Balbi
Quoted 7 lines · Felipe Balbi
Quoted 5 lines · Richard Purdie
The simple triggers were designed to cause minimum interference to the usually external subsystem code they were added into. As an example this meant things like errors were just handled gracefully with a printk warning and did not take down the whole subsystem. I therefore don't regard this patch as a simplification, more a complication.
Quoted 4 lines
Quoted 1 line
That's a matter of changing the return
ERR_PTR(err); back to a printk.And here you are. I still think we should at least kfree(trigger) in case of error, though.
This patch now just changes the calling convention of the function which doesn't seem to serve much purpose.
In answer to your question about kfree, I agree it needs to be called upon error. The callers should just be calling led_trigger_unregister_simple() in their failure paths though which should take care of all problems? I know we used to register the simple triggers late in paths so no error handling was needed to keep the code simple and minimise the LED triggers impact on those systems.
Cheers,
Richard
Quoted 9 lines · ext Richard Purdie
Quoted 8 lines · Felipe Balbi
Quoted 7 lines · Felipe Balbi
Quoted 5 lines · Richard Purdie
The simple triggers were designed to cause minimum interference to the usually external subsystem code they were added into. As an example this meant things like errors were just handled gracefully with a printk warning and did not take down the whole subsystem. I therefore don't regard this patch as a simplification, more a complication.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
That's a matter of changing the return
ERR_PTR(err); back to a printk.And here you are. I still think we should at least kfree(trigger) in case of error, though.
This patch now just changes the calling convention of the function which doesn't seem to serve much purpose.
Well, it's your call then. But I don't like the idea of declaring a pointer, passing a pointer to that pointer to a function and changing the value of the original pointer.
I think allocating inside led_trigger_register_simple() and returning the pointer would look better.
Quoted 6 lines
In answer to your question about kfree, I agree it needs to be called upon error. The callers should just be calling
led_trigger_unregister_simple()in their failure paths though which should take care of all problems? I know we used to register the simple triggers late in paths so no error handling was needed to keep the code simple and minimise the LED triggers impact on those systems.
Well, led_trigger_register_simple() doesn't return anything. Imagine led_trigger_register_simple() fails, but the driver author decides it's not a failure if, let's say, a led doesn't turn on when we insert a mmc card to the slot since it doesn't change functionality.
Now, imagine the user notes the led is not turning on and decides to unload and reload the module to try again. Once again the led doesn't go on. If the user keeps trying, it's quite a dangerous memory leak, right ?
Signature · 1 line
balbi
Quoted 12 lines · Felipe Balbi
Quoted 6 lines
In answer to your question about kfree, I agree it needs to be called upon error. The callers should just be calling
led_trigger_unregister_simple()in their failure paths though which should take care of all problems? I know we used to register the simple triggers late in paths so no error handling was needed to keep the code simple and minimise the LED triggers impact on those systems.Well,
led_trigger_register_simple()doesn't return anything. Imagineled_trigger_register_simple()fails, but the driver author decides it's not a failure if, let's say, a led doesn't turn on when we insert a mmc card to the slot since it doesn't change functionality.
Quoted 4 lines
Now, imagine the user notes the led is not turning on and decides to unload and reload the module to try again. Once again the led doesn't go on. If the user keeps trying, it's quite a dangerous memory leak, right ?
So we have the module loading and one of two things happens:
led_trigger_register_simple() succeeds led_trigger_register_simple() fails (probably from kmalloc failure)
The module doesn't know or care which happened. When the module unloads it calls led_trigger_unregister_simple() which will free the memory in the success case and do nothing in the case where it had failed.
So there is no memory leak?
Cheers,
Richard
Quoted 20 lines · ext Richard Purdie
Quoted 12 lines · Felipe Balbi
Quoted 6 lines
In answer to your question about kfree, I agree it needs to be called upon error. The callers should just be calling
led_trigger_unregister_simple()in their failure paths though which should take care of all problems? I know we used to register the simple triggers late in paths so no error handling was needed to keep the code simple and minimise the LED triggers impact on those systems.Well,
led_trigger_register_simple()doesn't return anything. Imagineled_trigger_register_simple()fails, but the driver author decides it's not a failure if, let's say, a led doesn't turn on when we insert a mmc card to the slot since it doesn't change functionality.Quoted 4 lines
Now, imagine the user notes the led is not turning on and decides to unload and reload the module to try again. Once again the led doesn't go on. If the user keeps trying, it's quite a dangerous memory leak, right ?
So we have the module loading and one of two things happens:
Quoted 8 lines
led_trigger_register_simple()succeedsled_trigger_register_simple()fails (probably from kmalloc failure)The module doesn't know or care which happened. When the module unloads it calls
led_trigger_unregister_simple()which will free the memory in the success case and do nothing in the case where it had failed.So there is no memory leak?
Hmmm, you are right. Didin't think about the exit path. But freeing in led_triger_register_simple() if led_trigger_register() fails, also doesn't seem wrong.
Signature · 1 line
balbi