Skip to contentSkip to conversation

Fix boot failure due to misaligned DMA buffer

PATCH In review Nam Cao 3 participants 6 messages Nov 7, 2024 – Nov 18, 2024 VFS

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

v2 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 extlinux_read_bootflow() to use proper buffer alignment for DMA.

The second patch changes the default alignment of fs_read_alloc() to be DMA-suitable, to fix other potential bugs.

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

 boot/bootmeth_extlinux.c | 3 ++-
 fs/fs.c                  | 4 ++++
 include/fs.h             | 2 +-
 3 files changed, 7 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.5
author
#1 lore raw

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

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 buffer alignment to ARCH_DMA_MINALIGN.

Fixes 31aefaf89a5b ("bootstd: Add an implementation of distro boot")

---
v2:
- use ARCH_DMA_MINALIGN to be consistent with bootmeth_script [Simon]

 boot/bootmeth_extlinux.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
boot/bootmeth_extlinux.c +2 -1
index ae0ad1d53e3..ac046705033 100644
@@ -8,6 +8,7 @@
 
 #define LOG_CATEGORY UCLASS_BOOTSTD
 
+#include <asm/cache.h>
 #include <common.h>
 #include <bootdev.h>
 #include <bootflow.h>
@@ -127,7 +128,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, ARCH_DMA_MINALIGN);
 	if (ret)
 		return log_msg_ret("read", ret);
 
Signature · 1 line
2.39.5
#2 lore raw

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

Quoted 16 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)

Quoted 5 lines

Change the buffer alignment to ARCH_DMA_MINALIGN.

Fixes 31aefaf89a5b ("bootstd: Add an implementation of distro boot")

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
author
#3 lore raw

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

In reply to 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.

Change the default alignment to ARCH_DMA_MINALIGN.

Fixes de7b5a8a1ac0 ("fs: Create functions to load and allocate a file")

---
v2:
- mention the default value in function comment [Simon]

 fs/fs.c      | 4 ++++
 include/fs.h | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)
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);
include/fs.h +1 -1
index ef540e7c23d..2f879968d0c 100644
@@ -309,7 +309,7 @@ int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
  *
  * @fname: Filename to read
  * @size: Size of file to read (must be correct!)
- * @align: Alignment to use for memory allocation (0 for default)
+ * @align: Alignment to use for memory allocation (0 for default: ARCH_DMA_MINALIGN)
  * @bufp: On success, returns the allocated buffer with the nul-terminated file
  *	in it
  * Return: 0 if OK, -ENOMEM if out of memory, -EIO if read failed
Signature · 1 line
2.39.5
#4 lore raw

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

Quoted 7 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.

Quoted 8 lines

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")

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
Reviewed-by on the v2 series
#5 lore raw
In reply to Nam Cao

Hi Nam,

Quoted 34 lines · Nam Cao

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 extlinux_read_bootflow() to use proper buffer alignment for DMA.

The second patch changes the default alignment of fs_read_alloc() to be DMA-suitable, to fix other potential bugs.

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

Quoted 8 lines
 boot/bootmeth_extlinux.c | 3 ++-
 fs/fs.c                  | 4 ++++
 include/fs.h             | 2 +-
 3 files changed, 7 insertions(+), 2 deletions(-)

--
2.39.5

FYI, this "CACHE: Misaligned operation" problem was also fixed in bootstd script method by this previous patch:
https://lists.denx.de/pipermail/u-boot/2023-September/530927.html

For the series.

All the best,
Tony