]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mtd: rawnand: omap2: fix device leak on probe failure
authorJohan Hovold <johan@kernel.org>
Mon, 22 Sep 2025 15:22:04 +0000 (17:22 +0200)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Mon, 29 Sep 2025 15:55:40 +0000 (17:55 +0200)
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>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
drivers/mtd/nand/raw/omap2.c

index b8af3a3533fc57fb24b07906aa02c6187f7aeca7..61fd98ad5c784b4d9324faed59a2f64b9b0dc182 100644 (file)
@@ -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,
 };