Re: [PATCH] mtd: serialize dynamic partition registration with removal
From: Miquel Raynal
Date: Fri Nov 20 2026 - 10:04:02 EST
On 08/11/2026 at 14:13:59 -07, James Hilliard <james.hilliard1@xxxxxxxxx> wrote:
> mtd_add_partition() drops partitions_lock after linking the new child
> but before registering its MTD device and adding the partition sysfs
> attributes. Concurrent removal can unlink that child before it has been
> registered, or release it after registration but before the attributes
> are added. It can also finish deleting partitions before a new child
> is linked, leaving a partition published after master removal.
>
> Keep partitions_lock held across allocation, publication and attribute
> creation. Record that master unregistration has started under the same
> lock and reject new dynamic partitions from that point. Check before
> allocation, which can scan the parent's bad-block markers, so it cannot
> start hardware access after removal has passed this synchronization point.
>
> Initialize the state when registering the master, allowing drivers that
> reuse their MTD object to register it again.
>
> Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
> ---
...
> --- a/drivers/mtd/mtdpart.c
> +++ b/drivers/mtd/mtdpart.c
> @@ -278,28 +278,34 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
> part.size = length;
> part.offset = offset;
Open question: would a guard(mutex) prevent most of the code churn
here?
Miquèl