Skip to contentSkip to conversation

Input: gpio-keys - update to devm_* API

PATCH In review Manish Badarkhe 3 participants 7 messages Sep 14, 2013 – Sep 20, 2013 4 messages not in the archive

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

PATCH 1/1 Input: gpio-keys - update to devm_* API

Update the code to use devm_* API so that driver core will manage resources.

---
:100644 100644 440ce32... b4db721... M drivers/input/keyboard/gpio_keys.c

 drivers/input/keyboard/gpio_keys.c |   18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/input/keyboard/gpio_keys.c +6 -12
index 440ce32..b4db721 100644
@@ -588,7 +588,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 		goto err_out;
 	}
 
-	pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
+	pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof(*button)),
 			GFP_KERNEL);
 	if (!pdata) {
 		error = -ENOMEM;
@@ -618,7 +618,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 				dev_err(dev,
 					"Failed to get gpio flags, error: %d\n",
 					error);
-			goto err_free_pdata;
+			goto err_out;
 		}
 
 		button = &pdata->buttons[i++];
@@ -630,7 +630,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 			dev_err(dev, "Button without keycode: 0x%x\n",
 				button->gpio);
 			error = -EINVAL;
-			goto err_free_pdata;
+			goto err_out;
 		}
 
 		button->desc = of_get_property(pp, "label", NULL);
@@ -647,13 +647,11 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 
 	if (pdata->nbuttons == 0) {
 		error = -EINVAL;
-		goto err_free_pdata;
+		goto err_out;
 	}
 
 	return pdata;
 
-err_free_pdata:
-	kfree(pdata);
 err_out:
 	return ERR_PTR(error);
 }
@@ -699,10 +697,10 @@ static int gpio_keys_probe(struct platform_device *pdev)
 			return PTR_ERR(pdata);
 	}
 
-	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
+	ddata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_keys_drvdata) +
 			pdata->nbuttons * sizeof(struct gpio_button_data),
 			GFP_KERNEL);
-	input = input_allocate_device();
+	input = devm_input_allocate_device(&pdev->dev);
 	if (!ddata || !input) {
 		dev_err(dev, "failed to allocate state\n");
 		error = -ENOMEM;
@@ -768,8 +766,6 @@ static int gpio_keys_probe(struct platform_device *pdev)
 		gpio_remove_key(&ddata->data[i]);
 
  fail1:
-	input_free_device(input);
-	kfree(ddata);
 	/* If we have no platform data, we allocated pdata dynamically. */
 	if (!dev_get_platdata(&pdev->dev))
 		kfree(pdata);
@@ -796,8 +792,6 @@ static int gpio_keys_remove(struct platform_device *pdev)
 	if (!dev_get_platdata(&pdev->dev))
 		kfree(ddata->pdata);
 
-	kfree(ddata);
-
 	return 0;
 }
 
Signature · 1 line
1.7.10.4
#1 lore raw

Hi Manish,

Thanks for the patch. I have few comments inline, though.

Quoted 25 lines · On Sunday 15 of September 2013 01:22:23 Manish Badarkhe wrote:

Update the code to use devm_* API so that driver core will manage resources.

---

:100644 100644 440ce32... b4db721... M drivers/input/keyboard/gpio_keys.c

 drivers/input/keyboard/gpio_keys.c |   18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
(quoted diff) +0 -0

b/drivers/input/keyboard/gpio_keys.c index 440ce32..b4db721 100644

drivers/input/keyboard/gpio_keys.c +1 -1
@@ -588,7 +588,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 		goto err_out;
 	}
 
-	pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
+	pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons *
 (sizeof(*button)), GFP_KERNEL);
 	if (!pdata) {
 		error = -ENOMEM;

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 621

Quoted 5 lines
drivers/input/keyboard/gpio_keys.c +1 -1
@@ -618,7 +618,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 				dev_err(dev,
 					"Failed to get gpio flags, error: %d\n",
 					error);
-			goto err_free_pdata;
+			goto err_out;

Since the only thing done after err_out label is returning the error, you can simply return the error here.

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 633

Quoted 9 lines
drivers/input/keyboard/gpio_keys.c +1 -1
 		}
 
 		button = &pdata->buttons[i++];
@@ -630,7 +630,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 			dev_err(dev, "Button without keycode: 0x%x\n",
 				button->gpio);
 			error = -EINVAL;
-			goto err_free_pdata;
+			goto err_out;

Ditto.

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 650

Quoted 9 lines
drivers/input/keyboard/gpio_keys.c +1 -1
 		}
 
 		button->desc = of_get_property(pp, "label", NULL);
@@ -647,13 +647,11 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 
 	if (pdata->nbuttons == 0) {
 		error = -EINVAL;
-		goto err_free_pdata;
+		goto err_out;

Ditto.

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 656

Quoted 8 lines
drivers/input/keyboard/gpio_keys.c +0 -2
 	}
 
 	return pdata;
 
-err_free_pdata:
-	kfree(pdata);
 err_out:
 	return ERR_PTR(error);

Then the whole error path here can be dropped.

Quoted 15 lines
drivers/input/keyboard/gpio_keys.c +2 -2
 }
@@ -699,10 +697,10 @@ static int gpio_keys_probe(struct platform_device
 *pdev) return PTR_ERR(pdata);
 	}
 
-	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
+	ddata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_keys_drvdata) +
 			pdata->nbuttons * sizeof(struct gpio_button_data),
 			GFP_KERNEL);
-	input = input_allocate_device();
+	input = devm_input_allocate_device(&pdev->dev);
 	if (!ddata || !input) {
 		dev_err(dev, "failed to allocate state\n");
 		error = -ENOMEM;

drivers/input/keyboard/gpio_keys.c static int gpio_keys_probe(struct platform_device · line 771

Quoted 8 lines
drivers/input/keyboard/gpio_keys.c +0 -2
@@ -768,8 +766,6 @@ static int gpio_keys_probe(struct platform_device
 *pdev) gpio_remove_key(&ddata->data[i]);
 
  fail1:
-	input_free_device(input);
-	kfree(ddata);
 	/* If we have no platform data, we allocated pdata dynamically. */
 	if (!dev_get_platdata(&pdev->dev))
 		kfree(pdata);

This is incorrect and unnecessary. Since pdata was allocated using devm_ helper it will be freed automatically. As a side note, if you want to explicitly free memory allocated using devm_ helpers, you need to do it using their devm_ free counterparts, not directly.

drivers/input/keyboard/gpio_keys.c static int gpio_keys_remove(struct platform_device · line 793

Quoted 3 lines
drivers/input/keyboard/gpio_keys.c +0 -0
@@ -796,8 +792,6 @@ static int gpio_keys_remove(struct platform_device
 *pdev) if (!dev_get_platdata(&pdev->dev))
 		kfree(ddata->pdata);

Same here.

Best regards,
Tomasz

#2 lore raw

Hi Tomasz

Quoted 24 lines · Tomasz Figa

Hi Manish,

Thanks for the patch. I have few comments inline, though.

Quoted 19 lines · On Sunday 15 of September 2013 01:22:23 Manish Badarkhe wrote:

Update the code to use devm_* API so that driver core will manage resources.

---

:100644 100644 440ce32... b4db721... M drivers/input/keyboard/gpio_keys.c

 drivers/input/keyboard/gpio_keys.c |   18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
(quoted diff) +0 -0

b/drivers/input/keyboard/gpio_keys.c index 440ce32..b4db721 100644

drivers/input/keyboard/gpio_keys.c +0 -0
@@ -588,7 +588,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
              goto err_out;
      }
 
Quoted 14 lines

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 621

Quoted 11 lines
drivers/input/keyboard/gpio_keys.c +2 -2
-     pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
+     pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons *
 (sizeof(*button)), GFP_KERNEL);
      if (!pdata) {
              error = -ENOMEM;
@@ -618,7 +618,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
                              dev_err(dev,
                                      "Failed to get gpio flags, error: %d\n",
                                      error);
-                     goto err_free_pdata;
+                     goto err_out;

Since the only thing done after err_out label is returning the error, you can simply return the error here.

Agreed, will do that.

Quoted 12 lines

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 633

Quoted 10 lines
drivers/input/keyboard/gpio_keys.c +1 -1
              }
 
              button = &pdata->buttons[i++];
@@ -630,7 +630,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
                      dev_err(dev, "Button without keycode: 0x%x\n",
                              button->gpio);
                      error = -EINVAL;
-                     goto err_free_pdata;
+                     goto err_out;

Ditto.

Ok

Quoted 6 lines
Quoted 6 lines
drivers/input/keyboard/gpio_keys.c +0 -0
              }
 
              button->desc = of_get_property(pp, "label", NULL);
@@ -647,13 +647,11 @@ gpio_keys_get_devtree_pdata(struct device *dev)
 
Quoted 6 lines

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 650

Quoted 4 lines
drivers/input/keyboard/gpio_keys.c +1 -1
      if (pdata->nbuttons == 0) {
              error = -EINVAL;
-             goto err_free_pdata;
+             goto err_out;

Ditto.

Ok

Quoted 11 lines

drivers/input/keyboard/gpio_keys.c gpio_keys_get_devtree_pdata(struct device *dev) · line 656

Quoted 9 lines
drivers/input/keyboard/gpio_keys.c +0 -2
      }
 
      return pdata;
 
-err_free_pdata:
-     kfree(pdata);
 err_out:
      return ERR_PTR(error);

Then the whole error path here can be dropped.

Ok, I will drop this error path.

Quoted 18 lines
Quoted 18 lines
drivers/input/keyboard/gpio_keys.c +2 -2
 }
@@ -699,10 +697,10 @@ static int gpio_keys_probe(struct platform_device
 *pdev) return PTR_ERR(pdata);
      }
 
-     ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
+     ddata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_keys_drvdata) +
                      pdata->nbuttons * sizeof(struct gpio_button_data),
                      GFP_KERNEL);
-     input = input_allocate_device();
+     input = devm_input_allocate_device(&pdev->dev);
      if (!ddata || !input) {
              dev_err(dev, "failed to allocate state\n");
              error = -ENOMEM;
@@ -768,8 +766,6 @@ static int gpio_keys_probe(struct platform_device
 *pdev) gpio_remove_key(&ddata->data[i]);
 
Quoted 11 lines

drivers/input/keyboard/gpio_keys.c static int gpio_keys_probe(struct platform_device *pdev) · line 771

Quoted 6 lines
drivers/input/keyboard/gpio_keys.c +0 -2
  fail1:
-     input_free_device(input);
-     kfree(ddata);
      /* If we have no platform data, we allocated pdata dynamically. */
      if (!dev_get_platdata(&pdev->dev))
              kfree(pdata);

This is incorrect and unnecessary. Since pdata was allocated using devm_ helper it will be freed automatically. As a side note, if you want to explicitly free memory allocated using devm_ helpers, you need to do it using their devm_ free counterparts, not directly.

Ok, I will remove this as "pdata" is getting freed automatically.

Quoted 5 lines

drivers/input/keyboard/gpio_keys.c static int gpio_keys_remove(struct platform_device · line 793

Quoted 3 lines
drivers/input/keyboard/gpio_keys.c +0 -0
@@ -796,8 +792,6 @@ static int gpio_keys_remove(struct platform_device
 *pdev) if (!dev_get_platdata(&pdev->dev))
              kfree(ddata->pdata);

Same here.

Ok, I will remove this as "pdata" is getting freed automatically.

Regards
Manish Badarkhe

A message that is not in the archive (sent off-list or not yet received): 1379188343-18904-1-git-send-email-badarkhe.manish-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

Hi Manish,

Quoted 3 lines · Manish Badarkhe

Update the code to use devm_* API so that driver core will manage resources.

And the benefit of this would be...?

There are still resources that are managed in traditional way and I really dislike mixing the 2 styles. I can see applying patch that converts the driver to use devm_ for all its resources and gets rid of the remove() method altogether, but I am not sure how beneficial this kind of changes are for existing drivers.

Thanks.

Signature · 1 line
Dmitry

A message that is not in the archive (sent off-list or not yet received): 20130917185259.GA6894-WlK9ik9hQGAhIp7JRqBPierSzoNAToWh@public.gmane.org

#6 lore raw

Hi Dmitry,

Quoted 7 lines · Dmitry Torokhov

Hi Manish,

Quoted 3 lines · Manish Badarkhe

Update the code to use devm_* API so that driver core will manage resources.

Quoted 7 lines

And the benefit of this would be...?

There are still resources that are managed in traditional way and I really dislike mixing the 2 styles. I can see applying patch that converts the driver to use devm_ for all its resources and gets rid of the remove() method altogether, but I am not sure how beneficial this kind of changes are for existing drivers.

IMO devm_ makes us to manage resources properly without much care about freeing it ( as devm_handles freeing automatically). But, stiil there are use cases which makes
us to mix this devm_ with traditionally managed resources.
Not much sure about any other benefits.

Regards
Manish Badarkhe

A message that is not in the archive (sent off-list or not yet received): CAKDJKT5fvf=cCqU0ULs0o6NO10aDM73ewx=-sVG9Rzm-cKkECg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#8 lore raw
Quoted 13 lines · Manish Badarkhe

Hi Dmitry,

Quoted 9 lines · Dmitry Torokhov

Hi Manish,

Quoted 3 lines · Manish Badarkhe

Update the code to use devm_* API so that driver core will manage resources.

And the benefit of this would be...?

Quoted 8 lines
Quoted 5 lines

There are still resources that are managed in traditional way and I really dislike mixing the 2 styles. I can see applying patch that converts the driver to use devm_ for all its resources and gets rid of the remove() method altogether, but I am not sure how beneficial this kind of changes are for existing drivers.

IMO devm_ makes us to manage resources properly without much care about freeing it ( as devm_handles freeing automatically).

Are the resources released improperly in the current version of the driver? IOW I do not see the point in applying this patch as it does not make the driver materially better.

Thanks.

Signature · 1 line
Dmitry

A message that is not in the archive (sent off-list or not yet received): 20130919212253.GA16015-WlK9ik9hQGAhIp7JRqBPierSzoNAToWh@public.gmane.org

#10 lore raw

Hi Dmitry,

Quoted 20 lines · Dmitry Torokhov
Quoted 19 lines · Manish Badarkhe

Hi Dmitry,

Quoted 14 lines · Dmitry Torokhov

Hi Manish,

Quoted 3 lines · Manish Badarkhe

Update the code to use devm_* API so that driver core will manage resources.

And the benefit of this would be...?

There are still resources that are managed in traditional way and I really dislike mixing the 2 styles. I can see applying patch that converts the driver to use devm_ for all its resources and gets rid of the remove() method altogether, but I am not sure how beneficial this kind of changes are for existing drivers.

Quoted 6 lines
Quoted 2 lines

IMO devm_ makes us to manage resources properly without much care about freeing it ( as devm_handles freeing automatically).

Are the resources released improperly in the current version of the driver? IOW I do not see the point in applying this patch as it does not make the driver materially better.

Resources are released properly in current version of driver but using a traditional
way. This patch is just a clean up and to use new devm_ variant. Yes, This patch does
not make a driver materially better but make use of "devm_" variant as like any other
drivers.

Thanks
Manish Badarkhe