From: Nikolay Balandin <[email protected]>
Use devm_*() functions to make cleanup paths simpler.
Signed-off-by: Nikolay Balandin <[email protected]>
---
drivers/rtc/rtc-ds1307.c | 38 +++++++++++++++-----------------------
1 file changed, 15 insertions(+), 23 deletions(-)
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index b53992a..dbfb2a3 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -683,7 +683,9 @@ static int ds1307_probe(struct i2c_client *client,
&& !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
return -EIO;
- ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL);
+ ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307),
+ GFP_KERNEL);
+
if (!ds1307)
return -ENOMEM;
@@ -714,8 +716,7 @@ static int ds1307_probe(struct i2c_client *client,
DS1337_REG_CONTROL, 2, buf);
if (tmp != 2) {
dev_dbg(&client->dev, "read error %d\n", tmp);
- err = -EIO;
- goto exit_free;
+ return -EIO;
}
/* oscillator off? turn it on, so clock can tick. */
@@ -753,8 +754,7 @@ static int ds1307_probe(struct i2c_client *client,
RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
if (tmp != 2) {
dev_dbg(&client->dev, "read error %d\n", tmp);
- err = -EIO;
- goto exit_free;
+ return -EIO;
}
/* oscillator off? turn it on, so clock can tick. */
@@ -797,8 +797,7 @@ static int ds1307_probe(struct i2c_client *client,
RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
if (tmp != 2) {
dev_dbg(&client->dev, "read error %d\n", tmp);
- err = -EIO;
- goto exit_free;
+ return -EIO;
}
/* correct hour */
@@ -825,8 +824,7 @@ read_rtc:
tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
if (tmp != 8) {
dev_dbg(&client->dev, "read error %d\n", tmp);
- err = -EIO;
- goto exit_free;
+ return -EIO;
}
/*
@@ -867,8 +865,7 @@ read_rtc:
tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
if (tmp < 0) {
dev_dbg(&client->dev, "read error %d\n", tmp);
- err = -EIO;
- goto exit_free;
+ return -EIO;
}
/* oscillator fault? clear flag, and warn */
@@ -933,7 +930,7 @@ read_rtc:
err = PTR_ERR(ds1307->rtc);
dev_err(&client->dev,
"unable to register the class device\n");
- goto exit_free;
+ return err;
}
if (want_irq) {
@@ -951,8 +948,9 @@ read_rtc:
}
if (chip->nvram_size) {
- ds1307->nvram = kzalloc(sizeof(struct bin_attribute),
- GFP_KERNEL);
+ ds1307->nvram = devm_kzalloc(&client->dev,
+ sizeof(struct bin_attribute), GFP_KERNEL);
+
if (!ds1307->nvram) {
err = -ENOMEM;
goto exit_nvram;
@@ -965,10 +963,9 @@ read_rtc:
ds1307->nvram->size = chip->nvram_size;
ds1307->nvram_offset = chip->nvram_offset;
err = sysfs_create_bin_file(&client->dev.kobj, ds1307->nvram);
- if (err) {
- kfree(ds1307->nvram);
+ if (err)
goto exit_nvram;
- }
+
set_bit(HAS_NVRAM, &ds1307->flags);
dev_info(&client->dev, "%zu bytes nvram\n", ds1307->nvram->size);
}
@@ -978,8 +975,6 @@ read_rtc:
exit_nvram:
exit_irq:
rtc_device_unregister(ds1307->rtc);
-exit_free:
- kfree(ds1307);
return err;
}
@@ -992,13 +987,10 @@ static int ds1307_remove(struct i2c_client *client)
cancel_work_sync(&ds1307->work);
}
- if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags)) {
+ if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
- kfree(ds1307->nvram);
- }
rtc_device_unregister(ds1307->rtc);
- kfree(ds1307);
return 0;
}
--
1.7.9.5
On Saturday, May 25, 2013 10:51 AM, Devendra Naga wrote:
> On Sat, May 25, 2013 at 10:41 AM, Nikolay Balandin wrote:
> > From: Nikolay Balandin <[email protected]>
[.....]
> >
> > - ds1307->rtc = rtc_device_register(client->name, &client->dev,
> > + ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
> > &ds13xx_rtc_ops, THIS_MODULE);
Hi Nikolay Baladin, Devendra Naga,
Here, "devm"_rtc_device_register() is used.
Thus, rtc_device_unregister() will be called after remove() or
on probe failure.
> > if (IS_ERR(ds1307->rtc)) {
> > err = PTR_ERR(ds1307->rtc);
> > dev_err(&client->dev,
> > "unable to register the class device\n");
> > - goto exit_free;
> > + return err;
> > }
[.....]
> > -exit_nvram:
> > -exit_irq:
> > - rtc_device_unregister(ds1307->rtc);
>
> Please dont remove this unregister, there's no devm_* for the rtc subsystem.
Above mentioned, rtc_device_unregister() can be removed, because
"devm"_rtc_device_register() was already called.
If I am wrong, please let me know kindly. :)
See you.
Best regards,
Jingoo Han
>
>
> > -exit_free:
> > - kfree(ds1307);
> > - return err;
> > }
> >
> > static int ds1307_remove(struct i2c_client *client)
> > @@ -992,13 +981,9 @@ static int ds1307_remove(struct i2c_client *client)
> > cancel_work_sync(&ds1307->work);
> > }
> >
> > - if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags)) {
> > + if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
> > sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
> > - kfree(ds1307->nvram);
> > - }
> >
> > - rtc_device_unregister(ds1307->rtc);
>
> Here too.
>
>
On Mon, May 27, 2013 at 6:32 AM, Jingoo Han <[email protected]> wrote:
> On Saturday, May 25, 2013 10:51 AM, Devendra Naga wrote:
>> On Sat, May 25, 2013 at 10:41 AM, Nikolay Balandin wrote:
>> > From: Nikolay Balandin <[email protected]>
>
> [.....]
>
>> >
>> > - ds1307->rtc = rtc_device_register(client->name, &client->dev,
>> > + ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
>> > &ds13xx_rtc_ops, THIS_MODULE);
>
> Hi Nikolay Baladin, Devendra Naga,
>
> Here, "devm"_rtc_device_register() is used.
> Thus, rtc_device_unregister() will be called after remove() or
> on probe failure.
>
You are correct, I am not aware of the new API that is added by you
in c100a5e0255.
Very sorry Nikolay Baladin, I would really have seen and commented on the patch.
Thanks for letting me know. :)
>
>> > if (IS_ERR(ds1307->rtc)) {