DMA Engine development
 help / color / mirror / Atom feed
* [PATCH v2 0/1] dmaengine: axi-dmac: fix incorrect segment size calculation
@ 2026-11-17  5:40 Sergey Matsievskiy
  2026-11-17  5:40 ` [PATCH v2 1/1] " Sergey Matsievskiy
  0 siblings, 1 reply; 3+ messages in thread
From: Sergey Matsievskiy @ 2026-11-17  5:40 UTC (permalink / raw)
  To: dmaengine; +Cc: lars, vkoul, Frank.Li, linux-kernel, Sergey Matsievskiy

v2:
- add reviewed-by line
v1:
Link: https://lore.kernel.org/dmaengine/20261105131547.305510-1-matsievskiysv@gmail.com/

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

-- 
2.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/1] dmaengine: axi-dmac: fix incorrect segment size calculation
  2026-11-17  5:40 [PATCH v2 0/1] dmaengine: axi-dmac: fix incorrect segment size calculation Sergey Matsievskiy
@ 2026-11-17  5:40 ` Sergey Matsievskiy
  2026-11-17  6:02   ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Sergey Matsievskiy @ 2026-11-17  5:40 UTC (permalink / raw)
  To: dmaengine
  Cc: lars, vkoul, Frank.Li, linux-kernel, Sergey Matsievskiy, Frank Li

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.

Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Sergey Matsievskiy <matsievskiysv@gmail.com>
---
 drivers/dma/dma-axi-dmac.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/dma/dma-axi-dmac.c b/drivers/dma/dma-axi-dmac.c
index dbc31b185c4d..30ea905840fe 100644
--- a/drivers/dma/dma-axi-dmac.c
+++ b/drivers/dma/dma-axi-dmac.c
@@ -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);
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/1] dmaengine: axi-dmac: fix incorrect segment size calculation
  2026-11-17  5:40 ` [PATCH v2 1/1] " Sergey Matsievskiy
@ 2026-11-17  6:02   ` sashiko-bot
  0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-17  6:02 UTC (permalink / raw)
  To: Sergey Matsievskiy, dmaengine; +Cc: vkoul, Frank.Li

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 <matsievskiysv@gmail.com>

> 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.
>
> Reviewed-by: Frank Li <Frank.Li@nxp.com>
> Signed-off-by: Sergey Matsievskiy <matsievskiysv@gmail.com>

[Severity: Low]
Should this bug fix include Fixes tags for the commits that introduced
these calculations?

  Fixes: 921234e0c5d7 ("dmaengine: axi-dmac: Split too large segments")
  Fixes: 74609e568670 ("dmaengine: dma-axi-dmac: Implement device_prep_peripheral_dma_vec")

> diff --git a/drivers/dma/dma-axi-dmac.c b/drivers/dma/dma-axi-dmac.c
> index dbc31b185c4d4..30ea905840fe2 100644
> --- a/drivers/dma/dma-axi-dmac.c
> +++ b/drivers/dma/dma-axi-dmac.c
> @@ -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.

> @@ -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);

[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[].

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117054054.22409-1-matsievskiysv@gmail.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-17  6:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  5:40 [PATCH v2 0/1] dmaengine: axi-dmac: fix incorrect segment size calculation Sergey Matsievskiy
2026-11-17  5:40 ` [PATCH v2 1/1] " Sergey Matsievskiy
2026-11-17  6:02   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox