[PATCH] mtd: mtdoops: free page bitmap when the backing MTD is removed

raoxu posted 1 patch 1 week, 5 days ago
drivers/mtd/mtdoops.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] mtd: mtdoops: free page bitmap when the backing MTD is removed
Posted by raoxu 1 week, 5 days ago
From: Xu Rao <raoxu@uniontech.com>

mtdoops_notify_add() allocates oops_page_used when the configured MTD
device is registered.  mtdoops_notify_remove() detaches from that device
but leaves the bitmap allocated.  If the same MTD device is later
registered again, the add path allocates a new bitmap and overwrites the
old pointer, leaking one vmalloc allocation per remove/add cycle.

This is only visible when the backing MTD device can disappear and be
registered again while mtdoops remains loaded, so the usual static MTD
case does not expose it.

Free the bitmap after unregistering the dumper and flushing the pending
workers, then clear the pointer and page count before a later attach can
allocate fresh state.  Clearing the pointer also keeps the module exit
path from freeing the same bitmap a second time after a remove event.

Fixes: be95745f0167 ("mtd: mtdoops: keep track of used/unused pages in an array")
Cc: stable@vger.kernel.org
Signed-off-by: Xu Rao <raoxu@uniontech.com>
---
 drivers/mtd/mtdoops.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/mtd/mtdoops.c b/drivers/mtd/mtdoops.c
index 39df7ce8f55f..1da7ef06a7a9 100644
--- a/drivers/mtd/mtdoops.c
+++ b/drivers/mtd/mtdoops.c
@@ -392,6 +392,9 @@ static void mtdoops_notify_remove(struct mtd_info *mtd)
 	cxt->mtd = NULL;
 	flush_work(&cxt->work_erase);
 	flush_work(&cxt->work_write);
+	vfree(cxt->oops_page_used);
+	cxt->oops_page_used = NULL;
+	cxt->oops_pages = 0;
 }


--
2.50.1
Re: [PATCH] mtd: mtdoops: free page bitmap when the backing MTD is removed
Posted by Miquel Raynal 1 week, 1 day ago
On Mon, 13 Jul 2026 13:22:03 +0800, raoxu wrote:
> mtdoops_notify_add() allocates oops_page_used when the configured MTD
> device is registered.  mtdoops_notify_remove() detaches from that device
> but leaves the bitmap allocated.  If the same MTD device is later
> registered again, the add path allocates a new bitmap and overwrites the
> old pointer, leaking one vmalloc allocation per remove/add cycle.
> 
> This is only visible when the backing MTD device can disappear and be
> registered again while mtdoops remains loaded, so the usual static MTD
> case does not expose it.
> 
> [...]

Applied to mtd/next, thanks!

[1/1] mtd: mtdoops: free page bitmap when the backing MTD is removed
      commit: 956e7da12c114f13c63d126ab1d79c3b6a819060

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