[PATCH] mtd: mtdswap: Avoid freeing registered blktrans device twice

Ruoyu Wang posted 1 patch 1 week, 5 days ago
drivers/mtd/mtdswap.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] mtd: mtdswap: Avoid freeing registered blktrans device twice
Posted by Ruoyu Wang 1 week, 5 days ago
In mtdswap_add_mtd(), debugfs setup failure after successful blktrans
registration can free mbd_dev twice.

add_mtd_blktrans_dev() initializes the blktrans device reference and
publishes the disk. Once that succeeds, del_mtd_blktrans_dev() tears the
disk down and drops the blktrans reference; when that reference reaches
zero, blktrans_dev_release() frees the mtd_blktrans_dev.

The debugfs failure path called del_mtd_blktrans_dev(mbd_dev), then fell
through the common cleanup label and called kfree(mbd_dev) again. Clear
the local pointer after deregistration so the common cleanup can still
release the mtdswap state without freeing the blktrans object twice.

This issue was found by a static analysis checker and confirmed by
manual source review.

Fixes: e8e3edb95ce6 ("mtd: create per-device and module-scope debugfs entries")
Signed-off-by: Ruoyu Wang <ruoyuw560@gmail.com>
---
 drivers/mtd/mtdswap.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/mtd/mtdswap.c b/drivers/mtd/mtdswap.c
index 866933fc84265..533b8948918cb 100644
--- a/drivers/mtd/mtdswap.c
+++ b/drivers/mtd/mtdswap.c
@@ -1450,6 +1450,7 @@ static void mtdswap_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
 
 debugfs_failed:
 	del_mtd_blktrans_dev(mbd_dev);
+	mbd_dev = NULL;
 
 cleanup:
 	mtdswap_cleanup(d);
-- 
2.51.0
Re: [PATCH] mtd: mtdswap: Avoid freeing registered blktrans device twice
Posted by Miquel Raynal 3 days, 21 hours ago
On Wed, 08 Jul 2026 22:33:55 +0800, Ruoyu Wang wrote:
> In mtdswap_add_mtd(), debugfs setup failure after successful blktrans
> registration can free mbd_dev twice.
> 
> add_mtd_blktrans_dev() initializes the blktrans device reference and
> publishes the disk. Once that succeeds, del_mtd_blktrans_dev() tears the
> disk down and drops the blktrans reference; when that reference reaches
> zero, blktrans_dev_release() frees the mtd_blktrans_dev.
> 
> [...]

Applied to mtd/next, thanks!

[1/1] mtd: mtdswap: Avoid freeing registered blktrans device twice
      commit: 779aa4c66a96bf43d2d62982ea1a9096a9128d87

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