DMA Engine development
 help / color / mirror / Atom feed
* [PATCH 0/4] dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling
@ 2026-11-20  9:18 Amelie Delaunay
  2026-11-20  9:18 ` [PATCH 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion Amelie Delaunay
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Amelie Delaunay @ 2026-11-20  9:18 UTC (permalink / raw)
  To: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue
  Cc: dmaengine, linux-stm32, linux-arm-kernel, linux-kernel,
	Amelie Delaunay, Jerome Audu, stable

This series fixes issues in the STM32 DMA3 driver around transfer
completion and linked-list (LL) handling, plus a related cleanup.

- Patch 1: clear channel flags before handling completion, so the
  Transfer Complete flag of a new transfer started from the completion
  path is no longer cleared, which could lead to a timeout.
- Patch 2: write the LL base address (CxLBAR) when starting a transfer
  rather than at allocation, as other transfers prepared in between could
  change it.
- Patch 3: reject linked lists spanning several 64 KiB windows, since
  CLBAR provides the common upper address bits. Lists may now fail to be
  prepared when crossing a 64KiB boundary, which is preferable to
  the hardware fetching descriptors from the wrong addresses.
- Patch 4: cleanup, derive the descriptor size from the structure and
  correct the maximum descriptor count to (CLLR_LA / size) + 1.

Patches 1 to 3 fix issues present since the driver was introduced and
are marked for stable.

Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
---
Amelie Delaunay (3):
      dmaengine: stm32-dma3: set LL base address when transfer starts
      dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup
      dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size

Jerome Audu (1):
      dmaengine: stm32-dma3: clear flags before signaling transfer completion

 drivers/dma/stm32/stm32-dma3.c | 78 ++++++++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 29 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261119-dma3-fixes-d6c3ee1dada0

Best regards,
--  
Amelie Delaunay <amelie.delaunay@foss.st.com>


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

* [PATCH 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion
  2026-11-20  9:18 [PATCH 0/4] dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling Amelie Delaunay
@ 2026-11-20  9:18 ` Amelie Delaunay
  2026-11-20 17:48   ` Frank Li
  2026-11-20  9:18 ` [PATCH 2/4] dmaengine: stm32-dma3: set LL base address when transfer starts Amelie Delaunay
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Amelie Delaunay @ 2026-11-20  9:18 UTC (permalink / raw)
  To: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue
  Cc: dmaengine, linux-stm32, linux-arm-kernel, linux-kernel,
	Amelie Delaunay, Jerome Audu, stable

From: Jerome Audu <jerome.audu@st.com>

On a Transfer Complete event, a new transfer can be started from
stm32_dma3_chan_complete(). The new transfer may finish while the end of
stm32_dma3_chan_irq() is still executing, in which case the flag clearing
done at the end of the handler also clears the new transfer's Transfer
Complete flag without the end of that transfer being handled, which may
lead to a timeout.

Clear the flags before handling completion to make sure that if
stm32_dma3_chan_complete() starts an ultra-fast transfer, its TCF flag is
not accidentally cleared. This reordering is also safe for the other
handled interrupt conditions, since csr holds the channel status and is
used to select the handling path.

Fixes: f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")
Cc: stable@vger.kernel.org
Signed-off-by: Jerome Audu <jerome.audu@st.com>
Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
---
 drivers/dma/stm32/stm32-dma3.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
index 4fe226541efa..a464e0cefd64 100644
--- a/drivers/dma/stm32/stm32-dma3.c
+++ b/drivers/dma/stm32/stm32-dma3.c
@@ -1008,7 +1008,7 @@ static irqreturn_t stm32_dma3_chan_irq(int irq, void *devid)
 {
 	struct stm32_dma3_chan *chan = devid;
 	struct stm32_dma3_ddata *ddata = to_stm32_dma3_ddata(chan);
-	u32 misr, csr, ccr;
+	u32 misr, csr, ccr, cfcr;
 
 	spin_lock(&chan->vchan.lock);
 
@@ -1021,6 +1021,14 @@ static irqreturn_t stm32_dma3_chan_irq(int irq, void *devid)
 	csr = readl_relaxed(ddata->base + STM32_DMA3_CSR(chan->id));
 	ccr = readl_relaxed(ddata->base + STM32_DMA3_CCR(chan->id)) & CCR_ALLIE;
 
+	/*
+	 * Half Transfer Interrupt may be disabled but Half Transfer Flag can be set,
+	 * ensure HTF flag to be cleared, with other flags.
+	 */
+	cfcr = csr & (ccr | CCR_HTIE);
+	if (cfcr)
+		writel_relaxed(cfcr, ddata->base + STM32_DMA3_CFCR(chan->id));
+
 	if (csr & CSR_TCF && ccr & CCR_TCIE) {
 		if (chan->swdesc->cyclic)
 			vchan_cyclic_callback(&chan->swdesc->vdesc);
@@ -1050,15 +1058,6 @@ static irqreturn_t stm32_dma3_chan_irq(int irq, void *devid)
 		stm32_dma3_chan_reset(chan);
 	}
 
-	/*
-	 * Half Transfer Interrupt may be disabled but Half Transfer Flag can be set,
-	 * ensure HTF flag to be cleared, with other flags.
-	 */
-	csr &= (ccr | CCR_HTIE);
-
-	if (csr)
-		writel_relaxed(csr, ddata->base + STM32_DMA3_CFCR(chan->id));
-
 	spin_unlock(&chan->vchan.lock);
 
 	return IRQ_HANDLED;

-- 
2.43.0


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

* [PATCH 2/4] dmaengine: stm32-dma3: set LL base address when transfer starts
  2026-11-20  9:18 [PATCH 0/4] dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling Amelie Delaunay
  2026-11-20  9:18 ` [PATCH 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion Amelie Delaunay
@ 2026-11-20  9:18 ` Amelie Delaunay
  2026-11-20 17:51   ` Frank Li
  2026-11-20  9:18 ` [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup Amelie Delaunay
  2026-11-20  9:18 ` [PATCH 4/4] dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size Amelie Delaunay
  3 siblings, 1 reply; 10+ messages in thread
From: Amelie Delaunay @ 2026-11-20  9:18 UTC (permalink / raw)
  To: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue
  Cc: dmaengine, linux-stm32, linux-arm-kernel, linux-kernel,
	Amelie Delaunay, stable

Linked-list (LL) is allocated in stm32_dma3_chan_desc_alloc(). The LL is
then used when starting the transfer in stm32_dma3_chan_start(). Nothing
prevents a user from preparing another transfer before starting the
previous one. If, for the same channel, another kind of transfer is
prepared (using dmaengine_prep_xxx API), the LL base address may change.
This address could be overwritten in the CxLBAR register if the channel is
disabled, or the CxLBAR might not be updated if the channel is enabled,
leading to a mismatch between the transfer being started and the LL pointed
to by CxLBAR register.
To avoid this, write the LL base address in CxLBAR when starting the
transfer instead of when the linked-list is allocated.

Fixes: f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")
Cc: stable@vger.kernel.org
Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
---
 drivers/dma/stm32/stm32-dma3.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
index a464e0cefd64..a056f73fbe02 100644
--- a/drivers/dma/stm32/stm32-dma3.c
+++ b/drivers/dma/stm32/stm32-dma3.c
@@ -396,7 +396,6 @@ static void stm32_dma3_chan_dump_hwdesc(struct stm32_dma3_chan *chan,
 
 static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_chan *chan, u32 count)
 {
-	struct stm32_dma3_ddata *ddata = to_stm32_dma3_ddata(chan);
 	struct stm32_dma3_swdesc *swdesc;
 	int i;
 
@@ -423,10 +422,6 @@ static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch
 	}
 	swdesc->ccr = 0;
 
-	/* Set LL base address */
-	writel_relaxed(swdesc->lli[0].hwdesc_addr & CLBAR_LBA,
-		       ddata->base + STM32_DMA3_CLBAR(chan->id));
-
 	/* Set LL allocated port */
 	swdesc->ccr &= ~CCR_LAP;
 
@@ -790,6 +785,10 @@ static void stm32_dma3_chan_start(struct stm32_dma3_chan *chan)
 	writel_relaxed(hwdesc->cdar, ddata->base + STM32_DMA3_CDAR(id));
 	writel_relaxed(hwdesc->cllr, ddata->base + STM32_DMA3_CLLR(id));
 
+	/* Set LL base address */
+	writel_relaxed(chan->swdesc->lli[0].hwdesc_addr & CLBAR_LBA,
+		       ddata->base + STM32_DMA3_CLBAR(chan->id));
+
 	/* Clear any pending interrupts */
 	csr = readl_relaxed(ddata->base + STM32_DMA3_CSR(id));
 	if (csr & CSR_ALL_F)

-- 
2.43.0


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

* [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup
  2026-11-20  9:18 [PATCH 0/4] dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling Amelie Delaunay
  2026-11-20  9:18 ` [PATCH 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion Amelie Delaunay
  2026-11-20  9:18 ` [PATCH 2/4] dmaengine: stm32-dma3: set LL base address when transfer starts Amelie Delaunay
@ 2026-11-20  9:18 ` Amelie Delaunay
  2026-11-20  9:24   ` sashiko-bot
  2026-11-20 18:02   ` Frank Li
  2026-11-20  9:18 ` [PATCH 4/4] dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size Amelie Delaunay
  3 siblings, 2 replies; 10+ messages in thread
From: Amelie Delaunay @ 2026-11-20  9:18 UTC (permalink / raw)
  To: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue
  Cc: dmaengine, linux-stm32, linux-arm-kernel, linux-kernel,
	Amelie Delaunay, stable

All descriptors in a linked list must share the same 64 KiB address
window because CLBAR provides the common upper address bits. The DMA pool
boundary only constrains individual allocations, not the whole list, so
descriptors in a single list may be allocated from different pool pages in
different windows.

Check each descriptor's base address and reject lists that span multiple
windows, instead of letting the hardware fetch descriptors from wrong
addresses. Track the last allocated entry so that the error path frees
exactly the descriptors that were allocated, including the one that
failed the window check.

While at it, simplify the ccr initialisation, which is functionnaly
equivalent to the previous code.

Fixes: f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")
Cc: stable@vger.kernel.org
Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
---
 drivers/dma/stm32/stm32-dma3.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
index a056f73fbe02..7c509a69f5e5 100644
--- a/drivers/dma/stm32/stm32-dma3.c
+++ b/drivers/dma/stm32/stm32-dma3.c
@@ -397,7 +397,8 @@ static void stm32_dma3_chan_dump_hwdesc(struct stm32_dma3_chan *chan,
 static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_chan *chan, u32 count)
 {
 	struct stm32_dma3_swdesc *swdesc;
-	int i;
+	int i, last_alloc_idx = -1;
+	dma_addr_t base_addr = 0;
 
 	/*
 	 * If the memory to be allocated for the number of hwdesc (6 u32 members but 32-bytes
@@ -419,18 +420,38 @@ static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch
 							&swdesc->lli[i].hwdesc_addr);
 		if (!swdesc->lli[i].hwdesc)
 			goto err_pool_free;
+
+		last_alloc_idx = i;
+
+		if (i == 0) {
+			base_addr = swdesc->lli[0].hwdesc_addr & CLBAR_LBA;
+			continue;
+		}
+
+		/*
+		 * dma_pool boundary=SZ_64K only guarantees that one allocation does not span a 64KB
+		 * boundary. It does not guarantee that all allocations of a given linked-list land
+		 * in the same 64KB address window required by CLBAR/CLLR addressing.
+		 */
+		if (base_addr != (swdesc->lli[i].hwdesc_addr & CLBAR_LBA)) {
+			dev_err(vchan_chan_dev(&chan->vchan), "LLI crosses a 64KB window (%pad)\n",
+				&swdesc->lli[i].hwdesc_addr);
+			goto err_pool_free;
+		}
 	}
-	swdesc->ccr = 0;
 
 	/* Set LL allocated port */
-	swdesc->ccr &= ~CCR_LAP;
+	swdesc->ccr = FIELD_PREP(CCR_LAP, 0);
 
 	return swdesc;
 
 err_pool_free:
 	dev_err(vchan_chan_dev(&chan->vchan), "Failed to alloc descriptors\n");
-	while (--i >= 0)
-		dma_pool_free(chan->lli_pool, swdesc->lli[i].hwdesc, swdesc->lli[i].hwdesc_addr);
+	while (last_alloc_idx >= 0) {
+		dma_pool_free(chan->lli_pool, swdesc->lli[last_alloc_idx].hwdesc,
+			      swdesc->lli[last_alloc_idx].hwdesc_addr);
+		last_alloc_idx--;
+	}
 	kfree(swdesc);
 
 	return NULL;

-- 
2.43.0


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

* [PATCH 4/4] dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size
  2026-11-20  9:18 [PATCH 0/4] dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling Amelie Delaunay
                   ` (2 preceding siblings ...)
  2026-11-20  9:18 ` [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup Amelie Delaunay
@ 2026-11-20  9:18 ` Amelie Delaunay
  2026-11-20 18:05   ` Frank Li
  3 siblings, 1 reply; 10+ messages in thread
From: Amelie Delaunay @ 2026-11-20  9:18 UTC (permalink / raw)
  To: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue
  Cc: dmaengine, linux-stm32, linux-arm-kernel, linux-kernel,
	Amelie Delaunay

The DMA3 driver currently hardcodes a 32-byte descriptor size when
computing the maximum linked-list size and validating the number of
descriptors to allocate.

Compute the descriptor size from the aligned size of
struct stm32_dma3_hwdesc, introduce a dedicated STM32_DMA3_MAX_DESC_COUNT
macro, and use it both for the segment size limit (STM32_DMA3_MAX_SEG_SIZE)
and for the descriptor count check (in stm32_dma3_chan_desc_alloc()).

This also corrects the maximum descriptor count: CLLR_LA holds the offset
to the next descriptor, so for a descriptor size of N bytes the last
valid descriptor is at offset (count - 1) * N, and the maximum number of
descriptors is (CLLR_LA / N) + 1.

Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
---
 drivers/dma/stm32/stm32-dma3.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
index 7c509a69f5e5..e5724c4b2c84 100644
--- a/drivers/dma/stm32/stm32-dma3.c
+++ b/drivers/dma/stm32/stm32-dma3.c
@@ -251,12 +251,14 @@ struct stm32_dma3_hwdesc {
 } __packed __aligned(32);
 
 /*
- * CLLR_LA / sizeof(struct stm32_dma3_hwdesc) represents the number of hdwdesc that can be addressed
- * by the pointer to the next linked-list data structure. The __aligned forces the 32-byte
- * alignment. So use hardcoded 32. Multiplied by the max block size of each item, it represents
- * the sg size limitation.
+ * (CLLR_LA / STM32_DMA3_DESC_SIZE) + 1 represents the number of hwdesc that can be addressed
+ * by the pointer to the next linked-list data structure.
+ * Multiplied by the max block size of each item, it represents the sg size limitation.
  */
-#define STM32_DMA3_MAX_SEG_SIZE		((CLLR_LA / 32) * STM32_DMA3_MAX_BLOCK_SIZE)
+#define STM32_DMA3_DESC_SIZE		ALIGN(sizeof(struct stm32_dma3_hwdesc), \
+					      __alignof__(struct stm32_dma3_hwdesc))
+#define STM32_DMA3_MAX_DESC_COUNT	((CLLR_LA / STM32_DMA3_DESC_SIZE) + 1)
+#define STM32_DMA3_MAX_SEG_SIZE		(STM32_DMA3_MAX_DESC_COUNT * STM32_DMA3_MAX_BLOCK_SIZE)
 
 /*
  * Linked-list items
@@ -401,11 +403,10 @@ static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch
 	dma_addr_t base_addr = 0;
 
 	/*
-	 * If the memory to be allocated for the number of hwdesc (6 u32 members but 32-bytes
-	 * aligned) is greater than the maximum address of CLLR_LA, then the last items can't be
-	 * addressed, so abort the allocation.
+	 * If the memory to be allocated for the number of hwdesc is greater than the maximum
+	 * addressable by CLLR_LA, then the last items can't be addressed, so abort the allocation.
 	 */
-	if ((count * 32) > CLLR_LA) {
+	if (count > STM32_DMA3_MAX_DESC_COUNT) {
 		dev_err(vchan_chan_dev(&chan->vchan), "Transfer is too big (> %luB)\n", STM32_DMA3_MAX_SEG_SIZE);
 		return NULL;
 	}

-- 
2.43.0


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

* Re: [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup
  2026-11-20  9:18 ` [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup Amelie Delaunay
@ 2026-11-20  9:24   ` sashiko-bot
  2026-11-20 18:02   ` Frank Li
  1 sibling, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-20  9:24 UTC (permalink / raw)
  To: Amelie Delaunay; +Cc: dmaengine, vkoul, Frank.Li

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dmaengine: stm32-dma3: LIFO free order in
  stm32_dma3_chan_desc_alloc() wedges pool
--

commit 59d36dd2cf6d4c14f9f6d6159b6dc9354dffd965
Author: Amelie Delaunay <amelie.delaunay@foss.st.com>

dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup

This commit validates that all hardware descriptors in a linked list share
the same 64 KiB address window and frees all allocated descriptors on failure.

> diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
> index a056f73fbe025..7c509a69f5e54 100644
> --- a/drivers/dma/stm32/stm32-dma3.c
> +++ b/drivers/dma/stm32/stm32-dma3.c
> @@ -397,7 +397,8 @@ static void stm32_dma3_chan_dump_hwdesc(struct stm32_dma3_chan *chan,
>  static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_chan *chan, u32 count)
>  {
>  	struct stm32_dma3_swdesc *swdesc;
> -	int i;
> +	int i, last_alloc_idx = -1;
> +	dma_addr_t base_addr = 0;
>  
>  	/*
>  	 * If the memory to be allocated for the number of hwdesc (6 u32 members but 32-bytes
> @@ -419,18 +420,38 @@ static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch
>  							&swdesc->lli[i].hwdesc_addr);
>  		if (!swdesc->lli[i].hwdesc)
>  			goto err_pool_free;
> +
> +		last_alloc_idx = i;
> +
> +		if (i == 0) {
> +			base_addr = swdesc->lli[0].hwdesc_addr & CLBAR_LBA;
> +			continue;
> +		}
> +
> +		/*
> +		 * dma_pool boundary=SZ_64K only guarantees that one allocation does not span a 64KB
> +		 * boundary. It does not guarantee that all allocations of a given linked-list land
> +		 * in the same 64KB address window required by CLBAR/CLLR addressing.
> +		 */
> +		if (base_addr != (swdesc->lli[i].hwdesc_addr & CLBAR_LBA)) {
> +			dev_err(vchan_chan_dev(&chan->vchan), "LLI crosses a 64KB window (%pad)\n",
> +				&swdesc->lli[i].hwdesc_addr);
> +			goto err_pool_free;
> +		}
>  	}
> -	swdesc->ccr = 0;
>  
>  	/* Set LL allocated port */
> -	swdesc->ccr &= ~CCR_LAP;
> +	swdesc->ccr = FIELD_PREP(CCR_LAP, 0);
>  
>  	return swdesc;
>  
>  err_pool_free:
>  	dev_err(vchan_chan_dev(&chan->vchan), "Failed to alloc descriptors\n");
> -	while (--i >= 0)
> -		dma_pool_free(chan->lli_pool, swdesc->lli[i].hwdesc, swdesc->lli[i].hwdesc_addr);
> +	while (last_alloc_idx >= 0) {
> +		dma_pool_free(chan->lli_pool, swdesc->lli[last_alloc_idx].hwdesc,
> +			      swdesc->lli[last_alloc_idx].hwdesc_addr);
> +		last_alloc_idx--;
> +	}

[Severity: Medium]
Does freeing swdesc->lli[] in reverse order restore chan->lli_pool to the
exact same state after a 64KB window mismatch?

dma_pool keeps a LIFO free list (pool->next_block in mm/dmapool.c), where
dma_pool_alloc() pops from the head and dma_pool_free() pushes back onto the
head.  When a page in chan->lli_pool runs out and a newly allocated page lands
in a different 64KB address window, freeing from last_alloc_idx down to 0
pushes the descriptors from the old window back onto the head ahead of the new
page in the exact order they were popped.  Any subsequent call to
stm32_dma3_chan_desc_alloc() with count > i will pop the same sequence of
descriptors and fail at the same boundary check forever.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dma3-fixes-v1-0-f29c1f437399@foss.st.com?part=3

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

* Re: [PATCH 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion
  2026-11-20  9:18 ` [PATCH 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion Amelie Delaunay
@ 2026-11-20 17:48   ` Frank Li
  0 siblings, 0 replies; 10+ messages in thread
From: Frank Li @ 2026-11-20 17:48 UTC (permalink / raw)
  To: Amelie Delaunay
  Cc: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue,
	dmaengine, linux-stm32, linux-arm-kernel, linux-kernel,
	Jerome Audu, stable

On Fri, Nov 20, 2026 at 10:18:01AM +0100, Amelie Delaunay wrote:
> From: Jerome Audu <jerome.audu@st.com>
>
> On a Transfer Complete event, a new transfer can be started from
> stm32_dma3_chan_complete(). The new transfer may finish while the end of
> stm32_dma3_chan_irq() is still executing, in which case the flag clearing
> done at the end of the handler also clears the new transfer's Transfer
> Complete flag without the end of that transfer being handled, which may
> lead to a timeout.
>
> Clear the flags before handling completion to make sure that if
> stm32_dma3_chan_complete() starts an ultra-fast transfer, its TCF flag is
> not accidentally cleared. This reordering is also safe for the other
> handled interrupt conditions, since csr holds the channel status and is
> used to select the handling path.

Can you rephrase commit message. clean irq flags should just after
get status before do actually handle. Actual handle may cause new irqs.

It is quite common pattern for irq handler.  Some thing like

Move clear irq state just after fetch state to avoid lost irq because
actual handle work (i.e stm32_dma3_chan_complete()) may issue new irq.

Frank

>
> Fixes: f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jerome Audu <jerome.audu@st.com>
> Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
> ---
>  drivers/dma/stm32/stm32-dma3.c | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
> index 4fe226541efa..a464e0cefd64 100644
> --- a/drivers/dma/stm32/stm32-dma3.c
> +++ b/drivers/dma/stm32/stm32-dma3.c
> @@ -1008,7 +1008,7 @@ static irqreturn_t stm32_dma3_chan_irq(int irq, void *devid)
>  {
>  	struct stm32_dma3_chan *chan = devid;
>  	struct stm32_dma3_ddata *ddata = to_stm32_dma3_ddata(chan);
> -	u32 misr, csr, ccr;
> +	u32 misr, csr, ccr, cfcr;
>
>  	spin_lock(&chan->vchan.lock);
>
> @@ -1021,6 +1021,14 @@ static irqreturn_t stm32_dma3_chan_irq(int irq, void *devid)
>  	csr = readl_relaxed(ddata->base + STM32_DMA3_CSR(chan->id));
>  	ccr = readl_relaxed(ddata->base + STM32_DMA3_CCR(chan->id)) & CCR_ALLIE;
>
> +	/*
> +	 * Half Transfer Interrupt may be disabled but Half Transfer Flag can be set,
> +	 * ensure HTF flag to be cleared, with other flags.
> +	 */
> +	cfcr = csr & (ccr | CCR_HTIE);
> +	if (cfcr)
> +		writel_relaxed(cfcr, ddata->base + STM32_DMA3_CFCR(chan->id));
> +
>  	if (csr & CSR_TCF && ccr & CCR_TCIE) {
>  		if (chan->swdesc->cyclic)
>  			vchan_cyclic_callback(&chan->swdesc->vdesc);
> @@ -1050,15 +1058,6 @@ static irqreturn_t stm32_dma3_chan_irq(int irq, void *devid)
>  		stm32_dma3_chan_reset(chan);
>  	}
>
> -	/*
> -	 * Half Transfer Interrupt may be disabled but Half Transfer Flag can be set,
> -	 * ensure HTF flag to be cleared, with other flags.
> -	 */
> -	csr &= (ccr | CCR_HTIE);
> -
> -	if (csr)
> -		writel_relaxed(csr, ddata->base + STM32_DMA3_CFCR(chan->id));
> -
>  	spin_unlock(&chan->vchan.lock);
>
>  	return IRQ_HANDLED;
>
> --
> 2.43.0
>

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

* Re: [PATCH 2/4] dmaengine: stm32-dma3: set LL base address when transfer starts
  2026-11-20  9:18 ` [PATCH 2/4] dmaengine: stm32-dma3: set LL base address when transfer starts Amelie Delaunay
@ 2026-11-20 17:51   ` Frank Li
  0 siblings, 0 replies; 10+ messages in thread
From: Frank Li @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Amelie Delaunay
  Cc: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue,
	dmaengine, linux-stm32, linux-arm-kernel, linux-kernel, stable

On Fri, Nov 20, 2026 at 10:18:02AM +0100, Amelie Delaunay wrote:
> Linked-list (LL) is allocated in stm32_dma3_chan_desc_alloc(). The LL is
> then used when starting the transfer in stm32_dma3_chan_start(). Nothing
> prevents a user from preparing another transfer before starting the
> previous one. If, for the same channel, another kind of transfer is
> prepared (using dmaengine_prep_xxx API), the LL base address may change.
> This address could be overwritten in the CxLBAR register if the channel is
> disabled, or the CxLBAR might not be updated if the channel is enabled,
> leading to a mismatch between the transfer being started and the LL pointed
> to by CxLBAR register.

Nit: Add emtpy line between parapragh

> To avoid this, write the LL base address in CxLBAR when starting the
> transfer instead of when the linked-list is allocated.
>
> Fixes: f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
> ---

Reviewed-by: Frank Li <Frank.Li@nxp.com>

>  drivers/dma/stm32/stm32-dma3.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
> index a464e0cefd64..a056f73fbe02 100644
> --- a/drivers/dma/stm32/stm32-dma3.c
> +++ b/drivers/dma/stm32/stm32-dma3.c
> @@ -396,7 +396,6 @@ static void stm32_dma3_chan_dump_hwdesc(struct stm32_dma3_chan *chan,
>
>  static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_chan *chan, u32 count)
>  {
> -	struct stm32_dma3_ddata *ddata = to_stm32_dma3_ddata(chan);
>  	struct stm32_dma3_swdesc *swdesc;
>  	int i;
>
> @@ -423,10 +422,6 @@ static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch
>  	}
>  	swdesc->ccr = 0;
>
> -	/* Set LL base address */
> -	writel_relaxed(swdesc->lli[0].hwdesc_addr & CLBAR_LBA,
> -		       ddata->base + STM32_DMA3_CLBAR(chan->id));
> -
>  	/* Set LL allocated port */
>  	swdesc->ccr &= ~CCR_LAP;
>
> @@ -790,6 +785,10 @@ static void stm32_dma3_chan_start(struct stm32_dma3_chan *chan)
>  	writel_relaxed(hwdesc->cdar, ddata->base + STM32_DMA3_CDAR(id));
>  	writel_relaxed(hwdesc->cllr, ddata->base + STM32_DMA3_CLLR(id));
>
> +	/* Set LL base address */
> +	writel_relaxed(chan->swdesc->lli[0].hwdesc_addr & CLBAR_LBA,
> +		       ddata->base + STM32_DMA3_CLBAR(chan->id));
> +
>  	/* Clear any pending interrupts */
>  	csr = readl_relaxed(ddata->base + STM32_DMA3_CSR(id));
>  	if (csr & CSR_ALL_F)
>
> --
> 2.43.0
>

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

* Re: [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup
  2026-11-20  9:18 ` [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup Amelie Delaunay
  2026-11-20  9:24   ` sashiko-bot
@ 2026-11-20 18:02   ` Frank Li
  1 sibling, 0 replies; 10+ messages in thread
From: Frank Li @ 2026-11-20 18:02 UTC (permalink / raw)
  To: Amelie Delaunay
  Cc: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue,
	dmaengine, linux-stm32, linux-arm-kernel, linux-kernel, stable

On Fri, Nov 20, 2026 at 10:18:03AM +0100, Amelie Delaunay wrote:
> All descriptors in a linked list must share the same 64 KiB address
> window because CLBAR provides the common upper address bits. The DMA pool
> boundary only constrains individual allocations, not the whole list, so
> descriptors in a single list may be allocated from different pool pages in
> different windows.

/**
 * dmam_pool_create - Managed dma_pool_create()
 * @name: name of pool, for diagnostics
 * @dev: device that will be doing the DMA
 * @size: size of the blocks in this pool.
 * @align: alignment requirement for blocks; must be a power of two
 * @allocation: returned blocks won't cross this boundary (or zero)

suppose you can set allocation to 64K to avoid cross 64K boundary.

 *
 * Managed dma_pool_create().  DMA pool created with this function is
 * automatically destroyed on driver detach.
 *
 * Return: a managed dma allocation pool with the requested
 * characteristics, or %NULL if one can't be created.
 */
struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
				  size_t size, size_t align, size_t allocation)


Frank

>
> Check each descriptor's base address and reject lists that span multiple
> windows, instead of letting the hardware fetch descriptors from wrong
> addresses. Track the last allocated entry so that the error path frees
> exactly the descriptors that were allocated, including the one that
> failed the window check.
>
> While at it, simplify the ccr initialisation, which is functionnaly
> equivalent to the previous code.
>
> Fixes: f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
> ---
>  drivers/dma/stm32/stm32-dma3.c | 31 ++++++++++++++++++++++++++-----
>  1 file changed, 26 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
> index a056f73fbe02..7c509a69f5e5 100644
> --- a/drivers/dma/stm32/stm32-dma3.c
> +++ b/drivers/dma/stm32/stm32-dma3.c
> @@ -397,7 +397,8 @@ static void stm32_dma3_chan_dump_hwdesc(struct stm32_dma3_chan *chan,
>  static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_chan *chan, u32 count)
>  {
>  	struct stm32_dma3_swdesc *swdesc;
> -	int i;
> +	int i, last_alloc_idx = -1;
> +	dma_addr_t base_addr = 0;
>
>  	/*
>  	 * If the memory to be allocated for the number of hwdesc (6 u32 members but 32-bytes
> @@ -419,18 +420,38 @@ static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch
>  							&swdesc->lli[i].hwdesc_addr);
>  		if (!swdesc->lli[i].hwdesc)
>  			goto err_pool_free;
> +
> +		last_alloc_idx = i;
> +
> +		if (i == 0) {
> +			base_addr = swdesc->lli[0].hwdesc_addr & CLBAR_LBA;
> +			continue;
> +		}
> +
> +		/*
> +		 * dma_pool boundary=SZ_64K only guarantees that one allocation does not span a 64KB
> +		 * boundary. It does not guarantee that all allocations of a given linked-list land
> +		 * in the same 64KB address window required by CLBAR/CLLR addressing.
> +		 */
> +		if (base_addr != (swdesc->lli[i].hwdesc_addr & CLBAR_LBA)) {
> +			dev_err(vchan_chan_dev(&chan->vchan), "LLI crosses a 64KB window (%pad)\n",
> +				&swdesc->lli[i].hwdesc_addr);
> +			goto err_pool_free;
> +		}
>  	}
> -	swdesc->ccr = 0;
>
>  	/* Set LL allocated port */
> -	swdesc->ccr &= ~CCR_LAP;
> +	swdesc->ccr = FIELD_PREP(CCR_LAP, 0);
>
>  	return swdesc;
>
>  err_pool_free:
>  	dev_err(vchan_chan_dev(&chan->vchan), "Failed to alloc descriptors\n");
> -	while (--i >= 0)
> -		dma_pool_free(chan->lli_pool, swdesc->lli[i].hwdesc, swdesc->lli[i].hwdesc_addr);
> +	while (last_alloc_idx >= 0) {
> +		dma_pool_free(chan->lli_pool, swdesc->lli[last_alloc_idx].hwdesc,
> +			      swdesc->lli[last_alloc_idx].hwdesc_addr);
> +		last_alloc_idx--;
> +	}
>  	kfree(swdesc);
>
>  	return NULL;
>
> --
> 2.43.0
>

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

* Re: [PATCH 4/4] dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size
  2026-11-20  9:18 ` [PATCH 4/4] dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size Amelie Delaunay
@ 2026-11-20 18:05   ` Frank Li
  0 siblings, 0 replies; 10+ messages in thread
From: Frank Li @ 2026-11-20 18:05 UTC (permalink / raw)
  To: Amelie Delaunay
  Cc: Vinod Koul, Frank Li, Maxime Coquelin, Alexandre Torgue,
	dmaengine, linux-stm32, linux-arm-kernel, linux-kernel

On Fri, Nov 20, 2026 at 10:18:04AM +0100, Amelie Delaunay wrote:
> The DMA3 driver currently hardcodes a 32-byte descriptor size when
> computing the maximum linked-list size and validating the number of
> descriptors to allocate.
>
> Compute the descriptor size from the aligned size of
> struct stm32_dma3_hwdesc, introduce a dedicated STM32_DMA3_MAX_DESC_COUNT
> macro, and use it both for the segment size limit (STM32_DMA3_MAX_SEG_SIZE)
> and for the descriptor count check (in stm32_dma3_chan_desc_alloc()).
>
> This also corrects the maximum descriptor count: CLLR_LA holds the offset
> to the next descriptor, so for a descriptor size of N bytes the last
> valid descriptor is at offset (count - 1) * N, and the maximum number of
> descriptors is (CLLR_LA / N) + 1.
>
> Signed-off-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
> ---

Reviewed-by: Frank Li <Frank.Li@nxp.com>

>  drivers/dma/stm32/stm32-dma3.c | 19 ++++++++++---------
>  1 file changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
> index 7c509a69f5e5..e5724c4b2c84 100644
> --- a/drivers/dma/stm32/stm32-dma3.c
> +++ b/drivers/dma/stm32/stm32-dma3.c
> @@ -251,12 +251,14 @@ struct stm32_dma3_hwdesc {
>  } __packed __aligned(32);
>
>  /*
> - * CLLR_LA / sizeof(struct stm32_dma3_hwdesc) represents the number of hdwdesc that can be addressed
> - * by the pointer to the next linked-list data structure. The __aligned forces the 32-byte
> - * alignment. So use hardcoded 32. Multiplied by the max block size of each item, it represents
> - * the sg size limitation.
> + * (CLLR_LA / STM32_DMA3_DESC_SIZE) + 1 represents the number of hwdesc that can be addressed
> + * by the pointer to the next linked-list data structure.
> + * Multiplied by the max block size of each item, it represents the sg size limitation.
>   */
> -#define STM32_DMA3_MAX_SEG_SIZE		((CLLR_LA / 32) * STM32_DMA3_MAX_BLOCK_SIZE)
> +#define STM32_DMA3_DESC_SIZE		ALIGN(sizeof(struct stm32_dma3_hwdesc), \
> +					      __alignof__(struct stm32_dma3_hwdesc))
> +#define STM32_DMA3_MAX_DESC_COUNT	((CLLR_LA / STM32_DMA3_DESC_SIZE) + 1)
> +#define STM32_DMA3_MAX_SEG_SIZE		(STM32_DMA3_MAX_DESC_COUNT * STM32_DMA3_MAX_BLOCK_SIZE)
>
>  /*
>   * Linked-list items
> @@ -401,11 +403,10 @@ static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch
>  	dma_addr_t base_addr = 0;
>
>  	/*
> -	 * If the memory to be allocated for the number of hwdesc (6 u32 members but 32-bytes
> -	 * aligned) is greater than the maximum address of CLLR_LA, then the last items can't be
> -	 * addressed, so abort the allocation.
> +	 * If the memory to be allocated for the number of hwdesc is greater than the maximum
> +	 * addressable by CLLR_LA, then the last items can't be addressed, so abort the allocation.
>  	 */
> -	if ((count * 32) > CLLR_LA) {
> +	if (count > STM32_DMA3_MAX_DESC_COUNT) {
>  		dev_err(vchan_chan_dev(&chan->vchan), "Transfer is too big (> %luB)\n", STM32_DMA3_MAX_SEG_SIZE);
>  		return NULL;
>  	}
>
> --
> 2.43.0
>

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

end of thread, other threads:[~2026-11-20 18:06 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  9:18 [PATCH 0/4] dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling Amelie Delaunay
2026-11-20  9:18 ` [PATCH 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion Amelie Delaunay
2026-11-20 17:48   ` Frank Li
2026-11-20  9:18 ` [PATCH 2/4] dmaengine: stm32-dma3: set LL base address when transfer starts Amelie Delaunay
2026-11-20 17:51   ` Frank Li
2026-11-20  9:18 ` [PATCH 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup Amelie Delaunay
2026-11-20  9:24   ` sashiko-bot
2026-11-20 18:02   ` Frank Li
2026-11-20  9:18 ` [PATCH 4/4] dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size Amelie Delaunay
2026-11-20 18:05   ` Frank Li

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