All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael Walle" <mwalle@kernel.org>
To: <Manikandan.M@microchip.com>, <sashiko-reviews@lists.linux.dev>,
	<pratyush@kernel.org>, <mwalle@kernel.org>,
	<takahiro.kuwano@infineon.com>, <miquel.raynal@bootlin.com>,
	<richard@nod.at>, <vigneshr@ti.com>, <robh@kernel.org>,
	<krzk+dt@kernel.org>, <conor+dt@kernel.org>, <srini@kernel.org>,
	<Nicolas.Ferre@microchip.com>, <alexandre.belloni@bootlin.com>,
	<claudiu.beznea@tuxon.dev>, <linux@armlinux.org.uk>,
	<richardcochran@gmail.com>, <linusw@kernel.org>, <arnd@arndb.de>,
	<pengpeng@iscas.ac.cn>, <gregkh@linuxfoundation.org>,
	<mathieu.dubois-briand@bootlin.com>,
	<linux-mtd@lists.infradead.org>, <devicetree@vger.kernel.org>
Subject: Re: [PATCH v9 4/8] mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device
Date: Mon, 02 Nov 2026 08:55:19 +0100	[thread overview]
Message-ID: <DMKKB0CXCHFC.365E5XOS5BSE4@kernel.org> (raw)
In-Reply-To: <ba131ca0-c659-419e-9ff7-26c7c6de1028@microchip.com>

[-- Attachment #1: Type: text/plain, Size: 2624 bytes --]

On Mon Nov 2, 2026 at 6:03 AM CET, Manikandan.M wrote:
>>> +static int spi_nor_register_sfdp_nvmem(struct spi_nor *nor)
>>> +{
>>> +     struct device *dev = nor->dev;
>>> +     struct nvmem_config config = { };
>>> +     struct nvmem_device *nvmem;
>>> +     struct device_node *np;
>>> +
>>> +     if (!nor->sfdp)
>>> +             return 0;
>>> +
>>> +     np = of_get_compatible_child(dev_of_node(dev), "jedec,sfdp");
>>> +
>>> +     config.dev = dev;
>>> +     config.of_node = np;
>> 
>> [Severity: High]
>> If the jedec,sfdp child node is missing from the device tree, np will
>> be NULL and assigned to config.of_node. Does this inadvertently trigger the
>> NVMEM core's fallback behavior?
>> 
>> Looking at nvmem_register() in drivers/nvmem/core.c, there is a fallback
>> mechanism for missing nodes:
>> 
>>      if (config->of_node)
>>              nvmem->dev.of_node = of_node_get(config->of_node);
>>      else
>>              nvmem->dev.of_node = of_node_get(config->dev->of_node);
>> 
>> If config.of_node is NULL, the SFDP NVMEM device will incorrectly inherit
>> the SPI NOR flash's main device node.
>> 
>> The NVMEM core then searches for an nvmem-layout child node and will
>> find the layout intended for the main flash memory, attaching all NVMEM
>> cells to the small SFDP NVMEM device and marking the layout node populated.
>> 
>> Later, when the MTD subsystem registers the primary NVMEM device for the
>> actual flash contents, it will skip the layout node because it is already
>> marked populated.
>> 
>> Could any consumer attempting to read an NVMEM cell then be misdirected
>> to the small SFDP NVMEM device's read callback instead of the main flash,
>> resulting in -EINVAL bounds check errors or corrupted data?
>
> Yes, this is a valid issue. With no "jedec,sfdp" node, nvmem_register() 
> falls back to the flash's own node, so the SFDP NVMEM device would claim 
> an "nvmem-layout" meant for the whole flash.
>
> Michael,
> this comes from dropping the "if (!np) return 0;" check in v9. I'll 
> bring it back in v10: without a "jedec,sfdp" node there is nowhere to 
> describe SFDP cells, so the SFDP NVMEM device would have no consumers, 

There is no DT user, yes. But there is then a "sfdpN" file in sysfs
which can be used from user space. And that's what my first remark
was about. Why would that only exist if there is a "jedec,sfdp"
child in the device tree.

> while registering it can take over the flash's layout.
> Do you have any other suggestions?

Probably tell nvmem to not attach it to any of_node?

-michael

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 297 bytes --]

WARNING: multiple messages have this Message-ID (diff)
From: "Michael Walle" <mwalle@kernel.org>
To: <Manikandan.M@microchip.com>, <sashiko-reviews@lists.linux.dev>,
	<pratyush@kernel.org>, <mwalle@kernel.org>,
	<takahiro.kuwano@infineon.com>, <miquel.raynal@bootlin.com>,
	<richard@nod.at>, <vigneshr@ti.com>, <robh@kernel.org>,
	<krzk+dt@kernel.org>, <conor+dt@kernel.org>, <srini@kernel.org>,
	<Nicolas.Ferre@microchip.com>, <alexandre.belloni@bootlin.com>,
	<claudiu.beznea@tuxon.dev>, <linux@armlinux.org.uk>,
	<richardcochran@gmail.com>, <linusw@kernel.org>, <arnd@arndb.de>,
	<pengpeng@iscas.ac.cn>, <gregkh@linuxfoundation.org>,
	<mathieu.dubois-briand@bootlin.com>,
	<linux-mtd@lists.infradead.org>, <devicetree@vger.kernel.org>
Subject: Re: [PATCH v9 4/8] mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device
Date: Mon, 02 Nov 2026 08:55:19 +0100	[thread overview]
Message-ID: <DMKKB0CXCHFC.365E5XOS5BSE4@kernel.org> (raw)
In-Reply-To: <ba131ca0-c659-419e-9ff7-26c7c6de1028@microchip.com>


[-- Attachment #1.1: Type: text/plain, Size: 2624 bytes --]

On Mon Nov 2, 2026 at 6:03 AM CET, Manikandan.M wrote:
>>> +static int spi_nor_register_sfdp_nvmem(struct spi_nor *nor)
>>> +{
>>> +     struct device *dev = nor->dev;
>>> +     struct nvmem_config config = { };
>>> +     struct nvmem_device *nvmem;
>>> +     struct device_node *np;
>>> +
>>> +     if (!nor->sfdp)
>>> +             return 0;
>>> +
>>> +     np = of_get_compatible_child(dev_of_node(dev), "jedec,sfdp");
>>> +
>>> +     config.dev = dev;
>>> +     config.of_node = np;
>> 
>> [Severity: High]
>> If the jedec,sfdp child node is missing from the device tree, np will
>> be NULL and assigned to config.of_node. Does this inadvertently trigger the
>> NVMEM core's fallback behavior?
>> 
>> Looking at nvmem_register() in drivers/nvmem/core.c, there is a fallback
>> mechanism for missing nodes:
>> 
>>      if (config->of_node)
>>              nvmem->dev.of_node = of_node_get(config->of_node);
>>      else
>>              nvmem->dev.of_node = of_node_get(config->dev->of_node);
>> 
>> If config.of_node is NULL, the SFDP NVMEM device will incorrectly inherit
>> the SPI NOR flash's main device node.
>> 
>> The NVMEM core then searches for an nvmem-layout child node and will
>> find the layout intended for the main flash memory, attaching all NVMEM
>> cells to the small SFDP NVMEM device and marking the layout node populated.
>> 
>> Later, when the MTD subsystem registers the primary NVMEM device for the
>> actual flash contents, it will skip the layout node because it is already
>> marked populated.
>> 
>> Could any consumer attempting to read an NVMEM cell then be misdirected
>> to the small SFDP NVMEM device's read callback instead of the main flash,
>> resulting in -EINVAL bounds check errors or corrupted data?
>
> Yes, this is a valid issue. With no "jedec,sfdp" node, nvmem_register() 
> falls back to the flash's own node, so the SFDP NVMEM device would claim 
> an "nvmem-layout" meant for the whole flash.
>
> Michael,
> this comes from dropping the "if (!np) return 0;" check in v9. I'll 
> bring it back in v10: without a "jedec,sfdp" node there is nowhere to 
> describe SFDP cells, so the SFDP NVMEM device would have no consumers, 

There is no DT user, yes. But there is then a "sfdpN" file in sysfs
which can be used from user space. And that's what my first remark
was about. Why would that only exist if there is a "jedec,sfdp"
child in the device tree.

> while registering it can take over the flash's layout.
> Do you have any other suggestions?

Probably tell nvmem to not attach it to any of_node?

-michael

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 297 bytes --]

[-- Attachment #2: Type: text/plain, Size: 144 bytes --]

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2026-11-02  7:55 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16 10:38 [PATCH v9 0/8] Read MAC address from SST vendor specific SFDP region Manikandan Muralidharan
2026-10-16 10:38 ` Manikandan Muralidharan
2026-10-16 10:38 ` [PATCH v9 1/8] dt-bindings: mtd: jedec,spi-nor: allow the SFDP to be exposed via NVMEM Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan
2026-10-16 10:38 ` [PATCH v9 2/8] dt-bindings: nvmem: layouts: add Microchip/SST SFDP EUI layout Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan
2026-10-16 10:38 ` [PATCH v9 3/8] nvmem: core: take a reference on dev.of_node in nvmem_register() Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan
2026-10-16 10:38 ` [PATCH v9 4/8] mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan
2026-10-17 10:40   ` sashiko-bot
2026-10-17 10:40     ` sashiko-bot
2026-11-02  5:03     ` Manikandan.M
2026-11-02  5:03       ` Manikandan.M
2026-11-02  7:55       ` Michael Walle [this message]
2026-11-02  7:55         ` Michael Walle
2026-11-02 10:51         ` Manikandan.M
2026-11-02 10:51           ` Manikandan.M
2026-10-16 10:38 ` [PATCH v9 5/8] nvmem: layouts: add Microchip/SST SFDP EUI layout driver Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan
2026-10-17 10:40   ` sashiko-bot
2026-10-17 10:40     ` sashiko-bot
2026-10-16 10:38 ` [PATCH v9 6/8] ARM: dts: microchip: sama5d27_wlsom1: use fixed-partitions for QSPI flash Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan
2026-10-16 10:38 ` [PATCH v9 7/8] ARM: dts: microchip: sama5d27_wlsom1: read MAC address from QSPI SFDP Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan
2026-10-17 10:40   ` sashiko-bot
2026-10-17 10:40     ` sashiko-bot
2026-10-16 10:38 ` [PATCH v9 8/8] ARM: configs: sama5: enable Microchip/SST SFDP EUI NVMEM layout Manikandan Muralidharan
2026-10-16 10:38   ` Manikandan Muralidharan

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=DMKKB0CXCHFC.365E5XOS5BSE4@kernel.org \
    --to=mwalle@kernel.org \
    --cc=Manikandan.M@microchip.com \
    --cc=Nicolas.Ferre@microchip.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=arnd@arndb.de \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzk+dt@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=linux@armlinux.org.uk \
    --cc=mathieu.dubois-briand@bootlin.com \
    --cc=miquel.raynal@bootlin.com \
    --cc=pengpeng@iscas.ac.cn \
    --cc=pratyush@kernel.org \
    --cc=richard@nod.at \
    --cc=richardcochran@gmail.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=srini@kernel.org \
    --cc=takahiro.kuwano@infineon.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.