char: Introduce the use of the managed version of kzalloc
PATCH 1/1 char: 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 NULL assignment to np->units is removed as there is no interaction between this field and sun4v_hvapi_unregister. Also, the labels out_free_units and out_free are removed as they are no longer required.
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);
...>
}
---
I would like to know if removing the NULL assignment is OK as it may be used for security to prevent it from being read before reinitialization.
drivers/char/hw_random/n2-drv.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
drivers/char/hw_random/n2-drv.c +7 -17
@@ -632,7 +632,7 @@ static int n2rng_probe(struct platform_device *op) multi_capable = (match->data !=NULL );n2rng_driver_version (); - np =kzalloc (sizeof (*np),GFP_KERNEL ); + np =devm_kzalloc (&op->dev,sizeof (*np),GFP_KERNEL );if (!np) goto out; np->op = op;
@@ -653,7 +653,7 @@ static int n2rng_probe(struct platform_device *op) &np->hvapi_minor)) {dev_err (&op->dev,"Cannot register suitable " "HVAPI version.\n" ); - goto out_free; + goto out; } }
@@ -676,15 +676,16 @@ static int n2rng_probe(struct platform_device *op)dev_info (&op->dev,"Registered RNG HVAPI major %lu minor %lu\n" , np->hvapi_major, np->hvapi_minor); - np->units =kzalloc (sizeof (struct n2rng_unit ) * np->num_units, -GFP_KERNEL ); + np->units =devm_kzalloc (&op->dev, +sizeof (struct n2rng_unit ) * np->num_units, +GFP_KERNEL ); err = -ENOMEM ;if (!np->units) goto out_hvapi_unregister; err =n2rng_init_control (np);if (err) - goto out_free_units; + goto out_hvapi_unregister; dev_info(&op->dev, "Found %s RNG, units: %d\n", ((np->flags & N2RNG_FLAG_MULTI) ?
@@ -697,7 +698,7 @@ static int n2rng_probe(struct platform_device *op) err =hwrng_register (&np->hwrng);if (err) - goto out_free_units; + goto out_hvapi_unregister;platform_set_drvdata (op, np);
@@ -705,15 +706,9 @@ static int n2rng_probe(struct platform_device *op)return 0 ; -out_free_units: -kfree (np->units); - np->units =NULL ; - out_hvapi_unregister:sun4v_hvapi_unregister (HV_GRP_RNG ); -out_free: -kfree (np); out:return err; }
@@ -730,11 +725,6 @@ static int n2rng_remove(struct platform_device *op)sun4v_hvapi_unregister (HV_GRP_RNG ); -kfree (np->units); - np->units =NULL ; - -kfree (np); -return 0 ; }
Signature · 1 line
1.9.1
Herbert Xu
maintainerQuoted 40 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. The NULL assignment tonp->unitsis removed as there is no interaction between this field andsun4v_hvapi_unregister. Also, the labelsout_free_unitsandout_freeare removed as they are no longer required.The following Coccinelle semantic patch was used for making the change:
@platform@
identifier p, probefn, removefn;
@@
structplatform_driverp = {
.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);
...>
}
Quoted 4 lines
Signed-off-by Himangi Saraogi <[email protected]>---
I would like to know if removing the NULL assignment is OK as it may be used for security to prevent it from being read before reinitialization.
Looks good to me. Patch applied. Thanks!
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt