From: tze.yee.ng@altera.com
To: linux-kernel@vger.kernel.org,
Pratyush Yadav <pratyush@kernel.org>,
Michael Walle <mwalle@kernel.org>,
Takahiro Kuwano <takahiro.kuwano@infineon.com>,
linux-mtd@lists.infradead.org
Subject: [PATCH v3 1/2] mtd: core: drop the module reference after ->_put_device()
Date: Thu, 22 Oct 2026 11:53:51 +0800 [thread overview]
Message-ID: <20261022035352.52129-2-tze.yee.ng@altera.com> (raw)
In-Reply-To: <20261022035352.52129-1-tze.yee.ng@altera.com>
From: Tze Yee Ng <tze.yee.ng@altera.com>
__put_mtd_device() calls module_put() on the MTD owner and then calls
->_put_device(). If ->_put_device() frees the object that embeds the
mtd_info, it is still running code from the owner module after the
module reference is gone, so the module can be unloaded underneath it.
Call ->_put_device() first and module_put() last. ->_put_device() may
free master, so read master->owner beforehand.
The spi-nor fix that follows needs this: its ->_put_device() frees
struct spi_nor on the last put.
Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com>
---
Changes in v3:
- Reduced to the module_put() reorder. The core _free/removed hooks from
v2 are gone; spi-nor manages its own lifetime again (patch 2).
- __put_mtd_device() now reads master->owner first, calls ->_put_device()
and drops the module reference last (v2 left master->owner and
->_put_device() reachable after the core could free master).
Changes in v2:
- New.
- Drop the spi-nor-local kref and nor->removed.
- Add mtd->_free and mtd->removed in the core so the backing
object outlives unbind and later ops return -ENODEV.
---
drivers/mtd/mtdcore.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 491a27d0315f..1cf402e07b13 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -1435,6 +1435,8 @@ EXPORT_SYMBOL_GPL(put_mtd_device);
void __put_mtd_device(struct mtd_info *mtd)
{
struct mtd_info *master = mtd_get_master(mtd);
+ /* master can be freed in _put_device */
+ struct module *owner = master->owner;
while (mtd) {
/* kref_put() can relese mtd, so keep a reference mtd->parent */
@@ -1448,11 +1450,12 @@ void __put_mtd_device(struct mtd_info *mtd)
if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
kref_put(&master->refcnt, mtd_device_release);
- module_put(master->owner);
-
- /* must be the last as master can be freed in the _put_device */
+ /* must be the last use of master */
if (master->_put_device)
master->_put_device(master);
+
+ /* _put_device() runs code from @owner, so drop the module last */
+ module_put(owner);
}
EXPORT_SYMBOL_GPL(__put_mtd_device);
--
2.43.7
next prev parent reply other threads:[~2026-10-22 3:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 3:53 [PATCH v3 0/2] mtd: spi-nor: fix use-after-free on unbind with open handles tze.yee.ng
2026-10-22 3:53 ` tze.yee.ng [this message]
2026-10-22 3:53 ` [PATCH v3 2/2] " tze.yee.ng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261022035352.52129-2-tze.yee.ng@altera.com \
--to=tze.yee.ng@altera.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=mwalle@kernel.org \
--cc=pratyush@kernel.org \
--cc=takahiro.kuwano@infineon.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®