Skip to content

Commit

Permalink
Merge branch 'backport/uart_select_malloc_5.2' into 'release/v5.2'
Browse files Browse the repository at this point in the history
fix(vfs): fix uart malloc when locates ISR context in IRAM(Backport 5.2)

See merge request espressif/esp-idf!27383
  • Loading branch information
pacucha42 committed Nov 24, 2023
2 parents a9f7ea3 + be96274 commit b780287
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
9 changes: 8 additions & 1 deletion components/vfs/private_include/esp_vfs_private.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "sdkconfig.h"
#include "esp_vfs.h"
#include "esp_vfs_common.h"

#ifdef __cplusplus
extern "C" {
#endif

#if CONFIG_VFS_SELECT_IN_RAM
#define VFS_MALLOC_FLAGS MALLOC_CAP_INTERNAL
#else
#define VFS_MALLOC_FLAGS MALLOC_CAP_DEFAULT
#endif

typedef struct vfs_entry_ {
esp_vfs_t vfs; // contains pointers to VFS functions
char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS
Expand Down
6 changes: 3 additions & 3 deletions components/vfs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_v
return ESP_ERR_INVALID_ARG;
}
}
vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t));
vfs_entry_t *entry = (vfs_entry_t*) heap_caps_malloc(sizeof(vfs_entry_t), VFS_MALLOC_FLAGS);
if (entry == NULL) {
return ESP_ERR_NO_MEM;
}
Expand Down Expand Up @@ -970,7 +970,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
// because that could block the registration of new driver.
const size_t vfs_count = s_vfs_count;
fds_triple_t *vfs_fds_triple;
if ((vfs_fds_triple = calloc(vfs_count, sizeof(fds_triple_t))) == NULL) {
if ((vfs_fds_triple = heap_caps_calloc(vfs_count, sizeof(fds_triple_t), VFS_MALLOC_FLAGS)) == NULL) {
__errno_r(r) = ENOMEM;
ESP_LOGD(TAG, "calloc is unsuccessful");
return -1;
Expand Down Expand Up @@ -1047,7 +1047,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
}
}

void **driver_args = calloc(vfs_count, sizeof(void *));
void **driver_args = heap_caps_calloc(vfs_count, sizeof(void *), VFS_MALLOC_FLAGS);

if (driver_args == NULL) {
free(vfs_fds_triple);
Expand Down
17 changes: 9 additions & 8 deletions components/vfs/vfs_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
#include <sys/lock.h>
#include <sys/fcntl.h>
#include <sys/param.h>
#include "sdkconfig.h"
#include "esp_attr.h"
#include "esp_vfs.h"
#include "esp_vfs_dev.h"
#include "esp_attr.h"
#include "soc/uart_periph.h"
#include "esp_vfs_private.h"
#include "esp_rom_uart.h"
#include "driver/uart.h"
#include "sdkconfig.h"
#include "driver/uart_select.h"
#include "esp_rom_uart.h"
#include "soc/soc_caps.h"
#include "hal/uart_ll.h"
#include "soc/soc_caps.h"
#include "soc/uart_periph.h"

#define UART_NUM SOC_UART_HP_NUM

Expand Down Expand Up @@ -365,7 +366,7 @@ static esp_err_t register_select(uart_select_args_t *args)
portENTER_CRITICAL(&s_registered_select_lock);
const int new_size = s_registered_select_num + 1;
uart_select_args_t **new_selects;
if ((new_selects = realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *))) == NULL) {
if ((new_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), VFS_MALLOC_FLAGS)) == NULL) {
ret = ESP_ERR_NO_MEM;
} else {
s_registered_selects = new_selects;
Expand All @@ -391,7 +392,7 @@ static esp_err_t unregister_select(uart_select_args_t *args)
// The item is removed by overwriting it with the last item. The subsequent rellocation will drop the
// last item.
s_registered_selects[i] = s_registered_selects[new_size];
s_registered_selects = realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *));
s_registered_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), VFS_MALLOC_FLAGS);
// Shrinking a buffer with realloc is guaranteed to succeed.
s_registered_select_num = new_size;
ret = ESP_OK;
Expand Down Expand Up @@ -448,7 +449,7 @@ static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds,
}
}

uart_select_args_t *args = malloc(sizeof(uart_select_args_t));
uart_select_args_t *args = heap_caps_malloc(sizeof(uart_select_args_t), VFS_MALLOC_FLAGS);

if (args == NULL) {
return ESP_ERR_NO_MEM;
Expand Down

0 comments on commit b780287

Please sign in to comment.