Skip to content

drivers: usdhc: don't invalidate cache on non-blocking non-DMA transfer#25

Open
tchebb wants to merge 1 commit intonxp-mcuxpresso:mainfrom
tchebb:fix-no-dma-invalidate
Open

drivers: usdhc: don't invalidate cache on non-blocking non-DMA transfer#25
tchebb wants to merge 1 commit intonxp-mcuxpresso:mainfrom
tchebb:fix-no-dma-invalidate

Conversation

@tchebb
Copy link
Copy Markdown

@tchebb tchebb commented Apr 6, 2026

In USDHC_TransferNonBlocking(), DMA may be disabled if the user passes a NULL dmaConfig or if USDHC_SetAdmaTableConfig() fails. In that case, USDHC_TransferNonBlocking() correctly skips its cache clean, but USDHC_TransferHandleData() doesn't skip its cache invalidate, meaning it replaces the entire data buffer's cache line with stale data from RAM. This memory corruption causes undefined behavior and system instability.

Fix the issue by remembering if we used DMA and only invalidating the cache if we did. This matches the behavior of USDHC_TransferBlocking(). Also fix USDHC_TransferScatterGatherADMANonBlocking().

Memory corruption may still occur if the DMA buffer shares a cache line with unrelated data that the CPU updates during a transfer, but there is no way for us to detect that in the SDK. The user should avoid that situation.

Fixes #20

In USDHC_TransferNonBlocking(), DMA may be disabled if the user passes a
NULL dmaConfig or if USDHC_SetAdmaTableConfig() fails. In that case,
USDHC_TransferNonBlocking() correctly skips its cache clean, but
USDHC_TransferHandleData() doesn't skip its cache invalidate, meaning it
replaces the entire data buffer's cache line with stale data from RAM.
This memory corruption causes undefined behavior and system instability.

Fix the issue by remembering if we used DMA and only invalidating the
cache if we did. This matches the behavior of USDHC_TransferBlocking().
Also fix USDHC_TransferScatterGatherADMANonBlocking().

Memory corruption may still occur if the DMA buffer shares a cache line
with unrelated data that the CPU updates during a transfer, but there is
no way for us to detect that in the SDK. The user should avoid that
situation.

Fixes nxp-mcuxpresso#20
@yangbolu1991
Copy link
Copy Markdown
Contributor

@tchebb Thanks a lot for the fix-up.
I will cherry-pick it into NXP internal repo and make sure it's in next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory corruption on USDHC non-DMA transfer when D-Cache enabled

2 participants