Skip to contentSkip to conversation

fix data+OOB writes, add ioctl

RFC In review Brian Norris 5 participants 36 messages Aug 17, 2011 – Aug 26, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover fix data+OOB writes, add ioctl

Hello all,

Warning: This patch series is not a finished product yet; it is merely a "Request For Comments" on my goals, methods, etc. Please comment on this, and in the near future, there may be a full update to this series.

First off, these fixes are motivated by a number of things:

1) Broken "noecc" writes; `nandwrite -n -o' does not work on my hardware, at least, without these patches

2) We cannot write twice to the same page on MLC NAND flash. Thus, the current implementation for nandwrite, where we write OOB and page data in two separate ioctl operations, will not work. We need a new ioctl. See this thread:

http://lists.infradead.org/pipermail/linux-mtd/2011-August/037316.html

3) It is beneficial to expose the mtd_oob_mode_t options to the user-space, since we often want to use MTD_OOB_RAW, MTD_OOB_AUTO, etc. on a per-operation basis. This naturally fits in with a new ioctl that can handle different combinations of page data and OOB as well as different OOB modes. See some discussion:

http://lists.infradead.org/pipermail/linux-mtd/2011-August/037511.html

Now, there are certainly some things that this patch series does NOT yet do. I will list some now:

4) Base the old ioctls (MEMWRITEOOB, MEMWRITEOOB64) on the new one (MEMWRITEDATAOOB). In fact, the first two patches update support for the old ioctls, whereas they could simply be "merged" with the new one instead.

5) Gracefully handle cases where mtd->write_oob doesn't exist. It simply quits with -EOPNOTSUPP. I test all of this code on NAND flash only, and the NAND subsystem always supplies this function. If there is a need for this ioctl without OOB writes, then perhaps this can be fixed.

Other thoughts:

Patch 3/5 is a weird one. I know code that the patch targets is wrong (since it's obviously assuming oobsize is a power of 2), but I'm not sure the *exact* intention of the wrong code - so it's hard to be sure that my fix is valid. Please look it over.

The name "MEMWRITEDATAOOB" is totally up for debate. This was the best I could come up with at the moment.

Perhaps something should be done about the MTDFILEMODE ioctl, since it seems to cause some confusing overlap, at least to me. I see two different RAW modes:

* MTD_MODE_RAW, which belongs to the mode field in `struct mtd_file_info'. It can be set by the ioctl MTDFILEMODE
* MTD_OOB_RAW, which belongs to the mode field in `struct mtd_oob_ops'. It is set indirectly by other operations.

Does MTD_MODE_RAW imply MTD_OOB_RAW when OOB operations are involved? Does MTD_MODE_RAW imply that no ECC is applied to data?
Is the MTD file mode persistent? If so, then it may conflict with a "per-operation" mode in our new MEMWRITEDATAOOB

Maybe the "something" to be done would just be some better documentation. Or something more drastic if there's an actual conflict.

Speaking of documentation, what is this supposed to mean (from struct nand_chip, in include/linux/mtd/nand.h)?

@oob_poi: poison value buffer

It seems like the oob_poi buffer is meant to be an intermediary buffer for storing the laid-out OOB data; it's used by the NAND subsystem, especially in mode MTD_OOB_AUTO, to layout data to be written. But this "documentation" means absolutely nothing to me. I'll edit that comment in nand.h and include it in my revised patch series if I'm on the right track.

OK, that's a lot to read :) Let me know if you have questions, and I'll follow up with more info if there's something I left out.

Brian

Brian Norris (5):
mtd: support MTD_MODE_RAW for writing OOB
mtd: support MTD_MODE_RAW for reading OOB
mtd: do not assume oobsize is power of 2
mtd: move mtd_oob_mode_t to shared kernel/user space
mtd: add MEMWRITEDATAOOB ioctl

 drivers/mtd/mtdchar.c        |   54 +++++++++++++++++++++++++++++++++--------
 drivers/mtd/nand/nand_base.c |   16 +++++++++++-
 include/linux/mtd/mtd.h      |   14 -----------
 include/mtd/mtd-abi.h        |   23 ++++++++++++++++++
 4 files changed, 80 insertions(+), 27 deletions(-)
#1 lore raw

RFC 1/5 mtd: support MTD_MODE_RAW for writing OOB

mtd_do_write_oob does not pass information about whether to write in RAW mode to the lower layers (e.g., NAND). We should pass MTD_OOB_RAW or MTD_OOB_PLACE based on the MTD file mode.

This fixes issues with `nandwrite -n' and the MEMWRITEOOB[64] ioctls.

---
 drivers/mtd/mtdchar.c        |    3 ++-
 drivers/mtd/nand/nand_base.c |    9 ++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
drivers/mtd/mtdchar.c +2 -1
index a75d555..7ca4361 100644
@@ -391,6 +391,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
 	uint64_t start, uint32_t length, void __user *ptr,
 	uint32_t __user *retp)
 {
+	struct mtd_file_info *mfi = file->private_data;
 	struct mtd_oob_ops ops;
 	uint32_t retlen;
 	int ret = 0;
@@ -412,7 +413,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
 	ops.ooblen = length;
 	ops.ooboffs = start & (mtd->oobsize - 1);
 	ops.datbuf = NULL;
-	ops.mode = MTD_OOB_PLACE;
+	ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;
 
 	if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
 		return -EINVAL;
drivers/mtd/nand/nand_base.c +8 -1
index d2ee68a..e2b1c90 100644
@@ -2401,7 +2401,14 @@ static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
 		chip->pagebuf = -1;
 
 	nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
-	status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
+
+	if (ops->mode == MTD_OOB_RAW) {
+		status = 0;
+		chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
+		chip->ecc.write_page_raw(mtd, chip, NULL);
+	} else {
+		status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
+	}
 
 	if (status)
 		return status;
Signature · 1 line
1.7.0.4
#7 lore raw

RFC 2/5 mtd: support MTD_MODE_RAW for reading OOB

In reply to Brian Norris

mtd_do_read_oob does not pass information about whether to read in RAW mode to the lower layers (e.g., NAND). We should pass MTD_OOB_RAW or MTD_OOB_PLACE based on the MTD file mode.

This fixes issues with `nanddump -n' and the MEMREADOOB[64] ioctls.

---
 drivers/mtd/mtdchar.c        |   14 ++++++++------
 drivers/mtd/nand/nand_base.c |    7 ++++++-
 2 files changed, 14 insertions(+), 7 deletions(-)
drivers/mtd/mtdchar.c +8 -6
index 7ca4361..4488e9a 100644
@@ -435,9 +435,11 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
 	return ret;
 }
 
-static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
-	uint32_t length, void __user *ptr, uint32_t __user *retp)
+static int mtd_do_readoob(struct file *file, struct mtd_info *mtd,
+	uint64_t start, uint32_t length, void __user *ptr,
+	uint32_t __user *retp)
 {
+	struct mtd_file_info *mfi = file->private_data;
 	struct mtd_oob_ops ops;
 	int ret = 0;
 
@@ -455,7 +457,7 @@ static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
 	ops.ooblen = length;
 	ops.ooboffs = start & (mtd->oobsize - 1);
 	ops.datbuf = NULL;
-	ops.mode = MTD_OOB_PLACE;
+	ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;
 
 	if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
 		return -EINVAL;
@@ -716,7 +718,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
 		if (copy_from_user(&buf, argp, sizeof(buf)))
 			ret = -EFAULT;
 		else
-			ret = mtd_do_readoob(mtd, buf.start, buf.length,
+			ret = mtd_do_readoob(file, mtd, buf.start, buf.length,
 				buf.ptr, &buf_user->start);
 		break;
 	}
@@ -743,7 +745,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
 		if (copy_from_user(&buf, argp, sizeof(buf)))
 			ret = -EFAULT;
 		else
-			ret = mtd_do_readoob(mtd, buf.start, buf.length,
+			ret = mtd_do_readoob(file, mtd, buf.start, buf.length,
 				(void __user *)(uintptr_t)buf.usr_ptr,
 				&buf_user->length);
 		break;
@@ -1029,7 +1031,7 @@ static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
 		if (copy_from_user(&buf, argp, sizeof(buf)))
 			ret = -EFAULT;
 		else
-			ret = mtd_do_readoob(mtd, buf.start,
+			ret = mtd_do_readoob(file, mtd, buf.start,
 				buf.length, compat_ptr(buf.ptr),
 				&buf_user->start);
 		break;
drivers/mtd/nand/nand_base.c +6 -1
index e2b1c90..9c4df4a 100644
@@ -1787,7 +1787,12 @@ static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
 	page = realpage & chip->pagemask;
 
 	while (1) {
-		sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
+		if (ops->mode == MTD_OOB_RAW) {
+			sndcmd = 0;
+			chip->ecc.read_page_raw(mtd, chip, NULL, page);
+		} else {
+			sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
+		}
 
 		len = min(len, readlen);
 		buf = nand_transfer_oob(chip, buf, ops, len);
Signature · 1 line
1.7.0.4
#9 lore raw

RFC 3/5 mtd: do not assume oobsize is power of 2

In reply to Brian Norris

Previous generations of MTDs all used OOB sizes that were powers of 2, (e.g., 64, 128). However, newer generations of flash, especially NAND, use irregular OOB sizes that are not powers of 2 (e.g., 218, 224, 448). This means we cannot use masks like "mtd->oobsize - 1" to assume that we will get a proper bitmask for OOB operations.

As I see it, we don't actually need these masks anyway, so kill them.

---
 drivers/mtd/mtdchar.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
drivers/mtd/mtdchar.c +2 -4
index 4488e9a..a0c404b 100644
@@ -411,7 +411,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
 		return ret;
 
 	ops.ooblen = length;
-	ops.ooboffs = start & (mtd->oobsize - 1);
+	ops.ooboffs = start;
 	ops.datbuf = NULL;
 	ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;
 
@@ -422,7 +422,6 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
 	if (IS_ERR(ops.oobbuf))
 		return PTR_ERR(ops.oobbuf);
 
-	start &= ~((uint64_t)mtd->oobsize - 1);
 	ret = mtd->write_oob(mtd, start, &ops);
 
 	if (ops.oobretlen > 0xFFFFFFFFU)
@@ -455,7 +454,7 @@ static int mtd_do_readoob(struct file *file, struct mtd_info *mtd,
 		return ret;
 
 	ops.ooblen = length;
-	ops.ooboffs = start & (mtd->oobsize - 1);
+	ops.ooboffs = start;
 	ops.datbuf = NULL;
 	ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;
 
@@ -466,7 +465,6 @@ static int mtd_do_readoob(struct file *file, struct mtd_info *mtd,
 	if (!ops.oobbuf)
 		return -ENOMEM;
 
-	start &= ~((uint64_t)mtd->oobsize - 1);
 	ret = mtd->read_oob(mtd, start, &ops);
 
 	if (put_user(ops.oobretlen, retp))
Signature · 1 line
1.7.0.4
#12 lore raw

RFC 4/5 mtd: move mtd_oob_mode_t to shared kernel/user space

In reply to Brian Norris

We will want to use the MTD_OOB_{PLACE,AUTO,RAW} modes in user-space applications through the introduction of new ioctls, so we should make this enum a shared type.

---
 include/linux/mtd/mtd.h |   14 --------------
 include/mtd/mtd-abi.h   |   14 ++++++++++++++
 2 files changed, 14 insertions(+), 14 deletions(-)
include/linux/mtd/mtd.h +0 -14
index 46682ac..e089bce 100644
@@ -68,20 +68,6 @@ struct mtd_erase_region_info {
 	unsigned long *lockmap;		/* If keeping bitmap of locks */
 };
 
-/*
- * oob operation modes
- *
- * MTD_OOB_PLACE:	oob data are placed at the given offset
- * MTD_OOB_AUTO:	oob data are automatically placed at the free areas
- *			which are defined by the ecclayout
- * MTD_OOB_RAW:		mode to read oob and data without doing ECC checking
- */
-typedef enum {
-	MTD_OOB_PLACE,
-	MTD_OOB_AUTO,
-	MTD_OOB_RAW,
-} mtd_oob_mode_t;
-
 /**
  * struct mtd_oob_ops - oob operation operands
  * @mode:	operation mode
include/mtd/mtd-abi.h +14 -0
index 2f7d45b..f850d9a 100644
@@ -45,6 +45,20 @@ struct mtd_oob_buf64 {
 	__u64 usr_ptr;
 };
 
+/*
+ * oob operation modes
+ *
+ * MTD_OOB_PLACE:       oob data are placed at the given offset
+ * MTD_OOB_AUTO:        oob data are automatically placed at the free areas
+ *                      which are defined by the ecclayout
+ * MTD_OOB_RAW:         mode to read oob and data without doing ECC checking
+ */
+typedef enum {
+	MTD_OOB_PLACE,
+	MTD_OOB_AUTO,
+	MTD_OOB_RAW,
+} mtd_oob_mode_t;
+
 #define MTD_ABSENT		0
 #define MTD_RAM			1
 #define MTD_ROM			2
Signature · 1 line
1.7.0.4
#17 lore raw

RFC 5/5 mtd: add MEMWRITEDATAOOB ioctl

In reply to Brian Norris

Implement a new ioctl for writing both page data and OOB to flash at the same time. This is especially useful for MLC NAND, which cannot be written twice (i.e., we cannot successfully write the page data and OOB in two separate operations).

---
 drivers/mtd/mtdchar.c |   31 +++++++++++++++++++++++++++++++
 include/mtd/mtd-abi.h |    9 +++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)
drivers/mtd/mtdchar.c +31 -0
index a0c404b..bef8462 100644
@@ -749,6 +749,37 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
 		break;
 	}
 
+	case MEMWRITEDATAOOB:
+	{
+		struct mtd_data_oob_buf buf;
+
+		if (copy_from_user(&buf, argp, sizeof(buf)) ||
+				!access_ok(VERIFY_READ, buf.usr_ptr, buf.len)) {
+			ret = -EFAULT;
+		} else if (!mtd->write_oob) {
+			ret = -EOPNOTSUPP;
+		} else {
+			struct mtd_oob_ops ops;
+
+			ops.mode = buf.mode;
+			ops.len = (size_t)buf.len;
+			ops.ooblen = (size_t)buf.ooblen;
+			ops.ooboffs = 0;
+
+			ops.datbuf = memdup_user(
+					(void __user *)(uintptr_t)buf.usr_ptr,
+					ops.len + ops.ooblen);
+			if (IS_ERR(ops.datbuf))
+				return PTR_ERR(ops.datbuf);
+
+			ops.oobbuf = ops.datbuf + ops.len;
+
+			ret = mtd->write_oob(mtd, (loff_t)buf.start, &ops);
+			kfree(ops.datbuf);
+		}
+		break;
+	}
+
 	case MEMLOCK:
 	{
 		struct erase_info_user einfo;
include/mtd/mtd-abi.h +9 -0
index f850d9a..20df299 100644
@@ -59,6 +59,14 @@ typedef enum {
 	MTD_OOB_RAW,
 } mtd_oob_mode_t;
 
+struct mtd_data_oob_buf {
+	__u64 start;
+	__u32 len;
+	__u32 ooblen;
+	__u64 usr_ptr;
+	mtd_oob_mode_t __user mode;
+};
+
 #define MTD_ABSENT		0
 #define MTD_RAM			1
 #define MTD_ROM			2
@@ -141,6 +149,7 @@ struct otp_info {
 #define MEMWRITEOOB64		_IOWR('M', 21, struct mtd_oob_buf64)
 #define MEMREADOOB64		_IOWR('M', 22, struct mtd_oob_buf64)
 #define MEMISLOCKED		_IOR('M', 23, struct erase_info_user)
+#define MEMWRITEDATAOOB		_IOWR('M', 24, struct mtd_data_oob_buf)
 
 /*
  * Obsolete legacy interface. Keep it in order not to break userspace
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [RFC 1/5] mtd: support MTD_MODE_RAW for writing OOB

In reply to Brian Norris
Quoted 7 lines · Brian Norris

mtd_do_write_oob does not pass information about whether to write in RAW mode to the lower layers (e.g., NAND). We should pass MTD_OOB_RAW or MTD_OOB_PLACE based on the MTD file mode.

This fixes issues with `nandwrite -n' and the MEMWRITEOOB[64] ioctls.

I guess this patch deserves to be non-RFC? Should it be pushed to l2-mtd-2.6.git? Should it even have "Cc: [email protected] [kernel version +] ?

Quoted 18 lines
---
 drivers/mtd/mtdchar.c        |    3 ++-
 drivers/mtd/nand/nand_base.c |    9 ++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
drivers/mtd/mtdchar.c +1 -0
index a75d555..7ca4361 100644
@@ -391,6 +391,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
 	uint64_t start, uint32_t length, void __user *ptr,
 	uint32_t __user *retp)
 {
+	struct mtd_file_info *mfi = file->private_data;
 	struct mtd_oob_ops ops;
 	uint32_t retlen;
 	int ret = 0;

drivers/mtd/mtdchar.c static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd, · line 416

Quoted 5 lines
drivers/mtd/mtdchar.c +1 -1
@@ -412,7 +413,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
 	ops.ooblen = length;
 	ops.ooboffs = start & (mtd->oobsize - 1);
 	ops.datbuf = NULL;
-	ops.mode = MTD_OOB_PLACE;
+	ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;

Would be really great to document things while you are in it - e.g., putting a little note in include/linux/mtd/mtd.h that MTD_OOB_PLACE is the default mode. And of course documenting the modes in this file a bit more would be also great. I encourage you to store the wisdom you gain in the comments :-)

