Skip to contentSkip to conversation

USB: apple-mfi-fastcharge: Use devm_kzalloc and simplify the code

PATCH In review Lucas Tanure 2 participants 6 messages Nov 14, 2020 – Nov 15, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: apple-mfi-fastcharge: Use devm_kzalloc and simplify the code

---
 drivers/usb/misc/apple-mfi-fastcharge.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/usb/misc/apple-mfi-fastcharge.c +5 -12
index 9de0171b5177..de86e389a008 100644
@@ -178,16 +178,13 @@ static int mfi_fc_probe(struct usb_device *udev)
 {
 	struct power_supply_config battery_cfg = {};
 	struct mfi_device *mfi = NULL;
-	int err;
 
 	if (!mfi_fc_match(udev))
 		return -ENODEV;
 
-	mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
-	if (!mfi) {
-		err = -ENOMEM;
-		goto error;
-	}
+	mfi = devm_kzalloc(&udev->dev, sizeof(*mfi), GFP_KERNEL);
+	if (!mfi)
+		return -ENOMEM;
 
 	battery_cfg.drv_data = mfi;
 
@@ -197,8 +194,7 @@ static int mfi_fc_probe(struct usb_device *udev)
 						&battery_cfg);
 	if (IS_ERR(mfi->battery)) {
 		dev_err(&udev->dev, "Can't register battery\n");
-		err = PTR_ERR(mfi->battery);
-		goto error;
+		return PTR_ERR(mfi->battery);
 	}
 
 	mfi->udev = usb_get_dev(udev);
@@ -206,9 +202,6 @@ static int mfi_fc_probe(struct usb_device *udev)
 
 	return 0;
 
-error:
-	kfree(mfi);
-	return err;
 }
 
 static void mfi_fc_disconnect(struct usb_device *udev)
@@ -220,7 +213,7 @@ static void mfi_fc_disconnect(struct usb_device *udev)
 		power_supply_unregister(mfi->battery);
 	dev_set_drvdata(&udev->dev, NULL);
 	usb_put_dev(mfi->udev);
-	kfree(mfi);
+	devm_kfree(&udev->dev, mfi);
 }
 
 static struct usb_device_driver mfi_fc_driver = {
Signature · 1 line
2.29.2
#1 lore raw
Quoted 1 line · Lucas Tanure

I can't take patches without any changelog text, sorry.

Quoted 49 lines
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/usb/misc/apple-mfi-fastcharge.c +4 -11
index 9de0171b5177..de86e389a008 100644
@@ -178,16 +178,13 @@ static int mfi_fc_probe(struct usb_device *udev)
 {
 	struct power_supply_config battery_cfg = {};
 	struct mfi_device *mfi = NULL;
-	int err;
 
 	if (!mfi_fc_match(udev))
 		return -ENODEV;
 
-	mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
-	if (!mfi) {
-		err = -ENOMEM;
-		goto error;
-	}
+	mfi = devm_kzalloc(&udev->dev, sizeof(*mfi), GFP_KERNEL);
+	if (!mfi)
+		return -ENOMEM;
 
 	battery_cfg.drv_data = mfi;
 
@@ -197,8 +194,7 @@ static int mfi_fc_probe(struct usb_device *udev)
 						&battery_cfg);
 	if (IS_ERR(mfi->battery)) {
 		dev_err(&udev->dev, "Can't register battery\n");
-		err = PTR_ERR(mfi->battery);
-		goto error;
+		return PTR_ERR(mfi->battery);
 	}
 
 	mfi->udev = usb_get_dev(udev);
@@ -206,9 +202,6 @@ static int mfi_fc_probe(struct usb_device *udev)
 
 	return 0;
 
-error:
-	kfree(mfi);
-	return err;
 }
 
 static void mfi_fc_disconnect(struct usb_device *udev)

drivers/usb/misc/apple-mfi-fastcharge.c static void mfi_fc_disconnect(struct usb_device *udev) · line 216

