Skip to contentSkip to conversation

Fix writing mtdoops to nand flash.

PATCH Superseded [email protected] 2 participants 7 messages Oct 30, 2017 – Nov 2, 2017

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

PATCH 1/1 Fix writing mtdoops to nand flash.

From: Brent Taylor <[email protected]>

When mtdoops calls mtd_panic_write, it eventually calls panic_nand_write in nand_base.c. In order to properly wait for the nand chip to be ready in panic_nand_wait, the chip must first be selected.

When using the atmel nand flash controller, a panic would occur due to a NULL pointer exception.

---
 drivers/mtd/nand/nand_base.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/nand/nand_base.c +5 -0
index 12edaae17d81..0a8058a66d93 100644
@@ -2802,9 +2802,14 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 	struct mtd_oob_ops ops;
 	int ret;
 
+	int chipnr = (int)(to >> chip->chip_shift);
+	chip->select_chip(mtd, chipnr);
+
 	/* Wait for the device to get ready */
 	panic_nand_wait(mtd, chip, 400);
 
+	chip->select_chip(mtd, -1);
+
 	/* Grab the device */
 	panic_nand_get_device(chip, mtd, FL_WRITING);
 
Signature · 1 line
2.14.2
#1 lore raw

Hi Brent,

Subject should be prefixed by "mtd: nand: ", so

"mtd: nand: Fix writing mtdoops to nand flash"

Quoted 21 lines · On Sun, 29 Oct 2017 23:23:43 -0500 [email protected] wrote:

From: Brent Taylor <[email protected]>

When mtdoops calls mtd_panic_write, it eventually calls panic_nand_write in nand_base.c. In order to properly wait for the nand chip to be ready in panic_nand_wait, the chip must first be selected.

When using the atmel nand flash controller, a panic would occur due to a NULL pointer exception.

---
 drivers/mtd/nand/nand_base.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/nand/nand_base.c +0 -0
index 12edaae17d81..0a8058a66d93 100644
@@ -2802,9 +2802,14 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 	struct mtd_oob_ops ops;

drivers/mtd/nand/nand_base.c static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, · line 2812

Quoted 10 lines
drivers/mtd/nand/nand_base.c +5 -0
 	int ret;
 
+	int chipnr = (int)(to >> chip->chip_shift);
+	chip->select_chip(mtd, chipnr);
+
 	/* Wait for the device to get ready */
 	panic_nand_wait(mtd, chip, 400);
 
+	chip->select_chip(mtd, -1);
+

Duh! Looks like a piece of code that is never tested. Did you face the problem or did you find out by inspecting the code?

Anyway, I fear the atmel driver is not the only one to rely on the chip to be selected when ->dev_ready() is called so this should definitely be fixed. Also, we should probably move the ->select_chip() and panic_nand_wait() calls after panic_nand_get_device(), and I don't think we need to unselect the chip (it will be taken care of by nand_do_write_ops()).

Quoted 3 lines
drivers/mtd/nand/nand_base.c +0 -0
 	/* Grab the device */
 	panic_nand_get_device(chip, mtd, FL_WRITING);
 
#2 lore raw

Hi Bors, thanks for your quick reply.

Quoted 5 lines · Boris Brezillon

Hi Brent,

Subject should be prefixed by "mtd: nand: ", so

"mtd: nand: Fix writing mtdoops to nand flash"

Oops, yes, will fix.

Quoted 33 lines
Quoted 29 lines · On Sun, 29 Oct 2017 23:23:43 -0500 [email protected] wrote:

From: Brent Taylor <[email protected]>

When mtdoops calls mtd_panic_write, it eventually calls panic_nand_write in nand_base.c. In order to properly wait for the nand chip to be ready in panic_nand_wait, the chip must first be selected.

When using the atmel nand flash controller, a panic would occur due to a NULL pointer exception.

---
 drivers/mtd/nand/nand_base.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/nand/nand_base.c +3 -0
index 12edaae17d81..0a8058a66d93 100644
@@ -2802,9 +2802,14 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
      struct mtd_oob_ops ops;
      int ret;
 
+     int chipnr = (int)(to >> chip->chip_shift);
+     chip->select_chip(mtd, chipnr);
+
      /* Wait for the device to get ready */
      panic_nand_wait(mtd, chip, 400);
 
Quoted 5 lines

drivers/mtd/nand/nand_base.c static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, · line 2812

Quoted 2 lines
drivers/mtd/nand/nand_base.c +2 -0
+     chip->select_chip(mtd, -1);
+

