* [PATCH v2] mtd: serialize dynamic partition registration with removal
@ 2026-11-20 16:02 ` James Hilliard
0 siblings, 0 replies; 6+ messages in thread
From: James Hilliard @ 2026-11-20 16:02 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
Cc: linux-mtd, linux-kernel, stable, James Hilliard
If a flash driver is unbound while userspace adds a partition, removal
can find the new partition before it is registered or release it before
its sysfs attributes are added. If removal finishes first, the ioctl can
even register a new partition on a master that has been unregistered.
Hold partitions_lock from the start of partition allocation until the
device and its attributes are registered. Mark the master as being
unregistered under the same lock so a later ioctl fails before allocation
can read bad-block markers. Clear the flag when registering the master.
Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v2:
- Use mutex guards to keep publication and teardown lock scopes explicit.
- Link to v1: https://patch.msgid.link/20261108-submit-mtd-partition-publication-v1-v1-1-cbff20b1351e@gmail.com
---
drivers/mtd/mtdcore.c | 4 ++++
drivers/mtd/mtdpart.c | 9 ++++-----
include/linux/mtd/mtd.h | 2 ++
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index b170c3144ca8..5ee1eb29fc1a 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -918,6 +918,7 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd)
INIT_LIST_HEAD(&mtd->partitions);
mutex_init(&mtd->master.partitions_lock);
mutex_init(&mtd->master.chrdev_lock);
+ mtd->master.unregistering = false;
}
static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
@@ -1206,6 +1207,9 @@ int mtd_device_unregister(struct mtd_info *master)
{
int err;
+ scoped_guard(mutex, &master->master.partitions_lock)
+ master->master.unregistering = true;
+
if (master->_reboot) {
unregister_reboot_notifier(&master->reboot_notifier);
memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index ddded0dbe77e..b20fa4309084 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -278,13 +278,15 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
part.size = length;
part.offset = offset;
+ guard(mutex)(&master->master.partitions_lock);
+ if (master->master.unregistering)
+ return -ENODEV;
+
child = allocate_partition(parent, &part, -1, offset);
if (IS_ERR(child))
return PTR_ERR(child);
- mutex_lock(&master->master.partitions_lock);
list_add_tail(&child->part.node, &parent->partitions);
- mutex_unlock(&master->master.partitions_lock);
ret = add_mtd_device(child);
if (ret)
@@ -295,10 +297,7 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
return 0;
err_remove_part:
- mutex_lock(&master->master.partitions_lock);
list_del(&child->part.node);
- mutex_unlock(&master->master.partitions_lock);
-
free_partition(child);
return ret;
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 8d10d9d2e830..cd7b4671c456 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -224,6 +224,7 @@ struct mtd_part {
* not only the master partition list, but also all
* sub-partitions.
* @suspended: set to 1 when the device is suspended, 0 otherwise
+ * @unregistering: removal has started; protected by partitions_lock
*
* This struct is embedded in mtd_info and contains master-specific
* properties/fields. The master is the root MTD device from the MTD partition
@@ -233,6 +234,7 @@ struct mtd_master {
struct mutex partitions_lock;
struct mutex chrdev_lock;
unsigned int suspended : 1;
+ bool unregistering;
};
struct mtd_info {
---
base-commit: 14b6830335eb6d70dec7a235094d66c22d38eb07
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes
change-id: 20261108-submit-mtd-partition-publication-v1-4629be94d7e6
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2] mtd: serialize dynamic partition registration with removal
@ 2026-11-20 16:02 ` James Hilliard
0 siblings, 0 replies; 6+ messages in thread
From: James Hilliard @ 2026-11-20 16:02 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
Cc: linux-mtd, linux-kernel, stable, James Hilliard
If a flash driver is unbound while userspace adds a partition, removal
can find the new partition before it is registered or release it before
its sysfs attributes are added. If removal finishes first, the ioctl can
even register a new partition on a master that has been unregistered.
Hold partitions_lock from the start of partition allocation until the
device and its attributes are registered. Mark the master as being
unregistered under the same lock so a later ioctl fails before allocation
can read bad-block markers. Clear the flag when registering the master.
Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v2:
- Use mutex guards to keep publication and teardown lock scopes explicit.
- Link to v1: https://patch.msgid.link/20261108-submit-mtd-partition-publication-v1-v1-1-cbff20b1351e@gmail.com
---
drivers/mtd/mtdcore.c | 4 ++++
drivers/mtd/mtdpart.c | 9 ++++-----
include/linux/mtd/mtd.h | 2 ++
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index b170c3144ca8..5ee1eb29fc1a 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -918,6 +918,7 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd)
INIT_LIST_HEAD(&mtd->partitions);
mutex_init(&mtd->master.partitions_lock);
mutex_init(&mtd->master.chrdev_lock);
+ mtd->master.unregistering = false;
}
static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
@@ -1206,6 +1207,9 @@ int mtd_device_unregister(struct mtd_info *master)
{
int err;
+ scoped_guard(mutex, &master->master.partitions_lock)
+ master->master.unregistering = true;
+
if (master->_reboot) {
unregister_reboot_notifier(&master->reboot_notifier);
memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index ddded0dbe77e..b20fa4309084 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -278,13 +278,15 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
part.size = length;
part.offset = offset;
+ guard(mutex)(&master->master.partitions_lock);
+ if (master->master.unregistering)
+ return -ENODEV;
+
child = allocate_partition(parent, &part, -1, offset);
if (IS_ERR(child))
return PTR_ERR(child);
- mutex_lock(&master->master.partitions_lock);
list_add_tail(&child->part.node, &parent->partitions);
- mutex_unlock(&master->master.partitions_lock);
ret = add_mtd_device(child);
if (ret)
@@ -295,10 +297,7 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
return 0;
err_remove_part:
- mutex_lock(&master->master.partitions_lock);
list_del(&child->part.node);
- mutex_unlock(&master->master.partitions_lock);
-
free_partition(child);
return ret;
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 8d10d9d2e830..cd7b4671c456 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -224,6 +224,7 @@ struct mtd_part {
* not only the master partition list, but also all
* sub-partitions.
* @suspended: set to 1 when the device is suspended, 0 otherwise
+ * @unregistering: removal has started; protected by partitions_lock
*
* This struct is embedded in mtd_info and contains master-specific
* properties/fields. The master is the root MTD device from the MTD partition
@@ -233,6 +234,7 @@ struct mtd_master {
struct mutex partitions_lock;
struct mutex chrdev_lock;
unsigned int suspended : 1;
+ bool unregistering;
};
struct mtd_info {
---
base-commit: 14b6830335eb6d70dec7a235094d66c22d38eb07
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes
change-id: 20261108-submit-mtd-partition-publication-v1-4629be94d7e6
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] mtd: serialize dynamic partition registration with removal
2026-11-20 16:02 ` James Hilliard
@ 2026-11-24 10:23 ` Miquel Raynal
-1 siblings, 0 replies; 6+ messages in thread
From: Miquel Raynal @ 2026-11-24 10:23 UTC (permalink / raw)
To: James Hilliard
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
stable
On 20/11/2026 at 09:02:06 -07, James Hilliard <james.hilliard1@gmail.com> wrote:
> If a flash driver is unbound while userspace adds a partition, removal
> can find the new partition before it is registered or release it before
> its sysfs attributes are added. If removal finishes first, the ioctl can
> even register a new partition on a master that has been unregistered.
>
> Hold partitions_lock from the start of partition allocation until the
> device and its attributes are registered. Mark the master as being
> unregistered under the same lock so a later ioctl fails before allocation
> can read bad-block markers. Clear the flag when registering the master.
>
> Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests")
> Cc: stable@vger.kernel.org
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
> Changes in v2:
> - Use mutex guards to keep publication and teardown lock scopes explicit.
> - Link to v1: https://patch.msgid.link/20261108-submit-mtd-partition-publication-v1-v1-1-cbff20b1351e@gmail.com
> ---
> drivers/mtd/mtdcore.c | 4 ++++
> drivers/mtd/mtdpart.c | 9 ++++-----
> include/linux/mtd/mtd.h | 2 ++
> 3 files changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> index b170c3144ca8..5ee1eb29fc1a 100644
> --- a/drivers/mtd/mtdcore.c
> +++ b/drivers/mtd/mtdcore.c
> @@ -918,6 +918,7 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd)
> INIT_LIST_HEAD(&mtd->partitions);
> mutex_init(&mtd->master.partitions_lock);
> mutex_init(&mtd->master.chrdev_lock);
> + mtd->master.unregistering = false;
Maybe this is worth a WRITE_ONCE() to tell robots that there is no
possible conflict on this access?
> }
>
> static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
> @@ -1206,6 +1207,9 @@ int mtd_device_unregister(struct mtd_info *master)
> {
> int err;
>
> + scoped_guard(mutex, &master->master.partitions_lock)
> + master->master.unregistering = true;
> +
Actually I'm not sure my proposal actually fits. It does not make the
logic clearer. At least not implemented like that (and below).
> if (master->_reboot) {
> unregister_reboot_notifier(&master->reboot_notifier);
> memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
> diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> index ddded0dbe77e..b20fa4309084 100644
> --- a/drivers/mtd/mtdpart.c
> +++ b/drivers/mtd/mtdpart.c
> @@ -278,13 +278,15 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> part.size = length;
> part.offset = offset;
>
> + guard(mutex)(&master->master.partitions_lock);
> + if (master->master.unregistering)
> + return -ENODEV;
> +
> child = allocate_partition(parent, &part, -1, offset);
> if (IS_ERR(child))
> return PTR_ERR(child);
>
> - mutex_lock(&master->master.partitions_lock);
> list_add_tail(&child->part.node, &parent->partitions);
> - mutex_unlock(&master->master.partitions_lock);
That mutex becomes super wide. I'm not a super big fan of it. Isn't
there a possibility of deadlock with the notifiers or any other lock
inside? Would there an alternative?
>
> ret = add_mtd_device(child);
> if (ret)
In case of failure, should we clear the boolean?
> @@ -295,10 +297,7 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> return 0;
>
> err_remove_part:
> - mutex_lock(&master->master.partitions_lock);
> list_del(&child->part.node);
> - mutex_unlock(&master->master.partitions_lock);
> -
I think there is a rule where we should not use those guards with goto
statements.
> free_partition(child);
>
> return ret;
> diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
> index 8d10d9d2e830..cd7b4671c456 100644
> --- a/include/linux/mtd/mtd.h
> +++ b/include/linux/mtd/mtd.h
> @@ -224,6 +224,7 @@ struct mtd_part {
> * not only the master partition list, but also all
> * sub-partitions.
> * @suspended: set to 1 when the device is suspended, 0 otherwise
> + * @unregistering: removal has started; protected by partitions_lock
> *
> * This struct is embedded in mtd_info and contains master-specific
> * properties/fields. The master is the root MTD device from the MTD partition
> @@ -233,6 +234,7 @@ struct mtd_master {
> struct mutex partitions_lock;
> struct mutex chrdev_lock;
> unsigned int suspended : 1;
> + bool unregistering;
Maybe unsigned int xxx : 1 to be consistent?
> };
>
> struct mtd_info {
>
> ---
> base-commit: 14b6830335eb6d70dec7a235094d66c22d38eb07
> base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes
> change-id: 20261108-submit-mtd-partition-publication-v1-4629be94d7e6
>
> Best regards,
Miquèl
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] mtd: serialize dynamic partition registration with removal
@ 2026-11-24 10:23 ` Miquel Raynal
0 siblings, 0 replies; 6+ messages in thread
From: Miquel Raynal @ 2026-11-24 10:23 UTC (permalink / raw)
To: James Hilliard
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
stable
On 20/11/2026 at 09:02:06 -07, James Hilliard <james.hilliard1@gmail.com> wrote:
> If a flash driver is unbound while userspace adds a partition, removal
> can find the new partition before it is registered or release it before
> its sysfs attributes are added. If removal finishes first, the ioctl can
> even register a new partition on a master that has been unregistered.
>
> Hold partitions_lock from the start of partition allocation until the
> device and its attributes are registered. Mark the master as being
> unregistered under the same lock so a later ioctl fails before allocation
> can read bad-block markers. Clear the flag when registering the master.
>
> Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests")
> Cc: stable@vger.kernel.org
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
> Changes in v2:
> - Use mutex guards to keep publication and teardown lock scopes explicit.
> - Link to v1: https://patch.msgid.link/20261108-submit-mtd-partition-publication-v1-v1-1-cbff20b1351e@gmail.com
> ---
> drivers/mtd/mtdcore.c | 4 ++++
> drivers/mtd/mtdpart.c | 9 ++++-----
> include/linux/mtd/mtd.h | 2 ++
> 3 files changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> index b170c3144ca8..5ee1eb29fc1a 100644
> --- a/drivers/mtd/mtdcore.c
> +++ b/drivers/mtd/mtdcore.c
> @@ -918,6 +918,7 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd)
> INIT_LIST_HEAD(&mtd->partitions);
> mutex_init(&mtd->master.partitions_lock);
> mutex_init(&mtd->master.chrdev_lock);
> + mtd->master.unregistering = false;
Maybe this is worth a WRITE_ONCE() to tell robots that there is no
possible conflict on this access?
> }
>
> static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
> @@ -1206,6 +1207,9 @@ int mtd_device_unregister(struct mtd_info *master)
> {
> int err;
>
> + scoped_guard(mutex, &master->master.partitions_lock)
> + master->master.unregistering = true;
> +
Actually I'm not sure my proposal actually fits. It does not make the
logic clearer. At least not implemented like that (and below).
> if (master->_reboot) {
> unregister_reboot_notifier(&master->reboot_notifier);
> memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
> diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> index ddded0dbe77e..b20fa4309084 100644
> --- a/drivers/mtd/mtdpart.c
> +++ b/drivers/mtd/mtdpart.c
> @@ -278,13 +278,15 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> part.size = length;
> part.offset = offset;
>
> + guard(mutex)(&master->master.partitions_lock);
> + if (master->master.unregistering)
> + return -ENODEV;
> +
> child = allocate_partition(parent, &part, -1, offset);
> if (IS_ERR(child))
> return PTR_ERR(child);
>
> - mutex_lock(&master->master.partitions_lock);
> list_add_tail(&child->part.node, &parent->partitions);
> - mutex_unlock(&master->master.partitions_lock);
That mutex becomes super wide. I'm not a super big fan of it. Isn't
there a possibility of deadlock with the notifiers or any other lock
inside? Would there an alternative?
>
> ret = add_mtd_device(child);
> if (ret)
In case of failure, should we clear the boolean?
> @@ -295,10 +297,7 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> return 0;
>
> err_remove_part:
> - mutex_lock(&master->master.partitions_lock);
> list_del(&child->part.node);
> - mutex_unlock(&master->master.partitions_lock);
> -
I think there is a rule where we should not use those guards with goto
statements.
> free_partition(child);
>
> return ret;
> diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
> index 8d10d9d2e830..cd7b4671c456 100644
> --- a/include/linux/mtd/mtd.h
> +++ b/include/linux/mtd/mtd.h
> @@ -224,6 +224,7 @@ struct mtd_part {
> * not only the master partition list, but also all
> * sub-partitions.
> * @suspended: set to 1 when the device is suspended, 0 otherwise
> + * @unregistering: removal has started; protected by partitions_lock
> *
> * This struct is embedded in mtd_info and contains master-specific
> * properties/fields. The master is the root MTD device from the MTD partition
> @@ -233,6 +234,7 @@ struct mtd_master {
> struct mutex partitions_lock;
> struct mutex chrdev_lock;
> unsigned int suspended : 1;
> + bool unregistering;
Maybe unsigned int xxx : 1 to be consistent?
> };
>
> struct mtd_info {
>
> ---
> base-commit: 14b6830335eb6d70dec7a235094d66c22d38eb07
> base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes
> change-id: 20261108-submit-mtd-partition-publication-v1-4629be94d7e6
>
> Best regards,
Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] mtd: serialize dynamic partition registration with removal
2026-11-24 10:23 ` Miquel Raynal
@ 2026-11-24 16:31 ` James Hilliard
-1 siblings, 0 replies; 6+ messages in thread
From: James Hilliard @ 2026-11-24 16:31 UTC (permalink / raw)
To: Miquel Raynal
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
stable
On Tue, Nov 24, 2026 at 3:23 AM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
>
> On 20/11/2026 at 09:02:06 -07, James Hilliard <james.hilliard1@gmail.com> wrote:
>
> > If a flash driver is unbound while userspace adds a partition, removal
> > can find the new partition before it is registered or release it before
> > its sysfs attributes are added. If removal finishes first, the ioctl can
> > even register a new partition on a master that has been unregistered.
> >
> > Hold partitions_lock from the start of partition allocation until the
> > device and its attributes are registered. Mark the master as being
> > unregistered under the same lock so a later ioctl fails before allocation
> > can read bad-block markers. Clear the flag when registering the master.
> >
> > Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > ---
> > Changes in v2:
> > - Use mutex guards to keep publication and teardown lock scopes explicit.
> > - Link to v1: https://patch.msgid.link/20261108-submit-mtd-partition-publication-v1-v1-1-cbff20b1351e@gmail.com
> > ---
> > drivers/mtd/mtdcore.c | 4 ++++
> > drivers/mtd/mtdpart.c | 9 ++++-----
> > include/linux/mtd/mtd.h | 2 ++
> > 3 files changed, 10 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> > index b170c3144ca8..5ee1eb29fc1a 100644
> > --- a/drivers/mtd/mtdcore.c
> > +++ b/drivers/mtd/mtdcore.c
> > @@ -918,6 +918,7 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd)
> > INIT_LIST_HEAD(&mtd->partitions);
> > mutex_init(&mtd->master.partitions_lock);
> > mutex_init(&mtd->master.chrdev_lock);
> > + mtd->master.unregistering = false;
>
> Maybe this is worth a WRITE_ONCE() to tell robots that there is no
> possible conflict on this access?
This initializes the flag alongside the two mutexes, before the master
is published. A plain store should be enough here. After publication,
the add and unregister paths access the flag under the same mutex.
>
> > }
> >
> > static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
> > @@ -1206,6 +1207,9 @@ int mtd_device_unregister(struct mtd_info *master)
> > {
> > int err;
> >
> > + scoped_guard(mutex, &master->master.partitions_lock)
> > + master->master.unregistering = true;
> > +
>
> Actually I'm not sure my proposal actually fits. It does not make the
> logic clearer. At least not implemented like that (and below).
>
> > if (master->_reboot) {
> > unregister_reboot_notifier(&master->reboot_notifier);
> > memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
> > diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> > index ddded0dbe77e..b20fa4309084 100644
> > --- a/drivers/mtd/mtdpart.c
> > +++ b/drivers/mtd/mtdpart.c
> > @@ -278,13 +278,15 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> > part.size = length;
> > part.offset = offset;
> >
> > + guard(mutex)(&master->master.partitions_lock);
> > + if (master->master.unregistering)
> > + return -ENODEV;
> > +
> > child = allocate_partition(parent, &part, -1, offset);
> > if (IS_ERR(child))
> > return PTR_ERR(child);
> >
> > - mutex_lock(&master->master.partitions_lock);
> > list_add_tail(&child->part.node, &parent->partitions);
> > - mutex_unlock(&master->master.partitions_lock);
>
> That mutex becomes super wide. I'm not a super big fan of it. Isn't
> there a possibility of deadlock with the notifiers or any other lock
> inside? Would there an alternative?
I haven't found a specific deadlock, but v2 does call the MTD add
notifiers with both partitions_lock and mtd_table_mutex held.
Could we use the lock that already covers BLKPG instead? Both the
native and compat ioctls hold the master's chrdev_lock until the add
returns. Unregister could take that lock to set the flag and release
it before starting teardown. If the add gets there first, it finishes
setting up the child's sysfs files before removal starts. If unregister
gets there first, the add returns -ENODEV before scanning for bad blocks.
That keeps partitions_lock out of the add notifiers. The exported
helpers would need the same protection for direct callers, without
taking chrdev_lock twice from BLKPG. One tradeoff is that unregister
would also wait for unrelated ioctls on that master, so the lock order
on the unregister side would still need checking.
>
> >
> > ret = add_mtd_device(child);
> > if (ret)
>
> In case of failure, should we clear the boolean?
Not when add_mtd_device(child) fails. Only master removal sets the
flag; a failed add just removes its list entry and frees the child.
If master removal itself fails, some partitions or OTP providers may
already be gone, so it wouldn't be safe to allow new adds again.
>
> > @@ -295,10 +297,7 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> > return 0;
> >
> > err_remove_part:
> > - mutex_lock(&master->master.partitions_lock);
> > list_del(&child->part.node);
> > - mutex_unlock(&master->master.partitions_lock);
> > -
>
> I think there is a rule where we should not use those guards with goto
> statements.
Using explicit lock/unlock for the list updates would let us keep the
existing error label without mixing the two cleanup styles.
>
> > free_partition(child);
> >
> > return ret;
> > diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
> > index 8d10d9d2e830..cd7b4671c456 100644
> > --- a/include/linux/mtd/mtd.h
> > +++ b/include/linux/mtd/mtd.h
> > @@ -224,6 +224,7 @@ struct mtd_part {
> > * not only the master partition list, but also all
> > * sub-partitions.
> > * @suspended: set to 1 when the device is suspended, 0 otherwise
> > + * @unregistering: removal has started; protected by partitions_lock
> > *
> > * This struct is embedded in mtd_info and contains master-specific
> > * properties/fields. The master is the root MTD device from the MTD partition
> > @@ -233,6 +234,7 @@ struct mtd_master {
> > struct mutex partitions_lock;
> > struct mutex chrdev_lock;
> > unsigned int suspended : 1;
> > + bool unregistering;
>
> Maybe unsigned int xxx : 1 to be consistent?
The adjacent suspended bit is changed by mtd_suspend() and mtd_resume()
without taking the same mutex. With adjacent bitfields, setting one can
write back the byte containing both. A separate bool avoids that.
>
> > };
> >
> > struct mtd_info {
> >
> > ---
> > base-commit: 14b6830335eb6d70dec7a235094d66c22d38eb07
> > base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes
> > change-id: 20261108-submit-mtd-partition-publication-v1-4629be94d7e6
> >
> > Best regards,
>
> Miquèl
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] mtd: serialize dynamic partition registration with removal
@ 2026-11-24 16:31 ` James Hilliard
0 siblings, 0 replies; 6+ messages in thread
From: James Hilliard @ 2026-11-24 16:31 UTC (permalink / raw)
To: Miquel Raynal
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
stable
On Tue, Nov 24, 2026 at 3:23 AM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
>
> On 20/11/2026 at 09:02:06 -07, James Hilliard <james.hilliard1@gmail.com> wrote:
>
> > If a flash driver is unbound while userspace adds a partition, removal
> > can find the new partition before it is registered or release it before
> > its sysfs attributes are added. If removal finishes first, the ioctl can
> > even register a new partition on a master that has been unregistered.
> >
> > Hold partitions_lock from the start of partition allocation until the
> > device and its attributes are registered. Mark the master as being
> > unregistered under the same lock so a later ioctl fails before allocation
> > can read bad-block markers. Clear the flag when registering the master.
> >
> > Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > ---
> > Changes in v2:
> > - Use mutex guards to keep publication and teardown lock scopes explicit.
> > - Link to v1: https://patch.msgid.link/20261108-submit-mtd-partition-publication-v1-v1-1-cbff20b1351e@gmail.com
> > ---
> > drivers/mtd/mtdcore.c | 4 ++++
> > drivers/mtd/mtdpart.c | 9 ++++-----
> > include/linux/mtd/mtd.h | 2 ++
> > 3 files changed, 10 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> > index b170c3144ca8..5ee1eb29fc1a 100644
> > --- a/drivers/mtd/mtdcore.c
> > +++ b/drivers/mtd/mtdcore.c
> > @@ -918,6 +918,7 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd)
> > INIT_LIST_HEAD(&mtd->partitions);
> > mutex_init(&mtd->master.partitions_lock);
> > mutex_init(&mtd->master.chrdev_lock);
> > + mtd->master.unregistering = false;
>
> Maybe this is worth a WRITE_ONCE() to tell robots that there is no
> possible conflict on this access?
This initializes the flag alongside the two mutexes, before the master
is published. A plain store should be enough here. After publication,
the add and unregister paths access the flag under the same mutex.
>
> > }
> >
> > static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
> > @@ -1206,6 +1207,9 @@ int mtd_device_unregister(struct mtd_info *master)
> > {
> > int err;
> >
> > + scoped_guard(mutex, &master->master.partitions_lock)
> > + master->master.unregistering = true;
> > +
>
> Actually I'm not sure my proposal actually fits. It does not make the
> logic clearer. At least not implemented like that (and below).
>
> > if (master->_reboot) {
> > unregister_reboot_notifier(&master->reboot_notifier);
> > memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
> > diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> > index ddded0dbe77e..b20fa4309084 100644
> > --- a/drivers/mtd/mtdpart.c
> > +++ b/drivers/mtd/mtdpart.c
> > @@ -278,13 +278,15 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> > part.size = length;
> > part.offset = offset;
> >
> > + guard(mutex)(&master->master.partitions_lock);
> > + if (master->master.unregistering)
> > + return -ENODEV;
> > +
> > child = allocate_partition(parent, &part, -1, offset);
> > if (IS_ERR(child))
> > return PTR_ERR(child);
> >
> > - mutex_lock(&master->master.partitions_lock);
> > list_add_tail(&child->part.node, &parent->partitions);
> > - mutex_unlock(&master->master.partitions_lock);
>
> That mutex becomes super wide. I'm not a super big fan of it. Isn't
> there a possibility of deadlock with the notifiers or any other lock
> inside? Would there an alternative?
I haven't found a specific deadlock, but v2 does call the MTD add
notifiers with both partitions_lock and mtd_table_mutex held.
Could we use the lock that already covers BLKPG instead? Both the
native and compat ioctls hold the master's chrdev_lock until the add
returns. Unregister could take that lock to set the flag and release
it before starting teardown. If the add gets there first, it finishes
setting up the child's sysfs files before removal starts. If unregister
gets there first, the add returns -ENODEV before scanning for bad blocks.
That keeps partitions_lock out of the add notifiers. The exported
helpers would need the same protection for direct callers, without
taking chrdev_lock twice from BLKPG. One tradeoff is that unregister
would also wait for unrelated ioctls on that master, so the lock order
on the unregister side would still need checking.
>
> >
> > ret = add_mtd_device(child);
> > if (ret)
>
> In case of failure, should we clear the boolean?
Not when add_mtd_device(child) fails. Only master removal sets the
flag; a failed add just removes its list entry and frees the child.
If master removal itself fails, some partitions or OTP providers may
already be gone, so it wouldn't be safe to allow new adds again.
>
> > @@ -295,10 +297,7 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> > return 0;
> >
> > err_remove_part:
> > - mutex_lock(&master->master.partitions_lock);
> > list_del(&child->part.node);
> > - mutex_unlock(&master->master.partitions_lock);
> > -
>
> I think there is a rule where we should not use those guards with goto
> statements.
Using explicit lock/unlock for the list updates would let us keep the
existing error label without mixing the two cleanup styles.
>
> > free_partition(child);
> >
> > return ret;
> > diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
> > index 8d10d9d2e830..cd7b4671c456 100644
> > --- a/include/linux/mtd/mtd.h
> > +++ b/include/linux/mtd/mtd.h
> > @@ -224,6 +224,7 @@ struct mtd_part {
> > * not only the master partition list, but also all
> > * sub-partitions.
> > * @suspended: set to 1 when the device is suspended, 0 otherwise
> > + * @unregistering: removal has started; protected by partitions_lock
> > *
> > * This struct is embedded in mtd_info and contains master-specific
> > * properties/fields. The master is the root MTD device from the MTD partition
> > @@ -233,6 +234,7 @@ struct mtd_master {
> > struct mutex partitions_lock;
> > struct mutex chrdev_lock;
> > unsigned int suspended : 1;
> > + bool unregistering;
>
> Maybe unsigned int xxx : 1 to be consistent?
The adjacent suspended bit is changed by mtd_suspend() and mtd_resume()
without taking the same mutex. With adjacent bitfields, setting one can
write back the byte containing both. A separate bool avoids that.
>
> > };
> >
> > struct mtd_info {
> >
> > ---
> > base-commit: 14b6830335eb6d70dec7a235094d66c22d38eb07
> > base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes
> > change-id: 20261108-submit-mtd-partition-publication-v1-4629be94d7e6
> >
> > Best regards,
>
> Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-24 16:31 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 16:02 [PATCH v2] mtd: serialize dynamic partition registration with removal James Hilliard
2026-11-20 16:02 ` James Hilliard
2026-11-24 10:23 ` Miquel Raynal
2026-11-24 10:23 ` Miquel Raynal
2026-11-24 16:31 ` James Hilliard
2026-11-24 16:31 ` James Hilliard
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.