dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling
Subsystems (4)
Amelie Delaunay
maintainerv2 cover dmaengine: stm32-dma3: fix IRQ flag race and linked-list handling
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.
---
Changes in v2:
- Address Frank Li's feedback: rewrite Patch 1 description more clearly and add a blank line to separate paragraph in Patch 2 description.
- Link to v1: https://patch.msgid.link/[email protected]
---
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,
Signature · 1 line
Amelie Delaunay <[email protected]>
Amelie Delaunay
maintainerPATCH v2 1/4 dmaengine: stm32-dma3: clear flags before signaling transfer completion
From: Jerome Audu <[email protected]>
The interrupt handler clears channel flags after handling the captured status. However, stm32_dma3_chan_complete() can start another transfer. If that transfer completes before the flags are cleared, its Transfer Complete flag is cleared without handling its completion, potentially causing a timeout.
Clear the captured interrupt flags immediately after reading the channel status and interrupt enables, before handling the events. This prevents the handler from clearing a completion flag raised by a newly started transfer.
---
Changes in v2:
- Commit message rephrased according to Frank Li's review
--- drivers/dma/stm32/stm32-dma3.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/dma/stm32/stm32-dma3.c +9 -10
@@ -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 ;
Signature · 1 line
2.43.0
Re: [PATCH v2 1/4] dmaengine: stm32-dma3: clear flags before signaling transfer completion
Quoted 13 lines · Amelie Delaunay
From: Jerome Audu <[email protected]>
The interrupt handler clears channel flags after handling the captured status. However,
stm32_dma3_chan_complete()can start another transfer. If that transfer completes before the flags are cleared, its Transfer Complete flag is cleared without handling its completion, potentially causing a timeout.Clear the captured interrupt flags immediately after reading the channel status and interrupt enables, before handling the events. This prevents the handler from clearing a completion flag raised by a newly started transfer.
Quoted 4 lines
Fixes f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")Signed-off-by Jerome Audu <[email protected]>Signed-off-by Amelie Delaunay <[email protected]>
Quoted 56 lines
---
Changes in v2:
- Commit message rephrased according to Frank Li's review--- drivers/dma/stm32/stm32-dma3.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)drivers/dma/stm32/stm32-dma3.c +9
-11@@ -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
Amelie Delaunay
maintainerPATCH v2 2/4 dmaengine: stm32-dma3: set LL base address when transfer starts
In reply to Amelie DelaunayLinked-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.
---
Changes in v2:
- emtpy line added emtpy between parapragh following Frank Li's review
--- drivers/dma/stm32/stm32-dma3.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/dma/stm32/stm32-dma3.c +4 -5
@@ -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 )
Signature · 1 line
2.43.0
Amelie Delaunay
maintainerPATCH v2 3/4 dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup
In reply to Amelie DelaunayAll 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.
--- drivers/dma/stm32/stm32-dma3.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
drivers/dma/stm32/stm32-dma3.c +26 -5
@@ -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 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 3/4] dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dmaengine: stm32-dma3: LIFO free in stm32_dma3_chan_desc_alloc() wedges pool --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dmaengine: stm32-dma3: LIFO free in stm32_dma3_chan_desc_alloc() wedges pool
--
commit b1f0a54731f5c1a2b29b4e522069ca1bedc6096e
Author: Amelie Delaunay <[email protected]>
dmaengine: stm32-dma3: validate LLI pool allocations and fix cleanup
This commit checks that all LLI descriptors in a list share the same 64 KiB CLBAR address window and frees every allocated descriptor on error.
Quoted 47 lines
drivers/dma/stm32/stm32-dma3.c +21
-3@@ -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" );
drivers/dma/stm32/stm32-dma3.c static struct stm32_dma3_swdesc *stm32_dma3_chan_desc_alloc(struct stm32_dma3_ch · line 457
Quoted 10 lines
drivers/dma/stm32/stm32-dma3.c +5
-2-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 ;
[Severity: Medium]
Can freeing descriptors in reverse order wedge chan->lli_pool after a 64 KiB window mismatch?
dma_pool keeps free blocks on a LIFO stack (pool->next_block in mm/dmapool.c), and each backing pool page holds 4 KiB (128 descriptors) unless a larger pool page is allocated. When descriptors 0..k-1 come from one 64 KiB window and descriptor k comes from a newly allocated pool page in another window, freeing from last_alloc_idx (k) down to 0 pushes descriptor k and then descriptors k-1..0 back onto the head of pool->next_block in the exact order they were popped. Every subsequent call to stm32_dma3_chan_desc_alloc() with count > k will pop the same k + 1 descriptors and fail at the window check until smaller allocations drain the k blocks at the head.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Amelie Delaunay
maintainerPATCH v2 4/4 dmaengine: stm32-dma3: replace hardcoded LLI size with aligned descriptor size
In reply to Amelie DelaunayThe 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.
--- drivers/dma/stm32/stm32-dma3.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/dma/stm32/stm32-dma3.c +10 -9
@@ -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_chdma_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 ; }
Signature · 1 line
2.43.0