Skip to contentSkip to conversation

fpga: versalpl: Fix unaligned buffer handling

PATCH Applied Pranav Tilak 2 participants 2 messages May 7 – May 11 FPGA manager framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fpga: versalpl: Fix unaligned buffer handling

When fpga load is called with a misaligned buffer address, the versal_align_dma_buffer() function shifts the pointer forward to the next aligned boundary and uses memcpy() to copy the data. Since the destination is ahead of the source and the regions overlap, memcpy() produces undefined behavior; in practice U-Boot's generic memcpy() copies forward, repeating the first ARCH_DMA_MINALIGN-aligned chunk throughout the buffer.

Replace memcpy() with memmove() which correctly handles overlapping regions by copying backwards when the destination is ahead of the source.

Fixes 26e054c943a7 ("arm64: versal: fpga: Add PL bit stream load support")
---
 drivers/fpga/versalpl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/fpga/versalpl.c +1 -1
index 630d1ecfea3..3cb56cc0dc9 100644
@@ -17,7 +17,7 @@ static ulong versal_align_dma_buffer(ulong *buf, u32 len)
 
 	if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
 		new_buf = (ulong *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
-		memcpy(new_buf, buf, len);
+		memmove(new_buf, buf, len);
 		buf = new_buf;
 	}
 
Signature · 1 line
2.34.1
Applied
#1 lore raw
Quoted 23 lines · Pranav Tilak

When fpga load is called with a misaligned buffer address, the versal_align_dma_buffer() function shifts the pointer forward to the next aligned boundary and uses memcpy() to copy the data. Since the destination is ahead of the source and the regions overlap, memcpy() produces undefined behavior; in practice U-Boot's generic memcpy() copies forward, repeating the first ARCH_DMA_MINALIGN-aligned chunk throughout the buffer.

Replace memcpy() with memmove() which correctly handles overlapping regions by copying backwards when the destination is ahead of the source.

Fixes 26e054c943a7 ("arm64: versal: fpga: Add PL bit stream load support")
---
  drivers/fpga/versalpl.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/fpga/versalpl.c +0 -0
index 630d1ecfea3..3cb56cc0dc9 100644

drivers/fpga/versalpl.c static ulong versal_align_dma_buffer(ulong *buf, u32 len) · line 23

Quoted 8 lines
drivers/fpga/versalpl.c +1 -1
@@ -17,7 +17,7 @@ static ulong versal_align_dma_buffer(ulong *buf, u32 len)
 
  	if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
  		new_buf = (ulong *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
-		memcpy(new_buf, buf, len);
+		memmove(new_buf, buf, len);
  		buf = new_buf;
  	}
 

Applied.
M