Duh! Looks like a piece of code that is never tested. Did you face the problem or did you find out by inspecting the code?

I was playing with another driver on an atmel development board (at91sam9m10g45ek) and caused a panic with mtdoops enabled. While writing the mtdoops to nand, another panic occurred which caused a storm of panics to generated.

Quoted 12 lines

Anyway, I fear the atmel driver is not the only one to rely on the chip to be selected when ->dev_ready() is called so this should definitely be fixed. Also, we should probably move the ->select_chip() and panic_nand_wait() calls after panic_nand_get_device(), and I don't think we need to unselect the chip (it will be taken care of by nand_do_write_ops()).

Quoted 3 lines
drivers/mtd/nand/nand_base.c +0 -0
      /* Grab the device */
      panic_nand_get_device(chip, mtd, FL_WRITING);
 

After looking at this closer, a panic could happen at any point correct? If that's the case, the kernel could have been in the middle of a nand read/write operation (which may or may not be on the same chip). Would the chip the mtdoops is being written to need to be reset? I haven't drilled down into the nand_reset function yet, but can that be called in a "panic" state?

#3 lore raw
Quoted 64 lines · On Mon, 30 Oct 2017 07:46:18 -0500 Brent Taylor <[email protected]> wrote:

Hi Bors, thanks for your quick reply.

Quoted 5 lines · Boris Brezillon

Hi Brent,

Subject should be prefixed by "mtd: nand: ", so

"mtd: nand: Fix writing mtdoops to nand flash"

Oops, yes, will fix.

Quoted 33 lines
Quoted 29 lines · On Sun, 29 Oct 2017 23:23:43 -0500 [email protected] wrote:

From: Brent Taylor <[email protected]>

When mtdoops calls mtd_panic_write, it eventually calls panic_nand_write in nand_base.c. In order to properly wait for the nand chip to be ready in panic_nand_wait, the chip must first be selected.

When using the atmel nand flash controller, a panic would occur due to a NULL pointer exception.

---
 drivers/mtd/nand/nand_base.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/nand/nand_base.c +3 -0
index 12edaae17d81..0a8058a66d93 100644
@@ -2802,9 +2802,14 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
      struct mtd_oob_ops ops;
      int ret;
 
+     int chipnr = (int)(to >> chip->chip_shift);
+     chip->select_chip(mtd, chipnr);
+
      /* Wait for the device to get ready */
      panic_nand_wait(mtd, chip, 400);
 
Quoted 5 lines

drivers/mtd/nand/nand_base.c static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, · line 2812

Quoted 2 lines
drivers/mtd/nand/nand_base.c +2 -0
+     chip->select_chip(mtd, -1);
+  

Duh! Looks like a piece of code that is never tested. Did you face the problem or did you find out by inspecting the code?

I was playing with another driver on an atmel development board (at91sam9m10g45ek) and caused a panic with mtdoops enabled. While writing the mtdoops to nand, another panic occurred which caused a storm of panics to generated.

Quoted 8 lines

Anyway, I fear the atmel driver is not the only one to rely on the chip to be selected when ->dev_ready() is called so this should definitely be fixed. Also, we should probably move the ->select_chip() and panic_nand_wait() calls after panic_nand_get_device(), and I don't think we need to unselect the chip (it will be taken care of by nand_do_write_ops()).

Quoted 6 lines
Quoted 4 lines

drivers/mtd/nand/nand_base.c static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, · line 2815

Quoted 3 lines
drivers/mtd/nand/nand_base.c +0 -0
      /* Grab the device */
      panic_nand_get_device(chip, mtd, FL_WRITING);
 

After looking at this closer, a panic could happen at any point correct?

Yes.

Quoted 4 lines

If
that's the case, the kernel could have been in the middle of a nand read/write operation (which may or may not be on the same chip). Would the chip the mtdoops is being written to need to be reset?

I'd prefer not. Actually, the code calls panic_nand_wait() to let the NAND finish the operation it's currently doing, and most of the time it will relax the busy pin within 400ms (the timeout passed to panic_nand_wait()). If we do a RESET in the middle of a write operation we'll just corrupt the data that is being written, and I'm not sure we want that to happen.

Quoted 2 lines

I haven't drilled down into the
nand_reset function yet, but can that be called in a "panic" state?

Maybe, but I'm not sure we want to do that (see above).

Anyway, the write-on-panic path is fragile, and I'm not sure all controllers will play well in this situation: just because the NAND is ready does not mean the controller is, and there's nothing ensuring that in the NAND framework.

Regards,

Boris

#4 lore raw

