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

SD library: reduce stack size requirement by 4k by moving work buffer to heap #6745

Merged
merged 2 commits into from
May 27, 2022

Conversation

mazgch
Copy link
Contributor

@mazgch mazgch commented May 14, 2022

Description of Change

The stack requirement of this library is huge due to a work buffer allocated for f_mkfs in sdcard_mount. The work buffer is only needed if argument format_if_empty is set true (which is by default false) and the card was empty.

This change is quite important if you plan to use this library in a task. as now it increases the tasks stacks size by 4k, even this work memory is never used (due to format_if_empty is set false). If users are not aware of the large stack requirement during init this library they may have seen various side effects caused by the original code. the other variables in this function may have written addresses that were outside its valid stack range.

Tests scenarios

This issue was detected ESP32 when SD.begin was called from a task with a 2k stack (with format_if_empty set false).
Before this change either the stack of the task would have to been increased to 6k. Before it would show random crashed depending on time SD.begin was executed.

…er to heap.

Better allocate the buffer for f_mkfs from the heap otherwise the stack requirement of this library is huge due to a work buffer allocated for f_mkfs in sdcard_mount. The work buffer is only needed if argument format_if_empty is set true (which is by default false). 
This change is quite important if you plan to use this library in a task. as now it increased the tasks stacks size by 4k, even this memory is never used if users are not aware of the large stack requirement during init this library may have other variables on stack that would have written memory outsides its range which can cause various side effects.
@lbernstone
Copy link
Contributor

Please put "SD library" somewhere in the PR title.

@mazgch mazgch changed the title reduce stack size requirement by 4k by moving work buffer to heap SD library: reduce stack size requirement by 4k by moving work buffer to heap May 14, 2022
@VojtechBartoska VojtechBartoska added the Area: Libraries Issue is related to Library support. label May 16, 2022
Copy link
Member

@P-R-O-C-H-Y P-R-O-C-H-Y left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @mazgch :)

@VojtechBartoska VojtechBartoska added this to the 2.0.4 milestone May 18, 2022
@me-no-dev me-no-dev merged commit e5913c3 into espressif:master May 27, 2022
@prplz prplz mentioned this pull request Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Libraries Issue is related to Library support.
Projects
Development

Successfully merging this pull request may close these issues.

5 participants