linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: expose ooblayout information via sysfs
@ 2025-07-19 12:06 Gabor Juhos
  2025-08-06  6:57 ` Miquel Raynal
  0 siblings, 1 reply; 4+ messages in thread
From: Gabor Juhos @ 2025-07-19 12:06 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: linux-kernel, linux-mtd, Gabor Juhos

Add two new sysfs device attributes which allows to determine the OOB
layout used by a given MTD device. This can be useful to verify the
current layout during driver development without adding extra debug
code. The exposed information also makes it easier to analyze NAND
dumps without the need of cravling out the layout from the driver code.

The content of the new sysfs files is similar to this:

    # cat /sys/class/mtd/mtd0/ooblayout_ecc
    0      0   49
    1     65   63
    # cat /sys/class/mtd/mtd0/ooblayout_free
    0     49   16

Also update the ABI documentation about the new attributes.

Signed-off-by: Gabor Juhos <j4g8y7@gmail.com>
---
 Documentation/ABI/testing/sysfs-class-mtd | 14 +++++++++++
 drivers/mtd/mtdcore.c                     | 40 +++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-class-mtd b/Documentation/ABI/testing/sysfs-class-mtd
index f77fa4f6d46571175b156113a1afeeb9f5f51c0f..fd3000b9cc7aee4ea9069d1869f310fefa24f97a 100644
--- a/Documentation/ABI/testing/sysfs-class-mtd
+++ b/Documentation/ABI/testing/sysfs-class-mtd
@@ -240,3 +240,17 @@ Contact:	linux-mtd@lists.infradead.org
 Description:
 		Number of bytes available for a client to place data into
 		the out of band area.
+
+What:		/sys/class/mtd/mtdX/ooblayout_ecc
+What:		/sys/class/mtd/mtdX/ooblayout_free
+Date:		July 2025
+KernelVersion:	6.17
+Contact:	linux-mtd@lists.infradead.org
+Description:
+		Newline separated list of the regions in the out of band area.
+
+		Each line contains three decimal numbers separated by spaces.
+		The first number indicates the index of the region. The second
+		number describes the starting offset within the out of band
+		area. The last number specifies the amount of bytes available
+		in the region.
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 429d8c16baf045e6a030e309ce0fb1cbec669098..552d12f749e43b7d9abb07e0235a3ef2d2a98546 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -254,6 +254,44 @@ static ssize_t mtd_oobavail_show(struct device *dev,
 }
 MTD_DEVICE_ATTR_RO(oobavail);
 
+static ssize_t mtd_ooblayout_show(struct device *dev,
+				  struct device_attribute *attr, char *buf,
+				  int (*iter)(struct mtd_info *, int section,
+					      struct mtd_oob_region *region))
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+	ssize_t size = 0;
+	int section;
+
+	for (section = 0;; section++) {
+		struct mtd_oob_region region;
+		int err;
+
+		err = iter(mtd, section, &region);
+		if (err)
+			break;
+
+		size += sysfs_emit_at(buf, size, "%-3d %4u %4u\n", section,
+				      region.offset, region.length);
+	}
+
+	return size;
+}
+
+static ssize_t mtd_ooblayout_ecc_show(struct device *dev,
+				      struct device_attribute *attr, char *buf)
+{
+	return mtd_ooblayout_show(dev, attr, buf, mtd_ooblayout_ecc);
+}
+MTD_DEVICE_ATTR_RO(ooblayout_ecc);
+
+static ssize_t mtd_ooblayout_free_show(struct device *dev,
+				       struct device_attribute *attr, char *buf)
+{
+	return mtd_ooblayout_show(dev, attr, buf, mtd_ooblayout_free);
+}
+MTD_DEVICE_ATTR_RO(ooblayout_free);
+
 static ssize_t mtd_numeraseregions_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
@@ -366,6 +404,8 @@ static struct attribute *mtd_attrs[] = {
 	&dev_attr_subpagesize.attr,
 	&dev_attr_oobsize.attr,
 	&dev_attr_oobavail.attr,
+	&dev_attr_ooblayout_ecc.attr,
+	&dev_attr_ooblayout_free.attr,
 	&dev_attr_numeraseregions.attr,
 	&dev_attr_name.attr,
 	&dev_attr_ecc_strength.attr,

---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20250719-mtd-ooblayout-sysfs-77af651b3738

Best regards,
-- 
Gabor Juhos <j4g8y7@gmail.com>


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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: expose ooblayout information via sysfs
  2025-07-19 12:06 [PATCH] mtd: expose ooblayout information via sysfs Gabor Juhos
@ 2025-08-06  6:57 ` Miquel Raynal
  2025-08-06 18:40   ` Gabor Juhos
  0 siblings, 1 reply; 4+ messages in thread
From: Miquel Raynal @ 2025-08-06  6:57 UTC (permalink / raw)
  To: Gabor Juhos
  Cc: Richard Weinberger, Vignesh Raghavendra, linux-kernel, linux-mtd

Hello Gabor,

On 19/07/2025 at 14:06:48 +02, Gabor Juhos <j4g8y7@gmail.com> wrote:

> Add two new sysfs device attributes which allows to determine the OOB
> layout used by a given MTD device. This can be useful to verify the
> current layout during driver development without adding extra debug
> code. The exposed information also makes it easier to analyze NAND
> dumps without the need of cravling out the layout from the driver
> code.

I would prefer a debugfs entry, as this is mostly focusing on
development and debugging purposes. sysfs has a stable API, which makes
it a less relevant place in this case.

Thanks,
Miquèl

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: expose ooblayout information via sysfs
  2025-08-06  6:57 ` Miquel Raynal
