Skip to content

arch/arm/src/common/stm32: Bound SDIO command response wait by time. - #19793

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Luka-Filipovic:sdio-bound-waitresponse
Aug 12, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Luka-Filipovic:sdio-bound-waitresponse

Conversation

@Luka-Filipovic

@Luka-Filipovic Luka-Filipovic commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Potential fix for #19792.

stm32_waitresponse() in arch/arm/src/common/stm32/stm32_sdio_m3m4_v1.c
polls SDIO_STA in a busy loop bounded only by an iteration count
(SDIO_LONGTIMEOUT = 0x7fffffff for response-bearing commands). The
hardware CTIMEOUT flag normally ends the wait within microseconds, but it
is only generated while the card clock is running and the command state
machine is in its Wait state. If the peripheral gets into a bad state,
SDIO_STA never changes and the loop spins for minutes at 100% CPU while
holding the FAT semaphore. With CONFIG_PRIORITY_INHERITANCE=y the
spinning thread inherits the priority of the highest waiter blocked on the
filesystem, starving every task at or below that priority (details in
#19792).

Bound the wait by time instead: 250 ms for response-bearing commands (the
same value #3669 used for the F7/H7 data path timeout) and 10 ms for the
no-response/R3/R7 cases. CTIMEOUT remains the normal exit; the software
bound only fires when the peripheral is stuck, and returns -ETIMEDOUT so
the existing mmcsd error handling can run.

The same pattern exists in stm32f7/stm32_sdmmc.c, stm32h7/stm32_sdmmc.c,
at32/at32_sdio.c and gd32f4/gd32f4xx_sdio.c.

Impact

  • No behavior change for working hardware: the loop still exits via
    CMDREND/CCRCFAIL/CTIMEOUT within microseconds.
  • A stuck peripheral now produces -ETIMEDOUT after 250 ms instead of an
    unbounded spin (previously CPU-frequency dependent, ~2.5 minutes at
    168 MHz).
  • No configuration or API changes. SDIO_CMDTIMEOUT/SDIO_LONGTIMEOUT
    had no remaining users and are removed.

Testing

Host: Ubuntu 26.04 x86_64, ghcr.io/apache/nuttx/apache-nuttx-ci-linux.

  • Built stm32f401rc-rs485:sdcard (CONFIG_STM32_SDIO=y) - clean:

    LD: nuttx
    Memory region         Used Size  Region Size  %age Used
               flash:      119660 B       256 KB     45.65%
                sram:        8268 B        64 KB     12.62%
    CP: nuttx.hex
    CP: nuttx.bin
    
  • nxstyle clean on the changed file.

  • On STM32F427 hardware running the identical change: sd_bench works as expected, and no problems were found in
    regular usage (card init at 400 kHz, FAT mount, continuous logging at
    the full transfer clock).

stm32_waitresponse() polls SDIO_STA bounded only by an iteration
counter, set to 0x7fffffff for all R1/R1B/R2/R4/R5/R6 commands.  The
hardware CTIMEOUT flag is the intended exit for a missing response,
but it is only generated while the card clock is running and the CPSM
has reached its Wait state.  If the card clock stops or the peripheral
fails, SDIO_STA never updates and the loop spins for INT32_MAX
iterations while holding the FAT filesystem lock, at the caller's
priority-inheritance boosted priority if higher priority tasks block
on the filesystem.  Since the mmcsd layer retries failed commands,
the driver's recovery paths are never reached and the system never
recovers.

Bound the wait by time instead: 250 ms for response-bearing commands
(the largest timeout the SD specification allows for any operation)
and 10 ms for the no-response/R3/R7 cases.  CTIMEOUT remains the
normal error exit within microseconds; the software bound only fires
when the peripheral itself is dead, converting an unbounded spin into
-ETIMEDOUT so the existing mmcsd retry logic can run.

Signed-off-by: Luka Filipović <[email protected]>
Assisted-by: Claude Code:claude-fable-5
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small labels Aug 11, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@xiaoxiang781216
xiaoxiang781216 merged commit 62b41ad into apache:master Aug 12, 2026
50 checks passed
@Luka-Filipovic

Luka-Filipovic commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing and merging, but have in mind that:

The same pattern exists in stm32f7/stm32_sdmmc.c, stm32h7/stm32_sdmmc.c,
at32/at32_sdio.c and gd32f4/gd32f4xx_sdio.c.

I can make a new PR that includes these as well if you see a need.

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: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants