Re: [PATCH] mtd: nand: fix object reference leaks in atmel_pmecc_get_by_node

From: Boris Brezillon
Date: Sun Feb 03 2019 - 05:33:21 EST


On Sun, 3 Feb 2019 09:37:31 +0000
wen yang <yellowriver2010@xxxxxxxxxxx> wrote:

> of_find_device_by_node() takes a reference to the struct device
> when it finds a match via get_device, there is no need to call
> get_device() twice.
> We also should make sure to drop the reference to the device
> taken by of_find_device_by_node() on driver unbind.
>
> Signed-off-by: Wen Yang <yellowriver2010@xxxxxxxxxxx>
> Cc: Tudor Ambarus <tudor.ambarus@xxxxxxxxxxxxx>
> Cc: Boris Brezillon <bbrezillon@xxxxxxxxxx>
> Cc: Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
> Cc: Richard Weinberger <richard@xxxxxx>
> Cc: David Woodhouse <dwmw2@xxxxxxxxxxxxx>
> Cc: Brian Norris <computersforpeace@xxxxxxxxx>
> Cc: Marek Vasut <marek.vasut@xxxxxxxxx>
> Cc: Nicolas Ferre <nicolas.ferre@xxxxxxxxxxxxx>
> Cc: Alexandre Belloni <alexandre.belloni@xxxxxxxxxxx>
> Cc: Ludovic Desroches <ludovic.desroches@xxxxxxxxxxxxx>
> Cc: linux-mtd@xxxxxxxxxxxxxxxxxxx
> Cc: linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
> Cc: linux-kernel@xxxxxxxxxxxxxxx
> ---
> drivers/mtd/nand/raw/atmel/pmecc.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/mtd/nand/raw/atmel/pmecc.c b/drivers/mtd/nand/raw/atmel/pmecc.c
> index 555a74e..bee6551 100644
> --- a/drivers/mtd/nand/raw/atmel/pmecc.c
> +++ b/drivers/mtd/nand/raw/atmel/pmecc.c
> @@ -878,14 +878,17 @@ static struct atmel_pmecc *atmel_pmecc_get_by_node(struct device *userdev,
> struct atmel_pmecc *pmecc, **ptr;
>
> pdev = of_find_device_by_node(np);
> - if (!pdev || !platform_get_drvdata(pdev))
> + if (!pdev)
> return ERR_PTR(-EPROBE_DEFER);
> + if (!platform_get_drvdata(pdev)) {
> + put_device(&pdev->dev);
> + return ERR_PTR(-EPROBE_DEFER);
> + }
>
> ptr = devres_alloc(devm_atmel_pmecc_put, sizeof(*ptr), GFP_KERNEL);
> if (!ptr)
> return ERR_PTR(-ENOMEM);

You forgot to call put_device() here. I'd suggest adding an
err_put_device label which calls put_device() before returning
ERR_PTR(ret).

>
> - get_device(&pdev->dev);
> pmecc = platform_get_drvdata(pdev);
>
> *ptr = pmecc;