[PATCH v5 03/58] mtd: separate class devices from retained backing objects

sashiko-bot at kernel.org sashiko-bot at kernel.org
Sun Nov 15 23:21:31 PST 2026


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mtd: mtdpart: get_mtd_device() in add_mtd_partitions() breaks
  busy gluebi
--

commit ecf5ff75f8570e8ffd0f1f3b353ff398fca67be7
Author: James Hilliard <james.hilliard1 at gmail.com>

mtd: separate class devices from retained backing objects

This commit separates the MTD class device allocation from struct
mtd_info and adds reference counting for backing objects and partition
lifetimes across unregistration.

> diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> index c316cfe92c7e4..8be604f7c140e 100644
> --- a/drivers/mtd/mtdpart.c
> +++ b/drivers/mtd/mtdpart.c
[ ... ]
> @@ -419,35 +420,52 @@ int add_mtd_partitions(struct mtd_info *parent,
>  	       nbparts, parent->name);
>  
>  	for (i = 0; i < nbparts; i++) {
> +		mutex_lock(&master->master.partitions_lock);
> +		if (master->master.unregistering ||
> +		    (mtd_is_partition(parent) && !parent->registered)) {
> +			mutex_unlock(&master->master.partitions_lock);
> +			return -ENODEV;
> +		}
> +
>  		child = allocate_partition(parent, parts + i, i, cur_offset);
>  		if (IS_ERR(child)) {
>  			ret = PTR_ERR(child);
> -			goto err_del_partitions;
> +			goto err_unlock;
>  		}
>  
>  		if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) {
> -			if (mtd_virt_concat_add(child))
> +			if (mtd_virt_concat_add(child)) {
> +				mutex_unlock(&master->master.partitions_lock);
>  				continue;
> +			}
>  		}
>  
> -		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) {
> -			mutex_lock(&master->master.partitions_lock);
>  			list_del(&child->part.node);
> -			mutex_unlock(&master->master.partitions_lock);
> -
>  			free_partition(child);
> -			goto err_del_partitions;
> +			goto err_unlock;
>  		}
>  
>  		mtd_add_partition_attrs(child);
> +		cur_offset = child->part.offset + child->part.size;
> +
> +		/* Parsing drops the partition lock and may race with deletion. */
> +		ret = PTR_ERR_OR_ZERO(get_mtd_device(child, -1));
> +		if (ret)
> +			goto err_unlock;

[Severity: Medium]
Does calling get_mtd_device(child, -1) during partition registration break
partitioned gluebi devices when the underlying UBI volume is already open
for writing?

get_mtd_device() calls __get_mtd_device(), which now invokes
master->_get_device(master).  For a writable gluebi device,
gluebi_get_device() opens the volume with UBI_READWRITE:

drivers/mtd/ubi/gluebi.c:gluebi_get_device() {
    ...
	if (mtd->flags & MTD_WRITEABLE)
		ubi_mode = UBI_READWRITE;
    ...
	gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
				       ubi_mode);
	if (IS_ERR(gluebi->desc)) {
		mutex_unlock(&devices_mutex);
		return PTR_ERR(gluebi->desc);
	}
}

Previously, gluebi_get_device() only ran when the MTD device was actually
opened.  When gluebi is loaded while a UBI volume already has an active
writer or exclusive opener (such as a mounted read-write UBIFS
filesystem), ubi_open_volume() returns -EBUSY from inside the UBI volume
notifier, causing add_mtd_partitions() to roll back all partitions and
fail gluebi_create().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261115-submit-rawnand-retained-handle-lifetime-v1-v5-0-cbc49c5c5e74@gmail.com?part=3



More information about the linux-mtd mailing list