Quoted 8 lines
drivers/mtd/mtdchar.c +0 -0
 	if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
 		return -EINVAL;
drivers/mtd/nand/nand_base.c +0 -0
index d2ee68a..e2b1c90 100644

drivers/mtd/nand/nand_base.c static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, · line 2408

Quoted 9 lines
drivers/mtd/nand/nand_base.c +5 -1
@@ -2401,7 +2401,14 @@ static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
 		chip->pagebuf = -1;
 
 	nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
-	status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
+
+	if (ops->mode == MTD_OOB_RAW) {
+		status = 0;
+		chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
+		chip->ecc.write_page_raw(mtd, chip, NULL);

Strange that write_page_raw does not return an error code - it cannot fail? But this is a separate issue anyway.

Quoted 6 lines
drivers/mtd/nand/nand_base.c +3 -0
+	} else {
+		status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
+	}
 
 	if (status)
 		return status;
Signature · 2 lines
Best Regards,
Artem Bityutskiy
#8 lore raw

Re: [RFC 2/5] mtd: support MTD_MODE_RAW for reading OOB

In reply to Brian Norris
Quoted 7 lines · Brian Norris

mtd_do_read_oob does not pass information about whether to read in RAW mode to the lower layers (e.g., NAND). We should pass MTD_OOB_RAW or MTD_OOB_PLACE based on the MTD file mode.

This fixes issues with `nanddump -n' and the MEMREADOOB[64] ioctls.

I guess similar question applies to this patch - why is this RFC while it looks like a candidate for '[email protected]'?

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#10 lore raw

Re: [RFC 3/5] mtd: do not assume oobsize is power of 2

In reply to Brian Norris
Quoted 9 lines · Brian Norris

Previous generations of MTDs all used OOB sizes that were powers of 2, (e.g., 64, 128). However, newer generations of flash, especially NAND, use irregular OOB sizes that are not powers of 2 (e.g., 218, 224, 448). This means we cannot use masks like "mtd->oobsize - 1" to assume that we will get a proper bitmask for OOB operations.

As I see it, we don't actually need these masks anyway, so kill them.

Once you double checked that you caught all the places which assume power-of-2 and do things like "x & (mtd->oobsize - 1", and once you confirmed that you did some basic test by running few nandwrite commands which involve OOB operations, I think this patch can go in independently.

IOW, this does need to be RFC, IMO.

But yes, if there is a buggy piece of user-space code which uses non-aligned offsets, and kernel alignes them - this change will break it. IOW, this patch may expose bugs in user-space code. This is kind of ABI break, but I think for MTD which is not very widely used, we can do things like this.

IOW, I understand that this may break some user-space program, but working with MTD for many years I thing there are very very little of such programs and I think it is not a huge issue.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#13 lore raw

Re: [RFC 4/5] mtd: move mtd_oob_mode_t to shared kernel/user space

In reply to Brian Norris

include/mtd/mtd-abi.h struct mtd_oob_buf64 { · line 60

Quoted 13 lines · Brian Norris
include/mtd/mtd-abi.h +13 -0
+/*
+ * oob operation modes
+ *
+ * MTD_OOB_PLACE:       oob data are placed at the given offset
+ * MTD_OOB_AUTO:        oob data are automatically placed at the free areas
+ *                      which are defined by the ecclayout
+ * MTD_OOB_RAW:         mode to read oob and data without doing ECC checking
+ */
+typedef enum {
+	MTD_OOB_PLACE,
+	MTD_OOB_AUTO,
+	MTD_OOB_RAW,
+} mtd_oob_mode_t;

Could we get rid of this typedef and use anonymous enum instead:

enum {
	A,
	B,
};

Indeed, we do not need it, and we do not want to pollute the user-space namespaces unnecessarily.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#18 lore raw

Re: [RFC 5/5] mtd: add MEMWRITEDATAOOB ioctl

In reply to Brian Norris
Quoted 6 lines · Brian Norris

Implement a new ioctl for writing both page data and OOB to flash at the same time. This is especially useful for MLC NAND, which cannot be written twice (i.e., we cannot successfully write the page data and OOB in two separate operations).

Looks good except few nit-picks.

drivers/mtd/mtdchar.c static int mtd_ioctl(struct file *file, u_int cmd, u_long arg) · line 771

Quoted 8 lines
drivers/mtd/mtdchar.c +8 -0
+			ops.mode = buf.mode;
+			ops.len = (size_t)buf.len;
+			ops.ooblen = (size_t)buf.ooblen;
+			ops.ooboffs = 0;
+
+			ops.datbuf = memdup_user(
+					(void __user *)(uintptr_t)buf.usr_ptr,
+					ops.len + ops.ooblen);

I'd introduced a local variable for buf.usr_ptr and made the formatting nicer.

Quoted 19 lines
drivers/mtd/mtdchar.c +11 -0
+			if (IS_ERR(ops.datbuf))
+				return PTR_ERR(ops.datbuf);
+
+			ops.oobbuf = ops.datbuf + ops.len;
+
+			ret = mtd->write_oob(mtd, (loff_t)buf.start, &ops);
+			kfree(ops.datbuf);
+		}
+		break;
+	}
+
 	case MEMLOCK:
 	{
 		struct erase_info_user einfo;
include/mtd/mtd-abi.h +0 -0
index f850d9a..20df299 100644

include/mtd/mtd-abi.h typedef enum { · line 68

Quoted 10 lines
include/mtd/mtd-abi.h +7 -0
@@ -59,6 +59,14 @@ typedef enum {
 	MTD_OOB_RAW,
 } mtd_oob_mode_t;
 
+struct mtd_data_oob_buf {
+	__u64 start;
+	__u32 len;
+	__u32 ooblen;
+	__u64 usr_ptr;
+	mtd_oob_mode_t __user mode;
+};

Let's add some reserved space for future improvements - I think it is always a good idea to do for ioctls:

+ __u8 padding[8]

include/mtd/mtd-abi.h struct otp_info { · line 152

Quoted 9 lines
include/mtd/mtd-abi.h +2 -0
+
 #define MTD_ABSENT		0
 #define MTD_RAM			1
 #define MTD_ROM			2
@@ -141,6 +149,7 @@ struct otp_info {
 #define MEMWRITEOOB64		_IOWR('M', 21, struct mtd_oob_buf64)
 #define MEMREADOOB64		_IOWR('M', 22, struct mtd_oob_buf64)
 #define MEMISLOCKED		_IOR('M', 23, struct erase_info_user)
+#define MEMWRITEDATAOOB		_IOWR('M', 24, struct mtd_data_oob_buf)

Would be greate to add a short comment about what this ioctl does. Of course you can optionally do this for all of them, but at least for the new one it does not hurt to have.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#23 lore raw
In reply to Brian Norris

Hi,

Quoted 4 lines · Brian Norris

First off, these fixes are motivated by a number of things:

1) Broken "noecc" writes; `nandwrite -n -o' does not work on my hardware, at least, without these patches

I think Ivan complained about this at some point - he said he has images with crafted ECC codes to test his BCH library, but he could not flash them properly. So yes, this is worth fixing.

Quoted 10 lines

Perhaps something should be done about the MTDFILEMODE ioctl, since it seems to cause some confusing overlap, at least to me. I see two different RAW modes:

* MTD_MODE_RAW, which belongs to the mode field in `struct mtd_file_info'. It can be set by the ioctl MTDFILEMODE
* MTD_OOB_RAW, which belongs to the mode field in `struct mtd_oob_ops'. It is set indirectly by other operations.

Does MTD_MODE_RAW imply MTD_OOB_RAW when OOB operations are involved?

I would make sure these 2 set of macros have different prefixes. Probably re-naming the internal ones is easier, but I'd anyway re-named the external ones - indeed they are about access mode for a specific file descriptor, so I'd use the "MTD_FILE_MODE_" prefix. But not sure, you can invent something else. In any case, it is cleaner to have different prefixes for different name-spaces.

Quoted 1 line

Does MTD_MODE_RAW imply that no ECC is applied to data?

Not sure, but judging from how it is used - no.

Quoted 1 line

Is the MTD file mode persistent?

It is stored in the file descriptor (file->private_data), so the life-time of the mode is equivalent to the life-time for the file descriptor, AFAICS.

Quoted 2 lines

If so, then it may conflict with a
"per-operation" mode in our new MEMWRITEDATAOOB

Good point. Frankly, I find this stateful model with modes horrible. But we have what we have. I guess we will need to carefully document which ioctl's are affected by the MTD mode, and which are not (in the header file which we expose to the user-space).

I think MEMWRITEDATAOOB should ignore the mode.

My quick look at the code suggests me that the RAW/NORMAL file modes is about read/write operations - in normal mode we read/write from/to only the main area, in raw mode we consider the main area and OOB as one continuous region and read/write from/to this region.