@ 2025-08-06 18:40   ` Gabor Juhos
  2025-08-07  7:50     ` Miquel Raynal
  0 siblings, 1 reply; 4+ messages in thread
From: Gabor Juhos @ 2025-08-06 18:40 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Richard Weinberger, Vignesh Raghavendra, linux-kernel, linux-mtd

Hi Miquel,

2025. 08. 06. 8:57 keltezéssel, Miquel Raynal írta:
> Hello Gabor,
> 
> On 19/07/2025 at 14:06:48 +02, Gabor Juhos <j4g8y7@gmail.com> wrote:
> 
>> Add two new sysfs device attributes which allows to determine the OOB
>> layout used by a given MTD device. This can be useful to verify the
>> current layout during driver development without adding extra debug
>> code. The exposed information also makes it easier to analyze NAND
>> dumps without the need of cravling out the layout from the driver
>> code.
> 
> I would prefer a debugfs entry, as this is mostly focusing on
> development and debugging purposes. sysfs has a stable API, which makes
> it a less relevant place in this case.

Sorry, it seems that I misunderstood the ABI documentation.

Since the 'sysfs-class-mtd' file is under the 'testing' directory within
'Documentation/ABI' I thought that it can be extended by attributes used for
testing purposes. Additionally, the 'oobavail' and 'oobsize' attributes are
exposed via sysfs, so it seemed to be a logical place for the new oob related ones.

Nevertheless, I will check what can I do with the debugfs based approach.

Thanks,
Gabor

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: expose ooblayout information via sysfs
  2025-08-06 18:40   ` Gabor Juhos
@ 2025-08-07  7:50     ` Miquel Raynal
  0 siblings, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2025-08-07  7:50 UTC (permalink / raw)
  To: Gabor Juhos
  Cc: Richard Weinberger, Vignesh Raghavendra, linux-kernel, linux-mtd

Hi Gabor,

>> I would prefer a debugfs entry, as this is mostly focusing on
>> development and debugging purposes. sysfs has a stable API, which makes
>> it a less relevant place in this case.
>
> Sorry, it seems that I misunderstood the ABI documentation.
>
> Since the 'sysfs-class-mtd' file is under the 'testing' directory
> within

It is indeed unclear. I'm not sure myself what that exactly means TBH,
but it's been there for a long time and can for sure be considered
stable. Sysfs in general is not for debugging.

> 'Documentation/ABI' I thought that it can be extended by attributes used for
> testing purposes. Additionally, the 'oobavail' and 'oobsize' attributes are
> exposed via sysfs, so it seemed to be a logical place for the new oob related ones.
>
> Nevertheless, I will check what can I do with the debugfs based
> approach.

Sure, thanks!

Miquèl

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-07  7:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-19 12:06 [PATCH] mtd: expose ooblayout information via sysfs Gabor Juhos
2025-08-06  6:57 ` Miquel Raynal
2025-08-06 18:40   ` Gabor Juhos
2025-08-07  7:50     ` 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).