2019-05-21 08:58:40

by Sascha Hauer

[permalink] [raw]
Subject: nvmem creates multiple devices with the same name

Hi all,

nvmem derives the device name directly from the partition name of the
underlying device. IMO this is wrong since it's not possible to create
two partitions with the same name on different devices. In my case I
have a NAND device and a SPI NOR device which both happen to have a
partition named 'barebox'. This ends up with:

[ 11.222196] sysfs: cannot create duplicate filename '/bus/nvmem/devices/barebox'
[ 11.230136] CPU: 0 PID: 1 Comm: swapper/0 Tainted: G W 5.2.0-rc1-00014-g793f23e5adb0-dirty #676
[ 11.240414] Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree)
[ 11.247174] [<c0112928>] (unwind_backtrace) from [<c010d140>] (show_stack+0x10/0x14)
[ 11.255171] [<c010d140>] (show_stack) from [<c0bd65cc>] (dump_stack+0xd8/0x110)
[ 11.262722] [<c0bd65cc>] (dump_stack) from [<c031682c>] (sysfs_warn_dup+0x50/0x64)
[ 11.270527] [<c031682c>] (sysfs_warn_dup) from [<c0316b34>] (sysfs_do_create_link_sd+0xcc/0xd8)
[ 11.279487] [<c0316b34>] (sysfs_do_create_link_sd) from [<c06792a0>] (bus_add_device+0x80/0xfc)
[ 11.288441] [<c06792a0>] (bus_add_device) from [<c0676208>] (device_add+0x328/0x608)
[ 11.296423] [<c0676208>] (device_add) from [<c08bde64>] (nvmem_register.part.1+0x168/0x5e4)
[ 11.305030] [<c08bde64>] (nvmem_register.part.1) from [<c06edb34>] (add_mtd_device+0x1e8/0x404)
[ 11.313988] [<c06edb34>] (add_mtd_device) from [<c06f1004>] (add_mtd_partitions+0x74/0x15c)
[ 11.322589] [<c06f1004>] (add_mtd_partitions) from [<c06f0da8>] (parse_mtd_partitions+0x180/0x368)
[ 11.331807] [<c06f0da8>] (parse_mtd_partitions) from [<c06ede68>] (mtd_device_parse_register+0x40/0x164)
[ 11.341560] [<c06ede68>] (mtd_device_parse_register) from [<c070654c>] (m25p_probe+0x118/0x200)
[ 11.350513] [<c070654c>] (m25p_probe) from [<c073863c>] (spi_drv_probe+0x80/0xa4)

While it's easy to rename the partitions I see no reason why it should
be illegal to have two different (mtd) devices with eqeally named
partitions. Are there any suggestions how to register the nvmem devices
with a different name?

Sascha

--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |


2019-05-21 09:03:53

by Srinivas Kandagatla

[permalink] [raw]
Subject: Re: nvmem creates multiple devices with the same name



On 21/05/2019 09:56, Sascha Hauer wrote:
> . Are there any suggestions how to register the nvmem devices
> with a different name?

struct nvmem_config provides id field for this purpose, this will be
used by nvmem to set the device name space along with name field.

--srini

2019-05-21 09:23:52

by Sascha Hauer

[permalink] [raw]
Subject: Re: nvmem creates multiple devices with the same name

On Tue, May 21, 2019 at 10:02:32AM +0100, Srinivas Kandagatla wrote:
>
>
> On 21/05/2019 09:56, Sascha Hauer wrote:
> > . Are there any suggestions how to register the nvmem devices
> > with a different name?
>
> struct nvmem_config provides id field for this purpose, this will be used by
> nvmem to set the device name space along with name field.

There's no way for a caller to know a unique name/id combination.
The mtd layer could initialize the id field with the mtd number, but
that would still not guarantee that another caller, like an EEPROM
driver or such, doesn't use the same name/id combination.

Sascha

--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |

2019-07-01 08:27:09

by Sascha Hauer

[permalink] [raw]
Subject: Re: nvmem creates multiple devices with the same name

Hi Srinivas,

On Tue, May 21, 2019 at 11:21:07AM +0200, Sascha Hauer wrote:
> On Tue, May 21, 2019 at 10:02:32AM +0100, Srinivas Kandagatla wrote:
> >
> >
> > On 21/05/2019 09:56, Sascha Hauer wrote:
> > > . Are there any suggestions how to register the nvmem devices
> > > with a different name?
> >
> > struct nvmem_config provides id field for this purpose, this will be used by
> > nvmem to set the device name space along with name field.
>
> There's no way for a caller to know a unique name/id combination.
> The mtd layer could initialize the id field with the mtd number, but
> that would still not guarantee that another caller, like an EEPROM
> driver or such, doesn't use the same name/id combination.

This is still an unresolved issue. Do you have any input how we could
proceed here?

Thanks
Sascha

--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |

2019-07-02 16:56:53

by Srinivas Kandagatla

[permalink] [raw]
Subject: Re: nvmem creates multiple devices with the same name

Hi Sascha,

On 01/07/2019 09:06, Sascha Hauer wrote:
> Hi Srinivas,
>
> On Tue, May 21, 2019 at 11:21:07AM +0200, Sascha Hauer wrote:
>> On Tue, May 21, 2019 at 10:02:32AM +0100, Srinivas Kandagatla wrote:
>>>
>>>
>>> On 21/05/2019 09:56, Sascha Hauer wrote:
>>>> . Are there any suggestions how to register the nvmem devices
>>>> with a different name?
>>>
>>> struct nvmem_config provides id field for this purpose, this will be used by
>>> nvmem to set the device name space along with name field.
>>
>> There's no way for a caller to know a unique name/id combination.
>> The mtd layer could initialize the id field with the mtd number, but
>> that would still not guarantee that another caller, like an EEPROM
>> driver or such, doesn't use the same name/id combination.
>
> This is still an unresolved issue. Do you have any input how we could
> proceed here?

Sorry for the delay!
I think simplest solution would be to check if there is already an nvmem
provider with the same name before assigning name to the device and then
append the id in case it exists.

Let me know if below patch helps the situation so that I can take this
in next cycle!

----------------------------------->cut<----------------------------
nvmem: core: Check nvmem device name before adding the same one

In some usecases where nvmem names are directly derived from
partition names, its likely that different devices might have
same partition name.
This will be an issue as we will be creating two different
nvmem devices with same name and sysfs will not be very happy with
that.

Simple solution is to check the existance of the nvmem provider with
same name and append an id if it exists before creating the device
name.

Signed-off-by: Srinivas Kandagatla <[email protected]>

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index f24008b66826..cf70a405023c 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -641,7 +641,11 @@ struct nvmem_device *nvmem_register(const struct
nvmem_config *config)
nvmem->dev.of_node = config->dev->of_node;

