If a flash driver is unbound during BLKPG_ADD_PARTITION, removal can find a new partition before its device is registered or while the ioctl is creating its sysfs files. An open master can also accept a new partition after teardown. With CONFIG_MTD_PARTITIONED_MASTER, the same race is possible if partition parsing fails after the master is visible. Allow dynamic partition updates only after registration succeeds. Use the master's ioctl mutex to finish any active update and close admission before teardown, without holding the partition-list mutex across the bad-block scan or add notifiers. Both native and compat BLKPG already hold the ioctl mutex; direct callers take it in the exported helpers. Virtual concat registers a master directly, without partition parsing. Initialize and open the update gate in the direct registration path, and drain updates and remove its partitions before deleting that master. The parsed-registration path keeps the gate closed until parsing and publication finish. plat-ram can register the same master a second time to expose the whole device. Keep its existing partition list and locks, and reject an already published partitioned master before starting error cleanup. Fixes: 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- Changes in v3: - Use chrdev_lock to serialize BLKPG and direct partition updates with unregister. Keep the existing partition-list lock scopes. - Open dynamic updates only after registration succeeds, including on masters exposed before parsing. Preserve the locks on a second call. - Handle the virtual concat master's direct registration and teardown through the shared MTD publication paths. - Link to v2: https://patch.msgid.link/20261120-submit-mtd-partition-publication-v1-v2-1-8098cde01961@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/mtdchar.c | 4 +-- drivers/mtd/mtdcore.c | 93 +++++++++++++++++++++++++++++++++++++++---------- drivers/mtd/mtdcore.h | 3 ++ drivers/mtd/mtdpart.c | 31 +++++++++++++++-- include/linux/mtd/mtd.h | 6 ++++ 5 files changed, 114 insertions(+), 23 deletions(-) diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c index bf01e6ac7293..ec79cf68b90d 100644 --- a/drivers/mtd/mtdchar.c +++ b/drivers/mtd/mtdchar.c @@ -559,14 +559,14 @@ static int mtdchar_blkpg_ioctl(struct mtd_info *mtd, /* Sanitize user input */ p.devname[BLKPG_DEVNAMELTH - 1] = '\0'; - return mtd_add_partition(mtd, p.devname, p.start, p.length); + return mtd_add_partition_locked(mtd, p.devname, p.start, p.length); case BLKPG_DEL_PARTITION: if (p.pno < 0) return -EINVAL; - return mtd_del_partition(mtd, p.pno); + return mtd_del_partition_locked(mtd, p.pno); default: return -EINVAL; diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index b170c3144ca8..db11fb05c4c4 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -692,7 +692,7 @@ static void mtd_check_of_node(struct mtd_info *mtd) } /** - * add_mtd_device - register an MTD device + * __add_mtd_device - register an MTD device * @mtd: pointer to new MTD device info structure * * Add a device to the list of MTD devices present in the system, and @@ -700,7 +700,7 @@ static void mtd_check_of_node(struct mtd_info *mtd) * zero on success or non-zero on failure. */ -int add_mtd_device(struct mtd_info *mtd) +static int __add_mtd_device(struct mtd_info *mtd) { struct device_node *np = mtd_get_of_node(mtd); struct mtd_info *master = mtd_get_master(mtd); @@ -866,7 +866,7 @@ int add_mtd_device(struct mtd_info *mtd) } /** - * del_mtd_device - unregister an MTD device + * __del_mtd_device - unregister an MTD device * @mtd: pointer to MTD device info structure * * Remove a device from the list of MTD devices present in the system, @@ -875,7 +875,7 @@ int add_mtd_device(struct mtd_info *mtd) * if the requested device does not appear to be present in the list. */ -int del_mtd_device(struct mtd_info *mtd) +static int __del_mtd_device(struct mtd_info *mtd) { int ret; struct mtd_notifier *not; @@ -914,10 +914,64 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd) } else { pr_debug("mtd device won't show a device symlink in sysfs\n"); } +} + +static void mtd_set_partition_updates(struct mtd_info *master, bool allowed) +{ + guard(mutex)(&master->master.chrdev_lock); + master->master.partition_updates_allowed = allowed; +} + +static void mtd_init_partition_updates(struct mtd_info *master) +{ + if (!master->master.partitions_initialized) { + INIT_LIST_HEAD(&master->partitions); + mutex_init(&master->master.partitions_lock); + mutex_init(&master->master.chrdev_lock); + master->master.partitions_initialized = true; + } + mtd_set_partition_updates(master, false); +} + +/* A directly registered master has no partition parser to wait for. */ +int add_mtd_device(struct mtd_info *mtd) +{ + int ret; + + if (mtd_is_partition(mtd)) + return __add_mtd_device(mtd); + if (device_is_registered(&mtd->dev)) + return -EEXIST; + + mtd_init_partition_updates(mtd); + ret = __add_mtd_device(mtd); + if (!ret) + mtd_set_partition_updates(mtd, true); + + return ret; +} + +static int mtd_del_master(struct mtd_info *master) +{ + int ret; + + ret = del_mtd_partitions(master); + if (ret || !device_is_registered(&master->dev)) + return ret; + + return __del_mtd_device(master); +} + +int del_mtd_device(struct mtd_info *mtd) +{ + if (mtd_is_partition(mtd)) + return __del_mtd_device(mtd); + + if (!device_is_registered(&mtd->dev)) + return -ENODEV; - INIT_LIST_HEAD(&mtd->partitions); - mutex_init(&mtd->master.partitions_lock); - mutex_init(&mtd->master.chrdev_lock); + mtd_set_partition_updates(mtd, false); + return mtd_del_master(mtd); } static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user) @@ -1125,14 +1179,18 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, { int ret, err; + if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) && device_is_registered(&mtd->dev)) + return -EEXIST; + mtd_set_dev_defaults(mtd); + mtd_init_partition_updates(mtd); ret = mtd_otp_nvmem_add(mtd); if (ret) goto out; if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) { - ret = add_mtd_device(mtd); + ret = __add_mtd_device(mtd); if (ret) goto out; } @@ -1153,7 +1211,7 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, else if (nr_parts) ret = add_mtd_partitions(mtd, parts, nr_parts); else if (!device_is_registered(&mtd->dev)) - ret = add_mtd_device(mtd); + ret = __add_mtd_device(mtd); else ret = 0; @@ -1180,6 +1238,7 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, register_reboot_notifier(&mtd->reboot_notifier); } + mtd_set_partition_updates(mtd, true); out: if (ret) { nvmem_unregister(mtd->otp_user_nvmem); @@ -1187,7 +1246,7 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, } if (ret && device_is_registered(&mtd->dev)) { - err = del_mtd_device(mtd); + err = __del_mtd_device(mtd); if (err) pr_err("Error when deleting MTD device (%d)\n", err); } @@ -1206,6 +1265,11 @@ int mtd_device_unregister(struct mtd_info *master) { int err; + if (!master->master.partitions_initialized) + return 0; + + mtd_set_partition_updates(master, false); + if (master->_reboot) { unregister_reboot_notifier(&master->reboot_notifier); memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier)); @@ -1219,14 +1283,7 @@ int mtd_device_unregister(struct mtd_info *master) if (err) return err; } - err = del_mtd_partitions(master); - if (err) - return err; - - if (!device_is_registered(&master->dev)) - return 0; - - return del_mtd_device(master); + return mtd_del_master(master); } EXPORT_SYMBOL_GPL(mtd_device_unregister); diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h index b014861a06a6..caf359f8c21d 100644 --- a/drivers/mtd/mtdcore.h +++ b/drivers/mtd/mtdcore.h @@ -12,6 +12,9 @@ int __must_check add_mtd_device(struct mtd_info *mtd); int del_mtd_device(struct mtd_info *mtd); int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int); int del_mtd_partitions(struct mtd_info *); +int mtd_add_partition_locked(struct mtd_info *parent, const char *name, + long long offset, long long length); +int mtd_del_partition_locked(struct mtd_info *mtd, int partno); void release_mtd_partition(struct mtd_info *mtd); struct mtd_partitions; diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c index ddded0dbe77e..3810740f783c 100644 --- a/drivers/mtd/mtdpart.c +++ b/drivers/mtd/mtdpart.c @@ -246,8 +246,8 @@ static int mtd_add_partition_attrs(struct mtd_info *new) return ret; } -int mtd_add_partition(struct mtd_info *parent, const char *name, - long long offset, long long length) +int mtd_add_partition_locked(struct mtd_info *parent, const char *name, + long long offset, long long length) { struct mtd_info *master = mtd_get_master(parent); u64 parent_size = mtd_is_partition(parent) ? @@ -256,6 +256,10 @@ int mtd_add_partition(struct mtd_info *parent, const char *name, struct mtd_info *child; int ret = 0; + lockdep_assert_held(&master->master.chrdev_lock); + if (!master->master.partition_updates_allowed) + return -ENODEV; + /* the direct offset is expected */ if (offset == MTDPART_OFS_APPEND || offset == MTDPART_OFS_NXTBLK || @@ -303,6 +307,15 @@ int mtd_add_partition(struct mtd_info *parent, const char *name, return ret; } + +int mtd_add_partition(struct mtd_info *parent, const char *name, + long long offset, long long length) +{ + struct mtd_info *master = mtd_get_master(parent); + + guard(mutex)(&master->master.chrdev_lock); + return mtd_add_partition_locked(parent, name, offset, length); +} EXPORT_SYMBOL_GPL(mtd_add_partition); /** @@ -374,11 +387,15 @@ int del_mtd_partitions(struct mtd_info *mtd) return ret; } -int mtd_del_partition(struct mtd_info *mtd, int partno) +int mtd_del_partition_locked(struct mtd_info *mtd, int partno) { struct mtd_info *child, *master = mtd_get_master(mtd); int ret = -EINVAL; + lockdep_assert_held(&master->master.chrdev_lock); + if (!master->master.partition_updates_allowed) + return -ENODEV; + mutex_lock(&master->master.partitions_lock); list_for_each_entry(child, &mtd->partitions, part.node) { if (child->index == partno) { @@ -390,6 +407,14 @@ int mtd_del_partition(struct mtd_info *mtd, int partno) return ret; } + +int mtd_del_partition(struct mtd_info *mtd, int partno) +{ + struct mtd_info *master = mtd_get_master(mtd); + + guard(mutex)(&master->master.chrdev_lock); + return mtd_del_partition_locked(mtd, partno); +} EXPORT_SYMBOL_GPL(mtd_del_partition); /* diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h index 8d10d9d2e830..29ea70e76de0 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -223,7 +223,11 @@ struct mtd_part { * @partitions_lock: lock protecting accesses to the partition list. Protects * not only the master partition list, but also all * sub-partitions. + * @chrdev_lock: serializes character-device ioctls and dynamic partition updates * @suspended: set to 1 when the device is suspended, 0 otherwise + * @partitions_initialized: the partition list and its locks have been initialized + * @partition_updates_allowed: dynamic updates are allowed after registration; + * protected by chrdev_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 +237,8 @@ struct mtd_master { struct mutex partitions_lock; struct mutex chrdev_lock; unsigned int suspended : 1; + bool partitions_initialized; + bool partition_updates_allowed; }; 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