And MEMWRITEDATAOOB is about accession only OOB, so the modes should not affect it.

Quoted 2 lines

Maybe the "something" to be done would just be some better documentation. Or something more drastic if there's an actual conflict.

To me it looks like (a) re-naming and (be) some more comments in the header files is enough.

Quoted 2 lines

Speaking of documentation, what is this supposed to mean (from struct nand_chip, in include/linux/mtd/nand.h)?

Sorry, do not know off the top of my head.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#24 lore raw
Quoted 11 lines · Artem Bityutskiy

Hi,

Quoted 4 lines · Brian Norris

First off, these fixes are motivated by a number of things:

1) Broken "noecc" writes; `nandwrite -n -o' does not work on my hardware, at least, without these patches

I think Ivan complained about this at some point - he said he has images with crafted ECC codes to test his BCH library, but he could not flash them properly. So yes, this is worth fixing.

Actually it was Ricard Wanderlof, but indeed he was testing the BCH library
(http://lists.infradead.org/pipermail/linux-mtd/2011-March/034516.html).
It would be nice if he could ack the fix ?

--
Best Regards,

Ivan

#25 lore raw
Quoted 9 lines · Ivan Djelic
Quoted 8 lines · Artem Bityutskiy

Hi,

Quoted 4 lines · Brian Norris

First off, these fixes are motivated by a number of things:

1) Broken "noecc" writes; `nandwrite -n -o' does not work on my hardware, at least, without these patches

Quoted 7 lines
Quoted 3 lines

I think Ivan complained about this at some point - he said he has images with crafted ECC codes to test his BCH library, but he could not flash them properly. So yes, this is worth fixing.

Actually it was Ricard Wanderlof, but indeed he was testing the BCH library
(http://lists.infradead.org/pipermail/linux-mtd/2011-March/034516.html).
It would be nice if he could ack the fix ?

Sure, CCed.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#3 lore raw

Re: [RFC 1/5] mtd: support MTD_MODE_RAW for writing OOB

In reply to Artem Bityutskiy

I forgot to CC a contributor on this (and the complementary patch for "read OOB")

Cc Jim Quinlan <[email protected]>
Quoted 6 lines · Artem Bityutskiy
Quoted 1 line · Brian Norris

This fixes issues with `nandwrite -n' and the MEMWRITEOOB[64] ioctls.

I guess this patch deserves to be non-RFC? Should it be pushed to l2-mtd-2.6.git? Should it even have "Cc: [email protected] [kernel version +] ?

I'm not actually sure where this stands (RFC vs. patch) since I really wanted some outside opinion on the methods used here. I have a feeling that some of this is only working on my hardware. For instance, somehow (I'm really not sure how!) `nandwrite -n -o' is working in nandsim without my fix. Perhaps this is due to a different set of nand_ecc_ctrl functions (soft ECC vs. HW ECC).

With this patch applied, however, I get some strange kernel oopses with nandsim. I've identified at least one issue, I think, but I haven't completely resolved this discrepancy between nandsim and my driver. See sample commands:

# insmod nandsim.ko
# nandwrite /dev/mtdX <data.bin> -n -o

So for this "fix" (and its coming updates), I would appreciate some outside testing on other systems, especially before we send this to stable or even before including it upstream at all.

And any comments on the current status of noecc and MTD_OOB_RAW from others would be highly valuable to me; a bit of system information and a "working" or "not working since commit [XXX]" would be a start. In the meantime, I'm trying to get a hold of a wider variety of test systems for myself for these kind of issues...

Quoted 8 lines
Quoted 2 lines

-     ops.mode = MTD_OOB_PLACE;
+     ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;

Would be really great to document things while you are in it - e.g., putting a little note in include/linux/mtd/mtd.h that MTD_OOB_PLACE is the default mode. And of course documenting the modes in this file a bit more would be also great. I encourage you to store the wisdom you gain in the comments :-)

I'll see what I can do. Probably a task for after the dust settles on some of the changes I'm working on.

Brian

#11 lore raw

Re: [RFC 3/5] mtd: do not assume oobsize is power of 2

In reply to Artem Bityutskiy
Quoted 6 lines · Artem Bityutskiy

On Wed, 2011-08-17 at 16:50 -0700, Brian Norris wrote:
Once you double checked that you caught all the places which assume power-of-2 and do things like "x & (mtd->oobsize - 1", and once you confirmed that you did some basic test by running few nandwrite commands which involve OOB operations, I think this patch can go in independently.

I'll do a little more combing through the code and perform some independent tests for this patch, then either resend an amended patch or comment back here.

Quoted 1 line

IOW, this does need to be RFC, IMO.

I agree. I think I lumped it in with the others because I discovered it as a byproduct of my work on the other patches in this series.

Brian

#14 lore raw

Re: [RFC 4/5] mtd: move mtd_oob_mode_t to shared kernel/user space

In reply to Artem Bityutskiy
Quoted 9 lines · Artem Bityutskiy

include/mtd/mtd-abi.h struct mtd_oob_buf64 { · line 60

Quoted 5 lines · Brian Norris
include/mtd/mtd-abi.h +5 -0
+typedef enum {
+     MTD_OOB_PLACE,
+     MTD_OOB_AUTO,
+     MTD_OOB_RAW,
+} mtd_oob_mode_t;

Could we get rid of this typedef and use anonymous enum instead:

Quoted 7 lines

enum {
       A,
       B,
};

Indeed, we do not need it, and we do not want to pollute the user-space namespaces unnecessarily.

Well, we do *use* the typedef in a few kernel structs, and we will use it in user space as well. So we may run into issues with 32-bit vs. 64-bit integers if we just stick with an int-based type, right?

Also, I'm pondering the question: do we need to worry about the underlying numbering for such an enum? I believe there it is theoretically possible for different compilers to choose a different numbering scheme, potentially making user-compiled software incompatible with the internal kernel binary. Perhaps to be safe we could just do:

enum {
     MTD_OOB_PLACE = 0,
     MTD_OOB_AUTO = 1,
     MTD_OOB_RAW = 2,
};

Or instead, maybe we should just switch to a __u32 type and some #defines...

Sorry if this is a lot of thinking out loud here :)

Brian

#34 lore raw
In reply to Artem Bityutskiy
Quoted 7 lines · Artem Bityutskiy
Quoted 1 line · Brian Norris

Does MTD_MODE_RAW imply MTD_OOB_RAW when OOB operations are involved?

I would make sure these 2 set of macros have different prefixes. Probably re-naming the internal ones is easier, but I'd anyway re-named the external ones - indeed they are about access mode for a specific file descriptor, so I'd use the "MTD_FILE_MODE_" prefix.

To be sure, are you categorizing the internal/external modes as follows?

External = MTD_MODE_NORMAL, MTD_MODE_RAW, etc. Internal = MTD_OOB_PLACE, MTD_OOB_AUTO, MTD_OOB_RAW

Because my proposal here is to move those Internal ones to becoming a new set of External modes for the purpose of the new MEMWRITEDATAOOB. Finding good names for them makes more sense now than after they become External. But as you say, renaming the *current* External modes could help clarify things as well.

Quoted 3 lines
Quoted 1 line

Does MTD_MODE_RAW imply that no ECC is applied to data?

Not sure, but judging from how it is used - no.

I see at least one instance of code that seems to assume the answer is "yes"; see the "noecc" code in nandwrite.c. Where do you find evidence that says "no"?

Quoted 5 lines
Quoted 1 line

Is the MTD file mode persistent?

It is stored in the file descriptor (file->private_data), so the life-time of the mode is equivalent to the life-time for the file descriptor, AFAICS.

OK.

Quoted 1 line

I think MEMWRITEDATAOOB should ignore the mode.

Sure. May be easier said than done though. It looks like there are too many entry points into the MTD layer, with different ioctls plus the standard read/write controls. I'll see what I can do about keeping this under control.

Quoted 4 lines

My quick look at the code suggests me that the RAW/NORMAL file modes is about read/write operations - in normal mode we read/write from/to only the main area, in raw mode we consider the main area and OOB as one continuous region and read/write from/to this region.

I'm not sure I understand where the part about continuous region comes from, but I think I understand some more here... MTDFILEMODE only affects calls to mtd_write and mtd_read. It does not currently do anything for the MEM{READ,WRITE}OOB[64] ioctls. This has been a point of confusion for me and for others, I think.

It looks like going forward, we should agree on something like the following, although the names still may change:

1) the generic term "raw" mode means that we are writing or reading data exactly as-is to/from the flash, without error correction applied to it.
2) MTD_OOB_RAW means means that we read or write in raw mode. This can be directly user-controlled from the new MEMWRITEDATAOOB ioctl.
3) MTD_MODE_RAW is used only with the MTDFILEMODE ioctl, and it *only* affects error correction for read() and write() calls to the corresponding file descriptor. Its sole purpose is to enable MTD_OOB_RAW during standard read/write operations (no OOB interaction).

