* [PATCH] mtd: take a reference when opening an unpartitioned master
@ 2026-11-08 21:12 James Hilliard
2026-11-20 14:54 ` Miquel Raynal
0 siblings, 1 reply; 2+ messages in thread
From: James Hilliard @ 2026-11-08 21:12 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
Cc: linux-mtd, linux-kernel, stable, James Hilliard
CONFIG_MTD_PARTITIONED_MASTER controls whether a master is registered
alongside its partitions. Without that option, an unpartitioned device
is still registered as a master and can be opened directly.
__get_mtd_device() skips the master while walking the parent chain and
only takes its reference when CONFIG_MTD_PARTITIONED_MASTER is enabled.
A direct open therefore leaves the registration reference as the sole
reference. Unregistering the device releases its device-model and NVMEM
state while the handle remains open.
Take and release the master reference for direct opens as well. Retain
the existing behavior for partition handles whose master is not
registered. Decide from the original handle before walking the parent
chain so get and put use the same condition.
Fixes: 264725e35fbc ("mtd: Clean refcounting with MTD_PARTITIONED_MASTER")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdcore.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 491a27d0315f..444615804236 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -1330,6 +1330,7 @@ EXPORT_SYMBOL_GPL(get_mtd_device);
int __get_mtd_device(struct mtd_info *mtd)
{
struct mtd_info *master = mtd_get_master(mtd);
+ bool master_ref = mtd == master || IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER);
int err;
if (master->_get_device) {
@@ -1350,7 +1351,7 @@ int __get_mtd_device(struct mtd_info *mtd)
mtd = mtd->parent;
}
- if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
+ if (master_ref)
kref_get(&master->refcnt);
return 0;
@@ -1435,6 +1436,7 @@ EXPORT_SYMBOL_GPL(put_mtd_device);
void __put_mtd_device(struct mtd_info *mtd)
{
struct mtd_info *master = mtd_get_master(mtd);
+ bool master_ref = mtd == master || IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER);
while (mtd) {
/* kref_put() can relese mtd, so keep a reference mtd->parent */
@@ -1445,7 +1447,7 @@ void __put_mtd_device(struct mtd_info *mtd)
mtd = parent;
}
- if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
+ if (master_ref)
kref_put(&master->refcnt, mtd_device_release);
module_put(master->owner);
---
base-commit: 1c1a342aceec79528b3ba51f376eca2be5928fe7
change-id: 20261108-submit-mtd-master-open-reference-v1-a1b756dcde47
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mtd: take a reference when opening an unpartitioned master
2026-11-08 21:12 [PATCH] mtd: take a reference when opening an unpartitioned master James Hilliard
@ 2026-11-20 14:54 ` Miquel Raynal
0 siblings, 0 replies; 2+ messages in thread
From: Miquel Raynal @ 2026-11-20 14:54 UTC (permalink / raw)
To: James Hilliard
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
stable
On 08/11/2026 at 14:12:39 -07, James Hilliard <james.hilliard1@gmail.com> wrote:
> CONFIG_MTD_PARTITIONED_MASTER controls whether a master is registered
> alongside its partitions. Without that option, an unpartitioned device
> is still registered as a master and can be opened directly.
"can be opened directly"
> __get_mtd_device() skips the master while walking the parent chain and
> only takes its reference when CONFIG_MTD_PARTITIONED_MASTER is enabled.
> A direct open therefore leaves the registration reference as the sole
> reference. Unregistering the device releases its device-model and
> NVMEM
"releases its device-model"
> state while the handle remains open.
>
> Take and release the master reference for direct opens as well. Retain
> the existing behavior for partition handles whose master is not
> registered.
Is this really what you do? The code does not test whether the
master is registered. The condition is:
mtd == master || IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)
As a result:
- A handle on the master takes a master reference.
- A handle on a partition takes a master reference only when
CONFIG_MTD_PARTITIONED_MASTER is enabled.
No? I believe the code is correct, but that's again one of those very
imprecise commit logs where words taken independently are fine but put
alltogether don't build any meaningful message in my brain. Maybe:
A handle on the master now takes a master reference. A handle on a
partition takes a master reference only when CONFIG_MTD_PARTITIONED_MASTER
is enabled.
In general I think this commit log must be reworded.
Thanks,
Miquèl
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 14:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-08 21:12 [PATCH] mtd: take a reference when opening an unpartitioned master James Hilliard
2026-11-20 14:54 ` Miquel Raynal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox