Skip to contentSkip to conversation

mtd: rawnand: omap2: fix device leak on probe failure

PATCH Applied Johan Hovold 3 participants 4 messages Sep 22, 2025 – Sep 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: rawnand: omap2: fix device leak on probe failure

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.")
---
 drivers/mtd/nand/raw/omap2.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
drivers/mtd/nand/raw/omap2.c +21 -5
index b8af3a3533fc..61fd98ad5c78 100644
@@ -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,
 };
 
Signature · 1 line
2.49.1
#1 lore raw

…

Quoted 1 line

+++ b/drivers/mtd/nand/raw/omap2.c

…

Quoted 10 lines
drivers/mtd/nand/raw/omap2.c +2 -1
@@ -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) {

…

drivers/mtd/nand/raw/omap2.c static int omap_nand_attach_chip(struct nand_chip *chip) · line 2089

Quoted 8 lines
drivers/mtd/nand/raw/omap2.c +2 -1
@@ -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

maintainer
#2 lore raw

Hi Markus,

Quoted 11 lines

drivers/mtd/nand/raw/omap2.c static int omap_nand_attach_chip(struct nand_chip *chip) · line 2089

Quoted 8 lines
drivers/mtd/nand/raw/omap2.c +2 -1
@@ -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

maintainer Applied
#3 lore raw
In reply to Johan Hovold
Quoted 4 lines · Johan Hovold

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