Unfortunately, item 3 showcases our naming inconsistency, since MTD_OOB_RAW is actually utilized for a non-OOB operation. Perhaps this should be considered for renaming?

[Regarding MTD_MODE_RAW:]

Quoted 5 lines
Quoted 2 lines

Maybe the "something" to be done would just be some better documentation. Or something more drastic if there's an actual conflict.

To me it looks like (a) re-naming and (be) some more comments in the header files is enough.

OK, I may rename, and I'll try to add more comments when we've got the code set.

Speaking of renaming, I'm not so sure about the name I gave this ioctl; which of the following makes the most sense for naming the new ioctl? Speak soon or forever hold your peace.
1) MEMWRITEDATAOOB
2) MEMWRITE
3) MEMWRITEOPS
4) ...something else?

Brian

#19 lore raw

Re: [RFC 5/5] mtd: add MEMWRITEDATAOOB ioctl

In reply to Artem Bityutskiy
Quoted 7 lines · Artem Bityutskiy
Quoted 3 lines · Brian Norris

+                     ops.datbuf = memdup_user(
+                                     (void __user *)(uintptr_t)buf.usr_ptr,
+                                     ops.len + ops.ooblen);

I'd introduced a local variable for buf.usr_ptr and made the formatting nicer.

Sure. Will incorporate into v2.

Quoted 6 lines
Quoted 1 line

+struct mtd_data_oob_buf {

Let's add some reserved space for future improvements - I think it is always a good idea to do for ioctls:

+ __u8 padding[8]

OK. Will consider for v2.

Quoted 5 lines
Quoted 1 line

+#define MEMWRITEDATAOOB              _IOWR('M', 24, struct mtd_data_oob_buf)

Would be greate to add a short comment about what this ioctl does. Of course you can optionally do this for all of them, but at least for the new one it does not hurt to have.

Sure, will do. I'll probably add at least a few comments in the header for all the relevant ioctls we've been discussing, but that'll wait until we've finished the code.

BTW, I'm considering splitting the usr_ptr into separate buffers for data and oob. This will probably be a little easier for the user interface as well as for internal kernel operations. Anyway, the resulting struct is looking more and more like the existing `struct mtd_oob_ops' (this is kind of by design); is it still a good idea to keep the user-facing struct independent of the internal mtd_oob_ops struct? I'm thinking it would leave some flexibility for the future.

Brian

#4 lore raw

Re: [RFC 1/5] mtd: support MTD_MODE_RAW for writing OOB

In reply to Brian Norris
Quoted 13 lines · Brian Norris

I forgot to CC a contributor on this (and the complementary patch for "read OOB")

Cc Jim Quinlan <[email protected]>
Quoted 6 lines · Artem Bityutskiy
Quoted 1 line · Brian Norris

This fixes issues with `nandwrite -n' and the MEMWRITEOOB[64] ioctls.

I guess this patch deserves to be non-RFC? Should it be pushed to l2-mtd-2.6.git? Should it even have "Cc: [email protected] [kernel version +] ?

Quoted 6 lines

I'm not actually sure where this stands (RFC vs. patch) since I really wanted some outside opinion on the methods used here. I have a feeling that some of this is only working on my hardware. For instance, somehow (I'm really not sure how!) `nandwrite -n -o' is working in nandsim without my fix. Perhaps this is due to a different set of nand_ecc_ctrl functions (soft ECC vs. HW ECC).

May be one of the reasons is that nandsim just _copies_ data to the internal RAM buffer, instead of doing binary "&", so you can re-write in some cases, but not sure.

Quoted 12 lines

With this patch applied, however, I get some strange kernel oopses with nandsim. I've identified at least one issue, I think, but I haven't completely resolved this discrepancy between nandsim and my driver. See sample commands:

# insmod nandsim.ko
# nandwrite /dev/mtdX <data.bin> -n -o

So for this "fix" (and its coming updates), I would appreciate some outside testing on other systems, especially before we send this to stable or even before including it upstream at all.

Quoted 5 lines

And any comments on the current status of noecc and MTD_OOB_RAW from others would be highly valuable to me; a bit of system information and a "working" or "not working since commit [XXX]" would be a start. In the meantime, I'm trying to get a hold of a wider variety of test systems for myself for these kind of issues...

Yes, would be great to have more people to test. I always encourage people to look at patches _before_ they get in and break their systems, not afterwards. But even if nobody cares, we can merge your stuff after you gave it some more testing - absence of caring people should not stop the progress in MTD :-)

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#5 lore raw

Re: [RFC 1/5] mtd: support MTD_MODE_RAW for writing OOB

In reply to Brian Norris

Hi, Brian,

2011/8/18 Brian Norris <[email protected]>:

Quoted 5 lines

mtd_do_write_oob does not pass information about whether to write in RAW mode to the lower layers (e.g., NAND). We should pass MTD_OOB_RAW or MTD_OOB_PLACE based on the MTD file mode.

This fixes issues with `nandwrite -n' and the MEMWRITEOOB[64] ioctls.

It's worthy here stating clear what the issue you meet and how it fix.

Jason

#15 lore raw

Re: [RFC 4/5] mtd: move mtd_oob_mode_t to shared kernel/user space

In reply to Brian Norris
Quoted 15 lines · Brian Norris
Quoted 14 lines · Artem Bityutskiy

include/mtd/mtd-abi.h struct mtd_oob_buf64 { · line 60

Quoted 5 lines · Brian Norris
include/mtd/mtd-abi.h +5 -0
+typedef enum {
+     MTD_OOB_PLACE,
+     MTD_OOB_AUTO,
+     MTD_OOB_RAW,
+} mtd_oob_mode_t;

Could we get rid of this typedef and use anonymous enum instead:

enum {
       A,
       B,
};
Quoted 6 lines
Quoted 2 lines

Indeed, we do not need it, and we do not want to pollute the user-space namespaces unnecessarily.

Well, we do *use* the typedef in a few kernel structs, and we will use it in user space as well. So we may run into issues with 32-bit vs. 64-bit integers if we just stick with an int-based type, right?

I just find typedefs unreadable. Every time you see it - you need to look at the definition, find it, distract. And C does not do any type checking anway, so typedefs for enums really make little sense. If you really do not want to use int, let's use 'enum blah' type, which is at least readable - when you see it - you know it is just an integer, and you do not have to find the definition.

Anonymous enums is my preference. They are just like macro definition, but with assumed type, and potentially debugger-friendly.

So, to conclude:

1. typedefs for enums make no sense a all - C does not do any strict type-checking anyway, and typedefs only make reading code more difficult.

2. using 'enum blah' is more sensible - it does not hurt readability too much at least.

3. IMHO, and I even dare to say that many kernel gurus would agree, if we are talking about a simple enum with just few constants inside - anonymous enum is the best - the code is simpler and more readable.

It is not that difficult to remove in-kernel usage of typedefs, I think. VS user-space - the only user we care about - mtd-utils - has its own copy of the headers, and if we update the header files there, we can amend mtd-utils.

32/64 problems - what do you mean? enumeration = 'int' which is always 32 bits in all architectures Linux supports.

References:
1. C99 standard, section 6.4.4.3: enumeration = int
2. C99 section 6.2.5, marker 20: about enum = just definition of a constant and _not_ a typed form.

So, I'd be happy with 3, can live with 2, and dislike 1 very much :-)

Quoted 7 lines

Also, I'm pondering the question: do we need to worry about the underlying numbering for such an enum? I believe there it is theoretically possible for different compilers to choose a different numbering scheme, potentially making user-compiled software incompatible with the internal kernel binary. Perhaps to be safe we could just do:

Quoted 5 lines
enum {
     MTD_OOB_PLACE = 0,
     MTD_OOB_AUTO = 1,
     MTD_OOB_RAW = 2,
};

I cannot refer to a section in a standard, but I am sure unitialized tags will start with 0 reliably. But yes, it is much less error-prone to initialize the tags explicitly, because it makes it a bit more difficult to make a stupid mistake by adding a new constant and change values of other constants.

Quoted 1 line

Or instead, maybe we should just switch to a __u32 type and some #defines...

Anonymous enum is almost that, you can just treat it as int32_t.

Quoted 1 line

Sorry if this is a lot of thinking out loud here :)

Yeah, least significant topics usually cause most discussions - I was told recently by a colleague that this is called "bike shedding". Notice how much I wrote comparing to more important posts of yours :-)

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#35 lore raw
In reply to Brian Norris
Quoted 9 lines · Brian Norris
Quoted 7 lines · Artem Bityutskiy
Quoted 1 line · Brian Norris

Does MTD_MODE_RAW imply MTD_OOB_RAW when OOB operations are involved?

I would make sure these 2 set of macros have different prefixes. Probably re-naming the internal ones is easier, but I'd anyway re-named the external ones - indeed they are about access mode for a specific file descriptor, so I'd use the "MTD_FILE_MODE_" prefix.

Quoted 4 lines

To be sure, are you categorizing the internal/external modes as follows?

External = MTD_MODE_NORMAL, MTD_MODE_RAW, etc. Internal = MTD_OOB_PLACE, MTD_OOB_AUTO, MTD_OOB_RAW

Yes :-)

Quoted 2 lines

Because my proposal here is to move those Internal ones to becoming a new set of External modes for the purpose of the new MEMWRITEDATAOOB.

Right, I was talking about what is in mainline at the moment.

Quoted 3 lines

Finding good names for them makes more sense now than after they become External. But as you say, renaming the *current* External modes could help clarify things as well.

I think you can re-name any of them nicely - we can always amend mtd-utils.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Does MTD_MODE_RAW imply that no ECC is applied to data?

Not sure, but judging from how it is used - no.

I see at least one instance of code that seems to assume the answer is "yes"; see the "noecc" code in nandwrite.c. Where do you find evidence that says "no"?

You are right, this looks like "no ECC" actually.

Well, my cscopes find only 2 places where this symbol is used in the kernel:

2 220 drivers/mtd/mtdchar.c <<mtd_read>>
case MTD_MODE_RAW:
3 316 drivers/mtd/mtdchar.c <<mtd_write>>
case MTD_MODE_RAW:

So this is only about reading/writing and nothing else.

Then, let's look at one of them, say 3:

drivers/mtd/mtdchar.c: ret = mtd->write_oob(mtd, *ppos, &ops);

Then find out in nand_base.c that this maps to 'nand_write_oob()', which, in-turn calls then 'nand_do_write_ops()', and which then calls, _I guess_:

               ret = chip->write_page(mtd, chip, wbuf, page, cached,
                                       (ops->mode == MTD_OOB_RAW));

which then calls:

chip->ecc.write_page_raw(mtd, chip, buf);

which seems to be the "no ECC" write function.

But I guess MEMWRITEDATAOOB should anyway "override" the "global" mode.

Quoted 6 lines
Quoted 1 line

I think MEMWRITEDATAOOB should ignore the mode.

Sure. May be easier said than done though. It looks like there are too many entry points into the MTD layer, with different ioctls plus the standard read/write controls. I'll see what I can do about keeping this under control.

Yeah, it is a mess, I agree :-(

Quoted 6 lines

It looks like going forward, we should agree on something like the following, although the names still may change:

1) the generic term "raw" mode means that we are writing or reading data exactly as-is to/from the flash, without error correction applied to it.

Right.

Quoted 2 lines

2) MTD_OOB_RAW means means that we read or write in raw mode. This can be directly user-controlled from the new MEMWRITEDATAOOB ioctl.

OK

Quoted 5 lines

3) MTD_MODE_RAW is used only with the MTDFILEMODE ioctl, and it *only* affects error correction for read() and write() calls to the corresponding file descriptor. Its sole purpose is to enable MTD_OOB_RAW during standard read/write operations (no OOB interaction).

OK

Quoted 3 lines

Unfortunately, item 3 showcases our naming inconsistency, since MTD_OOB_RAW is actually utilized for a non-OOB operation. Perhaps this should be considered for renaming?

Probably the "OOB" part should go away? Give the "per-file" modes some other name which suggests that these are old-fashioned and then use MTD_RW_MODE_ or even just MTD_MODE_ for the "internal" constants?

Quoted 7 lines

Speaking of renaming, I'm not so sure about the name I gave this ioctl; which of the following makes the most sense for naming the new ioctl? Speak soon or forever hold your peace.
1) MEMWRITEDATAOOB
2) MEMWRITE
3) MEMWRITEOPS
4) ...something else?

1 or 2 seems OK to me, but I do not have a strong opinion here.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#20 lore raw

Re: [RFC 5/5] mtd: add MEMWRITEDATAOOB ioctl

In reply to Brian Norris
Quoted 7 lines · Brian Norris

BTW, I'm considering splitting the usr_ptr into separate buffers for data and oob. This will probably be a little easier for the user interface as well as for internal kernel operations. Anyway, the resulting struct is looking more and more like the existing `struct mtd_oob_ops' (this is kind of by design); is it still a good idea to keep the user-facing struct independent of the internal mtd_oob_ops struct? I'm thinking it would leave some flexibility for the future.

Yes, it is good idea, and you anyway cannot use the same struct for both, because structs for ioctl have more limitation WRT struct size (best to make it to be multiple of 64-bits), fileds (no pointers should be used, better types like _u32 should be used), reserve for future (a pool of bytes which user must set to 0 and which we can use in future).

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#21 lore raw

Re: [RFC 5/5] mtd: add MEMWRITEDATAOOB ioctl

In reply to Brian Norris
Quoted 6 lines · Brian Norris
Quoted 4 lines

Let's add some reserved space for future improvements - I think it is always a good idea to do for ioctls:

+ __u8 padding[8]

OK. Will consider for v2.

And add a comment that the user has to set them to 0. This way, when in the future we want to use them, we'll know that 0 means "original" behavior.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#22 lore raw

Re: [RFC 5/5] mtd: add MEMWRITEDATAOOB ioctl

In reply to Brian Norris
Quoted 30 lines · Brian Norris
Quoted 7 lines · Artem Bityutskiy

drivers/mtd/mtdchar.c static int mtd_ioctl(struct file *file, u_int cmd, u_long arg) · line 771

Quoted 3 lines · Brian Norris
drivers/mtd/mtdchar.c +3 -0
+                     ops.datbuf = memdup_user(
+                                     (void __user *)(uintptr_t)buf.usr_ptr,
+                                     ops.len + ops.ooblen);

I'd introduced a local variable for buf.usr_ptr and made the formatting nicer.

Sure. Will incorporate into v2.

Quoted 6 lines
Quoted 1 line

+struct mtd_data_oob_buf {

Let's add some reserved space for future improvements - I think it is always a good idea to do for ioctls:

+ __u8 padding[8]

OK. Will consider for v2.

Quoted 5 lines
Quoted 1 line

+#define MEMWRITEDATAOOB _IOWR('M', 24, struct mtd_data_oob_buf)

Would be greate to add a short comment about what this ioctl does. Of course you can optionally do this for all of them, but at least for the new one it does not hurt to have.

Sure, will do. I'll probably add at least a few comments in the header for all the relevant ioctls we've been discussing, but that'll wait until we've finished the code.

Quoted 7 lines

BTW, I'm considering splitting the usr_ptr into separate buffers for data and oob. This will probably be a little easier for the user interface as well as for internal kernel operations. Anyway, the resulting struct is looking more and more like the existing `struct mtd_oob_ops' (this is kind of by design); is it still a good idea to keep the user-facing struct independent of the internal mtd_oob_ops struct? I'm thinking it would leave some flexibility for the future.

BTW, being consistent and adding an "_user" or "_req" (request) or other suffix to all the ioctl request structures is a good idea. But this is again IMHO, and some people may consider this to be too pedantic. Anyway, for UBI ioctls all the structures end with "_req", you can see the example: include/mtd/ubi-user.h

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#26 lore raw
In reply to Artem Bityutskiy
Quoted 10 lines · Artem Bityutskiy
Quoted 9 lines · Ivan Djelic
Quoted 8 lines · Artem Bityutskiy

Hi,

Quoted 4 lines · Brian Norris

First off, these fixes are motivated by a number of things:

1) Broken "noecc" writes; `nandwrite -n -o' does not work on my hardware, at least, without these patches

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

I think Ivan complained about this at some point - he said he has images with crafted ECC codes to test his BCH library, but he could not flash them properly. So yes, this is worth fixing.

Actually it was Ricard Wanderlof, but indeed he was testing the BCH library
(http://lists.infradead.org/pipermail/linux-mtd/2011-March/034516.html).
It would be nice if he could ack the fix ?

(Hi guys, just got back, been away for a while over the summer).

This particular issue was fixed in a patch by Peter Wippich on June 6th:

http://lists.infradead.org/pipermail/linux-mtd/2011-June/036016.html

(I subsequently tested the patch and found it to fix the problem: http://lists.infradead.org/pipermail/linux-mtd/2011-June/036085.html).

I could test patch 1 of this set to see if it accomplishes the same thing, but initially to me it seems that the issue has already been fixed, albeit perhaps not pushed yet?

/Ricard

Signature · 3 lines
Ricard Wolf Wanderlöf                           ricardw(at)axis.com
Axis Communications AB, Lund, Sweden            www.axis.com
Phone +46 46 272 2016                           Fax +46 46 13 61 30
#27 lore raw
Quoted 7 lines · Ricard Wanderlof
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines · Brian Norris
Quoted 2 lines

1) Broken "noecc" writes; `nandwrite -n -o' does not work on my hardware, at least, without these patches

This particular issue was fixed in a patch by Peter Wippich on June 6th:

http://lists.infradead.org/pipermail/linux-mtd/2011-June/036016.html

Right, I actually found this patch already and have it lined up in my test kernel, but apparently my issue is separate.

I will try to describe my issue further on the thread for patch 1. Jason Liu has asked about the details there.

Quoted 3 lines

I could test patch 1 of this set to see if it accomplishes the same thing, but initially to me it seems that the issue has already been fixed, albeit perhaps not pushed yet?

I am now doubting that my fix will solve your problem like Peter Wippich's patch did. I believe the problem I am having is specifically related to my NAND controller's characteristics (how it handles ECC).

So for hardware on which Peter's fix solves your problems, I suppose I would be interested mostly in seeing if my patch *breaks* your build. FYI, I already see an issue for those who use both the builtin nand_write_page_raw()' and nand_write_buf()' functions. It can be fixed if you amend my patch with the following. I can resend later, but test these for breakage if you can!

drivers/mtd/nand/nand_base.c +2 -1
@@ -1898,7 +1898,8 @@ out:
 static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
                                const uint8_t *buf)
 {
-       chip->write_buf(mtd, buf, mtd->writesize);
+       if (buf)
+               chip->write_buf(mtd, buf, mtd->writesize);
        chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
 }
#16 lore raw

Re: [RFC 4/5] mtd: move mtd_oob_mode_t to shared kernel/user space

In reply to Artem Bityutskiy
Quoted 2 lines · Artem Bityutskiy

32/64 problems - what do you mean? enumeration = 'int' which is always 32 bits in all architectures Linux supports.

Oops, got things mixed up here (ints and pointers). Thanks for the correction. My worry was unfounded...

Quoted 3 lines

3. IMHO, and I even dare to say that many kernel gurus would agree, if we are talking about a simple enum with just few constants inside - anonymous enum is the best - the code is simpler and more readable.

...

Quoted 1 line

So, I'd be happy with 3, can live with 2, and dislike 1 very much :-)

3 sounds perfect, now that I've been straightened out.

Quoted 5 lines

I cannot refer to a section in a standard, but I am sure unitialized tags will start with 0 reliably. But yes, it is much less error-prone to initialize the tags explicitly, because it makes it a bit more difficult to make a stupid mistake by adding a new constant and change values of other constants.

It's also useful if we have to kill an old option at some point. I'll probably include initializers.

Quoted 5 lines
Quoted 1 line

Sorry if this is a lot of thinking out loud here :)

Yeah, least significant topics usually cause most discussions - I was told recently by a colleague that this is called "bike shedding". Notice how much I wrote comparing to more important posts of yours :-)

I actually learned/recalled a few things here; I had pieces of the puzzle but not the whole picture. And the "bike shed" is an important addition to my vocabulary.

Thanks,
Brian

#6 lore raw

Re: [RFC 1/5] mtd: support MTD_MODE_RAW for writing OOB

In reply to Jason Liu
Quoted 4 lines · Jason Liu

2011/8/18 Brian Norris <[email protected]>:

Quoted 1 line

This fixes issues with `nandwrite -n' and the MEMWRITEOOB[64] ioctls.

It's worthy here stating clear what the issue you meet and how it fix.

The issue is simply that nandwrite -n -o' does not work on my system; nandwrite -n' can write page data to my flash without applying ECC, but when I use the -o' option, ECC is applied (incorrectly), contrary to the --noecc' option.

I found that this is the case because my hardware computes and writes ECC data to flash upon either OOB write or page write. Thus, to support a proper "no ECC" write, my driver must know when we're performing a raw OOB write vs. a normal ECC OOB write. However, MTD does not pass any raw mode information to the write_oob functions. This patch addresses the problems by:
1) Passing MTD_OOB_RAW down to lower layers, instead of just defaulting to MTD_OOB_PLACE
2) Handling MTD_OOB_RAW within the NAND layer's `nand_do_write_oob'

Is this a better description? I'll include this description for v2.

Note that I have yet to update the appropriate `nand_write_page_raw' function to properly handle an OOB-only write. I've fixed my own driver, but there's an issue in the nand_base version. Will include in v2.

Thanks,
Brian

#28 lore raw
In reply to Brian Norris
Quoted 21 lines · Brian Norris
Quoted 7 lines · Ricard Wanderlof
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines · Brian Norris
Quoted 2 lines

1) Broken "noecc" writes; `nandwrite -n -o' does not work on my hardware, at least, without these patches

This particular issue was fixed in a patch by Peter Wippich on June 6th:

http://lists.infradead.org/pipermail/linux-mtd/2011-June/036016.html

Right, I actually found this patch already and have it lined up in my test kernel, but apparently my issue is separate.

Quoted 3 lines

I could test patch 1 of this set to see if it accomplishes the same thing, but initially to me it seems that the issue has already been fixed, albeit perhaps not pushed yet?

I am now doubting that my fix will solve your problem like Peter Wippich's patch did. I believe the problem I am having is specifically related to my NAND controller's characteristics (how it handles ECC).

Quoted 6 lines

So for hardware on which Peter's fix solves your problems, I suppose I would be interested mostly in seeing if my patch *breaks* your build. FYI, I already see an issue for those who use both the builtin nand_write_page_raw()' and nand_write_buf()' functions. It can be fixed if you amend my patch with the following. I can resend later, but test these for breakage if you can!

Ok. I tried applying patch #1 of your patch set, to a tree in which Peter's patch has already been applying, to see what would happen. I had a problem in that in the mtdchar.c I have it looks like this:

     memset(chip->oob_poi, 0xff, mtd->oobsize);
     nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
     status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
     memset(chip->oob_poi, 0xff, mtd->oobsize);

whereas your patch looks like it was made against a version which lacks the memsets. First I thought it was because I was running an older kernel (2.6.35), but I looked at HEAD of the linux-2.6 and mtd-2.6 trees at git.infradead.org, and it's the same there. So I'm not sure exactly which version your patch was made against. Perhaps it's obvious to someone but not me right now.

Nevertheless, after applying the patch, as you suspected, using 'nandwrite -o -n' fails, in this case the application hangs after outputting

Writing data to block 0

After applying the following patch from your email, 'nandwrite -o -n' initially appears to work, however upon closer inspection the oob's of all the written pages are all-ff, i.e. the oob's are never written.

drivers/mtd/nand/nand_base.c out: · line 1905

Quoted 12 lines
drivers/mtd/nand/nand_base.c +2 -1
@@ -1898,7 +1898,8 @@ out:
 static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
                               const uint8_t *buf)
 {
-       chip->write_buf(mtd, buf, mtd->writesize);
+       if (buf)
+               chip->write_buf(mtd, buf, mtd->writesize);
       chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
 }
 

Now, this may all be nonsense as as I mentioned I'm not sure that I've had the right version to start with. The memsets bug me, as they would explain the all-ff's stuff, but I don't really feel like just experimenting.

/Ricard

Signature · 3 lines
Ricard Wolf Wanderlöf                           ricardw(at)axis.com
Axis Communications AB, Lund, Sweden            www.axis.com
Phone +46 46 272 2016                           Fax +46 46 13 61 30
#29 lore raw

On Wed, Aug 24, 2011 at 8:36 AM, Ricard Wanderlof
<[email protected]> wrote:
...

Quoted 2 lines

I had a problem
in that in the mtdchar.c I have it looks like this:

Did you mean nand_base.c?

Quoted 6 lines

whereas your patch looks like it was made against a version which lacks the memsets. First I thought it was because I was running an older kernel (2.6.35), but I looked at HEAD of the linux-2.6 and mtd-2.6 trees at git.infradead.org, and it's the same there. So I'm not sure exactly which version your patch was made against. Perhaps it's obvious to someone but not me right now.

My patches were based on l2-mtd-2.6.git, actually. David Woodhouse rarely pulls patches into his mtd-2.6 tree, so I have moved to working with Artem's l2-mtd-2.6 tree, where all the MTD work that's waiting for upstream sits (some stuff's been there since May). This is not obvious, and usually when it matters, I try to mention it in the patch summaries.

Artem: is there any official change in policy on patch submission? I see documentation that says to base off mtd-2.6.git, but I've been using l2-mtd-2.6 to help you avoid merge conflicts:
http://www.linux-mtd.infradead.org/source.html

Anyway, I believe the relevant patch dependency (from l2-mtd-2.6.git) is:

commit a8ee364bbf14861d5d0af39c4da06c30441895fb
Author: THOMSON, Adam (Adam) <[email protected]>
mtd: nand_base: always initialise oob_poi before writing OOB data

Quoted 4 lines

Nevertheless, after applying the patch, as you suspected, using 'nandwrite -o -n' fails, in this case the application hangs after outputting

Writing data to block 0

I guessed you would be more likely to get a segmentation fault on a NULL pointer, but I may be wrong.

Quoted 3 lines

Now, this may all be nonsense as as I mentioned I'm not sure that I've had the right version to start with. The memsets bug me, as they would explain the all-ff's stuff, but I don't really feel like just experimenting.

Right, that's fair enough. I still think this patch is not 100% ready, anyway. Thanks for the help though!

If you're still up for trying, you can try applying Adam Thomson's patch, then my RFC, then the inlined amendment I sent. With your feedback, I will

I need to figure out structurally how to handle the differences between hardware that uses the standard functions (and works fine without my patch) and hardware like mine that needs more information passed to it. Perhaps there needs to be a replaceable `chip->ecc.write_oob_raw' function? Or at least some modifications to the other "replaceable" raw functions.

Brian

#30 lore raw
Quoted 15 lines · Brian Norris

On Wed, Aug 24, 2011 at 8:36 AM, Ricard Wanderlof
<[email protected]> wrote:
...

Quoted 2 lines

I had a problem
in that in the mtdchar.c I have it looks like this:

Did you mean nand_base.c?

Quoted 6 lines

whereas your patch looks like it was made against a version which lacks the memsets. First I thought it was because I was running an older kernel (2.6.35), but I looked at HEAD of the linux-2.6 and mtd-2.6 trees at git.infradead.org, and it's the same there. So I'm not sure exactly which version your patch was made against. Perhaps it's obvious to someone but not me right now.

Quoted 6 lines

My patches were based on l2-mtd-2.6.git, actually. David Woodhouse rarely pulls patches into his mtd-2.6 tree, so I have moved to working with Artem's l2-mtd-2.6 tree, where all the MTD work that's waiting for upstream sits (some stuff's been there since May). This is not obvious, and usually when it matters, I try to mention it in the patch summaries.

David's tree is desperately out-of-date now, I did not talk to him lately, he is not very reachable now.

There are patches from May because David did merge anything this merge window, probably he had some issues/etc, let's hope he'll merge everything next merge window. May be he wanted to ask me to merge it, but I have been having vacation and was not available at the IRC chat.

Quoted 4 lines

Artem: is there any official change in policy on patch submission? I see documentation that says to base off mtd-2.6.git, but I've been using l2-mtd-2.6 to help you avoid merge conflicts:
http://www.linux-mtd.infradead.org/source.html

There is no official policy, this all works because enthusiasts who just like MTD stuff and keep it alive. When I noticed that dwmw2 does not give MTD ML enough attention, I just started my l2 tree to help him - it was faster/easier for him to look with reviewed patches in my tree rather than look through whole MTD ML, find out which acks/reviewed-by to add and where, which patch versions are out of date, etc.

At this point I think, that you have to use the l2 tree, because David's tree is very out-of-date. Also, beware that the l2 tree is currently in linux-next.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#31 lore raw
In reply to Brian Norris
Quoted 7 lines · Brian Norris

On Wed, Aug 24, 2011 at 8:36 AM, Ricard Wanderlof
<[email protected]> wrote:
...

Quoted 2 lines

I had a problem
in that in the mtdchar.c I have it looks like this:

Did you mean nand_base.c?

Yes, sorry about that.

Quoted 9 lines
Quoted 3 lines

whereas your patch looks like it was made against a version which lacks the memsets. First I thought it was because I was running an older kernel
...

My patches were based on l2-mtd-2.6.git, actually. David Woodhouse rarely pulls patches into his mtd-2.6 tree, so I have moved to working with Artem's l2-mtd-2.6 tree, where all the MTD work that's waiting for upstream sits (some stuff's been there since May). This is not obvious, and usually when it matters, I try to mention it in the patch summaries.

I guess I should have thought of it but never thought to look there.

Quoted 5 lines

Anyway, I believe the relevant patch dependency (from l2-mtd-2.6.git) is:

commit a8ee364bbf14861d5d0af39c4da06c30441895fb
Author: THOMSON, Adam (Adam) <[email protected]>
mtd: nand_base: always initialise oob_poi before writing OOB data

That seems right.

Quoted 7 lines
Quoted 4 lines

Nevertheless, after applying the patch, as you suspected, using 'nandwrite -o -n' fails, in this case the application hangs after outputting

Writing data to block 0

I guessed you would be more likely to get a segmentation fault on a NULL pointer, but I may be wrong.

Something seems to hang at a lower level, because the system becomes unresponsive at this point (I can telnet in, but trying to access the flash with ls for instance causes the shell to hang).

Quoted 3 lines

If you're still up for trying, you can try applying Adam Thomson's patch, then my RFC, then the inlined amendment I sent. With your feedback, I will

(something seems missing here ... nevertheless)

I applied the patches as you mentioned, which brought success. Dumping a partition with nanddump, then writing it back with nandwrite -o -n results in the correct data being written both to the main and oob areas.

(Without your inlinend patch, the nandwrite application still hangs after Writing to data block 0).

So the conclusion would be that this combination of patches does not break Peter Wippich's patch.

/Ricard

Signature · 3 lines
Ricard Wolf Wanderlöf                           ricardw(at)axis.com
Axis Communications AB, Lund, Sweden            www.axis.com
Phone +46 46 272 2016                           Fax +46 46 13 61 30
#32 lore raw

Hi Ricard,

Quoted 9 lines · Ricard Wanderlof
Quoted 6 lines · Brian Norris

On Wed, Aug 24, 2011 at 8:36 AM, Ricard Wanderlof <[email protected]> wrote:
Anyway, I believe the relevant patch dependency (from l2-mtd-2.6.git) is:

 commit a8ee364bbf14861d5d0af39c4da06c30441895fb
 Author: THOMSON, Adam (Adam) <[email protected]>
 mtd: nand_base: always initialise oob_poi before writing OOB data

That seems right.

BTW, that patch seems a bit broken to me; I sent a fixup for it:
http://lists.infradead.org/pipermail/linux-mtd/2011-August/037698.html

Quoted 6 lines
Quoted 2 lines

I guessed you would be more likely to get a segmentation fault on a NULL pointer, but I may be wrong.

Something seems to hang at a lower level, because the system becomes unresponsive at this point (I can telnet in, but trying to access the flash with ls for instance causes the shell to hang).

I think that I had a little bit of the wrong approach. I was doing some ill advised hacking to the existing write functions. I spun off my first two patches here as a different series (I believe I CC'd you); I used a different approach that should make as little impact on currently working hardware as possible:
http://lists.infradead.org/pipermail/linux-mtd/2011-August/037695.html

Quoted 1 line

(something seems missing here ...

Acknowledged :)

Quoted 3 lines

I applied the patches as you mentioned, which brought success. Dumping a partition with nanddump, then writing it back with nandwrite -o -n results in the correct data being written both to the main and oob areas.

Great!

Quoted 2 lines

(Without your inlinend patch, the nandwrite application still hangs after Writing to data block 0).

Not quite sure what the hanging is all about - may be related to my improper use of CMD_SEQIN, then a nand wait function being called later? - but that's why I've changed my approach.

Quoted 2 lines

So the conclusion would be that this combination of patches does not break Peter Wippich's patch.

Thanks a lot for the testing. I think that my first approach still may easily have unintended consequences, though. I welcome any testing on my new patch series, and any more systems with broken "noecc" should be handled through that thread.

Brian

#33 lore raw
Quoted 6 lines · Brian Norris
Quoted 4 lines
Quoted 2 lines

 Author: THOMSON, Adam (Adam) <[email protected]>
 mtd: nand_base: always initialise oob_poi before writing OOB data

That seems right.

BTW, that patch seems a bit broken to me; I sent a fixup for it: http://lists.infradead.org/pipermail/linux-mtd/2011-August/037698.html

Thanks, I did see it but it failed to register. I've applied it to my tree now.

Quoted 8 lines

I think that I had a little bit of the wrong approach. I was doing some ill advised hacking to the existing write functions. I spun off my first two patches here as a different series (I believe I CC'd you); I used a different approach that should make as little impact on currently working hardware as possible:
http://lists.infradead.org/pipermail/linux-mtd/2011-August/037695.html

Thanks a lot for the testing. I think that my first approach still may easily have unintended consequences, though. I welcome any testing on my new patch series, and any more systems with broken "noecc" should be handled through that thread.

I applied the two patches in the above mentioned patch series to my tree (after applying Peter's patch, Adam's, and your amendment to it). Conclusion: nandwrite -o -n still works for me, i.e. the patch doesn't break it.

One little caveat, the tree I'm using is 2.6.35 based. The patches don't apply cleanly, but it just takes a little effort. So I think the results are relevant, just wanted to mention it.

/Ricard

Signature · 3 lines
Ricard Wolf Wanderlöf                           ricardw(at)axis.com
Axis Communications AB, Lund, Sweden            www.axis.com
Phone +46 46 272 2016                           Fax +46 46 13 61 30