Fix dma_addr_t for R5 SPL
Subsystems (3)
cover Fix dma_addr_t for R5 SPL
Hi all,
On various TI's K3 platforms boot failure was observed on SPI NOR since the commit 5609f200d062 ("arm: Kconfig: enable LTO for ARCH_K3"). This issue was root caused to stack corruption by the 'udma_transfer' function. Where the local variable 'paddr' of type 'dma_addr_t' was being written to as a 64-bit value which overwrote the stack frame of the caller (dma_memcpy) as only 32-bits had been reserved for paddr on the stack, specifically the r4 register in the frame of dma_memcpy was being overwritten with a 0.
drivers/dma/ti/k3-udma.c:2192:
int udma_transfer(...)
{
...
dma_addr_t paddr = 0;
...
/* paddr was written to as 64-bit value here */
udma_poll_completion(uc, &paddr);
}drivers/dma/dma-uclass.c:234:
int dma_memcpy(...)
{
dma_addr_t destination;
dma_addr_t source;
int ret;
...
/* This call resolves to udma_transfer */
ret = ops->transfer(...);
...
dma_unmap_single(destination, ...);
dma_unmap_single(...);
return ret;
}Enabling LTO changed how gcc mapped local variables of dma_memcpy to CPU registers, where earlier the bug was hidden since the overwritten register 'r4' was allotted to 'ret' but was allotted to 'destination' once LTO was enabled. And since the overwritten value was 0, the bug remained undetected as it just meant ret was 0, but having 'destination' set to 0 caused dma_unmap_single to fail silently leading to boot failures.
The fix entails enabling DMA_ADDR_T_64BIT which changes dma_addr_t from u32 to u64 for the R5 SPL thus reserving enough space for 'paddr' to prevent the overflow.
Regards,
Anshul
---
Anshul Dalal (2):
dma: ti: k3-udma: fix dma_addr_t typecasts
config: arch: k3: enable DMA_ADDR_T_64BIT
arch/arm/Kconfig | 1 + drivers/dma/ti/k3-udma.c | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-)
Signature · 1 line
2.50.1
PATCH 1/2 dma: ti: k3-udma: fix dma_addr_t typecasts
With the change to dma_addr_t from u32 to u64 for the R5 SPL, the existing typecasts from void* to int or vice-versa cause the compiler to throw a "cast from pointer to integer of different size".
Therefore this patch changes the casts to a uintptr_t which is guaranteed to hold any pointer value, thus supressing the compiler warning.
--- drivers/dma/ti/k3-udma.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/dma/ti/k3-udma.c +3 -3
@@ -2327,7 +2327,7 @@ static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) {struct udma_dev *ud =dev_get_priv (dma->dev);struct cppi5_host_desc_t *desc_tx; -dma_addr_t dma_src = (dma_addr_t )src; +dma_addr_t dma_src = (uintptr_t )src;struct ti_udma_drv_packet_data packet_data = {0 };dma_addr_t paddr;struct udma_chan *uc;
@@ -2426,7 +2426,7 @@ static int udma_receive(struct dma *dma, void **dst, void *metadata)cppi5_desc_get_tags_ids (&desc_rx->hdr, &port_id,NULL ); - *dst = (void *)buf_dma; + *dst = (void *)(uintptr_t )buf_dma; uc->num_rx_bufs--;return pkt_len;
@@ -2518,7 +2518,7 @@ int udma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size) desc_num = uc->desc_rx_cur %UDMA_RX_DESC_NUM ; desc_rx = uc->desc_rx + (desc_num * uc->config.hdesc_size); - dma_dst = (dma_addr_t )dst; + dma_dst = (uintptr_t )dst;cppi5_hdesc_reset_hbdesc (desc_rx);
Signature · 1 line
2.50.1
Re: [PATCH v1 1/2] dma: ti: k3-udma: fix dma_addr_t typecasts
Hi Anshul,
Quoted 1 line · Anshul Dalal
With the change to
dma_addr_tfrom u32 to u64 for the R5 SPL, the
From above line, Does this patch meant to after patch 2/2 ?
Quoted 17 lines
existing typecasts from void* to int or vice-versa cause the compiler to throw a "cast from pointer to integer of different size".
Therefore this patch changes the casts to a
uintptr_twhich is guaranteed to hold any pointer value, thus supressing the compiler warning.Signed-off-by Anshul Dalal <[email protected]>--- drivers/dma/ti/k3-udma.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/dma/ti/k3-udma.c +0
-0
drivers/dma/ti/k3-udma.c static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) · line 2330
Quoted 5 lines
drivers/dma/ti/k3-udma.c +1
-1@@ -2327,7 +2327,7 @@ static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) {struct udma_dev *ud =dev_get_priv (dma->dev);struct cppi5_host_desc_t *desc_tx; -dma_addr_t dma_src = (dma_addr_t )src; +dma_addr_t dma_src = (uintptr_t )src;
Considering after the dma_addr_t changed to u64, please help me understand what warning does the above change fix.
Thanks,
Prasanth
Quoted 24 lines
drivers/dma/ti/k3-udma.c +2
-2struct ti_udma_drv_packet_data packet_data = {0 };dma_addr_t paddr;struct udma_chan *uc;@@ -2426,7 +2426,7 @@ static int udma_receive(struct dma *dma, void **dst, void *metadata)cppi5_desc_get_tags_ids (&desc_rx->hdr, &port_id,NULL ); - *dst = (void *)buf_dma; + *dst = (void *)(uintptr_t )buf_dma; uc->num_rx_bufs--;return pkt_len;@@ -2518,7 +2518,7 @@ int udma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size) desc_num = uc->desc_rx_cur %UDMA_RX_DESC_NUM ; desc_rx = uc->desc_rx + (desc_num * uc->config.hdesc_size); - dma_dst = (dma_addr_t )dst; + dma_dst = (uintptr_t )dst;cppi5_hdesc_reset_hbdesc (desc_rx);Signature · 1 line
2.50.1
Re: [PATCH v1 1/2] dma: ti: k3-udma: fix dma_addr_t typecasts
Hi Prasanth,
Quoted 18 lines · Prasanth Mantena
Hi Anshul,
Quoted 1 line · Anshul Dalal
With the change to
dma_addr_tfrom u32 to u64 for the R5 SPL, theFrom above line, Does this patch meant to after patch 2/2 ?
Quoted 12 lines
existing typecasts from void* to int or vice-versa cause the compiler to throw a "cast from pointer to integer of different size".
Therefore this patch changes the casts to a
uintptr_twhich is guaranteed to hold any pointer value, thus supressing the compiler warning.Signed-off-by Anshul Dalal <[email protected]>--- drivers/dma/ti/k3-udma.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 14 lines
drivers/dma/ti/k3-udma.c static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) · line 2330
Quoted 10 lines
drivers/dma/ti/k3-udma.c +1
-1@@ -2327,7 +2327,7 @@ static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) {struct udma_dev *ud =dev_get_priv (dma->dev);struct cppi5_host_desc_t *desc_tx; -dma_addr_t dma_src = (dma_addr_t )src; +dma_addr_t dma_src = (uintptr_t )src;Considering after the
dma_addr_tchanged to u64, please help me understand what warning does the above change fix.
This patch was meant to prevent warnings from the last patch of the series, though I understand the confusion here. I will reword the commit description to be more clear in v2.
Thanks,
Anshul
Re: [PATCH v1 1/2] dma: ti: k3-udma: fix dma_addr_t typecasts
Quoted 36 lines · Anshul Dalal
Hi Prasanth,
Quoted 18 lines · Prasanth Mantena
Hi Anshul,
Quoted 1 line · Anshul Dalal
With the change to
dma_addr_tfrom u32 to u64 for the R5 SPL, theFrom above line, Does this patch meant to after patch 2/2 ?
Quoted 12 lines
existing typecasts from void* to int or vice-versa cause the compiler to throw a "cast from pointer to integer of different size".
Therefore this patch changes the casts to a
uintptr_twhich is guaranteed to hold any pointer value, thus supressing the compiler warning.Signed-off-by Anshul Dalal <[email protected]>--- drivers/dma/ti/k3-udma.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)Quoted 14 lines
drivers/dma/ti/k3-udma.c static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) · line 2330
Quoted 10 lines
drivers/dma/ti/k3-udma.c +1
-1@@ -2327,7 +2327,7 @@ static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) {struct udma_dev *ud =dev_get_priv (dma->dev);struct cppi5_host_desc_t *desc_tx; -dma_addr_t dma_src = (dma_addr_t )src; +dma_addr_t dma_src = (uintptr_t )src;Considering after the
dma_addr_tchanged to u64, please help me understand what warning does the above change fix.
Quoted 4 lines
This patch was meant to prevent warnings from the last patch of the series, though I understand the confusion here. I will reword the commit description to be more clear in v2.
To give further context:
The fix would ideally be to use dma_addr_t for all dma-uclass APIs similar to how the linux kernel handles it. Though given what we have (using void* instead), a direct typecast to dma_addr_t with the second patch would essentially amount to casting a 32-bit pointer to u64 hence the warning "cast from pointer to integer of different size".
uintptr_t is u32 for the R5 SPL so the 32-bit pointer is casted to a u32 first and then to dma_addr_t which is u64. Thus keeping the compiler happy :)
And since K3's R5 SPL is the only case where dma_addr_t isn't the same size as void* but with the dma addresses guaranteed to fit in a 32-bit address space, I think a few typecasts are the easier solution than refactoring the dma APIs to use dma_addr_t.
Regards,
Anshul
Re: [PATCH v1 1/2] dma: ti: k3-udma: fix dma_addr_t typecasts
In reply to Anshul DalalQuoted 3 lines · Anshul Dalal
With the change to
dma_addr_tfrom u32 to u64 for the R5 SPL, the existing typecasts from void* to int or vice-versa cause the compiler to throw a "cast from pointer to integer of different size".
is this some compiler specific ?. Please mention that
I don't see this warning https://gist.github.com/uditkumarti/1c4238a7101d97cdc57b7a40d10147d2
I am on gcc version 11.3.1 20220712 (Arm GNU Toolchain 11.3.Rel1)
Quoted 40 lines
Therefore this patch changes the casts to a
uintptr_twhich is guaranteed to hold any pointer value, thus supressing the compiler warning.Signed-off-by Anshul Dalal <[email protected]>--- drivers/dma/ti/k3-udma.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/dma/ti/k3-udma.c +3
-3@@ -2327,7 +2327,7 @@ static int udma_send(struct dma *dma, void *src, size_t len, void *metadata) {struct udma_dev *ud =dev_get_priv (dma->dev);struct cppi5_host_desc_t *desc_tx; -dma_addr_t dma_src = (dma_addr_t )src; +dma_addr_t dma_src = (uintptr_t )src;struct ti_udma_drv_packet_data packet_data = {0 };dma_addr_t paddr;struct udma_chan *uc;@@ -2426,7 +2426,7 @@ static int udma_receive(struct dma *dma, void **dst, void *metadata)cppi5_desc_get_tags_ids (&desc_rx->hdr, &port_id,NULL ); - *dst = (void *)buf_dma; + *dst = (void *)(uintptr_t )buf_dma; uc->num_rx_bufs--;return pkt_len;@@ -2518,7 +2518,7 @@ int udma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size) desc_num = uc->desc_rx_cur %UDMA_RX_DESC_NUM ; desc_rx = uc->desc_rx + (desc_num * uc->config.hdesc_size); - dma_dst = (dma_addr_t )dst; + dma_dst = (uintptr_t )dst;cppi5_hdesc_reset_hbdesc (desc_rx);
Re: [PATCH v1 1/2] dma: ti: k3-udma: fix dma_addr_t typecasts
Quoted 6 lines · Udit Kumar
Quoted 3 lines · Anshul Dalal
With the change to
dma_addr_tfrom u32 to u64 for the R5 SPL, the existing typecasts from void* to int or vice-versa cause the compiler to throw a "cast from pointer to integer of different size".
Quoted 7 lines
is this some compiler specific ?. Please mention that
I don't see this warning
https://gist.github.com/uditkumarti/1c4238a7101d97cdc57b7a40d10147d2I am on gcc version 11.3.1 20220712 (Arm GNU Toolchain 11.3.Rel1)
Sorry for the confusing language in the commit description, this was a preventative fix, it fixes the warnings that would've been emitted if you had applied the second patch of the series by itself.
GCC 11 would still emit the warning if you apply only the second patch.
Regards,
Anshul
PATCH 2/2 config: arch: k3: enable DMA_ADDR_T_64BIT
In reply to Anshul DalalARCH_K3 encompasses both 32 and 64-bit cores on the same SoC, though the DMA addresses are always 64-bit in size.
With the current implementation, the R5 SPL uses a u32 for dma_addr_t which leads to data overflow when functions such as k3_nav_*_pop_mem try to write a 64-bit address to dma_addr_t variable.
In certain cases it leads to stack corruption which manifest as boot failures on certain compilers, such as SPI boot on GCC 14.2 or 13.3.
Therefore this patch selects CONFIG_DMA_ADDR_T_64BIT for all ARCH_K3.
--- arch/arm/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/arm/Kconfig +1 -0
@@ -833,6 +833,7 @@ config ARCH_K3 select FIT select REGEX select FIT_SIGNATURE if ARM64 + select DMA_ADDR_T_64BIT select LTO imply TI_SECURE_DEVICE imply DM_RNG if ARM64
Signature · 1 line
2.50.1