#19159·esp-idf

sdmmc: transaction timeout silently masked by trailing esp_cache_msync() return value in sd_host_slot_sdmmc_do_transaction() (IDFGH-18373)

Author: naizhaoCreated Sep 30, 2026Updated Sep 30, 2026
LabelsStatus: Opened

IDF version.

v6.0.1 (verified line-by-line in v6.0.2 and current master as well)

Component

esp_driver_sdmmc / SDMMC host driver

Target

ESP32-P4 (any target with SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE)

Bug description

In sd_host_slot_sdmmc_do_transaction() (components/esp_driver_sdmmc/src/sd_trans_sdmmc.c), the trailing post-transfer cache invalidate unconditionally overwrites ret with the return value of esp_cache_msync():

c
    // process events until transfer is complete
    ...
    while (state != SDMMC_IDLE) {
        ret = handle_event(slot_ctx, cmdinfo, &state, &unhandled_events);
        if (ret != ESP_OK) {
            break;                       // ret == ESP_ERR_TIMEOUT here
        }
    }
    ...
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
    if (cmdinfo->data) {
        ret = esp_cache_msync((void *)cmdinfo->data, cache_sync_len, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
        if (ret != ESP_OK) {
            goto out;
        }
    }
#endif
    ...
    return ret;

When the transfer itself fails (e.g. handle_event returns ESP_ERR_TIMEOUT because the card did not complete the data stage) but the cleanup esp_cache_msync() succeeds, the real transaction error is silently replaced by ESP_OK and the function reports success.

sdmmc_cmd.c (sdmmc_wait_for_response/sdmmc_read_sectors path) primarily propagates the host return value, so the failure never reaches FatFS diskio_sdmmc: sdmmc_read_sectors() returns ESP_OK while the destination buffer was never (fully) filled or invalidated by DMA — i.e. the caller consumes stale cache contents as fresh card data.

Steps to reproduce

Hard to reduce to a minimal example because it requires an actual data-stage timeout, but on a board under sustained, high-concurrency SD I/O (map/tile reads + continuous WAV recording + directory scans, PSRAM-heavy workload):

  1. Induce sd_host_wait_for_event() timeouts on read transactions (we observed them under load as ESP_ERR_TIMEOUT bursts).
  2. Observe diskio_sdmmc/ff reporting successful reads whose payload does not match what is on the card (FAT sectors read back stale).

In our incident, a FatFS FAT window refreshed by such a "successful" read fed a read-modify-write cycle, which is a plausible filesystem-corruption vector under card-level recovery attempts afterwards.

Expected behavior

A cache-synchronization result in the cleanup path must not mask a card/controller transaction error. The transaction error should be preserved, e.g.:

c
    if (cmdinfo->data) {
        esp_err_t cache_ret = esp_cache_msync((void *)cmdinfo->data, cache_sync_len, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
        if (ret == ESP_OK && cmdinfo->error == ESP_OK) ret = cache_ret;
    }

(or equivalently: latch the first error before cleanup.)

Actual behavior

Read/write transactions that timed out mid-transfer are reported as ESP_OK whenever the trailing esp_cache_msync() succeeds, yielding stale-buffer "successful" reads. The same overwrite pattern applies to the write direction.

Debug logs

Not attaching device logs since the mechanism is fully visible in the source; happy to provide if needed.

Additional context

  • Master already gates both msync calls with esp_cache_get_line_size_by_addr() (which also nicely resolves the ESP_ERR_NOT_SUPPORTED returned for non-cacheable buffers), but the return-value overwrite semantics are unchanged there.
  • The analogous ESP_RETURN_ON_ERROR(esp_pm_lock_release(...)) at the out: label has a related asymmetry: a PM release failure replaces an earlier transaction error and also skips xSemaphoreGive(mutex) on that path.

We have carried a build-local overlay of this translation unit implementing the suggested "preserve first error" behavior and it behaves as expected under the same load; we would rather see it fixed upstream than maintain the overlay.