Quoted 5 lines
drivers/usb/misc/apple-mfi-fastcharge.c +1 -1
@@ -220,7 +213,7 @@ static void mfi_fc_disconnect(struct usb_device *udev)
 		power_supply_unregister(mfi->battery);
 	dev_set_drvdata(&udev->dev, NULL);
 	usb_put_dev(mfi->udev);
-	kfree(mfi);
+	devm_kfree(&udev->dev, mfi);

Are you sure about this?

And what's wrong with the existing code? Using the devm_*() variants seems like a "cleanup", but it's not always the case.

thanks,

greg k-h

#2 lore raw
Quoted 53 lines · Greg Kroah-Hartman
Quoted 1 line · Lucas Tanure

I can't take patches without any changelog text, sorry.

Quoted 47 lines
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/usb/misc/apple-mfi-fastcharge.c +4 -11
index 9de0171b5177..de86e389a008 100644
@@ -178,16 +178,13 @@ static int mfi_fc_probe(struct usb_device *udev)
 {
      struct power_supply_config battery_cfg = {};
      struct mfi_device *mfi = NULL;
-     int err;
 
      if (!mfi_fc_match(udev))
              return -ENODEV;
 
-     mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
-     if (!mfi) {
-             err = -ENOMEM;
-             goto error;
-     }
+     mfi = devm_kzalloc(&udev->dev, sizeof(*mfi), GFP_KERNEL);
+     if (!mfi)
+             return -ENOMEM;
 
      battery_cfg.drv_data = mfi;
 
@@ -197,8 +194,7 @@ static int mfi_fc_probe(struct usb_device *udev)
                                              &battery_cfg);
      if (IS_ERR(mfi->battery)) {
              dev_err(&udev->dev, "Can't register battery\n");
-             err = PTR_ERR(mfi->battery);
-             goto error;
+             return PTR_ERR(mfi->battery);
      }
 
      mfi->udev = usb_get_dev(udev);
@@ -206,9 +202,6 @@ static int mfi_fc_probe(struct usb_device *udev)
 
      return 0;
 
-error:
-     kfree(mfi);
-     return err;
 }
 
Quoted 9 lines

drivers/usb/misc/apple-mfi-fastcharge.c static void mfi_fc_disconnect(struct usb_device *udev) · line 216

Quoted 7 lines
drivers/usb/misc/apple-mfi-fastcharge.c +1 -1
 static void mfi_fc_disconnect(struct usb_device *udev)
@@ -220,7 +213,7 @@ static void mfi_fc_disconnect(struct usb_device *udev)
              power_supply_unregister(mfi->battery);
      dev_set_drvdata(&udev->dev, NULL);
      usb_put_dev(mfi->udev);
-     kfree(mfi);
+     devm_kfree(&udev->dev, mfi);

Are you sure about this?

I think so, as the probe will allocate again that struct, the disconnect should free the previous one.

Quoted 3 lines

And what's wrong with the existing code? Using the devm_*() variants seems like a "cleanup", but it's not always the case.

I don't know what's wrong, but I will figure out.

Quoted 4 lines

thanks,

greg k-h

Thanks
Lucas

#3 lore raw
Quoted 64 lines · Lucas Tanure
Quoted 53 lines · Greg Kroah-Hartman
Quoted 1 line · Lucas Tanure

I can't take patches without any changelog text, sorry.

Quoted 47 lines
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/usb/misc/apple-mfi-fastcharge.c +4 -11
index 9de0171b5177..de86e389a008 100644
@@ -178,16 +178,13 @@ static int mfi_fc_probe(struct usb_device *udev)
 {
      struct power_supply_config battery_cfg = {};
      struct mfi_device *mfi = NULL;
-     int err;
 
      if (!mfi_fc_match(udev))
              return -ENODEV;
 
-     mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
-     if (!mfi) {
-             err = -ENOMEM;
-             goto error;
-     }
+     mfi = devm_kzalloc(&udev->dev, sizeof(*mfi), GFP_KERNEL);
+     if (!mfi)
+             return -ENOMEM;
 
      battery_cfg.drv_data = mfi;
 
@@ -197,8 +194,7 @@ static int mfi_fc_probe(struct usb_device *udev)
                                              &battery_cfg);
      if (IS_ERR(mfi->battery)) {
              dev_err(&udev->dev, "Can't register battery\n");
-             err = PTR_ERR(mfi->battery);
-             goto error;
+             return PTR_ERR(mfi->battery);
      }
 
      mfi->udev = usb_get_dev(udev);
