Skip to content

arch/arm: Bound SDMMC/SDIO command response wait by time - #20908

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32-sdio-timeout-hang
Oct 26, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32-sdio-timeout-hang

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Follow up on #19793 (commit 62b41ad9b40d5a513486a3b7f9be86ed8cdfe0e5) to address and completely close #19792.

Background & Problem

In the STM32 SDMMC/SDIO drivers, stm32_waitresponse() (or gd32_wait_response / at32_waitresponse) polls the status register (STA/STAT) waiting for command completion flags (CMDDONE or RESPDONE).

Previously, the timeout mechanism was bounded solely by an iteration counter:

  • SDMMC_CMDTIMEOUT = 100000
  • SDMMC_LONGTIMEOUT = 0x7fffffff (~2.14 billion iterations)

The hardware CTIMEOUT flag in the status register is intended to signal a missing response, but it relies on the Command Path State Machine (CPSM) and card clock running. If the SD card is abruptly removed, the card clock stops, or the peripheral freezes/fails:

  1. STA/STAT never updates with CTIMEOUT or RESPDONE.
  2. The software loop spins for INT32_MAX (2.14 billion) iterations, consuming 100% CPU time for seconds or even minutes.
  3. Because the calling thread often holds the VFS filesystem mount/inode lock (and may be boosted via priority inheritance), lower-priority threads are completely starved.
  4. With higher-level mmcsd retries, the system remains unresponsive indefinitely.

While PR #19793 fixed this in arch/arm/src/common/stm32/stm32_sdio_m3m4_v1.c, issue #19792 remained open because all other STM32 family variants and clone architectures shared the exact same iteration-counter loop.

What This PR Does

This patch replaces the loop-count timeouts with deterministic, tick-based timeouts (clock_systime_ticks()) across all remaining affected drivers:

  • arch/arm/src/stm32f7/stm32_sdmmc.c
  • arch/arm/src/stm32h5/stm32_sdmmc.c
  • arch/arm/src/stm32h7/stm32_sdmmc.c
  • arch/arm/src/stm32l4/stm32l4_sdmmc.c
  • arch/arm/src/at32/at32_sdio.c
  • arch/arm/src/gd32f4/gd32f4xx_sdio.c

Timeout Values & Design

  • SDMMC_LONGTIMEOUT_MS (250 ms): Configured using MSEC2TICK(250) for response-bearing commands (R1, R1B, R2, R4, R5, R6). 250 ms is the maximum response timeout defined by the SD Physical Layer Specification for busy operations.
  • SDMMC_CMDTIMEOUT_MS (10 ms): Configured using MSEC2TICK(10) for commands with no response, R3, or R7.
  • Hardware vs. Software Exit: Under normal operating conditions, the hardware CTIMEOUT flag still detects missing card responses within microseconds. The software tick timeout acts strictly as a safety backstop when the hardware peripheral or card clock is unresponsive, gracefully returning -ETIMEDOUT so the higher-level mmcsd error recovery and retry logic can execute.

Fixes #19792

Impact

  • Is new feature added? No.
  • Is existing feature changed? Yes (SDMMC/SDIO command response wait loop).
  • User impact: Eliminates system hangs, CPU starvation, and priority inversion when SD cards fail or are removed on STM32F7, STM32H5, STM32H7, STM32L4, AT32, and GD32F4 platforms.
  • Build / ABI / API impact: None. Fully compatible with existing drivers and configurations.

Testing

  • Style & Static Check: Ran ./tools/checkpatch.sh -m -g HEAD on the commit; all style and commit message checks pass without warnings or errors.
  • Consistency Verification: Timeout semantics and implementation match the merged reference commit 62b41ad9b40d5a513486a3b7f9be86ed8cdfe0e5 (arch/arm/src/common/stm32/stm32_sdio_m3m4_v1.c).

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Size: M The size of the change in this PR is medium labels Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@github-actions github-actions Bot added Size: L The size of the change in this PR is large and removed Size: M The size of the change in this PR is medium labels Oct 23, 2026
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@Swatantra-66 please fix the conflict.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@Swatantra-66 ping again.

@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 will fix it soon, maybe before 12 pm IST

Follow up on commit 62b41ad to fix the same SDMMC/SDIO polling
timeout starvation issue across the remaining STM32 families and clone
architectures (stm32f7, stm32h5, stm32h7, stm32l4, at32, gd32f4).

The command waitresponse functions previously polled the status register
bounded only by an iteration counter (up to 0x7fffffff iterations).
If the card clock stops or the peripheral freezes, the hardware CTIMEOUT
flag is never generated and the loop spins for INT32_MAX iterations
while holding the filesystem lock, causing high CPU load and starving
lower-priority threads.

Bound the wait by time using clock_systime_ticks(): 250 ms for
response-bearing commands (the maximum timeout allowed by the SD
physical layer specification) and 10 ms for no-response/R3/R7 commands.

Fixes apache#19792

Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/stm32-sdio-timeout-hang branch from 6e38f1f to 79706e5 Compare October 24, 2026 06:30
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: L The size of the change in this PR is large labels Oct 24, 2026
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Rebased onto latest master and resolved merge conflicts in stm32f7 and stm32l4 SDMMC drivers.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

let's ignore ci error which is fixed by #20949 instead wasting ci resource by retrigger.

@xiaoxiang781216
xiaoxiang781216 merged commit 46c0aaf into apache:master Oct 26, 2026
25 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] SD card failure causes high CPU load and starves lower prio threads

3 participants