* [PATCH] mtd: core: Add nand_id sysfs attribute for NAND devices
@ 2025-10-07 22:40 jayxu1990
2025-10-11 20:03 ` kernel test robot
2025-10-14 19:24 ` [PATCH v2] " jayxu1990
0 siblings, 2 replies; 5+ messages in thread
From: jayxu1990 @ 2025-10-07 22:40 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
Cc: linux-mtd, linux-kernel, avnerkhan, rdlee.upstream, Jay Xu
From: Jay Xu <jayxu1990@gmail.com>
[Problem]
Currently, NAND devices do not expose their NAND ID through sysfs,
making it difficult for userspace applications to identify the specific
NAND flash chip in use. For supply management reasons, electronics
products are typically manufactured with multiple storage device
suppliers, creating a need to identify which storage device is used
on a particular product. The NAND ID is a semi-unique identifier that can
be used to determine chip-specific characteristics such as maximum P/E
cycles, which is essential for NAND health monitoring and wear leveling
algorithms.
[Solution]
This patch adds a new 'nand_id' sysfs attribute that:
1. Exposes the full NAND ID (typically 5-8 bytes) in hexadecimal format
2. Only appears on physical NAND devices (MTD_NANDFLASH/MTD_MLCNANDFLASH)
3. Is hidden on virtual MTD devices
4. Reads from the master device to ensure consistent ID across partitions
5. Handles on-demand ID reading if not already populated during probe
The implementation uses a separate attribute group with visibility control
to avoid affecting existing MTD sysfs attributes. All NAND partitions
from the same physical chip will show the same ID, as expected.
This enables userspace tools to reliably identify NAND chips for
health monitoring, bad block management, and device-specific
optimizations.
Signed-off-by: Jay Xu <jayxu1990@gmail.com>
---
drivers/mtd/mtdcore.c | 60 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 59 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 5ba9a741f5ac..9290dbe04093 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -34,6 +34,7 @@
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
+#include <linux/mtd/rawnand.h>
#include "mtdcore.h"
@@ -339,6 +340,54 @@ static ssize_t mtd_bbt_blocks_show(struct device *dev,
}
MTD_DEVICE_ATTR_RO(bbt_blocks);
+static ssize_t mtd_nand_id_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct mtd_info *mtd = dev_get_drvdata(dev);
+ struct mtd_info *master = mtd_get_master(mtd);
+ struct nand_chip *chip;
+ int ret;
+
+ /* Ensure this is actually a NAND device */
+ if (master->type != MTD_NANDFLASH && master->type != MTD_MLCNANDFLASH)
+ return -ENODEV;
+
+ chip = mtd_to_nand(master);
+
+ /* If ID not populated, try to read it now */
+ if (!chip->id.len) {
+ ret = nand_readid_op(chip, 0, chip->id.data, NAND_MAX_ID_LEN);
+ if (ret)
+ return sysfs_emit(buf, "read-error\n");
+ chip->id.len = strnlen(chip->id.data, NAND_MAX_ID_LEN);
+ }
+
+ return sysfs_emit(buf, "%*phN\n", chip->id.len, chip->id.data);
+}
+MTD_DEVICE_ATTR_RO(nand_id);
+
+static umode_t mtd_nand_id_visible(struct kobject *kobj, struct attribute *attr, int n)
+{
+ struct device *dev = kobj_to_dev(kobj);
+ struct mtd_info *mtd = dev_get_drvdata(dev);
+
+ /* Only show on NAND devices (excludes UBI volumes which have type 'ubi') */
+ if (mtd->type != MTD_NANDFLASH && mtd->type != MTD_MLCNANDFLASH)
+ return 0;
+
+ return attr->mode;
+}
+
+static struct attribute *mtd_nand_attrs[] = {
+ &dev_attr_nand_id.attr,
+ NULL,
+};
+
+static const struct attribute_group mtd_nand_group = {
+ .attrs = mtd_nand_attrs,
+ .is_visible = mtd_nand_id_visible,
+};
+
static struct attribute *mtd_attrs[] = {
&dev_attr_type.attr,
&dev_attr_flags.attr,
@@ -359,7 +408,16 @@ static struct attribute *mtd_attrs[] = {
&dev_attr_bitflip_threshold.attr,
NULL,
};
-ATTRIBUTE_GROUPS(mtd);
+
+static const struct attribute_group mtd_group = {
+ .attrs = mtd_attrs,
+};
+
+static const struct attribute_group *mtd_groups[] = {
+ &mtd_group,
+ &mtd_nand_group,
+ NULL,
+};
static const struct device_type mtd_devtype = {
.name = "mtd",
--
2.47.3
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] mtd: core: Add nand_id sysfs attribute for NAND devices
2025-10-07 22:40 [PATCH] mtd: core: Add nand_id sysfs attribute for NAND devices jayxu1990
@ 2025-10-11 20:03 ` kernel test robot
2025-10-14 19:24 ` [PATCH v2] " jayxu1990
1 sibling, 0 replies; 5+ messages in thread
From: kernel test robot @ 2025-10-11 20:03 UTC (permalink / raw)
To: jayxu1990, Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
Cc: llvm, oe-kbuild-all, linux-mtd, linux-kernel, avnerkhan,
rdlee.upstream, Jay Xu
Hi,
kernel test robot noticed the following build errors:
[auto build test ERROR on mtd/mtd/next]
[also build test ERROR on mtd/mtd/fixes linus/master v6.17 next-20251010]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/jayxu1990-gmail-com/mtd-core-Add-nand_id-sysfs-attribute-for-NAND-devices/20251010-092101
base: https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next
patch link: https://lore.kernel.org/r/20251007224049.2798233-1-jayxu1990%40gmail.com
patch subject: [PATCH] mtd: core: Add nand_id sysfs attribute for NAND devices
config: i386-buildonly-randconfig-002-20251011 (https://download.01.org/0day-ci/archive/20251012/202510120356.STGKDkA5-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251012/202510120356.STGKDkA5-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202510120356.STGKDkA5-lkp@intel.com/
All errors (new ones prefixed by >>):
>> ld.lld: error: undefined symbol: nand_readid_op
>>> referenced by mtdcore.c:359 (drivers/mtd/mtdcore.c:359)
>>> drivers/mtd/mtdcore.o:(mtd_nand_id_show) in archive vmlinux.a
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2] mtd: core: Add nand_id sysfs attribute for NAND devices
2025-10-07 22:40 [PATCH] mtd: core: Add nand_id sysfs attribute for NAND devices jayxu1990
2025-10-11 20:03 ` kernel test robot
@ 2025-10-14 19:24 ` jayxu1990
2025-10-22 14:19 ` Miquel Raynal
1 sibling, 1 reply; 5+ messages in thread
From: jayxu1990 @ 2025-10-14 19:24 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
Cc: linux-mtd, linux-kernel, avnerkhan, rdlee.upstream, Jay Xu,
kernel test robot
From: Jay Xu <jayxu1990@gmail.com>
[Problem]
Currently, NAND devices do not expose their NAND ID through sysfs,
making it difficult for userspace applications to identify the specific
NAND flash chip in use. For supply management reasons, electronics
products are typically manufactured with multiple storage device
suppliers, creating a need to identify which storage device is used
on a particular product. The NAND ID is a semi-unique identifier that can
be used to determine chip-specific characteristics such as maximum P/E
cycles, which is essential for NAND health monitoring and wear leveling
algorithms.
[Solution]
This patch adds a new 'nand_id' sysfs attribute that:
1. Exposes the full NAND ID (typically 5-8 bytes) in hexadecimal format
2. Only appears on physical NAND devices (MTD_NANDFLASH/MTD_MLCNANDFLASH)
3. Is hidden on virtual MTD devices
4. Reads from the master device to ensure consistent ID across partitions
5. Handles on-demand ID reading if not already populated during probe
The implementation uses a separate attribute group with visibility control
to avoid affecting existing MTD sysfs attributes. All NAND partitions
from the same physical chip will show the same ID, as expected.
The NAND-specific code is conditionally compiled with CONFIG_MTD_RAW_NAND
to ensure clean builds when raw NAND support is not enabled.
This enables userspace tools to reliably identify NAND chips for
health monitoring, bad block management, and device-specific
optimizations.
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202510120356.STGKDkA5-lkp@intel.com/
Signed-off-by: Jay Xu <jayxu1990@gmail.com>
---
drivers/mtd/mtdcore.c | 66 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 5ba9a741f5ac..215e316194b4 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -34,6 +34,9 @@
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
+#ifdef CONFIG_MTD_RAW_NAND
+#include <linux/mtd/rawnand.h>
+#endif
#include "mtdcore.h"
@@ -339,6 +342,56 @@ static ssize_t mtd_bbt_blocks_show(struct device *dev,
}
MTD_DEVICE_ATTR_RO(bbt_blocks);
+#ifdef CONFIG_MTD_RAW_NAND
+static ssize_t mtd_nand_id_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct mtd_info *mtd = dev_get_drvdata(dev);
+ struct mtd_info *master = mtd_get_master(mtd);
+ struct nand_chip *chip;
+ int ret;
+
+ /* Ensure this is actually a NAND device */
+ if (master->type != MTD_NANDFLASH && master->type != MTD_MLCNANDFLASH)
+ return -ENODEV;
+
+ chip = mtd_to_nand(master);
+
+ /* If ID not populated, try to read it now */
+ if (!chip->id.len) {
+ ret = nand_readid_op(chip, 0, chip->id.data, NAND_MAX_ID_LEN);
+ if (ret)
+ return sysfs_emit(buf, "read-error\n");
+ chip->id.len = strnlen(chip->id.data, NAND_MAX_ID_LEN);
+ }
+
+ return sysfs_emit(buf, "%*phN\n", chip->id.len, chip->id.data);
+}
+MTD_DEVICE_ATTR_RO(nand_id);
+
+static umode_t mtd_nand_id_visible(struct kobject *kobj, struct attribute *attr, int n)
+{
+ struct device *dev = kobj_to_dev(kobj);
+ struct mtd_info *mtd = dev_get_drvdata(dev);
+
+ /* Only show on NAND devices (excludes UBI volumes which have type 'ubi') */
+ if (mtd->type != MTD_NANDFLASH && mtd->type != MTD_MLCNANDFLASH)
+ return 0;
+
+ return attr->mode;
+}
+
+static struct attribute *mtd_nand_attrs[] = {
+ &dev_attr_nand_id.attr,
+ NULL,
+};
+
+static const struct attribute_group mtd_nand_group = {
+ .attrs = mtd_nand_attrs,
+ .is_visible = mtd_nand_id_visible,
+};
+#endif /* CONFIG_MTD_RAW_NAND */
+
static struct attribute *mtd_attrs[] = {
&dev_attr_type.attr,
&dev_attr_flags.attr,
@@ -359,7 +412,18 @@ static struct attribute *mtd_attrs[] = {
&dev_attr_bitflip_threshold.attr,
NULL,
};
-ATTRIBUTE_GROUPS(mtd);
+
+static const struct attribute_group mtd_group = {
+ .attrs = mtd_attrs,
+};
+
+static const struct attribute_group *mtd_groups[] = {
+ &mtd_group,
+#ifdef CONFIG_MTD_RAW_NAND
+ &mtd_nand_group,
+#endif
+ NULL,
+};
static const struct device_type mtd_devtype = {
.name = "mtd",
--
2.47.3
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] mtd: core: Add nand_id sysfs attribute for NAND devices
2025-10-14 19:24 ` [PATCH v2] " jayxu1990
@ 2025-10-22 14:19 ` Miquel Raynal
[not found] ` <CABcGJDxxGSDVc9TPcTMR_7abreBchHEKHsqtfZVQ5m+0nFhJ1A@mail.gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Miquel Raynal @ 2025-10-22 14:19 UTC (permalink / raw)
To: jayxu1990
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
avnerkhan, rdlee.upstream, kernel test robot
Hello,
On 15/10/2025 at 03:24:55 +08, jayxu1990@gmail.com wrote:
> From: Jay Xu <jayxu1990@gmail.com>
>
> [Problem]
> Currently, NAND devices do not expose their NAND ID through sysfs,
> making it difficult for userspace applications to identify the specific
> NAND flash chip in use. For supply management reasons, electronics
> products are typically manufactured with multiple storage device
> suppliers, creating a need to identify which storage device is used
> on a particular product. The NAND ID is a semi-unique identifier that can
> be used to determine chip-specific characteristics such as maximum P/E
> cycles, which is essential for NAND health monitoring and wear leveling
> algorithms.
>
> [Solution]
> This patch adds a new 'nand_id' sysfs attribute that:
>
> 1. Exposes the full NAND ID (typically 5-8 bytes) in hexadecimal format
> 2. Only appears on physical NAND devices (MTD_NANDFLASH/MTD_MLCNANDFLASH)
> 3. Is hidden on virtual MTD devices
> 4. Reads from the master device to ensure consistent ID across partitions
> 5. Handles on-demand ID reading if not already populated during probe
>
> The implementation uses a separate attribute group with visibility control
> to avoid affecting existing MTD sysfs attributes. All NAND partitions
> from the same physical chip will show the same ID, as expected.
>
> The NAND-specific code is conditionally compiled with CONFIG_MTD_RAW_NAND
> to ensure clean builds when raw NAND support is not enabled.
>
> This enables userspace tools to reliably identify NAND chips for
> health monitoring, bad block management, and device-specific
> optimizations.
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202510120356.STGKDkA5-lkp@intel.com/
> Signed-off-by: Jay Xu <jayxu1990@gmail.com>
> ---
I haven't reviewed the code yet, but at a first glance I would not be
opposed to it. I remember though we already had some kind of similar
discussion and IIRC we declined (I cannot remember why nor find any
relevant link).
Richard, does that ring a bell?
If we go the route of printing an ID, maybe we should make sure the ID
is always populated (we always read it at probe time) so we do not need
to re-read it later. Also, we should probably store it somewhere in the
NAND core and not make it raw NAND specific as SPI NANDs will likely
suffer from the same issue at some point.
Thanks,
Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] mtd: core: Add nand_id sysfs attribute for NAND devices
[not found] ` <CABcGJDxxGSDVc9TPcTMR_7abreBchHEKHsqtfZVQ5m+0nFhJ1A@mail.gmail.com>
@ 2025-10-23 6:54 ` Miquel Raynal
0 siblings, 0 replies; 5+ messages in thread
From: Miquel Raynal @ 2025-10-23 6:54 UTC (permalink / raw)
To: Jay Xu
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
avnerkhan, rdlee.upstream, kernel test robot
Hi Jay,
On 22/10/2025 at 11:53:41 -05, Jay Xu <jayxu1990@gmail.com> wrote:
> Hi Miquèl:
> Thank you for the feedback and suggestions!
>
> You're right that the current implementation is raw NAND specific. I can look into extending this to support other NAND
> types by moving the functionality to the NAND core
> layer and ensure the ID is populated at probe time.
>
> Let me know if you have any other suggestions/questions,
Not at the moment, I'd like to gather feedback from other MTD folks
before we go forward. Sysfs is part of the stable API, we must be
careful.
> > Reported-by: kernel test robot <lkp@intel.com>
> > Closes:
> > https://lore.kernel.org/oe-kbuild-all/202510120356.STGKDkA5-lkp@intel.com/
Just as an FYI, these do not make any sense in a contribution like
yours. Include these tags if you are fixing something that is already in
the tree. This is a new submission and it is not a fix so "reported-by"
shall not be used.
Thanks,
Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-10-23 6:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-07 22:40 [PATCH] mtd: core: Add nand_id sysfs attribute for NAND devices jayxu1990
2025-10-11 20:03 ` kernel test robot
2025-10-14 19:24 ` [PATCH v2] " jayxu1990
2025-10-22 14:19 ` Miquel Raynal
[not found] ` <CABcGJDxxGSDVc9TPcTMR_7abreBchHEKHsqtfZVQ5m+0nFhJ1A@mail.gmail.com>
2025-10-23 6:54 ` Miquel Raynal
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).