mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'bugfix/select_with_sdmmc_mount_v4.0' into 'release/v4.0'
VFS: Fix bug which occurs when driver is installed during a select() call (v4.0) See merge request espressif/esp-idf!6429
This commit is contained in:
commit
059052acaa
@ -106,6 +106,10 @@ given VFS driver.
|
||||
:cpp:func:`end_select` is called to stop/deinitialize/free the
|
||||
environment which was setup by :cpp:func:`start_select`.
|
||||
|
||||
.. note::
|
||||
:cpp:func:`end_select` might be called without a previous :cpp:func:`start_select` call in some rare
|
||||
circumstances. :cpp:func:`end_select` should fail gracefully if this is the case.
|
||||
|
||||
Please refer to the
|
||||
reference implementation for the UART peripheral in
|
||||
:component_file:`vfs/vfs_uart.c` and most particularly to the functions
|
||||
@ -153,6 +157,10 @@ Please see :component_file:`lwip/port/esp32/vfs_lwip.c` for a reference socket d
|
||||
If you use :cpp:func:`select` for socket file descriptors only then you can enable the
|
||||
:envvar:`CONFIG_LWIP_USE_ONLY_LWIP_SELECT` option to reduce the code size and improve performance.
|
||||
|
||||
.. note::
|
||||
Don't change the socket driver during an active :cpp:func:`select` call or you might experience some undefined
|
||||
behavior.
|
||||
|
||||
Paths
|
||||
-----
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "driver/uart.h"
|
||||
#include "esp_vfs.h"
|
||||
#include "esp_vfs_dev.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "test_utils.h"
|
||||
@ -549,3 +550,65 @@ TEST_CASE("concurrent selects work", "[vfs]")
|
||||
deinit(uart_fd, socket_fd);
|
||||
close(dummy_socket_fd);
|
||||
}
|
||||
|
||||
TEST_CASE("select() works with concurrent mount", "[vfs][fatfs]")
|
||||
{
|
||||
wl_handle_t test_wl_handle;
|
||||
int uart_fd, socket_fd;
|
||||
|
||||
init(&uart_fd, &socket_fd);
|
||||
const int dummy_socket_fd = open_dummy_socket();
|
||||
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 2
|
||||
};
|
||||
|
||||
// select() will be waiting for a socket & UART and FATFS mount will occur in parallel
|
||||
|
||||
struct timeval tv = {
|
||||
.tv_sec = 1,
|
||||
.tv_usec = 0,
|
||||
};
|
||||
|
||||
fd_set rdfds;
|
||||
FD_ZERO(&rdfds);
|
||||
FD_SET(uart_fd, &rdfds);
|
||||
FD_SET(dummy_socket_fd, &rdfds);
|
||||
|
||||
test_select_task_param_t param = {
|
||||
.rdfds = &rdfds,
|
||||
.wrfds = NULL,
|
||||
.errfds = NULL,
|
||||
.maxfds = MAX(uart_fd, dummy_socket_fd) + 1,
|
||||
.tv = &tv,
|
||||
.select_ret = 0, // expected timeout
|
||||
.sem = xSemaphoreCreateBinary(),
|
||||
};
|
||||
TEST_ASSERT_NOT_NULL(param.sem);
|
||||
|
||||
start_select_task(¶m);
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS); //make sure the task has started and waits in select()
|
||||
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &test_wl_handle));
|
||||
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(param.sem, 1500 / portTICK_PERIOD_MS));
|
||||
|
||||
// select() will be waiting for a socket & UART and FATFS unmount will occur in parallel
|
||||
|
||||
FD_ZERO(&rdfds);
|
||||
FD_SET(uart_fd, &rdfds);
|
||||
FD_SET(dummy_socket_fd, &rdfds);
|
||||
|
||||
start_select_task(¶m);
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS); //make sure the task has started and waits in select()
|
||||
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", test_wl_handle));
|
||||
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(param.sem, 1500 / portTICK_PERIOD_MS));
|
||||
|
||||
vSemaphoreDelete(param.sem);
|
||||
|
||||
deinit(uart_fd, socket_fd);
|
||||
close(dummy_socket_fd);
|
||||
}
|
||||
|
@ -877,8 +877,12 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Capture s_vfs_count to a local variable in case a new driver is registered or removed during this actual select()
|
||||
// call. s_vfs_count cannot be protected with a mutex during a select() call (which can be one without a timeout)
|
||||
// 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(s_vfs_count, sizeof(fds_triple_t))) == NULL) {
|
||||
if ((vfs_fds_triple = calloc(vfs_count, sizeof(fds_triple_t))) == NULL) {
|
||||
__errno_r(r) = ENOMEM;
|
||||
ESP_LOGD(TAG, "calloc is unsuccessful");
|
||||
return -1;
|
||||
@ -952,7 +956,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
|
||||
}
|
||||
}
|
||||
|
||||
void **driver_args = calloc(s_vfs_count, sizeof(void *));
|
||||
void **driver_args = calloc(vfs_count, sizeof(void *));
|
||||
|
||||
if (driver_args == NULL) {
|
||||
free(vfs_fds_triple);
|
||||
@ -961,7 +965,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < s_vfs_count; ++i) {
|
||||
for (int i = 0; i < vfs_count; ++i) {
|
||||
const vfs_entry_t *vfs = get_vfs_for_index(i);
|
||||
fds_triple_t *item = &vfs_fds_triple[i];
|
||||
|
||||
@ -977,7 +981,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
|
||||
|
||||
if (err != ESP_OK) {
|
||||
call_end_selects(i, vfs_fds_triple, driver_args);
|
||||
(void) set_global_fd_sets(vfs_fds_triple, s_vfs_count, readfds, writefds, errorfds);
|
||||
(void) set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds);
|
||||
if (sel_sem.is_sem_local && sel_sem.sem) {
|
||||
vSemaphoreDelete(sel_sem.sem);
|
||||
sel_sem.sem = NULL;
|
||||
@ -1022,9 +1026,9 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
|
||||
xSemaphoreTake(sel_sem.sem, ticks_to_wait);
|
||||
}
|
||||
|
||||
call_end_selects(s_vfs_count, vfs_fds_triple, driver_args); // for VFSs for start_select was called before
|
||||
call_end_selects(vfs_count, vfs_fds_triple, driver_args); // for VFSs for start_select was called before
|
||||
if (ret >= 0) {
|
||||
ret += set_global_fd_sets(vfs_fds_triple, s_vfs_count, readfds, writefds, errorfds);
|
||||
ret += set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds);
|
||||
}
|
||||
if (sel_sem.is_sem_local && sel_sem.sem) {
|
||||
vSemaphoreDelete(sel_sem.sem);
|
||||
@ -1049,6 +1053,8 @@ void esp_vfs_select_triggered(esp_vfs_select_sem_t sem)
|
||||
// which has a permanent FD. But in order to avoid to lock
|
||||
// s_fd_table_lock we go through the VFS table.
|
||||
for (int i = 0; i < s_vfs_count; ++i) {
|
||||
// Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't
|
||||
// matter here stop_socket_select() will be called for only valid VFS drivers.
|
||||
const vfs_entry_t *vfs = s_vfs[i];
|
||||
if (vfs != NULL && vfs->vfs.stop_socket_select != NULL) {
|
||||
vfs->vfs.stop_socket_select(sem.sem);
|
||||
@ -1067,6 +1073,8 @@ void esp_vfs_select_triggered_isr(esp_vfs_select_sem_t sem, BaseType_t *woken)
|
||||
// which has a permanent FD. But in order to avoid to lock
|
||||
// s_fd_table_lock we go through the VFS table.
|
||||
for (int i = 0; i < s_vfs_count; ++i) {
|
||||
// Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't
|
||||
// matter here stop_socket_select() will be called for only valid VFS drivers.
|
||||
const vfs_entry_t *vfs = s_vfs[i];
|
||||
if (vfs != NULL && vfs->vfs.stop_socket_select_isr != NULL) {
|
||||
vfs->vfs.stop_socket_select_isr(sem.sem, woken);
|
||||
|
Loading…
Reference in New Issue
Block a user