mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 18:50:34 +03:00
fix(sdmmc): release aligned DMA buffer on card deinit
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -25,6 +25,7 @@ extern "C" {
|
||||
* @param host pointer to structure defining host controller
|
||||
* @param out_card pointer to structure which will receive information
|
||||
* about the card when the function completes
|
||||
* @note Call sdmmc_card_deinit before reinitializing the same card structure.
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - One of the error codes from SDMMC host controller
|
||||
@@ -32,6 +33,27 @@ extern "C" {
|
||||
esp_err_t sdmmc_card_init(const sdmmc_host_t* host,
|
||||
sdmmc_card_t* out_card);
|
||||
|
||||
/**
|
||||
* Release resources allocated by sdmmc_card_init
|
||||
*
|
||||
* Currently the only persistent heap allocation made by sdmmc_card_init is
|
||||
* host.dma_aligned_buffer, and only when SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF
|
||||
* is set. Host DMA, descriptors, and per-transfer bounce buffers are owned
|
||||
* by the host driver or the I/O path, not by this function.
|
||||
*
|
||||
* This function does not deinitialize the host or free the card structure.
|
||||
* It is safe to call more than once on the same card.
|
||||
* Do not also free dma_aligned_buffer if the flag was set; if the application
|
||||
* provided that buffer without the flag, it remains caller-owned.
|
||||
*
|
||||
* @param card pointer to card information structure initialized using
|
||||
* sdmmc_card_init
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_INVALID_ARG if card is NULL
|
||||
*/
|
||||
esp_err_t sdmmc_card_deinit(sdmmc_card_t* card);
|
||||
|
||||
/**
|
||||
* @brief Print information about the card to a stream
|
||||
* @param stream stream obtained using fopen or fdopen
|
||||
|
||||
@@ -24,10 +24,10 @@ static const char* TAG = "sdmmc_init";
|
||||
#define SDMMC_INIT_STEP(condition, function) \
|
||||
do { \
|
||||
if ((condition)) { \
|
||||
esp_err_t err = (function)(card); \
|
||||
if (err != ESP_OK) { \
|
||||
ESP_LOGD(TAG, "%s: %s returned 0x%x", __func__, #function, err); \
|
||||
return err; \
|
||||
ret = (function)(card); \
|
||||
if (ret != ESP_OK) { \
|
||||
ESP_LOGD(TAG, "%s: %s returned 0x%x", __func__, #function, ret); \
|
||||
goto cleanup; \
|
||||
} \
|
||||
} \
|
||||
} while(0);
|
||||
@@ -35,10 +35,10 @@ static const char* TAG = "sdmmc_init";
|
||||
#define SDMMC_INIT_STEP_PARAM(condition, function, param) \
|
||||
do { \
|
||||
if ((condition)) { \
|
||||
esp_err_t err = (function)(card, param); \
|
||||
if (err != ESP_OK) { \
|
||||
ESP_LOGD(TAG, "%s: %s returned 0x%x", __func__, #function, err); \
|
||||
return err; \
|
||||
ret = (function)(card, param); \
|
||||
if (ret != ESP_OK) { \
|
||||
ESP_LOGD(TAG, "%s: %s returned 0x%x", __func__, #function, ret); \
|
||||
goto cleanup; \
|
||||
} \
|
||||
} \
|
||||
} while(0);
|
||||
@@ -46,6 +46,12 @@ static const char* TAG = "sdmmc_init";
|
||||
esp_err_t sdmmc_card_init(const sdmmc_host_t* config, sdmmc_card_t* card)
|
||||
{
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
|
||||
if ((config->flags & SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF) && config->dma_aligned_buffer != NULL) {
|
||||
ESP_LOGE(TAG, "%s: dma_aligned_buffer must be NULL when SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF is set", __func__);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
memset(card, 0, sizeof(*card));
|
||||
memcpy(&card->host, config, sizeof(*config));
|
||||
|
||||
@@ -189,5 +195,24 @@ esp_err_t sdmmc_card_init(const sdmmc_host_t* config, sdmmc_card_t* card)
|
||||
SDMMC_INIT_STEP_PARAM(is_sdio, sdmmc_io_init_check_card_cap, &card_cap);
|
||||
#endif
|
||||
|
||||
return ESP_OK;
|
||||
|
||||
cleanup:
|
||||
sdmmc_card_deinit(card);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t sdmmc_card_deinit(sdmmc_card_t* card)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(card, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
|
||||
|
||||
// sdmmc_card_init allocates at most this one buffer (see sdmmc_allocate_aligned_buf).
|
||||
// Flag set => we allocated it and must free it. Flag clear => caller owns the pointer.
|
||||
// Other DMA/bounce buffers are not card-init resources.
|
||||
if (card->host.flags & SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF) {
|
||||
free(card->host.dma_aligned_buffer);
|
||||
card->host.dma_aligned_buffer = NULL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -14,11 +14,94 @@
|
||||
#include "sdmmc_cmd.h"
|
||||
#include "sdmmc_test_begin_end_sd.h"
|
||||
#include "sdmmc_test_rw_common.h"
|
||||
#include "esp_private/sdmmc_common.h"
|
||||
#include "esp_blockdev.h"
|
||||
#include "esp_private/sdmmc_blockdev.h"
|
||||
|
||||
#define TEST_MEMORY_LEAK_THRESHOLD (200)
|
||||
|
||||
static size_t test_get_bus_width(int slot)
|
||||
{
|
||||
(void)slot;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool test_check_buffer_alignment(int slot, const void *buf, size_t size)
|
||||
{
|
||||
(void)slot;
|
||||
(void)buf;
|
||||
(void)size;
|
||||
return true;
|
||||
}
|
||||
|
||||
static esp_err_t test_fail_transaction(int slot, sdmmc_command_t *cmdinfo)
|
||||
{
|
||||
(void)slot;
|
||||
(void)cmdinfo;
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
TEST_CASE("sdmmc card deinit releases allocated aligned buffer", "[sdmmc]")
|
||||
{
|
||||
sdmmc_card_t card = {};
|
||||
card.host.flags = SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF;
|
||||
card.host.dma_aligned_buffer = malloc(SDMMC_IO_BLOCK_SIZE);
|
||||
TEST_ASSERT_NOT_NULL(card.host.dma_aligned_buffer);
|
||||
|
||||
TEST_ESP_OK(sdmmc_card_deinit(&card));
|
||||
TEST_ASSERT_NULL(card.host.dma_aligned_buffer);
|
||||
TEST_ESP_OK(sdmmc_card_deinit(&card));
|
||||
|
||||
void *caller_owned_buffer = malloc(SDMMC_IO_BLOCK_SIZE);
|
||||
TEST_ASSERT_NOT_NULL(caller_owned_buffer);
|
||||
card.host.flags = 0;
|
||||
card.host.dma_aligned_buffer = caller_owned_buffer;
|
||||
TEST_ESP_OK(sdmmc_card_deinit(&card));
|
||||
TEST_ASSERT_EQUAL_PTR(caller_owned_buffer, card.host.dma_aligned_buffer);
|
||||
free(caller_owned_buffer);
|
||||
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, sdmmc_card_deinit(NULL));
|
||||
}
|
||||
|
||||
TEST_CASE("sdmmc card init releases allocated aligned buffer on failure", "[sdmmc]")
|
||||
{
|
||||
sdmmc_host_t host = {
|
||||
.flags = SDMMC_HOST_FLAG_1BIT | SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF,
|
||||
.get_bus_width = test_get_bus_width,
|
||||
.do_transaction = test_fail_transaction,
|
||||
.check_buffer_alignment = test_check_buffer_alignment,
|
||||
};
|
||||
sdmmc_card_t card;
|
||||
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, sdmmc_card_init(&host, &card));
|
||||
TEST_ASSERT_NULL(card.host.dma_aligned_buffer);
|
||||
}
|
||||
|
||||
TEST_CASE("sdmmc card init rejects preset buffer with ALLOC_ALIGNED_BUF", "[sdmmc]")
|
||||
{
|
||||
void *caller_owned_buffer = malloc(SDMMC_IO_BLOCK_SIZE);
|
||||
TEST_ASSERT_NOT_NULL(caller_owned_buffer);
|
||||
|
||||
sdmmc_host_t host = {
|
||||
.flags = SDMMC_HOST_FLAG_1BIT | SDMMC_HOST_FLAG_ALLOC_ALIGNED_BUF,
|
||||
.get_bus_width = test_get_bus_width,
|
||||
.do_transaction = test_fail_transaction,
|
||||
.check_buffer_alignment = test_check_buffer_alignment,
|
||||
.dma_aligned_buffer = caller_owned_buffer,
|
||||
};
|
||||
sdmmc_card_t card;
|
||||
memset(&card, 0xAA, sizeof(card));
|
||||
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, sdmmc_card_init(&host, &card));
|
||||
/* card must be left untouched, and the caller keeps ownership of the buffer */
|
||||
TEST_ASSERT_EQUAL_PTR(caller_owned_buffer, host.dma_aligned_buffer);
|
||||
for (size_t i = 0; i < sizeof(card); i++) {
|
||||
TEST_ASSERT_EQUAL_UINT8(0xAA, ((uint8_t *)&card)[i]);
|
||||
}
|
||||
|
||||
free(caller_owned_buffer);
|
||||
}
|
||||
|
||||
TEST_CASE("sdmmc blockdev converts byte ranges to sectors", "[sdmmc]")
|
||||
{
|
||||
size_t start_sector;
|
||||
|
||||
Reference in New Issue
Block a user