Input: Introduce the use of the managed version of kzalloc
Subsystems (2)
PATCH 1/1 Input: Introduce the use of the managed version of kzalloc
This patch moves data allocated using kzalloc to managed data allocated using devm_kzalloc and cleans now unnecessary kfrees in probe and remove functions.
The following Coccinelle semantic patch was used for making the change:
@platform@
identifier p, probefn, removefn;
@@
struct platform_driver p = {
.probe = probefn,
.remove = removefn,
};
@prb@
identifier platform.probefn, pdev;
expression e, e1, e2;
@@probefn(struct platform_device *pdev, ...) {
<+...
- e = kzalloc(e1, e2)
+ e = devm_kzalloc(&pdev->dev, e1, e2)
...
?-kfree(e);
...+>
}
@rem depends on prb@
identifier platform.removefn;
expression e;
@@
removefn(...) {
<...
- kfree(e);
...>
}
--- drivers/input/misc/pcf50633-input.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/input/misc/pcf50633-input.c +2 -7
@@ -58,16 +58,13 @@ static int pcf50633_input_probe(struct platform_device *pdev)struct input_dev *input_dev;int ret; - - input =kzalloc (sizeof (*input),GFP_KERNEL ); + input =devm_kzalloc (&pdev->dev,sizeof (*input),GFP_KERNEL );if (!input)return -ENOMEM ; input_dev =input_allocate_device (); -if (!input_dev) { -kfree (input); +if (!input_dev)return -ENOMEM ; - }platform_set_drvdata (pdev, input); input->pcf =dev_to_pcf50633 (pdev->dev.parent);
@@ -81,7 +78,6 @@ static int pcf50633_input_probe(struct platform_device *pdev) ret =input_register_device (input_dev);if (ret) {input_free_device (input_dev); -kfree (input);return ret; } pcf50633_register_irq(input->pcf, PCF50633_IRQ_ONKEYR,
@@ -100,7 +96,6 @@ static int pcf50633_input_remove(struct platform_device *pdev)pcf50633_free_irq (input->pcf,PCF50633_IRQ_ONKEYF );input_unregister_device (input->input_dev); -kfree (input);return 0 ; }
Signature · 1 line
1.9.1
Dmitry Torokhov
maintainerHi Himangi,
Quoted 3 lines · Himangi Saraogi
This patch moves data allocated using kzalloc to managed data allocated using
devm_kzallocand cleans now unnecessary kfrees in probe and remove functions.
I prefer not to mix managed and unmanaged resources in one driver. I.e. either all (or most) resources are managed or they are all unmanaged, otherwise it is really hard to track which one should be freed and which will be freed automatically.
In this very simple driver I do not see converting only memory allocation to devm as bringing clear benefit,
Thanks.
Signature · 1 line
Dmitry