From 9c73b80ee01c7a6151f6f81ea58392436bf4dd3a Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 12 Feb 2021 11:13:47 +1100 Subject: [PATCH 1/2] freertos: Increase minimum task stack size when stack smashing checker is enabled Fixes issue with DPORT init task, this task uses minimum stack size and may not be enough if stack smashing detection is set to Overall mode. Also reworks the way we calculate minimum stack to allow for adding multiple contributing factors. Closes https://github.com/espressif/esp-idf/issues/6403 --- components/esp32/dport_access.c | 3 ++ .../riscv/include/freertos/FreeRTOSConfig.h | 36 +++++++++++++++---- .../xtensa/include/freertos/FreeRTOSConfig.h | 35 ++++++++++++++---- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/components/esp32/dport_access.c b/components/esp32/dport_access.c index 979553abf8..acbd3e5f1d 100644 --- a/components/esp32/dport_access.c +++ b/components/esp32/dport_access.c @@ -163,6 +163,9 @@ static void dport_access_init_core(void *arg) dport_access_end[core_id] = 0; dport_core_state[core_id] = DPORT_CORE_STATE_RUNNING; + /* If this fails then the minimum stack size for this config is too close to running out */ + assert(uxTaskGetStackHighWaterMark(NULL) > 128); + vTaskDelete(NULL); } #endif diff --git a/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h b/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h index 63d5aa44cc..5adcf6e87a 100644 --- a/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h +++ b/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h @@ -143,16 +143,38 @@ #define configMAX_PRIORITIES ( 25 ) #endif -#if defined(CONFIG_APPTRACE_ENABLE) -/* apptrace module requires at least 2KB of stack per task */ -#define configMINIMAL_STACK_SIZE 2048 -#elif defined(CONFIG_COMPILER_OPTIMIZATION_NONE) -/* with optimizations disabled, scheduler uses additional stack */ -#define configMINIMAL_STACK_SIZE 1024 +/* Various things that impact minimum stack sizes */ + +/* Higher stack checker modes cause overhead on each function call */ +#if CONFIG_STACK_CHECK_ALL || CONFIG_STACK_CHECK_STRONG +#define configSTACK_OVERHEAD_CHECKER 256 #else -#define configMINIMAL_STACK_SIZE 768 +#define configSTACK_OVERHEAD_CHECKER 0 #endif +/* with optimizations disabled, scheduler uses additional stack */ +#if CONFIG_COMPILER_OPTIMIZATION_NONE +#define configSTACK_OVERHEAD_OPTIMIZATION 256 +#else +#define configSTACK_OVERHEAD_OPTIMIZATION 0 +#endif + +/* apptrace mdule increases minimum stack usage */ +#if CONFIG_APPTRACE_ENABLE +#define configSTACK_OVERHEAD_APPTRACE 1280 +#else +#define configSTACK_OVERHEAD_APPTRACE 0 +#endif + +#define configSTACK_OVERHEAD_TOTAL ( \ + configSTACK_OVERHEAD_CHECKER + \ + configSTACK_OVERHEAD_OPTIMIZATION + \ + configSTACK_OVERHEAD_APPTRACE \ + ) + +#define configMINIMAL_STACK_SIZE (768 + configSTACK_OVERHEAD_TOTAL) + + #define configNUM_THREAD_LOCAL_STORAGE_POINTERS CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS #define configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS 1 diff --git a/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h b/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h index 93c7f81c98..4a623a1e98 100644 --- a/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h +++ b/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h @@ -183,16 +183,37 @@ int xt_clock_freq(void) __attribute__((deprecated)); #define configMAX_PRIORITIES ( 25 ) #endif -#if defined(CONFIG_APPTRACE_ENABLE) -/* apptrace module requires at least 2KB of stack per task */ -#define configMINIMAL_STACK_SIZE 2048 -#elif defined(CONFIG_COMPILER_OPTIMIZATION_NONE) -/* with optimizations disabled, scheduler uses additional stack */ -#define configMINIMAL_STACK_SIZE 1024 +/* Various things that impact minimum stack sizes */ + +/* Higher stack checker modes cause overhead on each function call */ +#if CONFIG_STACK_CHECK_ALL || CONFIG_STACK_CHECK_STRONG +#define configSTACK_OVERHEAD_CHECKER 256 #else -#define configMINIMAL_STACK_SIZE 768 +#define configSTACK_OVERHEAD_CHECKER 0 #endif +/* with optimizations disabled, scheduler uses additional stack */ +#if CONFIG_COMPILER_OPTIMIZATION_NONE +#define configSTACK_OVERHEAD_OPTIMIZATION 256 +#else +#define configSTACK_OVERHEAD_OPTIMIZATION 0 +#endif + +/* apptrace mdule increases minimum stack usage */ +#if CONFIG_APPTRACE_ENABLE +#define configSTACK_OVERHEAD_APPTRACE 1280 +#else +#define configSTACK_OVERHEAD_APPTRACE 0 +#endif + +#define configSTACK_OVERHEAD_TOTAL ( \ + configSTACK_OVERHEAD_CHECKER + \ + configSTACK_OVERHEAD_OPTIMIZATION + \ + configSTACK_OVERHEAD_APPTRACE \ + ) + +#define configMINIMAL_STACK_SIZE (768 + configSTACK_OVERHEAD_TOTAL) + #ifndef configIDLE_TASK_STACK_SIZE #define configIDLE_TASK_STACK_SIZE CONFIG_FREERTOS_IDLE_TASK_STACKSIZE #endif From ef0bbc5baa4f30842b94a995e780b9de7aa9dec0 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 4 Mar 2021 17:56:58 +1100 Subject: [PATCH 2/2] freertos: Add a small additional stack when optimization set to None Fix for occasional crash on startup when DPORT task overwrites its stack during context switch, otherwise. --- .../freertos/port/riscv/include/freertos/FreeRTOSConfig.h | 2 +- .../freertos/port/xtensa/include/freertos/FreeRTOSConfig.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h b/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h index 5adcf6e87a..93eb4474ab 100644 --- a/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h +++ b/components/freertos/port/riscv/include/freertos/FreeRTOSConfig.h @@ -154,7 +154,7 @@ /* with optimizations disabled, scheduler uses additional stack */ #if CONFIG_COMPILER_OPTIMIZATION_NONE -#define configSTACK_OVERHEAD_OPTIMIZATION 256 +#define configSTACK_OVERHEAD_OPTIMIZATION 320 #else #define configSTACK_OVERHEAD_OPTIMIZATION 0 #endif diff --git a/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h b/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h index 4a623a1e98..54321fec38 100644 --- a/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h +++ b/components/freertos/port/xtensa/include/freertos/FreeRTOSConfig.h @@ -194,7 +194,7 @@ int xt_clock_freq(void) __attribute__((deprecated)); /* with optimizations disabled, scheduler uses additional stack */ #if CONFIG_COMPILER_OPTIMIZATION_NONE -#define configSTACK_OVERHEAD_OPTIMIZATION 256 +#define configSTACK_OVERHEAD_OPTIMIZATION 320 #else #define configSTACK_OVERHEAD_OPTIMIZATION 0 #endif