Skip to contentSkip to conversation

mtd: onenand: Fix unaligned access

PATCH New Marek Vasut 5 participants 10 messages Dec 26, 2013 – Jan 1, 2014

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

PATCH 1/3 mtd: onenand: Fix unaligned access

Fix unaligned access in OneNAND core. The problem is that the ffchars[] array is an array of "unsigned char", but in onenand_write_ops_nolock() can be passed to the memcpy_16() function. The memcpy_16() function will treat the buffer as an array of "unsigned short", thus triggering unaligned access if the compiler decided ffchars[] to be not aligned.

I managed to trigger the problem with regular ELDK 5.4 GCC compiler.

Cc Albert Aribaud <[email protected]>
Cc Scott Wood <[email protected]>
Cc Tom Rini <[email protected]>
---
 drivers/mtd/onenand/onenand_base.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/mtd/onenand/onenand_base.c +7 -1
index 979e4af..e33e8d3 100644
@@ -91,7 +91,13 @@ static struct nand_ecclayout onenand_oob_32 = {
 	.oobfree	= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
 };
 
-static const unsigned char ffchars[] = {
+/*
+ * Warning! This array is used with the memcpy_16() function, thus
+ * it must be aligned to 2 bytes. GCC can make this array unaligned
+ * as the array is made of unsigned char, which memcpy16() doesn't
+ * like and will cause unaligned access.
+ */
+static const unsigned char __aligned(2) ffchars[] = {
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 16 */
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Signature · 1 line
1.8.4.2
#1 lore raw

PATCH 1/3 mtd: onenand: Fix unaligned access

Quoted 25 lines · Marek Vasut

Fix unaligned access in OneNAND core. The problem is that the ffchars[] array is an array of "unsigned char", but in onenand_write_ops_nolock() can be passed to the memcpy_16() function. The memcpy_16() function will treat the buffer as an array of "unsigned short", thus triggering unaligned access if the compiler decided ffchars[] to be not aligned.

I managed to trigger the problem with regular ELDK 5.4 GCC compiler.

Cc Albert Aribaud <[email protected]>
Cc Scott Wood <[email protected]>
Cc Tom Rini <[email protected]>
---
 drivers/mtd/onenand/onenand_base.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/mtd/onenand/onenand_base.c index 979e4af..e33e8d3 100644

drivers/mtd/onenand/onenand_base.c +0 -1
@@ -91,7 +91,13 @@ static struct nand_ecclayout onenand_oob_32 = {
 	.oobfree	= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
 };
 
-static const unsigned char ffchars[] = {

drivers/mtd/onenand/onenand_base.c static struct nand_ecclayout onenand_oob_32 = { · line 103

Quoted 10 lines
drivers/mtd/onenand/onenand_base.c +7 -0
+/*
+ * Warning! This array is used with the memcpy_16() function, thus
+ * it must be aligned to 2 bytes. GCC can make this array unaligned
+ * as the array is made of unsigned char, which memcpy16() doesn't
+ * like and will cause unaligned access.
+ */
+static const unsigned char __aligned(2) ffchars[] = {
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/* 16 */
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

Lukasz, can you please review this one?

Best regards,
Marek Vasut

#2 lore raw

PATCH 1/3 mtd: onenand: Fix unaligned access

Hi Marek,

Quoted 26 lines · Marek Vasut
Quoted 25 lines

Fix unaligned access in OneNAND core. The problem is that the ffchars[] array is an array of "unsigned char", but in onenand_write_ops_nolock() can be passed to the memcpy_16() function. The memcpy_16() function will treat the buffer as an array of "unsigned short", thus triggering unaligned access if the compiler decided ffchars[] to be not aligned.

I managed to trigger the problem with regular ELDK 5.4 GCC compiler.

Cc Albert Aribaud <[email protected]>
Cc Scott Wood <[email protected]>
Cc Tom Rini <[email protected]>
---
 drivers/mtd/onenand/onenand_base.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/mtd/onenand/onenand_base.c index 979e4af..e33e8d3 100644

drivers/mtd/onenand/onenand_base.c +0 -0
@@ -91,7 +91,13 @@ static struct nand_ecclayout onenand_oob_32 = {
 	.oobfree	= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
 };
 
Quoted 13 lines

drivers/mtd/onenand/onenand_base.c static struct nand_ecclayout onenand_oob_32 = {

Quoted 11 lines
drivers/mtd/onenand/onenand_base.c +7 -1
-static const unsigned char ffchars[] = {
+/*
+ * Warning! This array is used with the memcpy_16() function, thus
+ * it must be aligned to 2 bytes. GCC can make this array unaligned
+ * as the array is made of unsigned char, which memcpy16() doesn't
+ * like and will cause unaligned access.
+ */
+static const unsigned char __aligned(2) ffchars[] = {
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/*
 16 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

Lukasz, can you please review this one?

No problem, I will review the patch when I come back to the office (and be able to test it on the proper HW).

Quoted 3 lines

Best regards,
Marek Vasut

Best regards,
Lukasz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131228/77c33a29/attachment.pgp>

#3 lore raw

PATCH 1/3 mtd: onenand: Fix unaligned access

Quoted 43 lines · Lukasz Majewski

Hi Marek,

Quoted 41 lines · Marek Vasut
Quoted 40 lines

Fix unaligned access in OneNAND core. The problem is that the ffchars[] array is an array of "unsigned char", but in onenand_write_ops_nolock() can be passed to the memcpy_16() function. The memcpy_16() function will treat the buffer as an array of "unsigned short", thus triggering unaligned access if the compiler decided ffchars[] to be not aligned.

I managed to trigger the problem with regular ELDK 5.4 GCC compiler.

Cc Albert Aribaud <[email protected]>
Cc Scott Wood <[email protected]>
Cc Tom Rini <[email protected]>

---

 drivers/mtd/onenand/onenand_base.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/mtd/onenand/onenand_base.c index 979e4af..e33e8d3 100644

drivers/mtd/onenand/onenand_base.c +7 -1
@@ -91,7 +91,13 @@ static struct nand_ecclayout onenand_oob_32 = {
 
 	.oobfree	= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
 
 };
 
-static const unsigned char ffchars[] = {
+/*
+ * Warning! This array is used with the memcpy_16() function, thus
+ * it must be aligned to 2 bytes. GCC can make this array unaligned
+ * as the array is made of unsigned char, which memcpy16() doesn't
+ * like and will cause unaligned access.
+ */
+static const unsigned char __aligned(2) ffchars[] = {
 
drivers/mtd/onenand/onenand_base.c +0 -0
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,	/*
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

16 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

Lukasz, can you please review this one?

No problem, I will review the patch when I come back to the office (and be able to test it on the proper HW).

Thanks, I'd love to get this fix into 2014.01 . Maybe the real fix here would be to fix the memcpy_16() instead tho.

Best regards,
Marek Vasut

#4 lore raw

PATCH 1/3 mtd: onenand: Fix unaligned access

Hi Marek,

Quoted 54 lines · Lukasz Majewski
Quoted 53 lines

Hi Marek,

Quoted 50 lines · Marek Vasut
Quoted 46 lines

Fix unaligned access in OneNAND core. The problem is that the ffchars[] array is an array of "unsigned char", but in onenand_write_ops_nolock() can be passed to the memcpy_16() function. The memcpy_16() function will treat the buffer as an array of "unsigned short", thus triggering unaligned access if the compiler decided ffchars[] to be not aligned.

I managed to trigger the problem with regular ELDK 5.4 GCC compiler.

Cc Albert Aribaud <[email protected]>
Cc Scott Wood <[email protected]>
Cc Tom Rini <[email protected]>

---

 drivers/mtd/onenand/onenand_base.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/mtd/onenand/onenand_base.c index 979e4af..e33e8d3 100644 --- a/drivers/mtd/onenand/onenand_base.c +++ b/drivers/mtd/onenand/onenand_base.c

drivers/mtd/onenand/onenand_base.c +2 -1
@@ -91,7 +91,13 @@ static struct nand_ecclayout onenand_oob_32
 = {
 
 	.oobfree	= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
 
 };
 
-static const unsigned char ffchars[] = {
+/*
+ * Warning! This array is used with the memcpy_16() function,

thus
+ * it must be aligned to 2 bytes. GCC can make this array unaligned
+ * as the array is made of unsigned char, which memcpy16() doesn't
+ * like and will cause unaligned access.
+ */

drivers/mtd/onenand/onenand_base.c +1 -0
+static const unsigned char __aligned(2) ffchars[] = {
 
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

0xff, /*

Quoted 1 line

16 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

Lukasz, can you please review this one?

Quoted 6 lines
Quoted 2 lines

No problem, I will review the patch when I come back to the office (and be able to test it on the proper HW).

Thanks, I'd love to get this fix into 2014.01 . Maybe the real fix here would be to fix the memcpy_16() instead tho.

I've tested it with eldk-5.4 toolchain on GONI device. It seems that no regression is caused by this patch.

As a side note - this problem didn't show up for GONI board. Apparently I was lucky.

I will scrutinize this code in respect of code alignment.

Applied to u-boot-onenand.

I will send pull request to Tom ASAP.

Quoted 2 lines

Best regards,
Marek Vasut

Signature · 5 lines
Best regards,

Lukasz Majewski

Samsung R&D Institute Poland (SRPOL) | Linux Platform Group
#5 lore raw

PATCH 1/3 mtd: onenand: Fix unaligned access

Quoted 66 lines · Lukasz Majewski

Hi Marek,

Quoted 55 lines · Lukasz Majewski
Quoted 54 lines

Hi Marek,

Quoted 51 lines · Marek Vasut
Quoted 45 lines

Fix unaligned access in OneNAND core. The problem is that the ffchars[] array is an array of "unsigned char", but in onenand_write_ops_nolock() can be passed to the memcpy_16() function. The memcpy_16() function will treat the buffer as an array of "unsigned short", thus triggering unaligned access if the compiler decided ffchars[] to be not aligned.

I managed to trigger the problem with regular ELDK 5.4 GCC compiler.

Cc Albert Aribaud <[email protected]>
Cc Scott Wood <[email protected]>
Cc Tom Rini <[email protected]>

---

 drivers/mtd/onenand/onenand_base.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/mtd/onenand/onenand_base.c index 979e4af..e33e8d3 100644 --- a/drivers/mtd/onenand/onenand_base.c +++ b/drivers/mtd/onenand/onenand_base.c

drivers/mtd/onenand/onenand_base.c +2 -1
@@ -91,7 +91,13 @@ static struct nand_ecclayout onenand_oob_32
 = {
 
 	.oobfree	= { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
 
 };
 
-static const unsigned char ffchars[] = {
+/*
+ * Warning! This array is used with the memcpy_16() function,

thus
+ * it must be aligned to 2 bytes. GCC can make this array unaligned
+ * as the array is made of unsigned char, which memcpy16() doesn't
+ * like and will cause unaligned access.
+ */

drivers/mtd/onenand/onenand_base.c +1 -0
+static const unsigned char __aligned(2) ffchars[] = {
 
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Quoted 3 lines

0xff, /*

16 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

Lukasz, can you please review this one?

Quoted 5 lines
Quoted 2 lines

No problem, I will review the patch when I come back to the office (and be able to test it on the proper HW).

Thanks, I'd love to get this fix into 2014.01 . Maybe the real fix here would be to fix the memcpy_16() instead tho.

I've tested it with eldk-5.4 toolchain on GONI device. It seems that no regression is caused by this patch.

Quoted 6 lines

As a side note - this problem didn't show up for GONI board. Apparently I was lucky.

I will scrutinize this code in respect of code alignment.

Applied to u-boot-onenand.

Thank you very much!

Have a happy new year, guys !

Best regards,
Marek Vasut

#6 lore raw

PATCH 2/3 ARM: pxa: Fix OneNAND SPL builds

In reply to Marek Vasut

The OneNAND SPL used on PXA is slightly obscure. Due to the OneNAND limitation, where we have only the first 1KiB of the OneNAND available upon power-up as a memory-mapped area, from which the CPU starts executing, we place only the most essential code into this first 1KiB . This code copies the rest of the SPL into SRAM and jumps to it. This code is stored in section .text.0 .

The rest of the SPL is stored in section .text.1 . When running the OBJCOPY on the SPL, it will preserve only .text section, but the .text.0 and .text.1 are stripped away from the result, thus making the SPL binary empty. The patch adds additional -j parameters to the OBJCOPY for PXA during the SPL build, which will preserve the .text.0 and .text.1 sections.

Moreover, this patch also adds missing functions into the .text.0 section, since otherwise the PXA270 with 1KiB-window OneNAND won't be able to boot.

Cc Albert Aribaud <[email protected]>
Cc Tom Rini <[email protected]>
---
 arch/arm/cpu/pxa/config.mk   | 13 +++++++++++++
 board/vpac270/u-boot-spl.lds |  1 +
 2 files changed, 14 insertions(+)
arch/arm/cpu/pxa/config.mk +13 -0
index d8d263d..f2befbe 100644
@@ -14,3 +14,16 @@ PLATFORM_CPPFLAGS += -mcpu=xscale
 # ========================================================================
 PF_RELFLAGS_SLB_AT := $(call cc-option,-mshort-load-bytes,$(call cc-option,-malignment-traps,))
 PLATFORM_RELFLAGS += $(PF_RELFLAGS_SLB_AT)
+
+#
+# !WARNING!
+# The PXA's OneNAND SPL uses .text.0 and .text.1 segments to allow booting from
+# really small OneNAND memories where the mmap'd window is only 1KiB big. The
+# .text.0 contains only the bare minimum needed to load the real SPL into SRAM.
+# Add .text.0 and .text.1 into OBJFLAGS, so when the SPL is being objcopy'd,
+# they are not discarded.
+#
+
+#ifdef CONFIG_SPL_BUILD
+OBJCFLAGS += -j .text.0 -j .text.1
+#endif
board/vpac270/u-boot-spl.lds +1 -0
index 02d107c..b6fdde4 100644
@@ -20,6 +20,7 @@ SECTIONS
 	.text.0	:
 	{
 		arch/arm/cpu/pxa/start.o		(.text*)
+		arch/arm/lib/built-in.o			(.text*)
 		board/vpac270/built-in.o		(.text*)
 		drivers/mtd/onenand/built-in.o		(.text*)
 	}
Signature · 1 line
1.8.4.2
#7 lore raw

PATCH 1/3 mtd: onenand: Fix unaligned access

In reply to Marek Vasut

Dear Marek Vasut,

Quoted 6 lines · Marek Vasut <marex <at> denx.de> writes:

+/*
+ * Warning! This array is used with the memcpy_16() function, thus
+ * it must be aligned to 2 bytes. GCC can make this array unaligned
+ * as the array is made of unsigned char, which memcpy16() doesn't
+ * like and will cause unaligned access.
+ */

This is just really a nitpick but memcpy16() -> memcpy_16().

All the best,
Rommel

#8 lore raw

PATCH 1/3 mtd: onenand: Fix unaligned access

In reply to Marek Vasut
Quoted 6 lines · Marek Vasut

Fix unaligned access in OneNAND core. The problem is that the ffchars[] array is an array of "unsigned char", but in onenand_write_ops_nolock() can be passed to the memcpy_16() function. The memcpy_16() function will treat the buffer as an array of "unsigned short", thus triggering unaligned access if the compiler decided ffchars[] to be not aligned.

Quoted 6 lines

I managed to trigger the problem with regular ELDK 5.4 GCC compiler.

Cc Albert Aribaud <[email protected]>
Cc Scott Wood <[email protected]>
Cc Tom Rini <[email protected]>

I don't see the onenand custodian on that CC list.

-Scott

#9 lore raw

PATCH 3/3 ARM: pxa: Fix OneNAND window access on VPAC270

In reply to Marek Vasut

Access the OneNAND 1KiB window on the VPAC270 as an SRAM instead of accessing it as a burst-RAM. This fixes a problem where the board failed to reboot sometimes as the CPU couldn't start executing from the OneNAND 1KiB window.

Cc Albert Aribaud <[email protected]>
Cc Tom Rini <[email protected]>
---
 include/configs/vpac270.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/configs/vpac270.h +1 -1
index c3ac612..46acac4 100644
@@ -287,7 +287,7 @@
 /*
  * Memory settings
  */
-#define	CONFIG_SYS_MSC0_VAL	0x3ffc95fa
+#define	CONFIG_SYS_MSC0_VAL	0x3ffc95f9
 #define	CONFIG_SYS_MSC1_VAL	0x02ccf974
 #define	CONFIG_SYS_MSC2_VAL	0x00000000
 #ifdef	CONFIG_RAM_256M
Signature · 1 line
1.8.4.2