[PATCH 1/2] mtd: onenand: samsung: use devm_ function to simplify code and fix some leaks
Boris Brezillon
boris.brezillon at free-electrons.com
Fri Dec 8 13:22:38 PST 2017
On Fri, 8 Dec 2017 22:11:04 +0100
Christophe JAILLET <christophe.jaillet at wanadoo.fr> 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 at wanadoo.fr>
> ---
> 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);
> if (!onenand->base_res) {
> dev_err(&pdev->dev, "failed to request memory resource\n");
> - err = -EBUSY;
> - goto resource_failed;
> + return -EBUSY;
> }
>
> - onenand->base = ioremap(r->start, resource_size(r));
> + onenand->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
> if (!onenand->base) {
> dev_err(&pdev->dev, "failed to map memory resource\n");
> - err = -EFAULT;
> - goto ioremap_failed;
> + return -EFAULT;
> }
Can still be simplified with the following pattern:
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
onenand->base = devm_ioremap_resource(&pdev->dev, r);
if (IS_ERR(onenand->base))
return PTR_ERR(onenand->base);
No need to keep the error messages, since devm_ioremap_resource()
already takes care of that.
More information about the linux-mtd
mailing list