Re: [PATCH 25/25] leds: convert One-shot LED trigger driver todevm_kzalloc() and cleanup error exit path

From: Fabio Baltieri
Date: Thu Jul 05 2012 - 15:48:24 EST


On Thu, Jul 05, 2012 at 10:47:19AM -0600, Shuah Khan wrote:
> On Wed, 2012-07-04 at 13:10 +0800, Bryan Wu wrote:
> > Cc: Fabio Baltieri <fabio.baltieri@xxxxxxxxx>
> > Signed-off-by: Bryan Wu <bryan.wu@xxxxxxxxxxxxx>
> > ---
> > drivers/leds/ledtrig-oneshot.c | 8 +++-----
> > 1 file changed, 3 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/leds/ledtrig-oneshot.c b/drivers/leds/ledtrig-oneshot.c
> > index 2c029aa..6395a38 100644
> > --- a/drivers/leds/ledtrig-oneshot.c
> > +++ b/drivers/leds/ledtrig-oneshot.c
> > @@ -127,7 +127,8 @@ static void oneshot_trig_activate(struct led_classdev *led_cdev)
> > struct oneshot_trig_data *oneshot_data;
> > int rc;
> >
> > - oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL);
> > + oneshot_data = devm_kzalloc(led_cdev->dev, sizeof(*oneshot_data),
> > + GFP_KERNEL);
> > if (!oneshot_data)
> > return;
> >
> > @@ -135,7 +136,7 @@ static void oneshot_trig_activate(struct led_classdev *led_cdev)
> >
> > rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
> > if (rc)
> > - goto err_out_trig_data;
> > + return;
> > rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
> > if (rc)
> > goto err_out_delayon;
> > @@ -159,8 +160,6 @@ 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);
> > -err_out_trig_data:
> > - kfree(led_cdev->trigger_data);
> > }
> >
> > static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
> > @@ -172,7 +171,6 @@ static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
> > device_remove_file(led_cdev->dev, &dev_attr_delay_off);
> > device_remove_file(led_cdev->dev, &dev_attr_invert);
> > device_remove_file(led_cdev->dev, &dev_attr_shot);
> > - kfree(oneshot_data);
> > led_cdev->activated = false;
> > }
> >
> Bryan,
>
> I don't believe memory triggers allocate in their activate routine
> should be converted to devm_kzalloc(). Based on my understanding, the
> memory allocated using devm_kzalloc() us free'ed when driver is
> detached. In the case of led triggers, driver stays registered while the
> triggers it supports can be activated and deactivated many times. By
> converting these allocations into devm_kzalloc()s could lead to memory
> leaks. Please correct me if my understanding is incorrect.

Shuah,

I'm as the same opinion - as triggers are not really "driver", in the
sense that they are not using the driver structures, using devm_
functions doesn't seems to be really appropriate.

Specifically:

> > + oneshot_data = devm_kzalloc(led_cdev->dev, sizeof(*oneshot_data),
> > + GFP_KERNEL);

this one is registering the data on the relative LED's resources, so
that they are not freed until you unload the LED's driver itself.
I also think that this leads to memory leak if you keep activating
triggers, as the deactivate function is not actually freeing the
resources, so in that case the correct procedure should be the usual
kalloc/kfree.

Is this consistent with what your conclusions, Shuah?

Fabio

> If what I am saying makes sense, please take this as comment that is
> applicable to all led triggers, not just this one.
>
> Thanks,
> -- Shuah
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-leds" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/