[PATCH v3] mtd: leave the device type unset when naming fails

From: James Hilliard

Date: Fri Dec 04 2026 - 17:28:58 EST


add_mtd_device() uses dev.type to reject a second registration of the
same MTD. It sets that field before dev_set_name(), but the failure path
for the name allocation leaves it set even though no device was
registered. Retrying registration then warns and fails with -EEXIST.

Set the type only after naming succeeds. The later registration error
paths already clear it before releasing the device.

Fixes: 6697dae1e2da ("mtd: capture device name setting failure when adding mtd")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
Changes in v3:
- Rebase on mtd/next after the partition-node lookup change.
- Link to v2: https://patch.msgid.link/20261108-submit-mtd-devname-retry-v1-v2-1-09f576bcc51f@xxxxxxxxx

Changes in v2:
- Correct the Fixes tag to identify the commit that added the
device-name allocation error path.
- Link to v1: https://patch.msgid.link/20261108-submit-mtd-devname-retry-v1-v1-1-a67f332c54dd@xxxxxxxxx
---
drivers/mtd/mtdcore.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 0d041892e173..50f63a86de49 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -790,12 +790,12 @@ int add_mtd_device(struct mtd_info *mtd)
/* Caller should have set dev.parent to match the
* physical device, if appropriate.
*/
- mtd->dev.type = &mtd_devtype;
mtd->dev.class = &mtd_class;
mtd->dev.devt = MTD_DEVT(i);
error = dev_set_name(&mtd->dev, "mtd%d", i);
if (error)
goto fail_devname;
+ mtd->dev.type = &mtd_devtype;
dev_set_drvdata(&mtd->dev, mtd);
mtd_set_of_node(mtd, mtd_find_of_node(mtd));
error = device_register(&mtd->dev);

---
base-commit: 4e5457fcbc14d21e7c004c3159e78fc63838029e
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next
change-id: 20261108-submit-mtd-devname-retry-v1-3f7cb2ed8083

Best regards,
--
James Hilliard <james.hilliard1@xxxxxxxxx>