Skip to contentSkip to conversation

mtd: gpmi: add NAND write verify support

PATCH New Huang Shijie 3 participants 6 messages Aug 11, 2012 – Aug 13, 2012

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

PATCH v4 1/1 mtd: gpmi: add NAND write verify support

Add NAND write verify support in gpmi-nand driver.

This patch fixes a kernel crash when we select CONFIG_MTD_NAND_VERIFY_WRITE=y, and do some write tests:

root at freescale /$ dd if=/dev/zero of=/dev/mtd0 bs=128k count=1

[   41.070000] Unable to handle kernel NULL pointer dereference at virtual addr0
[   41.080000] pgd = c77ac000
[   41.080000] [00000000] *pgd=4773c831, *pte=00000000, *ppte=00000000
[   41.090000] Internal error: Oops: 17 [#1] ARM
[   41.090000] Modules linked in:
[   41.090000] CPU: 0    Not tainted (3.6.0-rc1-next-20120809-00002-ga25d017-d)
[   41.090000] PC is at nand_verify_buf+0x18/0x5c
[   41.090000] LR is at nand_write_page+0xc0/0x140
[   41.090000] pc : [<c023e7d4>]    lr : [<c023fcfc>]    psr: 20000013
[   41.090000] sp : c7779de4  ip : 00000000  fp : c6e60000
[   41.090000] r10: 00000000  r9 : 00020000  r8 : 00000000
[   41.090000] r7 : 00000000  r6 : c6e60000  r5 : c762cb10  r4 : 00000000
[   41.090000] r3 : c762c8a8  r2 : 00000800  r1 : c6e60000  r0 : 00000000
[   41.090000] Flags: nzCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment user
[   41.090000] Control: 0005317f  Table: 477ac000  DAC: 00000015
[   41.090000] Process dd (pid: 427, stack limit = 0xc7778270)
[   41.090000] Stack: (0xc7779de4 to 0xc777a000)
	...

The crash log is fetched from Fabio's patch. thanks to Fabio's hard work.

---
v3 --> v4:
[1] add credit to Fabio.

v2 --> v3:
[1] free the verify_buf when we exit.

v1 --> v2:

    [1] use the mtd->writesize to allocate the verify buffer.
    [2] add new field `verify_buf` in gpmi_nand_data{}

    tested this patch on imx6q-arm2 board.
---
 drivers/mtd/nand/gpmi-nand/gpmi-nand.c |   25 +++++++++++++++++++++++++
 drivers/mtd/nand/gpmi-nand/gpmi-nand.h |    1 +
 2 files changed, 26 insertions(+), 0 deletions(-)
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +25 -0
index 8c0d2f0..e1e4c51 100644
@@ -1512,12 +1512,34 @@ static int gpmi_pre_bbt_scan(struct gpmi_nand_data  *this)
 	return nand_boot_init(this);
 }
 
+static int gpmi_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
+{
+	struct nand_chip *chip = mtd->priv;
+	struct gpmi_nand_data *this = chip->priv;
+	int ret;
+
+	ret = chip->ecc.read_page(mtd, chip, this->verify_buf, 0, 0);
+	if (ret)
+		return -EFAULT;
+	if (memcmp(buf, this->verify_buf, len))
+		return -EFAULT;
+	return 0;
+}
+
 static int gpmi_scan_bbt(struct mtd_info *mtd)
 {
 	struct nand_chip *chip = mtd->priv;
 	struct gpmi_nand_data *this = chip->priv;
 	int ret;
 
+	/*
+	 * We have gotten the right page size now, use it to allocate
+	 * the proper verify buffer.
+	 */
+	this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
+	if (!this->verify_buf)
+		return -ENOMEM;
+
 	/* Prepare for the BBT scan. */
 	ret = gpmi_pre_bbt_scan(this);
 	if (ret)
@@ -1531,6 +1553,8 @@ void gpmi_nfc_exit(struct gpmi_nand_data *this)
 {
 	nand_release(&this->mtd);
 	gpmi_free_dma_buffer(this);
+	kfree(this->verify_buf);
+	this->verify_buf = NULL;
 }
 
 static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
@@ -1556,6 +1580,7 @@ static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
 	chip->read_byte		= gpmi_read_byte;
 	chip->read_buf		= gpmi_read_buf;
 	chip->write_buf		= gpmi_write_buf;
+	chip->verify_buf	= gpmi_verify_buf;
 	chip->ecc.read_page	= gpmi_ecc_read_page;
 	chip->ecc.write_page	= gpmi_ecc_write_page;
 	chip->ecc.read_oob	= gpmi_ecc_read_oob;
drivers/mtd/nand/gpmi-nand/gpmi-nand.h +1 -0
index 1547a60..8ddf115 100644
@@ -144,6 +144,7 @@ struct gpmi_nand_data {
 	/* MTD / NAND */
 	struct nand_chip	nand;
 	struct mtd_info		mtd;
+	uint8_t			*verify_buf;
 
 	/* General-use Variables */
 	int			current_chip;
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH v4 1/1 mtd: gpmi: add NAND write verify support

Dear Huang Shijie,

Quoted 26 lines

Add NAND write verify support in gpmi-nand driver.

This patch fixes a kernel crash when we select CONFIG_MTD_NAND_VERIFY_WRITE=y, and do some write tests:

root at freescale /$ dd if=/dev/zero of=/dev/mtd0 bs=128k count=1

[   41.070000] Unable to handle kernel NULL pointer dereference at virtual
addr0 [   41.080000] pgd = c77ac000
[   41.080000] [00000000] *pgd=4773c831, *pte=00000000, *ppte=00000000
[   41.090000] Internal error: Oops: 17 [#1] ARM
[   41.090000] Modules linked in:
[   41.090000] CPU: 0    Not tainted
(3.6.0-rc1-next-20120809-00002-ga25d017-d) [   41.090000] PC is at
nand_verify_buf+0x18/0x5c
[   41.090000] LR is at nand_write_page+0xc0/0x140
[   41.090000] pc : [<c023e7d4>]    lr : [<c023fcfc>]    psr: 20000013
[   41.090000] sp : c7779de4  ip : 00000000  fp : c6e60000
[   41.090000] r10: 00000000  r9 : 00020000  r8 : 00000000
[   41.090000] r7 : 00000000  r6 : c6e60000  r5 : c762cb10  r4 : 00000000
[   41.090000] r3 : c762c8a8  r2 : 00000800  r1 : c6e60000  r0 : 00000000
[   41.090000] Flags: nzCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment
user [   41.090000] Control: 0005317f  Table: 477ac000  DAC: 00000015
[   41.090000] Process dd (pid: 427, stack limit = 0xc7778270)
[   41.090000] Stack: (0xc7779de4 to 0xc777a000)
	...
Quoted 1 line

The crash log is fetched from Fabio's patch. thanks to Fabio's hard

work.

Quoted 44 lines

---
v3 --> v4:
[1] add credit to Fabio.

v2 --> v3:
[1] free the verify_buf when we exit.

v1 --> v2:

    [1] use the mtd->writesize to allocate the verify buffer.
    [2] add new field `verify_buf` in gpmi_nand_data{}

    tested this patch on imx6q-arm2 board.
---
 drivers/mtd/nand/gpmi-nand/gpmi-nand.c |   25 +++++++++++++++++++++++++
 drivers/mtd/nand/gpmi-nand/gpmi-nand.h |    1 +
 2 files changed, 26 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/mtd/nand/gpmi-nand/gpmi-nand.c index 8c0d2f0..e1e4c51 100644

drivers/mtd/nand/gpmi-nand/gpmi-nand.c +13 -0
@@ -1512,12 +1512,34 @@ static int gpmi_pre_bbt_scan(struct gpmi_nand_data 
 *this) return nand_boot_init(this);
 }
 
+static int gpmi_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int
 len) +{
+	struct nand_chip *chip = mtd->priv;
+	struct gpmi_nand_data *this = chip->priv;
+	int ret;
+
+	ret = chip->ecc.read_page(mtd, chip, this->verify_buf, 0, 0);
+	if (ret)
+		return -EFAULT;
+	if (memcmp(buf, this->verify_buf, len))
+		return -EFAULT;
+	return 0;
+}
+
 static int gpmi_scan_bbt(struct mtd_info *mtd)

drivers/mtd/nand/gpmi-nand/gpmi-nand.c static int gpmi_pre_bbt_scan(struct gpmi_nand_data · line 1539

Quoted 10 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +5 -0
 {
 	struct nand_chip *chip = mtd->priv;
 	struct gpmi_nand_data *this = chip->priv;
 	int ret;
 
+	/*
+	 * We have gotten the right page size now, use it to allocate
+	 * the proper verify buffer.
+	 */
+	this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);

devm_

drivers/mtd/nand/gpmi-nand/gpmi-nand.c void gpmi_nfc_exit(struct gpmi_nand_data *this) · line 1557

Quoted 12 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +5 -0
+	if (!this->verify_buf)
+		return -ENOMEM;
+
 	/* Prepare for the BBT scan. */
 	ret = gpmi_pre_bbt_scan(this);
 	if (ret)
@@ -1531,6 +1553,8 @@ void gpmi_nfc_exit(struct gpmi_nand_data *this)
 {
 	nand_release(&this->mtd);
 	gpmi_free_dma_buffer(this);
+	kfree(this->verify_buf);
+	this->verify_buf = NULL;

Then you won't have to care for this ... I told you at least once before.

Quoted 23 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +1 -0
 }
 
 static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
@@ -1556,6 +1580,7 @@ static int __devinit gpmi_nfc_init(struct
 gpmi_nand_data *this) chip->read_byte		= gpmi_read_byte;
 	chip->read_buf		= gpmi_read_buf;
 	chip->write_buf		= gpmi_write_buf;
+	chip->verify_buf	= gpmi_verify_buf;
 	chip->ecc.read_page	= gpmi_ecc_read_page;
 	chip->ecc.write_page	= gpmi_ecc_write_page;
 	chip->ecc.read_oob	= gpmi_ecc_read_oob;
(quoted diff) +0 -0

b/drivers/mtd/nand/gpmi-nand/gpmi-nand.h index 1547a60..8ddf115 100644

drivers/mtd/nand/gpmi-nand/gpmi-nand.h +1 -0
@@ -144,6 +144,7 @@ struct gpmi_nand_data {
 	/* MTD / NAND */
 	struct nand_chip	nand;
 	struct mtd_info		mtd;
+	uint8_t			*verify_buf;
 
 	/* General-use Variables */
 	int			current_chip;
#2 lore raw

PATCH v4 1/1 mtd: gpmi: add NAND write verify support

Quoted 79 lines · Marek Vasut

Dear Huang Shijie,

Quoted 26 lines

Add NAND write verify support in gpmi-nand driver.

This patch fixes a kernel crash when we select CONFIG_MTD_NAND_VERIFY_WRITE=y, and do some write tests:

root at freescale /$ dd if=/dev/zero of=/dev/mtd0 bs=128k count=1

[   41.070000] Unable to handle kernel NULL pointer dereference at virtual
addr0 [   41.080000] pgd = c77ac000
[   41.080000] [00000000] *pgd=4773c831, *pte=00000000, *ppte=00000000
[   41.090000] Internal error: Oops: 17 [#1] ARM
[   41.090000] Modules linked in:
[   41.090000] CPU: 0    Not tainted
(3.6.0-rc1-next-20120809-00002-ga25d017-d) [   41.090000] PC is at
nand_verify_buf+0x18/0x5c
[   41.090000] LR is at nand_write_page+0xc0/0x140
[   41.090000] pc : [<c023e7d4>]    lr : [<c023fcfc>]    psr: 20000013
[   41.090000] sp : c7779de4  ip : 00000000  fp : c6e60000
[   41.090000] r10: 00000000  r9 : 00020000  r8 : 00000000
[   41.090000] r7 : 00000000  r6 : c6e60000  r5 : c762cb10  r4 : 00000000
[   41.090000] r3 : c762c8a8  r2 : 00000800  r1 : c6e60000  r0 : 00000000
[   41.090000] Flags: nzCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment
user [   41.090000] Control: 0005317f  Table: 477ac000  DAC: 00000015
[   41.090000] Process dd (pid: 427, stack limit = 0xc7778270)
[   41.090000] Stack: (0xc7779de4 to 0xc777a000)
      ...
Quoted 1 line

The crash log is fetched from Fabio's patch. thanks to Fabio's hard

work.

Quoted 49 lines

---
v3 --> v4:
[1] add credit to Fabio.

v2 --> v3:
[1] free the verify_buf when we exit.

v1 --> v2:

    [1] use the mtd->writesize to allocate the verify buffer.
    [2] add new field `verify_buf` in gpmi_nand_data{}

    tested this patch on imx6q-arm2 board.
---
 drivers/mtd/nand/gpmi-nand/gpmi-nand.c |   25 +++++++++++++++++++++++++
 drivers/mtd/nand/gpmi-nand/gpmi-nand.h |    1 +
 2 files changed, 26 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/mtd/nand/gpmi-nand/gpmi-nand.c index 8c0d2f0..e1e4c51 100644

drivers/mtd/nand/gpmi-nand/gpmi-nand.c +13 -0
@@ -1512,12 +1512,34 @@ static int gpmi_pre_bbt_scan(struct gpmi_nand_data
 *this) return nand_boot_init(this);
 }
 
+static int gpmi_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int
 len) +{
+     struct nand_chip *chip = mtd->priv;
+     struct gpmi_nand_data *this = chip->priv;
+     int ret;
+
+     ret = chip->ecc.read_page(mtd, chip, this->verify_buf, 0, 0);
+     if (ret)
+             return -EFAULT;
+     if (memcmp(buf, this->verify_buf, len))
+             return -EFAULT;
+     return 0;
+}
+
 static int gpmi_scan_bbt(struct mtd_info *mtd)
 {
      struct nand_chip *chip = mtd->priv;
      struct gpmi_nand_data *this = chip->priv;
      int ret;
 
Quoted 7 lines

drivers/mtd/nand/gpmi-nand/gpmi-nand.c static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this) · line 1539

Quoted 5 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +5 -0
+     /*
+      * We have gotten the right page size now, use it to allocate
+      * the proper verify buffer.
+      */
+     this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);

devm_

emm...
The devm_kzalloc() is the wrong choose in this case. It may makes we waste much more memory when the NAND page is 8K.

Assume the gpmi uses a 8K page nand chip. The devm_kzalloc() will allocate (sizeof(struct devres) + 8K).
It's obvious that the size is bigger then the SLUB_MAX_SIZE. So the kernel gets 2 with the
get_order(sizeof(struct devres) + 8K) which means we have to use 4 memory page for the verify_buf.
But in actually, we only use 8K memory. So the rest nearly 8K( sizeof(struct devres) is very small) memory
is wasted.

So I prefer to use the kzalloc() here.

thanks
Huang Shijie

Quoted 39 lines

drivers/mtd/nand/gpmi-nand/gpmi-nand.c void gpmi_nfc_exit(struct gpmi_nand_data *this) · line 1557

Quoted 13 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +5 -0
+     if (!this->verify_buf)
+             return -ENOMEM;
+
      /* Prepare for the BBT scan. */
      ret = gpmi_pre_bbt_scan(this);
      if (ret)
@@ -1531,6 +1553,8 @@ void gpmi_nfc_exit(struct gpmi_nand_data *this)
 {
      nand_release(&this->mtd);
      gpmi_free_dma_buffer(this);
+     kfree(this->verify_buf);
+     this->verify_buf = NULL;

Then you won't have to care for this ... I told you at least once before.

Quoted 23 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +1 -0
 }
 
 static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
@@ -1556,6 +1580,7 @@ static int __devinit gpmi_nfc_init(struct
 gpmi_nand_data *this) chip->read_byte         = gpmi_read_byte;
      chip->read_buf          = gpmi_read_buf;
      chip->write_buf         = gpmi_write_buf;
+     chip->verify_buf        = gpmi_verify_buf;
      chip->ecc.read_page     = gpmi_ecc_read_page;
      chip->ecc.write_page    = gpmi_ecc_write_page;
      chip->ecc.read_oob      = gpmi_ecc_read_oob;
(quoted diff) +0 -0

b/drivers/mtd/nand/gpmi-nand/gpmi-nand.h index 1547a60..8ddf115 100644

drivers/mtd/nand/gpmi-nand/gpmi-nand.h +1 -0
@@ -144,6 +144,7 @@ struct gpmi_nand_data {
      /* MTD / NAND */
      struct nand_chip        nand;
      struct mtd_info         mtd;
+     uint8_t                 *verify_buf;
 
      /* General-use Variables */
      int                     current_chip;
#3 lore raw

PATCH v4 1/1 mtd: gpmi: add NAND write verify support

Dear Huang Shijie,

[...]

Quoted 10 lines
Quoted 3 lines
Quoted 1 line

+ this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);

devm_

emm...
The devm_kzalloc() is the wrong choose in this case. It may makes we waste much more memory when the NAND page is 8K.

Assume the gpmi uses a 8K page nand chip. The devm_kzalloc() will allocate (sizeof(struct devres) + 8K).

Don't forget the malloc header and padding around malloc()'d space. So you're behind the 2 * PAGESIZE anyway.

Quoted 63 lines

It's obvious that the size is bigger then the SLUB_MAX_SIZE. So the kernel gets 2 with the
get_order(sizeof(struct devres) + 8K) which means we have to use 4 memory page for the verify_buf.
But in actually, we only use 8K memory. So the rest nearly 8K( sizeof(struct devres) is very small) memory
is wasted.

So I prefer to use the kzalloc() here.

thanks
Huang Shijie

Quoted 50 lines
Quoted 9 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +3 -0
+     if (!this->verify_buf)
+             return -ENOMEM;
+
 
      /* Prepare for the BBT scan. */
      ret = gpmi_pre_bbt_scan(this);
      if (ret)

drivers/mtd/nand/gpmi-nand/gpmi-nand.c void gpmi_nfc_exit(struct gpmi_nand_data *this) · line 1560

Quoted 8 lines
drivers/mtd/nand/gpmi-nand/gpmi-nand.c +2 -0
@@ -1531,6 +1553,8 @@ void gpmi_nfc_exit(struct gpmi_nand_data *this)
 
 {
 
      nand_release(&this->mtd);
      gpmi_free_dma_buffer(this);
 
+     kfree(this->verify_buf);
+     this->verify_buf = NULL;

Then you won't have to care for this ... I told you at least once before.

Quoted 30 lines

}

static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)

