[PATCH] mtd: rawnand: omap2: fix device leak on probe failure

Johan Hovold posted 1 patch 1 week, 2 days ago
drivers/mtd/nand/raw/omap2.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
[PATCH] mtd: rawnand: omap2: fix device leak on probe failure
Posted by Johan Hovold 1 week, 2 days ago
Make sure to drop the reference to the elm device taken by
of_find_device_by_node() during probe on errors and on driver unload.

Fixes: 62116e5171e0 ("mtd: nand: omap2: Support for hardware BCH error correction.")
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/mtd/nand/raw/omap2.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/drivers/mtd/nand/raw/omap2.c b/drivers/mtd/nand/raw/omap2.c
index b8af3a3533fc..61fd98ad5c78 100644
--- a/drivers/mtd/nand/raw/omap2.c
+++ b/drivers/mtd/nand/raw/omap2.c
@@ -1979,7 +1979,7 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
 		err = rawnand_sw_bch_init(chip);
 		if (err) {
 			dev_err(dev, "Unable to use BCH library\n");
-			return err;
+			goto err_put_elm_dev;
 		}
 		break;
 
@@ -2016,7 +2016,7 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
 		err = rawnand_sw_bch_init(chip);
 		if (err) {
 			dev_err(dev, "unable to use BCH library\n");
-			return err;
+			goto err_put_elm_dev;
 		}
 		break;
 
@@ -2054,7 +2054,8 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
 		break;
 	default:
 		dev_err(dev, "Invalid or unsupported ECC scheme\n");
-		return -EINVAL;
+		err = -EINVAL;
+		goto err_put_elm_dev;
 	}
 
 	if (elm_bch_strength >= 0) {
@@ -2073,7 +2074,7 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
 				 info->nsteps_per_eccpg, chip->ecc.size,
 				 chip->ecc.bytes);
 		if (err < 0)
-			return err;
+			goto err_put_elm_dev;
 	}
 
 	/* Check if NAND device's OOB is enough to store ECC signatures */
@@ -2083,10 +2084,24 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
 		dev_err(dev,
 			"Not enough OOB bytes: required = %d, available=%d\n",
 			min_oobbytes, mtd->oobsize);
-		return -EINVAL;
+		err = -EINVAL;
+		goto err_put_elm_dev;
 	}
 
 	return 0;
+
+err_put_elm_dev:
+	put_device(info->elm_dev);
+
+	return err;
+}
+
+static void omap_nand_detach_chip(struct nand_chip *chip)
+{
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	struct omap_nand_info *info = mtd_to_omap(mtd);
+
+	put_device(info->elm_dev);
 }
 
 static void omap_nand_data_in(struct nand_chip *chip, void *buf,
@@ -2187,6 +2202,7 @@ static int omap_nand_exec_op(struct nand_chip *chip,
 
 static const struct nand_controller_ops omap_nand_controller_ops = {
 	.attach_chip = omap_nand_attach_chip,
+	.detach_chip = omap_nand_detach_chip,
 	.exec_op = omap_nand_exec_op,
 };
 
-- 
2.49.1
Re: [PATCH] mtd: rawnand: omap2: fix device leak on probe failure
Posted by Miquel Raynal 2 days, 11 hours ago
On Mon, 22 Sep 2025 17:22:04 +0200, Johan Hovold wrote:
> Make sure to drop the reference to the elm device taken by
> of_find_device_by_node() during probe on errors and on driver unload.
> 
> 

Applied to nand/next, thanks!

[1/1] mtd: rawnand: omap2: fix device leak on probe failure
      commit: fa1f26b48fe43195aa0ac4badf5651063590326f

Patche(s) should be available on mtd/linux.git and will be
part of the next PR (provided that no robot complains by then).

Kind regards,
Miquèl

Re: [PATCH] mtd: rawnand: omap2: fix device leak on probe failure
Posted by Markus Elfring 1 week, 1 day ago
…
> +++ b/drivers/mtd/nand/raw/omap2.c
…
> @@ -2054,7 +2054,8 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
>  		break;
>  	default:
>  		dev_err(dev, "Invalid or unsupported ECC scheme\n");
> -		return -EINVAL;
> +		err = -EINVAL;
> +		goto err_put_elm_dev;
>  	}
>  
>  	if (elm_bch_strength >= 0) {
…
> @@ -2083,10 +2084,24 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
>  		dev_err(dev,
>  			"Not enough OOB bytes: required = %d, available=%d\n",
>  			min_oobbytes, mtd->oobsize);
> -		return -EINVAL;
> +		err = -EINVAL;
> +		goto err_put_elm_dev;
>  	}
…

May such an error code assignment be moved behind an additional label?

Regards,
Markus
Re: [PATCH] mtd: rawnand: omap2: fix device leak on probe failure
Posted by Miquel Raynal 1 week ago
Hi Markus,

>> @@ -2083,10 +2084,24 @@ static int omap_nand_attach_chip(struct nand_chip *chip)
>>  		dev_err(dev,
>>  			"Not enough OOB bytes: required = %d, available=%d\n",
>>  			min_oobbytes, mtd->oobsize);
>> -		return -EINVAL;
>> +		err = -EINVAL;
>> +		goto err_put_elm_dev;
>>  	}
> …
>
> May such an error code assignment be moved behind an additional label?

I don't think that's how labels are supposed to be used, current
proposal looks correct IMO.

Thanks,
Miquèl