Re: [PATCH 1/2] mtd: onenand: samsung: use devm_ function to simplify code and fix some leaks
From: Boris Brezillon
Date: Fri Dec 08 2017 - 16:28:44 EST
On Fri, 8 Dec 2017 22:11:04 +0100
Christophe JAILLET <christophe.jaillet@xxxxxxxxxx> wrote:
> Convert all error handling code in 's3c_onenand_probe()' to
> resource-managed alternatives in order to simplify code.
>
> This fixes a resource leak if 'platform_get_resource()' fails at line 872.
>
> The 'request_irq()' at line 971 was also un-balanced. It is now
> resource-managed
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@xxxxxxxxxx>
> ---
> Compile tested-only
> ---
> drivers/mtd/onenand/samsung.c | 141 +++++++++++++-----------------------------
> 1 file changed, 43 insertions(+), 98 deletions(-)
>
> diff --git a/drivers/mtd/onenand/samsung.c b/drivers/mtd/onenand/samsung.c
> index af0ac1a7bf8f..04039b967d59 100644
> --- a/drivers/mtd/onenand/samsung.c
> +++ b/drivers/mtd/onenand/samsung.c
> @@ -851,15 +851,14 @@ static int s3c_onenand_probe(struct platform_device *pdev)
> /* No need to check pdata. the platform data is optional */
>
> size = sizeof(struct mtd_info) + sizeof(struct onenand_chip);
> - mtd = kzalloc(size, GFP_KERNEL);
> + mtd = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
> if (!mtd)
> return -ENOMEM;
>
> - onenand = kzalloc(sizeof(struct s3c_onenand), GFP_KERNEL);
> - if (!onenand) {
> - err = -ENOMEM;
> - goto onenand_fail;
> - }
> + onenand = devm_kzalloc(&pdev->dev, sizeof(struct s3c_onenand),
> + GFP_KERNEL);
> + if (!onenand)
> + return -ENOMEM;
>
> this = (struct onenand_chip *) &mtd[1];
> mtd->priv = this;
> @@ -873,22 +872,20 @@ static int s3c_onenand_probe(struct platform_device *pdev)
> if (!r) {
> dev_err(&pdev->dev, "no memory resource defined\n");
> return -ENOENT;
> - goto ahb_resource_failed;
> }
>
> - onenand->base_res = request_mem_region(r->start, resource_size(r),
> - pdev->name);
> + onenand->base_res = devm_request_mem_region(&pdev->dev, r->start,
> + resource_size(r),
> + pdev->name);
Oh, and you should also get rid of all the onenand->xxx_res fields,
they're no longer needed after switching to devm_ helpers.
Just move the 'onenand->phys_base = r->start' assignment here and you
should be good.
> if (!onenand->base_res) {
> dev_err(&pdev->dev, "failed to request memory resource\n");
> - err = -EBUSY;
> - goto resource_failed;
> + return -EBUSY;
> }
>