mtd: nand: stop DMA before unmapping on error and timeout paths
Subsystems (7)
v2 cover mtd: nand: stop DMA before unmapping on error and timeout paths
This series addresses DMA lifecycle issues in six NAND drivers where error or timeout paths may unmap buffers before the associated DMA transfers have stopped. Some paths do not terminate the outstanding transfers, while others use dmaengine_terminate_all(), which does not guarantee that the channel has stopped before returning. The patches use dmaengine_terminate_sync() to ensure that DMA has stopped before releasing the mappings, and all affected functions already run in a context where sleeping is permitted.
These issues were identified by a research static analyzer for DMA lifecycle ordering and have not been validated on hardware. The patches have only been compile-tested on arm64 with W=1.
Changes in v2:
- Combined the previously submitted patches into a single series, as requested by Miquel, and included three additional drivers with similar issues (omap2, marvell, and sh_flctl).
- Removed the code comment from the gpmi patch.
- Left qpic_common and stm32_fmc2 unchanged from their earlier submissions.
Two questions remain for the qpic_common patch. The BAM driver warns that resetting a pipe while the attached engine is still active may cause a fatal error, so it is unclear whether the NAND controller must be stopped or reset before terminating the DMA channels. The success path also assumes that completion of the final command descriptor guarantees completion of the RX data descriptor, which may need confirmation from the hardware maintainers.
v1:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
Samarth Narang (6):
mtd: rawnand: gpmi: terminate the DMA chain on completion timeout
mtd: nand: qpic_common: terminate DMA channels on transfer timeout
mtd: rawnand: stm32_fmc2: retire data DMA before unmapping on errors
mtd: rawnand: omap2: terminate DMA when prefetch cannot be enabled
mtd: rawnand: marvell: stop DMA before unmapping on timeout
mtd: rawnand: sh_flctl: use dmaengine_terminate_sync() on DMA timeout
drivers/mtd/nand/qpic_common.c | 11 +++++++++-- drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c | 1 + drivers/mtd/nand/raw/marvell_nand.c | 9 +++++---- drivers/mtd/nand/raw/omap2.c | 4 +++- drivers/mtd/nand/raw/sh_flctl.c | 2 +- drivers/mtd/nand/raw/stm32_fmc2_nand.c | 13 ++++++++----- 6 files changed, 27 insertions(+), 13 deletions(-)
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.34.1
PATCH v2 1/6 mtd: rawnand: gpmi: terminate the DMA chain on completion timeout
When DMA completion times out in gpmi_nfc_exec_op(), the driver unmaps the operation's buffers and returns -ETIMEDOUT without stopping the APBH DMA channel. Since mxs-dma leaves the channel in DMA_IN_PROGRESS, the pending descriptor chain may resume after the timeout, while a subsequent operation can append new descriptors to the stale chain and cause the old DATA_IN transfer to execute again against an unmapped buffer.
This can result in DMA writes to memory that has already been released, including buffers freed after ONFI parameter page detection in nand_onfi_detect().
Terminate the channel with dmaengine_terminate_sync() before unmapping so that outstanding transfers are stopped and subsequent operations begin with a fresh descriptor chain.
--- drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c | 1 + 1 file changed, 1 insertion(+)
drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c +1 -0
@@ -2659,6 +2659,7 @@ static int gpmi_nfc_exec_op(struct nand_chip *chip,if (!to) {dev_err (this->dev,"DMA timeout, last DMA\n" );gpmi_dump_info (this); +dmaengine_terminate_sync (get_dma_chan (this)); ret = -ETIMEDOUT ; goto unmap; }
Signature · 1 line
2.34.1
PATCH v2 2/6 mtd: nand: qpic_common: terminate DMA channels on transfer timeout
In reply to Samarth NarangWhen DMA completion times out in qcom_submit_descs(), the driver unmaps the descriptors' scatterlists without stopping the BAM DMA channels. Since the issued descriptors may still be active, the RX and TX channels can continue accessing the caller's buffer after it has been unmapped and returned to the NAND core. The ADM path has a similar issue when dma_sync_wait() times out.
Terminate the affected channels with dmaengine_terminate_sync() before unmapping, ensuring that outstanding DMA transfers are stopped before their mappings are released. On BAM, termination resets the pipe and discards pending descriptors.
--- drivers/mtd/nand/qpic_common.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/mtd/nand/qpic_common.c +9 -2
@@ -592,11 +592,18 @@ int qcom_submit_descs(struct qcom_nand_controller *nandc)dma_async_issue_pending (nandc->cmd_chan);if (!wait_for_completion_timeout (&bam_txn->txn_done, -QPIC_NAND_COMPLETION_TIMEOUT )) +QPIC_NAND_COMPLETION_TIMEOUT )) { +/* Retire every issued pipe before unmapping its buffers. */ +dmaengine_terminate_sync (nandc->tx_chan); +dmaengine_terminate_sync (nandc->rx_chan); +dmaengine_terminate_sync (nandc->cmd_chan); ret = -ETIMEDOUT ; + } }else { -if (dma_sync_wait (nandc->chan, cookie) !=DMA_COMPLETE ) +if (dma_sync_wait (nandc->chan, cookie) !=DMA_COMPLETE ) { +dmaengine_terminate_sync (nandc->chan); ret = -ETIMEDOUT ; + } } err_unmap_free_desc:
Signature · 1 line
2.34.1
PATCH v2 3/6 mtd: rawnand: stm32_fmc2: retire data DMA before unmapping on errors
In reply to Samarth NarangWhen 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.
--- drivers/mtd/nand/raw/stm32_fmc2_nand.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/mtd/nand/raw/stm32_fmc2_nand.c +8 -5
@@ -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 ; } }
Signature · 1 line
2.34.1
PATCH v2 4/6 mtd: rawnand: omap2: terminate DMA when prefetch cannot be enabled
In reply to Samarth NarangWhen omap_prefetch_enable() fails because the prefetch engine is busy, omap_nand_dma_transfer() unmaps the buffer and falls back to a CPU copy without terminating the previously issued DMA descriptor. The descriptor may therefore continue accessing the unmapped buffer, while its callback could also signal info->comp for a subsequent transfer.
Terminate the DMA channel with dmaengine_terminate_sync() before unmapping the buffer, ensuring that the outstanding transfer has stopped before the fallback path proceeds.
---
This issue was identified by a research static analyzer for DMA lifecycle ordering and has not been validated on hardware.
drivers/mtd/nand/raw/omap2.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/mtd/nand/raw/omap2.c +3 -1
@@ -417,9 +417,11 @@ static inline int omap_nand_dma_transfer(struct nand_chip *chip,/* configure and start prefetch transfer */ ret =omap_prefetch_enable (info->gpmc_cs,PREFETCH_FIFOTHRESHOLD_MAX ,0x1 , len, is_write, info); -if (ret) +if (ret) {/* PFPW engine is busy, use cpu copy method */ +dmaengine_terminate_sync (info->dma); goto out_copy_unmap; + }wait_for_completion (&info->comp); tim =0 ;
Signature · 1 line
2.34.1
PATCH v2 5/6 mtd: rawnand: marvell: stop DMA before unmapping on timeout
In reply to Samarth NarangWhen marvell_nfc_wait_cmdd() times out in marvell_nfc_xfer_data_dma(), the driver unmaps the DMA buffer before calling dmaengine_terminate_all(), which does not guarantee that the transfer has stopped. As a result, the DMA engine may continue accessing the buffer after its mapping has been released.
Use dmaengine_terminate_sync() before unmapping the buffer on the timeout path, while preserving the existing status logging before termination so that it reflects the timed-out transfer.
---
This issue was identified by a research static analyzer for DMA lifecycle ordering and has not been validated on hardware.
drivers/mtd/nand/raw/marvell_nand.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/mtd/nand/raw/marvell_nand.c +5 -4
@@ -900,14 +900,15 @@ static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,dma_async_issue_pending (nfc->dma_chan); ret =marvell_nfc_wait_cmdd (nfc->selected_chip); -dma_unmap_sg (nfc->dma_chan->device->dev, &sg,1 , direction); -marvell_nfc_disable_dma (nfc);if (ret) {dev_err (nfc->dev,"Timeout waiting for DMA (status: %d)\n" ,dmaengine_tx_status (nfc->dma_chan, cookie,NULL )); -dmaengine_terminate_all (nfc->dma_chan); -return -ETIMEDOUT ; +dmaengine_terminate_sync (nfc->dma_chan); } +dma_unmap_sg (nfc->dma_chan->device->dev, &sg,1 , direction); +marvell_nfc_disable_dma (nfc); +if (ret) +return -ETIMEDOUT ;return 0 ; }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2 5/6] mtd: rawnand: marvell: stop DMA before unmapping on timeout
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mtd: rawnand: marvell: sleep in atomic context in marvell_nfc_xfer_data_dma() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mtd: rawnand: marvell: sleep in atomic context in marvell_nfc_xfer_data_dma() --
commit cbbe6eae8df4a34b02e625e64676a65024331f76
Author: Samarth Narang <[email protected]>
mtd: rawnand: marvell: stop DMA before unmapping on timeout
This commit replaces dmaengine_terminate_all() with dmaengine_terminate_sync() and moves it before dma_unmap_sg() on the timeout path in marvell_nfc_xfer_data_dma() so the DMA transfer is stopped before unmapping the buffer.
Quoted 6 lines
drivers/mtd/nand/raw/marvell_nand.c +0
-0@@ -900,14 +900,15 @@ static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
drivers/mtd/nand/raw/marvell_nand.c static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc, · line 906
Quoted 10 lines
drivers/mtd/nand/raw/marvell_nand.c +1
-4dma_async_issue_pending (nfc->dma_chan); ret =marvell_nfc_wait_cmdd (nfc->selected_chip); -dma_unmap_sg (nfc->dma_chan->device->dev, &sg,1 , direction); -marvell_nfc_disable_dma (nfc);if (ret) {dev_err (nfc->dev,"Timeout waiting for DMA (status: %d)\n" ,dmaengine_tx_status (nfc->dma_chan, cookie,NULL )); -dmaengine_terminate_all (nfc->dma_chan); -return -ETIMEDOUT ; +dmaengine_terminate_sync (nfc->dma_chan);
[Severity: Medium]
Can calling dmaengine_terminate_sync() here sleep in atomic context during a panic write?
This driver supports mtd->oops_panic_write by polling in marvell_nfc_wait_op() instead of sleeping:
drivers/mtd/nand/raw/marvell_nand.c:marvell_nfc_wait_op() {
if (mtd->oops_panic_write) {
ret = marvell_nfc_poll_status(nfc, NDSR_RDY(0),
NDSR_RDY(0),
timeout_ms);
} else {
init_completion(&nfc->complete);
...
}When nfc->use_dma is enabled on NFCv1, panic writes still go through marvell_nfc_xfer_data_dma():
mtd_panic_write()
-> panic_nand_write()
-> nand_do_write_ops()
-> nand_write_page()
-> marvell_nfc_hw_ecc_hmg_write_page()
-> marvell_nfc_hw_ecc_hmg_do_write_page()
-> marvell_nfc_xfer_data_dma()Because mtd_panic_write() can be invoked with interrupts disabled or under a raw spinlock (such as pstore_dump()), and dmaengine_terminate_sync() calls dmaengine_synchronize() (which calls might_sleep(), wait_event() in pxad_synchronize(), and tasklet_kill() in vchan_synchronize()), wouldn't a DMA timeout during a panic write trigger a sleep-in-atomic bug or hang waiting for the DMA tasklet while softirqs are disabled?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v2 6/6 mtd: rawnand: sh_flctl: use dmaengine_terminate_sync() on DMA timeout
In reply to Samarth NarangWhen DMA completion times out in flctl_dma_fifo0_transfer(), the driver calls dmaengine_terminate_all() before unmapping the buffer. Since this does not guarantee that the transfer has stopped, the DMA engine may continue accessing memory after its mapping has been released.
Replace dmaengine_terminate_all() with dmaengine_terminate_sync() to ensure that DMA has stopped before unmapping the buffer, which is safe because the function already sleeps while waiting for completion.
---
This issue was identified by a research static analyzer for DMA lifecycle ordering and has not been validated on hardware.
drivers/mtd/nand/raw/sh_flctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/sh_flctl.c +1 -1
@@ -430,7 +430,7 @@ static int flctl_dma_fifo0_transfer(struct sh_flctl *flctl, unsigned long *buf, msecs_to_jiffies(3000));if (time_left ==0 ) { -dmaengine_terminate_all (chan); +dmaengine_terminate_sync (chan);dev_err (&flctl->pdev->dev,"wait_for_completion_timeout\n" ); ret = -ETIMEDOUT ; }
Signature · 1 line
2.34.1