dmaengine: axi-dmac: fix incorrect segment size calculation
Subsystems (2)
v2 cover dmaengine: axi-dmac: fix incorrect segment size calculation
v2:
- add reviewed-by line
v1:
Sergey Matsievskiy (1):
dmaengine: axi-dmac: fix incorrect segment size calculation
drivers/dma/dma-axi-dmac.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.3
PATCH v2 1/1 dmaengine: axi-dmac: fix incorrect segment size calculation
When chan->max_length has a value of 0xffffffff (which is a possible IP configuration), DIV_ROUND_UP returns 0, resulting in incorrect number of segments calculation. Fix this by using DIV_ROUND_UP_ULL division function.
--- drivers/dma/dma-axi-dmac.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/dma/dma-axi-dmac.c +3 -3
@@ -706,7 +706,7 @@ static struct axi_dmac_sg *axi_dmac_fill_linear_sg(struct axi_dmac_chan *chan,unsigned int len;/* Split into multiple equally sized segments if necessary */ - num_segments =DIV_ROUND_UP (period_len, chan->max_length); + num_segments =DIV_ROUND_UP_ULL (period_len, chan->max_length); segment_size =DIV_ROUND_UP (period_len, num_segments);/* Take care of alignment */ segment_size = ((segment_size -1 ) | chan->length_align_mask) +1 ;
@@ -752,7 +752,7 @@ axi_dmac_prep_peripheral_dma_vec(struct dma_chan *c, const struct dma_vec *vecs,return NULL ;for (i =0 ; i < nb; i++) - num_sgs +=DIV_ROUND_UP (vecs[i].len, chan->max_length); + num_sgs +=DIV_ROUND_UP_ULL (vecs[i].len, chan->max_length); desc =axi_dmac_alloc_desc (chan, num_sgs);if (!desc)
@@ -839,7 +839,7 @@ static struct dma_async_tx_descriptor *axi_dmac_prep_dma_cyclic(return NULL ; num_periods = buf_len / period_len; - num_segments =DIV_ROUND_UP (period_len, chan->max_length); + num_segments =DIV_ROUND_UP_ULL (period_len, chan->max_length); num_sgs = num_periods * num_segments; desc =axi_dmac_alloc_desc (chan, num_sgs);
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] dmaengine: axi-dmac: 32-bit overflow in axi_dmac_prep_slave_sg() - [Medium] dmaengine: axi-dmac…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] dmaengine: axi-dmac: 32-bit overflow in axi_dmac_prep_slave_sg()
- [Medium] dmaengine: axi-dmac: 32-bit overflow in axi_dmac_fill_linear_sg()
- [Low] dmaengine: axi-dmac: missing Fixes trailers in commit message --
commit f72f25e5b5e585f311e3609163b81e15ebf9fa27
Author: Sergey Matsievskiy <[email protected]>
Quoted 9 lines
dmaengine: axi-dmac: fix incorrect segment size calculation
When
chan->max_lengthhas a value of0xffffffff(which is a possible IP configuration),DIV_ROUND_UPreturns 0, resulting in incorrect number of segments calculation. Fix this by usingDIV_ROUND_UP_ULLdivision function.Reviewed-by Frank Li <[email protected]>Signed-off-by Sergey Matsievskiy <[email protected]>
[Severity: Low]
Should this bug fix include Fixes tags for the commits that introduced these calculations?
drivers/dma/dma-axi-dmac.c static struct axi_dmac_sg *axi_dmac_fill_linear_sg(struct axi_dmac_chan *chan, · line 712
Quoted 13 lines
drivers/dma/dma-axi-dmac.c +1
-1@@ -706,7 +706,7 @@ static struct axi_dmac_sg *axi_dmac_fill_linear_sg(struct axi_dmac_chan *chan,unsigned int len;/* Split into multiple equally sized segments if necessary */ - num_segments =DIV_ROUND_UP (period_len, chan->max_length); + num_segments =DIV_ROUND_UP_ULL (period_len, chan->max_length); segment_size =DIV_ROUND_UP (period_len, num_segments);/* Take care of alignment */ segment_size = ((segment_size -1 ) | chan->length_align_mask) +1 ;
[Severity: Medium]
Can segment_size still overflow 32-bit unsigned arithmetic here when chan->max_length is smaller than UINT_MAX (for example, 0x01000000 for a 24-bit X_LENGTH configuration) and period_len is within num_segments - 1 of UINT_MAX?
In that case, period_len + num_segments - 1 wraps around in 32-bit DIV_ROUND_UP(), making segment_size evaluate to 0 before and after the alignment adjustment. The loop below then subtracts 0 from len on each iteration and advances sg past the end of desc->sg[]. Using DIV_ROUND_UP_ULL(period_len, num_segments) avoids the overflow.
Quoted 10 lines
drivers/dma/dma-axi-dmac.c +1
-1@@ -752,7 +752,7 @@ axi_dmac_prep_peripheral_dma_vec(struct dma_chan *c, const struct dma_vec *vecs,return NULL ;for (i =0 ; i < nb; i++) - num_sgs +=DIV_ROUND_UP (vecs[i].len, chan->max_length); + num_sgs +=DIV_ROUND_UP_ULL (vecs[i].len, chan->max_length); desc =axi_dmac_alloc_desc (chan, num_sgs);if (!desc)
drivers/dma/dma-axi-dmac.c static struct dma_async_tx_descriptor *axi_dmac_prep_dma_cyclic( · line 845
Quoted 8 lines
drivers/dma/dma-axi-dmac.c +1
-1@@ -839,7 +839,7 @@ static struct dma_async_tx_descriptor *axi_dmac_prep_dma_cyclic(return NULL ; num_periods = buf_len / period_len; - num_segments =DIV_ROUND_UP (period_len, chan->max_length); + num_segments =DIV_ROUND_UP_ULL (period_len, chan->max_length); num_sgs = num_periods * num_segments; desc =axi_dmac_alloc_desc (chan, num_sgs);
[Severity: High]
Does axi_dmac_prep_slave_sg() still hit the same 32-bit overflow on 32-bit architectures when chan->max_length is 0xffffffff?
drivers/dma/dma-axi-dmac.c:axi_dmac_prep_slave_sg() {
...
num_sgs = sg_nents_for_dma(sgl, sg_len, chan->max_length);
desc = axi_dmac_alloc_desc(chan, num_sgs);
...
for_each_sg(sgl, sg, sg_len, i) {
...
dsg = axi_dmac_fill_linear_sg(chan, direction, sg_dma_address(sg), 1,
sg_dma_len(sg), dsg);
}
}sg_nents_for_dma() computes DIV_ROUND_UP(sg_dma_len(sg), len) with an unsigned int dividend and a size_t divisor. On 32-bit platforms with chan->max_length == 0xffffffff, that addition wraps and counts 0 segments for every scatterlist entry with length >= 2, causing axi_dmac_alloc_desc() to index desc->sg[num_sgs - 1] out of bounds when num_sgs is 0 or axi_dmac_fill_linear_sg() to write past the end of desc->sg[].
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1