* [PATCH] mtd: rawnand: stm32_fmc2: retire data DMA before unmapping on errors
@ 2026-10-30 15:18 Samarth Narang
2026-10-30 15:30 ` Miquel Raynal
0 siblings, 1 reply; 2+ messages in thread
From: Samarth Narang @ 2026-10-30 15:18 UTC (permalink / raw)
To: Miquel Raynal, Vignesh Raghavendra, Maxime Coquelin,
Alexandre Torgue
Cc: Richard Weinberger, linux-mtd, linux-stm32, linux-arm-kernel,
linux-kernel, Samarth Narang
When ECC descriptor preparation or submission fails in
stm32_fmc2_nfc_xfer(), the driver unmaps the data scatterlist without
terminating the previously issued DMA descriptor. The descriptor may
remain queued and execute when the next page operation starts,
potentially writing into a buffer that has already been unmapped and
returned to the NAND core. The timeout paths have a similar issue, as
they use dmaengine_terminate_all(), which does not guarantee that DMA
has stopped before the buffer is unmapped.
Terminate the outstanding data descriptor on ECC error paths and use
dmaengine_terminate_sync() on timeout paths, ensuring that DMA has
stopped before the associated mapping is released.
Fixes: 2cd457f328c1 ("mtd: rawnand: stm32_fmc2: add STM32 FMC2 NAND flash controller driver")
Assisted-by: LLM
Signed-off-by: Samarth Narang <samarth.colleges@gmail.com>
---
This issue was identified by a research static analyzer for DMA
lifecycle ordering and has not been validated on hardware. The patch has
only been compile-tested on arm64 with W=1.
drivers/mtd/nand/raw/stm32_fmc2_nand.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/mtd/nand/raw/stm32_fmc2_nand.c b/drivers/mtd/nand/raw/stm32_fmc2_nand.c
index c08d6b1..82d6301 100644
--- a/drivers/mtd/nand/raw/stm32_fmc2_nand.c
+++ b/drivers/mtd/nand/raw/stm32_fmc2_nand.c
@@ -915,6 +915,7 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
DMA_PREP_INTERRUPT);
if (!desc_ecc) {
ret = -ENOMEM;
+ dmaengine_terminate_sync(dma_ch);
goto err_unmap_data;
}
@@ -922,8 +923,10 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
desc_ecc->callback = stm32_fmc2_nfc_dma_callback;
desc_ecc->callback_param = &nfc->dma_ecc_complete;
ret = dma_submit_error(dmaengine_submit(desc_ecc));
- if (ret)
+ if (ret) {
+ dmaengine_terminate_sync(dma_ch);
goto err_unmap_data;
+ }
dma_async_issue_pending(nfc->dma_ecc_ch);
}
@@ -939,9 +942,9 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
if (!wait_for_completion_timeout(&nfc->complete, timeout)) {
dev_err(nfc->dev, "seq timeout\n");
stm32_fmc2_nfc_disable_seq_irq(nfc);
- dmaengine_terminate_all(dma_ch);
+ dmaengine_terminate_sync(dma_ch);
if (!write_data && !raw)
- dmaengine_terminate_all(nfc->dma_ecc_ch);
+ dmaengine_terminate_sync(nfc->dma_ecc_ch);
ret = -ETIMEDOUT;
goto err_unmap_data;
}
@@ -949,7 +952,7 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
/* Wait DMA data transfer completion */
if (!wait_for_completion_timeout(&nfc->dma_data_complete, timeout)) {
dev_err(nfc->dev, "data DMA timeout\n");
- dmaengine_terminate_all(dma_ch);
+ dmaengine_terminate_sync(dma_ch);
ret = -ETIMEDOUT;
}
@@ -958,7 +961,7 @@ static int stm32_fmc2_nfc_xfer(struct nand_chip *chip, const u8 *buf,
if (!wait_for_completion_timeout(&nfc->dma_ecc_complete,
timeout)) {
dev_err(nfc->dev, "ECC DMA timeout\n");
- dmaengine_terminate_all(nfc->dma_ecc_ch);
+ dmaengine_terminate_sync(nfc->dma_ecc_ch);
ret = -ETIMEDOUT;
}
}
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mtd: rawnand: stm32_fmc2: retire data DMA before unmapping on errors
2026-10-30 15:18 [PATCH] mtd: rawnand: stm32_fmc2: retire data DMA before unmapping on errors Samarth Narang
@ 2026-10-30 15:30 ` Miquel Raynal
0 siblings, 0 replies; 2+ messages in thread
From: Miquel Raynal @ 2026-10-30 15:30 UTC (permalink / raw)
To: Samarth Narang
Cc: Vignesh Raghavendra, Maxime Coquelin, Alexandre Torgue,
Richard Weinberger, linux-mtd, linux-stm32, linux-arm-kernel,
linux-kernel
On 30/10/2026 at 15:18:43 GMT, Samarth Narang <samarth.colleges@gmail.com> wrote:
> When ECC descriptor preparation or submission fails in
> stm32_fmc2_nfc_xfer(), the driver unmaps the data scatterlist without
> terminating the previously issued DMA descriptor. The descriptor may
> remain queued and execute when the next page operation starts,
> potentially writing into a buffer that has already been unmapped and
> returned to the NAND core. The timeout paths have a similar issue, as
> they use dmaengine_terminate_all(), which does not guarantee that DMA
> has stopped before the buffer is unmapped.
>
> Terminate the outstanding data descriptor on ECC error paths and use
> dmaengine_terminate_sync() on timeout paths, ensuring that DMA has
> stopped before the associated mapping is released.
>
> Fixes: 2cd457f328c1 ("mtd: rawnand: stm32_fmc2: add STM32 FMC2 NAND flash controller driver")
> Assisted-by: LLM
> Signed-off-by: Samarth Narang <samarth.colleges@gmail.com>
Seems like you have a lot of them, so I'll drop the individual
submissions. Please create a series with all the ones related to the
subsystem you would like to contribute.
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-30 15:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-30 15:18 [PATCH] mtd: rawnand: stm32_fmc2: retire data DMA before unmapping on errors Samarth Narang
2026-10-30 15:30 ` Miquel Raynal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox