]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mtd: Replace kcalloc() with devm_kcalloc()
authorJiasheng Jiang <jiashengjiangcool@gmail.com>
Wed, 5 Feb 2025 02:31:40 +0000 (02:31 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 2 May 2025 05:40:54 +0000 (07:40 +0200)
commit 1b61a59876f0eafc19b23007c522ee407f55dbec upstream.

Replace kcalloc() with devm_kcalloc() to prevent memory leaks in case of
errors.

Fixes: 78c08247b9d3 ("mtd: Support kmsg dumper based on pstore/blk")
Cc: stable@vger.kernel.org # v5.10+
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/mtd/mtdpstore.c

index a3ae8778f6a9b90312a7846e61b959911ab814e7..5594a22c0137543e12196fa7927c4703561b96f3 100644 (file)
@@ -417,11 +417,11 @@ static void mtdpstore_notify_add(struct mtd_info *mtd)
        }
 
        longcnt = BITS_TO_LONGS(div_u64(mtd->size, info->kmsg_size));
-       cxt->rmmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
-       cxt->usedmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
+       cxt->rmmap = devm_kcalloc(&mtd->dev, longcnt, sizeof(long), GFP_KERNEL);
+       cxt->usedmap = devm_kcalloc(&mtd->dev, longcnt, sizeof(long), GFP_KERNEL);
 
        longcnt = BITS_TO_LONGS(div_u64(mtd->size, mtd->erasesize));
-       cxt->badmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
+       cxt->badmap = devm_kcalloc(&mtd->dev, longcnt, sizeof(long), GFP_KERNEL);
 
        cxt->dev.total_size = mtd->size;
        /* just support dmesg right now */
@@ -527,9 +527,6 @@ static void mtdpstore_notify_remove(struct mtd_info *mtd)
        mtdpstore_flush_removed(cxt);
 
        unregister_pstore_device(&cxt->dev);
-       kfree(cxt->badmap);
-       kfree(cxt->usedmap);
-       kfree(cxt->rmmap);
        cxt->mtd = NULL;
        cxt->index = -1;
 }