Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY depends on SPIRAM_USE_MALLOC (IDFGH-11604) #12722

Closed
3 tasks done
ftab opened this issue Dec 4, 2023 · 6 comments
Closed
3 tasks done
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally

Comments

@ftab
Copy link

ftab commented Dec 4, 2023

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

Reporting as per #9011 (comment)

See https://github.com/espressif/esp-idf/blob/master/components/esp_psram/esp32/Kconfig.spiram#L265

I cannot enable task stack in SPIRAM without also enabling SPIRAM allocation by malloc() (instead of only through heap_caps_malloc). However, enabling SPIRAM allocation by malloc() causes all sorts of other things to prefer internal RAM instead of SPIRAM as I want, and my internal RAM is exhausted before I finish booting.

@igrr is it as simple as just changing that line to depends on SPIRAM?

@espressif-bot espressif-bot added the Status: Opened Issue is new label Dec 4, 2023
@github-actions github-actions bot changed the title SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY depends on SPIRAM_USE_MALLOC SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY depends on SPIRAM_USE_MALLOC (IDFGH-11604) Dec 4, 2023
@0xjakob
Copy link
Contributor

0xjakob commented Dec 5, 2023

@ftab Which chip and version of ESP-IDF are you using?

And what do you mean by the following?

However, enabling SPIRAM allocation by malloc() causes all sorts of other things to prefer internal RAM instead of SPIRAM as I want,...

Without SPIRAM_USE_MALLOC disabled, malloc() should only allocate internal memory. When enabled, malloc() should allocate memory above a certain size on SPIRAM.

Edit: Note that this behavior for allocating FreeRTOS objects changed in version 5.1. Hence your IDF version is of interest, too.

@ftab
Copy link
Author

ftab commented Dec 5, 2023

@0xjakob thanks for the quick response!

@ftab Which chip and version of ESP-IDF are you using?

ESP32, 5.1.2

And what do you mean by the following?

However, enabling SPIRAM allocation by malloc() causes all sorts of other things to prefer internal RAM instead of SPIRAM as I want,...

Without SPIRAM_USE_MALLOC disabled, malloc() should only allocate internal memory. When enabled, malloc() should allocate memory above a certain size on SPIRAM.

When I enable SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY on the default ESP-IDF 5.1.2, I have to set SPIRAM_USE to SPIRAM_USE_MALLOC and my application fails to boot because it runs out of internal RAM. I don't know exactly why yet. I have checked that BT_ALLOCATION_FROM_SPIRAM_FIRST is true, SPIRAM_TRY_ALLOCATE_WIFI_LWIP is true, and all other similar options are set to true. I haven't fully investigated, there's a lot going on in my application (ESP-ADF, WiFi, Bluetooth A2DP/AVRCP/SPP, Bluetooth LE, esp console, esp rainmaker/insights, audio DSP, etc.). My application only successfully boots when SPIRAM_USE is set to SPIRAM_USE_CAPS_ALLOC, but then that disables SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY.

Edit: Note that this behavior for allocating FreeRTOS objects changed in version 5.1. Hence your IDF version is of interest, too.

Yes, I saw this and this is the reason I wanted to upgrade to this version of ESP-IDF, so I could use task stack in external memory and free up some internal memory. But in the default esp-idf 5.1.2 it is not possible to use task stack in external memory when SPIRAM_USE method is SPIRAM_USE_CAPS_ALLOC

I have made a fork of 5.1.2 that changes SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY to depend on SPIRAM instead of SPIRAM_USE_MALLOC, and the application seems to work, but I don't know enough to know whether this is safe.

@ftab
Copy link
Author

ftab commented Dec 18, 2023

The only issue I have had so far with this arrangement is that esp-idf/components/spi_flash/cache_utils.c is still blissfully unaware of SPIRAM being a valid location for task stack:

static inline bool esp_task_stack_is_sane_cache_disabled(void)
{
    const void *sp = (const void *)esp_cpu_get_sp();

    return esp_ptr_in_dram(sp)
#if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
        || esp_ptr_in_rtc_dram_fast(sp)
#endif
    ;
}

Thus anything calling spi_flash_disable_interrupts_caches_and_other_cpu() which includes

  • marking an OTA partition as valid
  • seemingly any NVS access?

causes an assert failure and I have to move that task back to internal RAM, meaning I'm almost out again.

@jaggi73
Copy link

jaggi73 commented Jul 4, 2024

Came upon this in a google search and I think I see something related. I am using the esp-adf and created my own element that uses himem. By default, adf wants to put element task stacks in SPIRAM as long as it is enabled. The problem is spi_flash_disable_interrupts_caches_and_other_cpu() fails as soon as I try to map himem with the task stack in SPIRAM. I had to specify this particular task stack to be in internal memory and then things work as they should

@espressif-bot espressif-bot added Status: Reviewing Issue is being reviewed Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: Opened Issue is new Status: Reviewing Issue is being reviewed labels Aug 13, 2024
@0xjakob
Copy link
Contributor

0xjakob commented Aug 22, 2024

@ftab We just merged 4c492cc, which allows SPIRAM task stack for statically allocated tasks.

@ftab
Copy link
Author

ftab commented Aug 22, 2024

@ftab We just merged 4c492cc, which allows SPIRAM task stack for statically allocated tasks.

Thanks! You're my hero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

4 participants