mtd: core: Add nand_id sysfs attribute for NAND devices
PATCH 1/1 mtd: core: Add nand_id sysfs attribute for NAND devices
From: Jay Xu <[email protected]>
[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.
--- drivers/mtd/mtdcore.c | 60 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-)
drivers/mtd/mtdcore.c +59 -1
@@ -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",
Signature · 1 line
2.47.3
kernel test robot
build botkernel test robot noticed the following build errors:
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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
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
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 1/1 mtd: core: Add nand_id sysfs attribute for NAND devices
In reply to [email protected]From: Jay Xu <[email protected]>
[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.
--- drivers/mtd/mtdcore.c | 66 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-)
drivers/mtd/mtdcore.c +65 -1
@@ -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",
Signature · 1 line
2.47.3
Miquel Raynal
maintainerHello,
Quoted 29 lines · On 15/10/2025 at 03:24:55 +08, [email protected] wrote:
From: Jay Xu <[email protected]>
[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 probeThe 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_NANDto ensure clean builds when raw NAND support is not enabled.
Quoted 8 lines
This enables userspace tools to reliably identify NAND chips for health monitoring, bad block management, and device-specific optimizations.
Reported-by kernel test robot <[email protected]>Signed-off-by Jay Xu <[email protected]>---
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
A message that is not in the archive (sent off-list or not yet received): CABcGJDxxGSDVc9TPcTMR_7abreBchHEKHsqtfZVQ5m+0nFhJ1A@mail.gmail.com
Miquel Raynal
maintainerHi Jay,
Quoted 8 lines · Jay Xu
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.
Quoted 3 lines
Quoted 3 lines
Reported-by kernel test robot <[email protected]>Closes:
https://lore.kernel.org/oe-kbuild-all/[email protected]/
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