On Sun, 3 Feb 2019 09:37:31 +0000
wen yang <[email protected]> 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 <[email protected]>
> Cc: Tudor Ambarus <[email protected]>
> Cc: Boris Brezillon <[email protected]>
> Cc: Miquel Raynal <[email protected]>
> Cc: Richard Weinberger <[email protected]>
> Cc: David Woodhouse <[email protected]>
> Cc: Brian Norris <[email protected]>
> Cc: Marek Vasut <[email protected]>
> Cc: Nicolas Ferre <[email protected]>
> Cc: Alexandre Belloni <[email protected]>
> Cc: Ludovic Desroches <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> ---
> 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;