dmaengine: loongson: loongson2-apb: fix broken bus width validation in ls2x_dmac_detect_burst()
Subsystems (2)
PATCH 1/1 dmaengine: loongson: loongson2-apb: fix broken bus width validation in ls2x_dmac_detect_burst()
The bus width validation check in ls2x_dmac_detect_burst() compares raw enum dma_slave_buswidth values (e.g. 4, 8) directly against LDMA_SLAVE_BUSWIDTHS, which is a BIT()-encoded bitmask (BIT(4) | BIT(8) = 0x110). Since 4 & 0x110 == 0 and 8 & 0x110 == 0, the condition is always false for valid bus widths, making the validation dead code.
Additionally, the logic was inverted: it rejected configurations where both widths matched valid values, rather than rejecting when neither width is supported.
Fix by wrapping the enum values with BIT() before masking (matching the pattern used in sun6i-dma.c) and inverting the logic to reject when neither width is supported by the hardware.
--- drivers/dma/loongson/loongson2-apb-dma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/loongson/loongson2-apb-dma.c +2 -2
@@ -220,8 +220,8 @@ static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)u32 maxburst, buswidth;/* Reject definitely invalid configurations */ -if ((lchan->sconfig.src_addr_width &LDMA_SLAVE_BUSWIDTHS ) && - (lchan->sconfig.dst_addr_width &LDMA_SLAVE_BUSWIDTHS )) +if (!(BIT (lchan->sconfig.src_addr_width) &LDMA_SLAVE_BUSWIDTHS ) && + !(BIT (lchan->sconfig.dst_addr_width) &LDMA_SLAVE_BUSWIDTHS ))return 0 ;if (lchan->sconfig.direction ==DMA_MEM_TO_DEV ) {
Signature · 1 line
2.53.0
Hi David:
Quoted 11 lines · David Carlier
The bus width validation check in
ls2x_dmac_detect_burst()compares raw enumdma_slave_buswidthvalues (e.g. 4, 8) directly againstLDMA_SLAVE_BUSWIDTHS, which is a BIT()-encoded bitmask (BIT(4) | BIT(8) =0x110). Since 4 &0x110== 0 and 8 &0x110== 0, the condition is always false for valid bus widths, making the validation dead code.Additionally, the logic was inverted: it rejected configurations where both widths matched valid values, rather than rejecting when neither width is supported.
Quoted 6 lines
Fix by wrapping the enum values with
BIT()before masking (matching the pattern used insun6i-dma.c) and inverting the logic to reject when neither width is supported by the hardware.Fixes 71e7d3cb6e55 ("dmaengine: ls2x-apb: New driver for the Loongson LS2X APB DMA controller")Signed-off-by David Carlier <[email protected]>
That was indeed my oversight. Thanks a lot!
Quoted 9 lines
--- drivers/dma/loongson/loongson2-apb-dma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/dma/loongson/loongson2-apb-dma.c +0
-0
drivers/dma/loongson/loongson2-apb-dma.c static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan) · line 227
Quoted 10 lines
drivers/dma/loongson/loongson2-apb-dma.c +2
-2@@ -220,8 +220,8 @@ static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)u32 maxburst, buswidth;/* Reject definitely invalid configurations */ -if ((lchan->sconfig.src_addr_width &LDMA_SLAVE_BUSWIDTHS ) && - (lchan->sconfig.dst_addr_width &LDMA_SLAVE_BUSWIDTHS )) +if (!(BIT (lchan->sconfig.src_addr_width) &LDMA_SLAVE_BUSWIDTHS ) && + !(BIT (lchan->sconfig.dst_addr_width) &LDMA_SLAVE_BUSWIDTHS ))return 0 ;if (lchan->sconfig.direction ==DMA_MEM_TO_DEV ) {
Thanks.
Binbin
Quoted 26 lines · David Carlier
The bus width validation check in
ls2x_dmac_detect_burst()compares raw enumdma_slave_buswidthvalues (e.g. 4, 8) directly againstLDMA_SLAVE_BUSWIDTHS, which is a BIT()-encoded bitmask (BIT(4) | BIT(8) =0x110). Since 4 &0x110== 0 and 8 &0x110== 0, the condition is always false for valid bus widths, making the validation dead code.Additionally, the logic was inverted: it rejected configurations where both widths matched valid values, rather than rejecting when neither width is supported.
Fix by wrapping the enum values with
BIT()before masking (matching the pattern used insun6i-dma.c) and inverting the logic to reject when neither width is supported by the hardware.Fixes 71e7d3cb6e55 ("dmaengine: ls2x-apb: New driver for the Loongson LS2X APB DMA controller")Signed-off-by David Carlier <[email protected]>--- drivers/dma/loongson/loongson2-apb-dma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/dma/loongson/loongson2-apb-dma.c +0
-0
drivers/dma/loongson/loongson2-apb-dma.c static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan) · line 224
Quoted 7 lines
drivers/dma/loongson/loongson2-apb-dma.c +2
-2@@ -220,8 +220,8 @@ static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)u32 maxburst, buswidth;/* Reject definitely invalid configurations */ -if ((lchan->sconfig.src_addr_width &LDMA_SLAVE_BUSWIDTHS ) && - (lchan->sconfig.dst_addr_width &LDMA_SLAVE_BUSWIDTHS )) +if (!(BIT (lchan->sconfig.src_addr_width) &LDMA_SLAVE_BUSWIDTHS ) && + !(BIT (lchan->sconfig.dst_addr_width) &LDMA_SLAVE_BUSWIDTHS ))
src_addr_width is enum dma_slave_buswidth, which allow DMA_SLAVE_BUSWIDTH_128_BYTES = 128,
BIT(128) will overflow.
Frank
Quoted 6 lines
drivers/dma/loongson/loongson2-apb-dma.c +0
-1return 0 ;if (lchan->sconfig.direction ==DMA_MEM_TO_DEV ) { --2.53.0
Hi,
Quoted 39 lines · Frank Li
Quoted 26 lines · David Carlier
The bus width validation check in
ls2x_dmac_detect_burst()compares raw enumdma_slave_buswidthvalues (e.g. 4, 8) directly againstLDMA_SLAVE_BUSWIDTHS, which is a BIT()-encoded bitmask (BIT(4) | BIT(8) =0x110). Since 4 &0x110== 0 and 8 &0x110== 0, the condition is always false for valid bus widths, making the validation dead code.Additionally, the logic was inverted: it rejected configurations where both widths matched valid values, rather than rejecting when neither width is supported.
Fix by wrapping the enum values with
BIT()before masking (matching the pattern used insun6i-dma.c) and inverting the logic to reject when neither width is supported by the hardware.Fixes 71e7d3cb6e55 ("dmaengine: ls2x-apb: New driver for the Loongson LS2X APB DMA controller")Signed-off-by David Carlier <[email protected]>--- drivers/dma/loongson/loongson2-apb-dma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/dma/loongson/loongson2-apb-dma.c +0
-0drivers/dma/loongson/loongson2-apb-dma.c static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan) · line 224
Quoted 7 lines
drivers/dma/loongson/loongson2-apb-dma.c +2
-2@@ -220,8 +220,8 @@ static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)u32 maxburst, buswidth;/* Reject definitely invalid configurations */ -if ((lchan->sconfig.src_addr_width &LDMA_SLAVE_BUSWIDTHS ) && - (lchan->sconfig.dst_addr_width &LDMA_SLAVE_BUSWIDTHS )) +if (!(BIT (lchan->sconfig.src_addr_width) &LDMA_SLAVE_BUSWIDTHS ) && + !(BIT (lchan->sconfig.dst_addr_width) &LDMA_SLAVE_BUSWIDTHS ))
src_addr_widthis enumdma_slave_buswidth, which allowDMA_SLAVE_BUSWIDTH_128_BYTES= 128,
Quoted 1 line
BIT(128) will overflow.
Thanks for the review Frank. You're right that BIT() would overflow for DMA_SLAVE_BUSWIDTH_128_BYTES. While this driver only supports 4-byte and 8-byte widths
today, relying on BIT() for buswidth validation is fragile. I'll
send a v2 that avoids BIT() altogether — would a direct comparison against the supported widths work
for you, or do you have a preferred pattern in mind?
Quoted 9 lines
Frank
Quoted 6 lines
drivers/dma/loongson/loongson2-apb-dma.c +0
-1return 0 ;if (lchan->sconfig.direction ==DMA_MEM_TO_DEV ) { --2.53.0
Quoted 44 lines · David CARLIER
Hi,
Quoted 39 lines · Frank Li
Quoted 26 lines · David Carlier
The bus width validation check in
ls2x_dmac_detect_burst()compares raw enumdma_slave_buswidthvalues (e.g. 4, 8) directly againstLDMA_SLAVE_BUSWIDTHS, which is a BIT()-encoded bitmask (BIT(4) | BIT(8) =0x110). Since 4 &0x110== 0 and 8 &0x110== 0, the condition is always false for valid bus widths, making the validation dead code.Additionally, the logic was inverted: it rejected configurations where both widths matched valid values, rather than rejecting when neither width is supported.
Fix by wrapping the enum values with
BIT()before masking (matching the pattern used insun6i-dma.c) and inverting the logic to reject when neither width is supported by the hardware.Fixes 71e7d3cb6e55 ("dmaengine: ls2x-apb: New driver for the Loongson LS2X APB DMA controller")Signed-off-by David Carlier <[email protected]>--- drivers/dma/loongson/loongson2-apb-dma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/dma/loongson/loongson2-apb-dma.c +0
-0drivers/dma/loongson/loongson2-apb-dma.c static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan) · line 224
Quoted 7 lines
drivers/dma/loongson/loongson2-apb-dma.c +2
-2@@ -220,8 +220,8 @@ static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)u32 maxburst, buswidth;/* Reject definitely invalid configurations */ -if ((lchan->sconfig.src_addr_width &LDMA_SLAVE_BUSWIDTHS ) && - (lchan->sconfig.dst_addr_width &LDMA_SLAVE_BUSWIDTHS )) +if (!(BIT (lchan->sconfig.src_addr_width) &LDMA_SLAVE_BUSWIDTHS ) && + !(BIT (lchan->sconfig.dst_addr_width) &LDMA_SLAVE_BUSWIDTHS ))
src_addr_widthis enumdma_slave_buswidth, which allowDMA_SLAVE_BUSWIDTH_128_BYTES= 128,Quoted 1 line
BIT(128) will overflow.
Quoted 7 lines
Thanks for the review Frank. You're right that
BIT()would overflow forDMA_SLAVE_BUSWIDTH_128_BYTES. While this driver only supports 4-byte and 8-byte widths
today, relying onBIT()for buswidth validation is fragile. I'll
send a v2 that avoidsBIT()altogether — would a direct comparison against the supported widths work
for you, or do you have a preferred pattern in mind?
I think LDMA_SLAVE_BUSWIDTHS use enum dma_slave_buswidth OR together.
Frank
Quoted 10 lines
Quoted 10 lines
Frank
Quoted 6 lines
drivers/dma/loongson/loongson2-apb-dma.c +0
-1return 0 ;if (lchan->sconfig.direction ==DMA_MEM_TO_DEV ) { --2.53.0
PATCH v2 1/1 dmaengine: loongson: loongson2-apb: fix broken bus width validation in ls2x_dmac_detect_burst()
In reply to Frank LiThe bus width validation check in ls2x_dmac_detect_burst() compares raw enum dma_slave_buswidth values (e.g. 4, 8) directly against LDMA_SLAVE_BUSWIDTHS, which is a BIT()-encoded bitmask (BIT(4) | BIT(8) = 0x110). Since 4 & 0x110 == 0 and 8 & 0x110 == 0, the condition is always false for valid bus widths, making the validation dead code.
Additionally, the logic was inverted: it rejected configurations where both widths matched valid values, rather than rejecting when neither width is supported.
Fix by comparing directly against the supported enum values, avoiding BIT() which would overflow for large dma_slave_buswidth values like DMA_SLAVE_BUSWIDTH_128_BYTES. Invert the logic to reject when neither width is supported by the hardware.
--- drivers/dma/loongson/loongson2-apb-dma.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/dma/loongson/loongson2-apb-dma.c +4 -2
@@ -220,8 +220,10 @@ static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)u32 maxburst, buswidth;/* Reject definitely invalid configurations */ -if ((lchan->sconfig.src_addr_width &LDMA_SLAVE_BUSWIDTHS ) && - (lchan->sconfig.dst_addr_width &LDMA_SLAVE_BUSWIDTHS )) +if (lchan->sconfig.src_addr_width !=DMA_SLAVE_BUSWIDTH_4_BYTES && + lchan->sconfig.src_addr_width !=DMA_SLAVE_BUSWIDTH_8_BYTES && + lchan->sconfig.dst_addr_width !=DMA_SLAVE_BUSWIDTH_4_BYTES && + lchan->sconfig.dst_addr_width !=DMA_SLAVE_BUSWIDTH_8_BYTES )return 0 ;if (lchan->sconfig.direction ==DMA_MEM_TO_DEV ) {
Signature · 1 line
2.53.0