Skip to contentSkip to conversation

Fix boot failure due to misaligned DMA buffer

PATCH Superseded Nam Cao 2 participants 7 messages Nov 6, 2024 – Nov 7, 2024 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

cover Fix boot failure due to misaligned DMA buffer

Hi,

We observed the following sporadic boot failure while booting from MMC device:

=> boot
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8] ** Booting bootflow '[email protected]_1' with extlinux Ignoring unknown command: �D���D��
Boot failed (err=-14)

The reason is because while allocating buffer to read a file from MMC, alignment of 1 byte is used. Thus, the buffer doesn't work for performing DMA, and garbage data is read.

While looking at this issue, I also noticed that if no alignment specified (align=0) then fs_read_alloc() is documented to use the default. But the default is no alignment. Therefore, other users of fs_read_alloc() which specify align=0 may be broken as well.

The first patch changes the default to align the buffer suitable for DMA.

The second patch changes extlinux_read_bootflow() to use the default alignment.

Nam Cao (2):
fs: Use ARCH_DMA_MINALIGN as default alignment for fs_read_alloc()
boot: extlinux: Fix unaligned buffer for reading data from file system

 boot/bootmeth_extlinux.c | 2 +-
 fs/fs.c                  | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.39.5
author
#1 lore raw

PATCH 1/2 fs: Use ARCH_DMA_MINALIGN as default alignment for fs_read_alloc()

The comment above fs_read_alloc() explains:

@align: Alignment to use for memory allocation (0 for default)

However, in the actual implementation, there is no alignment when @align is zero.

This current default is probably fine for most cases. But for some block devices which transfer data via DMA, ARCH_DMA_MINALIGN is needed.

Change the default alignment to ARCH_DMA_MINALIGN.

Fixes de7b5a8a1ac0 ("fs: Create functions to load and allocate a file")
---
 fs/fs.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/fs.c +4 -0
index acf465bdd80..bfa51c89d40 100644
@@ -23,6 +23,7 @@
 #include <semihostingfs.h>
 #include <ubifs_uboot.h>
 #include <btrfs.h>
+#include <asm/cache.h>
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <div64.h>
@@ -1001,6 +1002,9 @@ int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp)
 	char *buf;
 	int ret;
 
+	if (!align)
+		align = ARCH_DMA_MINALIGN;
+
 	buf = memalign(align, size + 1);
 	if (!buf)
 		return log_msg_ret("buf", -ENOMEM);
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/2] fs: Use ARCH_DMA_MINALIGN as default alignment for fs_read_alloc()

Hi Nam,

Quoted 11 lines · Nam Cao

The comment above fs_read_alloc() explains:

@align: Alignment to use for memory allocation (0 for default)

However, in the actual implementation, there is no alignment when @align is zero.

This current default is probably fine for most cases. But for some block devices which transfer data via DMA, ARCH_DMA_MINALIGN is needed.

Quoted 9 lines

Change the default alignment to ARCH_DMA_MINALIGN.

Fixes de7b5a8a1ac0 ("fs: Create functions to load and allocate a file")
---
 fs/fs.c | 4 ++++
 1 file changed, 4 insertions(+)

Please update the function comment in fs.h to specify the default value

Regards,
Simon

author
#3 lore raw

Re: [PATCH 1/2] fs: Use ARCH_DMA_MINALIGN as default alignment for fs_read_alloc()

Hi Simon,

Quoted 24 lines · Simon Glass

Hi Nam,

Quoted 11 lines · Nam Cao

The comment above fs_read_alloc() explains:

@align: Alignment to use for memory allocation (0 for default)

However, in the actual implementation, there is no alignment when @align is zero.

This current default is probably fine for most cases. But for some block devices which transfer data via DMA, ARCH_DMA_MINALIGN is needed.

Quoted 9 lines

Change the default alignment to ARCH_DMA_MINALIGN.

Fixes de7b5a8a1ac0 ("fs: Create functions to load and allocate a file")
---
 fs/fs.c | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 1 line

Please update the function comment in fs.h to specify the default value

Sure.

Best regards,
Nam

author
#4 lore raw

PATCH 2/2 boot: extlinux: Fix unaligned buffer for reading data from file system

In reply to Nam Cao

extlinux_read_bootflow() allocates a buffer to read from file system without any alignment.

But for some block devices which transfer data via DMA, ARCH_DMA_MINALIGN alignment is required. For example, due to misaligned buffer, the below boot failure is observed.

=> boot
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8] ** Booting bootflow '[email protected]_1' with extlinux Ignoring unknown command: �D���D��
Boot failed (err=-14)

Change the alignment to default (which is ARCH_DMA_MINALIGN).

Fixes 31aefaf89a5b ("bootstd: Add an implementation of distro boot")
---
 boot/bootmeth_extlinux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
boot/bootmeth_extlinux.c +1 -1
index ae0ad1d53e3..5424d179bf8 100644
@@ -127,7 +127,7 @@ static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
 		return log_msg_ret("try", ret);
 	size = bflow->size;
 
-	ret = bootmeth_alloc_file(bflow, 0x10000, 1);
+	ret = bootmeth_alloc_file(bflow, 0x10000, 0);
 	if (ret)
 		return log_msg_ret("read", ret);
 
Signature · 1 line
2.39.5
#5 lore raw

Re: [PATCH 2/2] boot: extlinux: Fix unaligned buffer for reading data from file system

Hi Nam,

Quoted 31 lines · Nam Cao

extlinux_read_bootflow() allocates a buffer to read from file system without any alignment.

But for some block devices which transfer data via DMA, ARCH_DMA_MINALIGN alignment is required. For example, due to misaligned buffer, the below boot failure is observed.

=> boot
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8] ** Booting bootflow '[email protected]_1' with extlinux Ignoring unknown command: �D���D��
Boot failed (err=-14)

Change the alignment to default (which is ARCH_DMA_MINALIGN).

Fixes 31aefaf89a5b ("bootstd: Add an implementation of distro boot")
---
 boot/bootmeth_extlinux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
boot/bootmeth_extlinux.c +0 -0
index ae0ad1d53e3..5424d179bf8 100644

boot/bootmeth_extlinux.c static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow) · line 130

Quoted 5 lines
boot/bootmeth_extlinux.c +1 -1
@@ -127,7 +127,7 @@ static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
                return log_msg_ret("try", ret);
        size = bflow->size;
 
-       ret = bootmeth_alloc_file(bflow, 0x10000, 1);
+       ret = bootmeth_alloc_file(bflow, 0x10000, 0);

Can you please use ARCH_DMA_MINALIGN here? Either that or update bootmeth_script to do the same as here.

Quoted 6 lines
boot/bootmeth_extlinux.c +0 -1
        if (ret)
                return log_msg_ret("read", ret);
 
--

2.39.5

Regards,
Simon

author
#6 lore raw

Re: [PATCH 2/2] boot: extlinux: Fix unaligned buffer for reading data from file system

Hi Simon,

Quoted 35 lines · Simon Glass
Quoted 34 lines · Nam Cao

extlinux_read_bootflow() allocates a buffer to read from file system without any alignment.

But for some block devices which transfer data via DMA, ARCH_DMA_MINALIGN alignment is required. For example, due to misaligned buffer, the below boot failure is observed.

=> boot
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8]
CACHE: Misaligned operation at range [9efa25f8, 9efa27f8] ** Booting bootflow '[email protected]_1' with extlinux Ignoring unknown command: �D���D��
Boot failed (err=-14)

Change the alignment to default (which is ARCH_DMA_MINALIGN).

Fixes 31aefaf89a5b ("bootstd: Add an implementation of distro boot")
---
 boot/bootmeth_extlinux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
boot/bootmeth_extlinux.c +0 -0
index ae0ad1d53e3..5424d179bf8 100644
@@ -127,7 +127,7 @@ static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
                return log_msg_ret("try", ret);
        size = bflow->size;
 
Quoted 5 lines

boot/bootmeth_extlinux.c static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow) · line 130

Quoted 2 lines
boot/bootmeth_extlinux.c +1 -1
-       ret = bootmeth_alloc_file(bflow, 0x10000, 1);
+       ret = bootmeth_alloc_file(bflow, 0x10000, 0);

Can you please use ARCH_DMA_MINALIGN here? Either that or update bootmeth_script to do the same as here.

No preference from me. Let's just use ARCH_DMA_MINALIGN here.

Thanks for the comments,
Nam