@@ -206,9 +202,6 @@ static int mfi_fc_probe(struct usb_device *udev)
 
      return 0;
 
-error:
-     kfree(mfi);
-     return err;
 }
 
Quoted 9 lines

drivers/usb/misc/apple-mfi-fastcharge.c static void mfi_fc_disconnect(struct usb_device *udev) · line 216

Quoted 7 lines
drivers/usb/misc/apple-mfi-fastcharge.c +1 -1
 static void mfi_fc_disconnect(struct usb_device *udev)
@@ -220,7 +213,7 @@ static void mfi_fc_disconnect(struct usb_device *udev)
              power_supply_unregister(mfi->battery);
      dev_set_drvdata(&udev->dev, NULL);
      usb_put_dev(mfi->udev);
-     kfree(mfi);
+     devm_kfree(&udev->dev, mfi);

Are you sure about this?

Quoted 2 lines

I think so, as the probe will allocate again that struct, the disconnect should free the previous one.

Why do you need to manually free it here like this?

Why are you trying to convert this file to this api anyway?

thanks,

greg k-h

#4 lore raw
Quoted 66 lines · Greg Kroah-Hartman
Quoted 64 lines · Lucas Tanure
Quoted 62 lines · Greg Kroah-Hartman
Quoted 1 line · Lucas Tanure

I can't take patches without any changelog text, sorry.

Quoted 49 lines
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/usb/misc/apple-mfi-fastcharge.c +4 -11
index 9de0171b5177..de86e389a008 100644
@@ -178,16 +178,13 @@ static int mfi_fc_probe(struct usb_device *udev)
 {
      struct power_supply_config battery_cfg = {};
      struct mfi_device *mfi = NULL;
-     int err;
 
      if (!mfi_fc_match(udev))
              return -ENODEV;
 
-     mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
-     if (!mfi) {
-             err = -ENOMEM;
-             goto error;
-     }
+     mfi = devm_kzalloc(&udev->dev, sizeof(*mfi), GFP_KERNEL);
+     if (!mfi)
+             return -ENOMEM;
 
      battery_cfg.drv_data = mfi;
 
@@ -197,8 +194,7 @@ static int mfi_fc_probe(struct usb_device *udev)
                                              &battery_cfg);
      if (IS_ERR(mfi->battery)) {
              dev_err(&udev->dev, "Can't register battery\n");
-             err = PTR_ERR(mfi->battery);
-             goto error;
+             return PTR_ERR(mfi->battery);
      }
 
      mfi->udev = usb_get_dev(udev);
@@ -206,9 +202,6 @@ static int mfi_fc_probe(struct usb_device *udev)
 
      return 0;
 
-error:
-     kfree(mfi);
-     return err;
 }
 
 static void mfi_fc_disconnect(struct usb_device *udev)

drivers/usb/misc/apple-mfi-fastcharge.c static void mfi_fc_disconnect(struct usb_device *udev) · line 216

Quoted 5 lines
drivers/usb/misc/apple-mfi-fastcharge.c +1 -1
@@ -220,7 +213,7 @@ static void mfi_fc_disconnect(struct usb_device *udev)
              power_supply_unregister(mfi->battery);
      dev_set_drvdata(&udev->dev, NULL);
      usb_put_dev(mfi->udev);
-     kfree(mfi);
+     devm_kfree(&udev->dev, mfi);

Are you sure about this?

Quoted 4 lines
Quoted 2 lines

I think so, as the probe will allocate again that struct, the disconnect should free the previous one.

Why do you need to manually free it here like this?

My understanding is that memory will only be freed when the driver gets unloaded and the next connection of the device will allocate a new one.
So every new disconnection and re-connection there will be a small memory leak until the driver gets unloaded.

Quoted 2 lines

Why are you trying to convert this file to this api anyway?

I was just trying to improve the code as the original source calls kfree even when kzalloc fails.
And using devm_* would remove the need for kfree and the end of probe.

Quoted 4 lines

thanks,

greg k-h

Thanks
Lucas

#5 lore raw
Quoted 71 lines · Lucas Tanure
Quoted 69 lines · Greg Kroah-Hartman
Quoted 66 lines · Lucas Tanure
Quoted 53 lines · Greg Kroah-Hartman
Quoted 1 line · Lucas Tanure

I can't take patches without any changelog text, sorry.

Quoted 47 lines
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/usb/misc/apple-mfi-fastcharge.c +4 -11
index 9de0171b5177..de86e389a008 100644
@@ -178,16 +178,13 @@ static int mfi_fc_probe(struct usb_device *udev)
 {
      struct power_supply_config battery_cfg = {};
      struct mfi_device *mfi = NULL;
-     int err;
 
      if (!mfi_fc_match(udev))
              return -ENODEV;
 
-     mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
-     if (!mfi) {
-             err = -ENOMEM;
-             goto error;
-     }
+     mfi = devm_kzalloc(&udev->dev, sizeof(*mfi), GFP_KERNEL);
+     if (!mfi)
+             return -ENOMEM;
 
      battery_cfg.drv_data = mfi;
 
@@ -197,8 +194,7 @@ static int mfi_fc_probe(struct usb_device *udev)
                                              &battery_cfg);
      if (IS_ERR(mfi->battery)) {
              dev_err(&udev->dev, "Can't register battery\n");
-             err = PTR_ERR(mfi->battery);
-             goto error;
+             return PTR_ERR(mfi->battery);
      }
 
      mfi->udev = usb_get_dev(udev);
@@ -206,9 +202,6 @@ static int mfi_fc_probe(struct usb_device *udev)
 
      return 0;
 
-error:
-     kfree(mfi);
-     return err;
 }
 
Quoted 9 lines

drivers/usb/misc/apple-mfi-fastcharge.c static void mfi_fc_disconnect(struct usb_device *udev) · line 216

Quoted 7 lines
drivers/usb/misc/apple-mfi-fastcharge.c +1 -1
 static void mfi_fc_disconnect(struct usb_device *udev)
@@ -220,7 +213,7 @@ static void mfi_fc_disconnect(struct usb_device *udev)
              power_supply_unregister(mfi->battery);
      dev_set_drvdata(&udev->dev, NULL);
      usb_put_dev(mfi->udev);
-     kfree(mfi);
+     devm_kfree(&udev->dev, mfi);

Are you sure about this?

I think so, as the probe will allocate again that struct, the disconnect should free the previous one.

Quoted 6 lines
Quoted 1 line

Why do you need to manually free it here like this?

My understanding is that memory will only be freed when the driver gets unloaded and the next connection of the device will allocate a new one.
So every new disconnection and re-connection there will be a small memory leak until the driver gets unloaded.

devm_* functions operate on the lifecycle of the device, not the driver. Two totally different things :)

Quoted 3 lines
Quoted 1 line

Why are you trying to convert this file to this api anyway?

I was just trying to improve the code as the original source calls kfree even when kzalloc fails.

Then please fix that bug independant of any conversion to a new api, as that is a bugfix that should be backported to older kernels.

Quoted 1 line

And using devm_* would remove the need for kfree and the end of probe.

Yes, but you can't introduce new bugs when trying to fix existing ones.

Also, you didn't say this was a bugfix anywhere, which is one reason that writing good changelog text is essencial.

thanks,

greg k-h