if (config->id == -1 && config->name) {
- dev_set_name(&nvmem->dev, "%s", config->name);
+ if (nvmem_find(config->name))
+ dev_set_name(&nvmem->dev, "%s%d", config->name,
+ nvmem->id);
+ else
+ dev_set_name(&nvmem->dev, "%s", config->name);
} else {
dev_set_name(&nvmem->dev, "%s%d",
config->name ? : "nvmem",
----------------------------------->cut<----------------------------
>
> Thanks
> Sascha
>

2019-07-02 19:01:25

by Boris Brezillon

[permalink] [raw]
Subject: Re: nvmem creates multiple devices with the same name

On Tue, 21 May 2019 10:56:41 +0200
Sascha Hauer <[email protected]> wrote:

> Hi all,
>
> nvmem derives the device name directly from the partition name of the
> underlying device. IMO this is wrong since it's not possible to create
> two partitions with the same name on different devices. In my case I
> have a NAND device and a SPI NOR device which both happen to have a
> partition named 'barebox'. This ends up with:

Hm, I think I had suggested to use dev_name(&mtd->dev) instead of
mtd->name at some point. But then you have the problem that MTD
numbering is dependent on the probe order which is not guaranteed to
stay the same, so exposing nvmem devices using "mtdXX" name is not super
user-friendly.

>
> [ 11.222196] sysfs: cannot create duplicate filename '/bus/nvmem/devices/barebox'
> [ 11.230136] CPU: 0 PID: 1 Comm: swapper/0 Tainted: G W 5.2.0-rc1-00014-g793f23e5adb0-dirty #676
> [ 11.240414] Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree)
> [ 11.247174] [<c0112928>] (unwind_backtrace) from [<c010d140>] (show_stack+0x10/0x14)
> [ 11.255171] [<c010d140>] (show_stack) from [<c0bd65cc>] (dump_stack+0xd8/0x110)
> [ 11.262722] [<c0bd65cc>] (dump_stack) from [<c031682c>] (sysfs_warn_dup+0x50/0x64)
> [ 11.270527] [<c031682c>] (sysfs_warn_dup) from [<c0316b34>] (sysfs_do_create_link_sd+0xcc/0xd8)
> [ 11.279487] [<c0316b34>] (sysfs_do_create_link_sd) from [<c06792a0>] (bus_add_device+0x80/0xfc)
> [ 11.288441] [<c06792a0>] (bus_add_device) from [<c0676208>] (device_add+0x328/0x608)
> [ 11.296423] [<c0676208>] (device_add) from [<c08bde64>] (nvmem_register.part.1+0x168/0x5e4)
> [ 11.305030] [<c08bde64>] (nvmem_register.part.1) from [<c06edb34>] (add_mtd_device+0x1e8/0x404)
> [ 11.313988] [<c06edb34>] (add_mtd_device) from [<c06f1004>] (add_mtd_partitions+0x74/0x15c)
> [ 11.322589] [<c06f1004>] (add_mtd_partitions) from [<c06f0da8>] (parse_mtd_partitions+0x180/0x368)
> [ 11.331807] [<c06f0da8>] (parse_mtd_partitions) from [<c06ede68>] (mtd_device_parse_register+0x40/0x164)
> [ 11.341560] [<c06ede68>] (mtd_device_parse_register) from [<c070654c>] (m25p_probe+0x118/0x200)
> [ 11.350513] [<c070654c>] (m25p_probe) from [<c073863c>] (spi_drv_probe+0x80/0xa4)
>
> While it's easy to rename the partitions I see no reason why it should
> be illegal to have two different (mtd) devices with eqeally named
> partitions. Are there any suggestions how to register the nvmem devices
> with a different name?

Note that some MTD users are expecting MTD names to be unique to work
properly, the example I have in mind is UBI that can be passed the
partition to attach to using the ubi:<part-name> format, but I'm pretty
sure we have other places making the same assumption. I guess not
enforcing mtd->name uniqueness was a bad idea, but I'm not sure we can
change that now.

2019-07-08 10:28:55

by Sascha Hauer

[permalink] [raw]
Subject: Re: nvmem creates multiple devices with the same name

On Tue, Jul 02, 2019 at 05:54:54PM +0100, Srinivas Kandagatla wrote:
> Hi Sascha,
>
> On 01/07/2019 09:06, Sascha Hauer wrote:
> > Hi Srinivas,
> >
> > On Tue, May 21, 2019 at 11:21:07AM +0200, Sascha Hauer wrote:
> > > On Tue, May 21, 2019 at 10:02:32AM +0100, Srinivas Kandagatla wrote:
> > > >
> > > >
> > > > On 21/05/2019 09:56, Sascha Hauer wrote:
> > > > > . Are there any suggestions how to register the nvmem devices
> > > > > with a different name?
> > > >
> > > > struct nvmem_config provides id field for this purpose, this will be used by
> > > > nvmem to set the device name space along with name field.
> > >
> > > There's no way for a caller to know a unique name/id combination.
> > > The mtd layer could initialize the id field with the mtd number, but
> > > that would still not guarantee that another caller, like an EEPROM
> > > driver or such, doesn't use the same name/id combination.
> >
> > This is still an unresolved issue. Do you have any input how we could
> > proceed here?
>
> Sorry for the delay!
> I think simplest solution would be to check if there is already an nvmem
> provider with the same name before assigning name to the device and then
> append the id in case it exists.
>
> Let me know if below patch helps the situation so that I can take this in
> next cycle!
>
> ----------------------------------->cut<----------------------------
> nvmem: core: Check nvmem device name before adding the same one
>
> In some usecases where nvmem names are directly derived from
> partition names, its likely that different devices might have
> same partition name.
> This will be an issue as we will be creating two different
> nvmem devices with same name and sysfs will not be very happy with that.
>
> Simple solution is to check the existance of the nvmem provider with
> same name and append an id if it exists before creating the device name.

This solution obviously works for me. I am not sure if that's really
what we want as the resulting names in sysfs are not predictable in any
way. In that case we might be better off using mtdx as Boris suggested.

Sascha

--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |