From: Ricardo Ribalda Delgado <ribalda@kernel.org>
To: Miquel Raynal <miquel.raynal@bootlin.com>,
Richard Weinberger <richard@nod.at>,
Vignesh Raghavendra <vigneshr@ti.com>,
linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: Ricardo Ribalda Delgado <ribalda@kernel.org>
Subject: [PATCH] mtd: Fix mtd not the same name not registered if nvmem
Date: Wed, 1 Apr 2020 12:02:40 +0200 [thread overview]
Message-ID: <20200401100240.445447-1-ribalda@kernel.org> (raw)
When the nvmem framework is enabled, a nvmem device is created per mtd
device/partition.
It is not uncommon that a device can have multiple mtd devices with
partitions that have the same name. Eg, when there DT overlay is allowed
and the same device with mtd is attached twice.
Under that circumstances, the mtd fails to register due to a name
duplication on the nvmem framework.
With this patch we add a _1, _2, _X to the subsequent names if there is
a collition, and throw a warning, instead of not starting the mtd
device.
[ 8.948991] sysfs: cannot create duplicate filename '/bus/nvmem/devices/Production Data'
[ 8.948992] CPU: 7 PID: 246 Comm: systemd-udevd Not tainted 5.5.0-qtec-standard #13
[ 8.948993] Hardware name: AMD Dibbler/Dibbler, BIOS 05.22.04.0019 10/26/2019
[ 8.948994] Call Trace:
[ 8.948996] dump_stack+0x50/0x70
[ 8.948998] sysfs_warn_dup.cold+0x17/0x2d
[ 8.949000] sysfs_do_create_link_sd.isra.0+0xc2/0xd0
[ 8.949002] bus_add_device+0x74/0x140
[ 8.949004] device_add+0x34b/0x850
[ 8.949006] nvmem_register.part.0+0x1bf/0x640
...
[ 8.948926] mtd mtd8: Failed to register NVMEM device
Signed-off-by: Ricardo Ribalda Delgado <ribalda@kernel.org>
---
drivers/mtd/mtdcore.c | 36 +++++++++++++++++++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 5fac4355b9c2..7653d45a470a 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -28,6 +28,7 @@
#include <linux/leds.h>
#include <linux/debugfs.h>
#include <linux/nvmem-provider.h>
+#include <linux/nvmem-consumer.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
@@ -545,13 +546,34 @@ static int mtd_nvmem_reg_read(void *priv, unsigned int offset,
return retlen == bytes ? 0 : -EIO;
}
+static int nvmem_next_name(const char *init_name, char *name, size_t len)
+{
+ unsigned int i = 0;
+ int ret = 0;
+ struct nvmem_device *dev = NULL;
+
+ strlcpy(name, init_name, len);
+
+ while ((ret < len) &&
+ !IS_ERR(dev = nvmem_device_find(name, device_match_name))) {
+ nvmem_device_put(dev);
+ ret = snprintf(name, len, "%s_%u", init_name, ++i);
+ }
+
+ if (ret >= len)
+ return -ENOMEM;
+
+ return i;
+}
+
static int mtd_nvmem_add(struct mtd_info *mtd)
{
struct nvmem_config config = {};
+ char name[128];
+ int ret = 0;
config.id = -1;
config.dev = &mtd->dev;
- config.name = mtd->name;
config.owner = THIS_MODULE;
config.reg_read = mtd_nvmem_reg_read;
config.size = mtd->size;
@@ -562,6 +584,13 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
config.no_of_node = true;
config.priv = mtd;
+ if (mtd->name) {
+ ret = nvmem_next_name(mtd->name, name, sizeof(name));
+ if (ret < 0)
+ return ret;
+ config.name = name;
+ }
+
mtd->nvmem = nvmem_register(&config);
if (IS_ERR(mtd->nvmem)) {
/* Just ignore if there is no NVMEM support in the kernel */
@@ -569,10 +598,15 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
mtd->nvmem = NULL;
} else {
dev_err(&mtd->dev, "Failed to register NVMEM device\n");
+ mtd->nvmem = NULL;
return PTR_ERR(mtd->nvmem);
}
}
+ if (ret)
+ dev_warn(&mtd->dev, "mtdev %s renamed to %s due to name collision",
+ mtd->name, nvmem_dev_name(mtd->nvmem));
+
return 0;
}
--
2.25.1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
next reply other threads:[~2020-04-01 10:03 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-01 10:02 Ricardo Ribalda Delgado [this message]
2020-04-02 6:59 ` [PATCH v2] mtd: Fix mtd not the same name not registered if nvmem Ricardo Ribalda Delgado
2020-04-14 13:47 ` Ricardo Ribalda Delgado
2020-04-27 14:22 ` Miquel Raynal
2020-04-27 14:37 ` Boris Brezillon
2020-04-27 14:49 ` Miquel Raynal
2020-04-27 19:10 ` Boris Brezillon
2020-04-30 12:26 ` Ricardo Ribalda Delgado
2020-04-30 12:52 ` Boris Brezillon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200401100240.445447-1-ribalda@kernel.org \
--to=ribalda@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).