SPIFFS register fails if it has been unregistered previously

marclee
Posts: 51
Joined: Fri Apr 09, 2021 1:09 pm

SPIFFS register fails if it has been unregistered previously

Postby marclee » Thu Aug 18, 2022 4:43 pm

Hello!

Equipment: ESP32-S2-WROVER with SDK esp-idf.v5.0-dev-4770-gd622bcfd46
The Problem: SPIFFS register fails if it has been unregistered previously

Actions:
1) Mounting SPIFFS -> ok
2) Starting HTTPD-Server -> ok
3) Unmounting SPIFFS -> ok
4) Updating SPIFFS via OTA -> ok
5) reMounting SPIFFS -> fail

The code we use for (1) and (5):
  1. void zzz_spiffs_init(partition_check_t* s_pc)
  2. {
  3.   ESP_LOGI(TAG, "Initializing SPIFFS");
  4.  
  5.   // Use settings defined above to initialize and mount SPIFFS filesystem.
  6.   // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  7.   esp_err_t ret = esp_vfs_spiffs_register(&s_spiffs_conf);
  8.  
  9.   if (ret != ESP_OK)
  10.   {
  11.     if (ret == ESP_FAIL) { ESP_LOGE(TAG, "Failed to mount or format filesystem"); }
  12.     else if (ret == ESP_ERR_NOT_FOUND) { ESP_LOGE(TAG, "Failed to find SPIFFS partition"); }
  13.     else { ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret)); }
  14.   }
  15.  
  16.   else
  17.   {
  18.     ret = esp_spiffs_check(s_spiffs_conf.partition_label);
  19.  
  20.     if (ret != ESP_OK) { ESP_LOGE(TAG, "esp_spiffs_check failed (%s)", esp_err_to_name(ret)); }
  21.  
  22.     else
  23.     {
  24.       ESP_LOGI(TAG, "esp_spiffs_check succeeded");
  25.  
  26.       size_t total = 0, used = 0;
  27.  
  28.       ret = esp_spiffs_info(s_spiffs_conf.partition_label, &total, &used);
  29.  
  30.       if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret)); }
  31.  
  32.       else { ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);}
  33.     }
  34.   }
  35. }

The code we use for (3):
  1. void zzz_spiffs_deinit(partition_check_t* s_pc)
  2. {
  3.   esp_err_t ret = esp_vfs_spiffs_unregister(s_spiffs_conf.partition_label);
  4.  
  5.   if (ret != ESP_OK) { ESP_LOGE(TAG, "SPIFFS unregister failed (%s)", esp_err_to_name(ret)); }
  6.   else { ESP_LOGI(TAG, "SPIFFS unmounted"); }
  7. }

The following error occurs:
  1. I (36975) zzz_spiffs.c: Initializing SPIFFS
  2. Guru Meditation Error: Core  0 panic'ed (LoadStoreAlignment). Exception was unhandled.
  3.  
  4. Core  0 register dump:
  5. PC      : 0x400a5be6  PS      : 0x00060230  A0      : 0x800a6287  A1      : 0x3ffdf5f0  
  6. 0x400a5be6: spiffs_page_consistency_check_i at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/spiffs/spiffs/src/spiffs_check.c:598
  7.  
  8. A2      : 0x3f50fd0d  A3      : 0x00000000  A4      : 0x3f50fc7c  A5      : 0x00000000  
  9. A6      : 0x3f50fd0d  A7      : 0x3fff053c  A8      : 0x00000000  A9      : 0x00000037  
  10. A10     : 0x00000000  A11     : 0x00000000  A12     : 0x3ffeffc0  A13     : 0x00000100  
  11. A14     : 0x00000100  A15     : 0x3f50fc7c  SAR     : 0x00000010  EXCCAUSE: 0x00000009  
  12. EXCVADDR: 0x3f50fd0d  LBEG    : 0x3ffeffc0  LEND    : 0x00000100  LCOUNT  : 0x40027000  
  13. 0x40027000: _xt_user_exc at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/freertos/FreeRTOS-Kernel/portable/xtensa/xtensa_vectors.S:643
  14.  
  15.  
  16.  
  17. Backtrace: 0x400a5be3:0x3ffdf5f0 0x400a6284:0x3ffdf660 0x400a2127:0x3ffdf680 0x400a0da2:0x3ffdf6a0 0x4009dd27:0x3ffdf6d0 0x40095a5d:0x3ffdf710 0x400d3c87:0x3ffdf980 0x400d29e1:0x3ffdf9c0 0x400d2a7d:0x3ffdfa50 0x400d3170:0x3ffdfa70 0x400d1a7c:0x3ffdfa90 0x4012765f:0x3ffdfab0 0x400d1cef:0x3ffdfad0 0x400d1d5b:0x3ffdfb20 0x400325f1:0x3ffdfb40
  18. 0x400a5be3: spiffs_page_consistency_check_i at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/spiffs/spiffs/src/spiffs_check.c:598
  19.  
  20. 0x400a6284: spiffs_page_consistency_check at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/spiffs/spiffs/src/spiffs_check.c:866 (discriminator 3)
  21.  
  22. 0x400a2127: SPIFFS_check at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/spiffs/spiffs/src/spiffs_hydrogen.c:1155 (discriminator 2)
  23.  
  24. 0x400a0da2: esp_spiffs_check at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/spiffs/esp_spiffs.c:315
  25.  
  26. 0x4009dd27: zzz_spiffs_init at /home/xxx/yyy/main/zzz_spiffs.c:218
  27.  
  28. 0x40095a5d: ota_upload_post_handler at /home/xxx/yyy/main/zzz_http-server.c:2801 (discriminator 13)
  29.  
  30. 0x400d3c87: httpd_uri at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_uri.c:329
  31.  
  32. 0x400d29e1: httpd_parse_req at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_parse.c:659 (discriminator 15)
  33.  
  34. 0x400d2a7d: httpd_req_new at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_parse.c:787
  35.  
  36. 0x400d3170: httpd_sess_process at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_sess.c:412 (discriminator 15)
  37.  
  38. 0x400d1a7c: httpd_process_session at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_main.c:200 (discriminator 15)
  39.  
  40. 0x4012765f: httpd_sess_enum at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_sess.c:50 (discriminator 1)
  41.  
  42. 0x400d1cef: httpd_server at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_main.c:250
  43.  
  44. 0x400d1d5b: httpd_thread at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/esp_http_server/src/httpd_main.c:272 (discriminator 15)
  45.  
  46. 0x400325f1: vPortTaskWrapper at /home/xxx/esp/esp-idf.v5.0-dev-4770-gd622bcfd46/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:151
  47.  
  48.  
  49.  
  50.  
  51.  
  52. ELF file SHA256: 720feae3db5ea198
  53.  
  54. Rebooting...

The mentioned line
0x4009dd27: zzz_spiffs_init at /home/xxx/yyy/main/zzz_spiffs.c:218
refers to
ret = esp_spiffs_check(s_spiffs_conf.partition_label);
.

I also have to mention that this error only occurs after remounting SPIFFS. This error doesn't occur after reboot. After reboot SPIFFS mounts without errors. Our current workaround is to reboot after SPIFFS OTA update.

Any idea? Thank you in advance!

marclee
Posts: 51
Joined: Fri Apr 09, 2021 1:09 pm

Re: SPIFFS register fails if it has been unregistered previously

Postby marclee » Mon Sep 19, 2022 8:46 pm

Is there anybody of the ESP32 team?

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: SPIFFS register fails if it has been unregistered previously

Postby ESP_igrr » Tue Sep 20, 2022 7:12 am

Hi marclee,
I have tried reproducing the issue using the spiffs example from esp-idf, but registering spiffs the second time worked correctly.
Given the value of the invalid pointer (from EXCVADDR), you might be running into a heap corruption. I would recommend enabling heap memory debugging options to troubleshoot this: https://docs.espressif.com/projects/esp ... corruption

marclee
Posts: 51
Joined: Fri Apr 09, 2021 1:09 pm

Re: SPIFFS register fails if it has been unregistered previously

Postby marclee » Wed Oct 05, 2022 4:59 pm

Thank you for your answer. You were right. It was a problem with memory management. I should mention that we use SPI-RAM because internal DRAM is not sufficient for our purpose.

Following the instructions in https://docs.espressif.com/projects/esp ... corruption I replaced every "malloc" with "heap_caps_malloc(... , MALLOC_CAP_DEFAULT)" and "heap_caps_malloc(... , MALLOC_CAP_DMA)" in case of buffers for SPI access.

I never experienced a "heap_caps_register_failed_alloc_callback" but also the problem with remounting SPIFFS disappeared.

Is it mandatory to use "heap_caps_malloc" instead of "malloc" if external SPI-RAM is used?

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: SPIFFS register fails if it has been unregistered previously

Postby ESP_igrr » Wed Oct 05, 2022 7:33 pm

In general it's not necessary to use heap_caps_malloc, malloc should work fine. Exceptions are cases where a buffer in internal RAM is necessary, for example when doing DMA.

The fact that the issue has disappeared after this change could indicate that there is some place where we don't properly handle buffers located in PSRAM. Out of the changes you did, were there any in IDF code, or only in your application?

marclee
Posts: 51
Joined: Fri Apr 09, 2021 1:09 pm

Re: SPIFFS register fails if it has been unregistered previously

Postby marclee » Thu Oct 06, 2022 8:36 pm

The fact that the issue has disappeared after this change could indicate that there is some place where we don't properly handle buffers located in PSRAM. Out of the changes you did, were there any in IDF code, or only in your application?
I've changed only the code in our application.

Who is online

Users browsing this forum: alubee, zelenecul and 132 guests