PATCH v2 1/1 mtd: nand: Fix writing mtdoops to nand flash.

In reply to [email protected]

From: Brent Taylor <[email protected]>

When mtdoops calls mtd_panic_write, it eventually calls panic_nand_write in nand_base.c. In order to properly wait for the nand chip to be ready in panic_nand_wait, the chip must first be selected.

When using the atmel nand flash controller, a panic would occur due to a NULL pointer exception.

---
Changes in v2:
- drop deselecting the chip
- move select_chip and panic_nand_wait after panic_nand_get_device

 drivers/mtd/nand/nand_base.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/mtd/nand/nand_base.c +6 -2
index 12edaae17d81..2093667e2bcb 100644
@@ -2802,12 +2802,16 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 	struct mtd_oob_ops ops;
 	int ret;
 
-	/* Wait for the device to get ready */
-	panic_nand_wait(mtd, chip, 400);
+	int chipnr = (int)(to >> chip->chip_shift);
 
 	/* Grab the device */
 	panic_nand_get_device(chip, mtd, FL_WRITING);
 
+	chip->select_chip(mtd, chipnr);
+
+	/* Wait for the device to get ready */
+	panic_nand_wait(mtd, chip, 400);
+
 	memset(&ops, 0, sizeof(ops));
 	ops.len = len;
 	ops.datbuf = (uint8_t *)buf;
Signature · 1 line
2.14.2
#5 lore raw

Re: [PATCH v2] mtd: nand: Fix writing mtdoops to nand flash.

Quoted 11 lines · On Mon, 30 Oct 2017 22:32:45 -0500 [email protected] wrote:

From: Brent Taylor <[email protected]>

When mtdoops calls mtd_panic_write, it eventually calls panic_nand_write in nand_base.c. In order to properly wait for the nand chip to be ready in panic_nand_wait, the chip must first be selected.

When using the atmel nand flash controller, a panic would occur due to a NULL pointer exception.

I'll add

Fixes 2af7c6539931 ("mtd: Add panic_write for NAND flashes")

when applying the patch.

Thanks,

Boris

Quoted 31 lines

---
Changes in v2:
- drop deselecting the chip
- move select_chip and panic_nand_wait after panic_nand_get_device

 drivers/mtd/nand/nand_base.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/mtd/nand/nand_base.c +6 -2
index 12edaae17d81..2093667e2bcb 100644
@@ -2802,12 +2802,16 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 	struct mtd_oob_ops ops;
 	int ret;
 
-	/* Wait for the device to get ready */
-	panic_nand_wait(mtd, chip, 400);
+	int chipnr = (int)(to >> chip->chip_shift);
 
 	/* Grab the device */
 	panic_nand_get_device(chip, mtd, FL_WRITING);
 
+	chip->select_chip(mtd, chipnr);
+
+	/* Wait for the device to get ready */
+	panic_nand_wait(mtd, chip, 400);
+
 	memset(&ops, 0, sizeof(ops));
 	ops.len = len;
 	ops.datbuf = (uint8_t *)buf;
#6 lore raw

Re: [PATCH v2] mtd: nand: Fix writing mtdoops to nand flash.

In reply to [email protected]
Quoted 10 lines · On Mon, 30 Oct 2017 22:32:45 -0500 [email protected] wrote:

From: Brent Taylor <[email protected]>

When mtdoops calls mtd_panic_write, it eventually calls panic_nand_write in nand_base.c. In order to properly wait for the nand chip to be ready in panic_nand_wait, the chip must first be selected.

When using the atmel nand flash controller, a panic would occur due to a NULL pointer exception.

Applied.

Thanks,

Boris

Quoted 32 lines

---
Changes in v2:
- drop deselecting the chip
- move select_chip and panic_nand_wait after panic_nand_get_device

 drivers/mtd/nand/nand_base.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/mtd/nand/nand_base.c +6 -2
index 12edaae17d81..2093667e2bcb 100644
@@ -2802,12 +2802,16 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 	struct mtd_oob_ops ops;
 	int ret;
 
-	/* Wait for the device to get ready */
-	panic_nand_wait(mtd, chip, 400);
+	int chipnr = (int)(to >> chip->chip_shift);
 
 	/* Grab the device */
 	panic_nand_get_device(chip, mtd, FL_WRITING);
 
+	chip->select_chip(mtd, chipnr);
+
+	/* Wait for the device to get ready */
+	panic_nand_wait(mtd, chip, 400);
+
 	memset(&ops, 0, sizeof(ops));
 	ops.len = len;
 	ops.datbuf = (uint8_t *)buf;