(quoted diff) +1 -0
@@ -1556,6 +1580,7 @@ static int __devinit gpmi_nfc_init(struct
 gpmi_nand_data *this) chip->read_byte         = gpmi_read_byte;
 
      chip->read_buf          = gpmi_read_buf;
      chip->write_buf         = gpmi_write_buf;
 
+     chip->verify_buf        = gpmi_verify_buf;
 
      chip->ecc.read_page     = gpmi_ecc_read_page;
      chip->ecc.write_page    = gpmi_ecc_write_page;
      chip->ecc.read_oob      = gpmi_ecc_read_oob;
(quoted diff) +0 -0

b/drivers/mtd/nand/gpmi-nand/gpmi-nand.h index 1547a60..8ddf115 100644

drivers/mtd/nand/gpmi-nand/gpmi-nand.h +1 -0
@@ -144,6 +144,7 @@ struct gpmi_nand_data {
 
      /* MTD / NAND */
      struct nand_chip        nand;
      struct mtd_info         mtd;
 
+     uint8_t                 *verify_buf;
 
      /* General-use Variables */
      int                     current_chip;
#4 lore raw

PATCH v4 1/1 mtd: gpmi: add NAND write verify support

? 2012?08?13? 03:14, Marek Vasut ??:

Quoted 2 lines

Don't forget the malloc header and padding around malloc()'d space. So you're behind the 2 * PAGESIZE anyway.

If we use the kzalloc(). There is no malloc header and padding. We get the memory by
the __get_free_pages(). So we do _NOT_ behind the 2*PAGE. We use exactly the 2*PAGE.

thanks
Huang Shijie

#5 lore raw

PATCH v4 1/1 mtd: gpmi: add NAND write verify support

In reply to Marek Vasut

? 2012?08?13? 03:14, Marek Vasut ??:

Quoted 3 lines

Don't forget the malloc header and padding around malloc()'d space. So you're behind the 2 * PAGESIZE anyway.

sorry, I misunderstood your meaning. My meaning is even the SLUB maybe costs some extra memory, it still save much memory
then we use the devm_kzalloc().

thanks
Huang Shijie