-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
fs: littlefs: Avoid using static buffers that are too small #79377
Conversation
85afcb7
to
3da3787
Compare
@@ -842,8 +842,25 @@ static int littlefs_init_cfg(struct fs_littlefs *fs, int flags) | |||
|
|||
lcp->read_size = block_size; | |||
lcp->prog_size = block_size; | |||
lcp->cache_size = block_size; | |||
lcp->lookahead_size = block_size * 4; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issues:
- in line 837 there is statement that here we are setting "validated/defaulted" values, which means that this change should not be happening in this block
- this is hidden logic that fixes problem by leading us into next, where mounting of LFS takes away heap, and make LFS or other subsystems fail, depending on the order of execution.
I think the proper solution here would be to fail initialization each time when condition happens, with LOG, because in that case we should have stable path of execution that uniformly warns user on misconfiguration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failing with a LOG indicating misconfiguration makes sense to me, thanks! I'll make that change and update the PR after I try it out on my setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to use LOG_ERR and return when this situation is encountered. Example output with logging enabled:
$ fs mount littlefs /stuff
Error mounting as littlefs: -12
[00:00:15.760,000] <inf> littlefs: LittleFS version 2.9, disk version 2.1
[00:00:15.940,000] <inf> littlefs: FS at SDMMC: is 524288 0x200-byte blocks with 512 cycle
[00:00:15.940,000] <err> littlefs: Configured cache size is too small: 64 < 512
[00:00:15.940,000] <err> littlefs: Configured lookahead size is too small: 32 < 2048
[00:00:15.940,000] <err> fs: fs mount error (-12)
Hello @pahindman, and thank you very much for your first pull request to the Zephyr project! |
e3eb00c
to
4fd8d97
Compare
The cache_size and lookahead_size are set at compile time using the CONFIG_FS_LITTLEFS_CACHE_SIZE and CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE values from Kconfig, or from the cache-size and lookahead-size properties in a 'zephyr,fstab,littlefs' compatible in the devicetree. Those values are also used to statically allocate buffers that are pointed at by the read_buffer, prog_buffer, and lookahead_buffer members of the lfs_config structure. At runtime, when using a block device, the cache_size and lookahead_size are updated to be multiples of the underlying block device's block_size, which may make them bigger than the original size used to allocate the static buffers. Log an error and fail the operation when this occurs. Signed-off-by: Phil Hindman <[email protected]>
4fd8d97
to
f8d2efc
Compare
The cache_size and lookahead_size are set at compile time using the CONFIG_FS_LITTLEFS_CACHE_SIZE and CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE values from Kconfig. Those values are also used to statically allocate buffers that are pointed at by the read_buffer, prog_buffer, and lookahead_buffer members of the lfs_config structure.
At runtime, when using a block device, the cache_size and lookahead_size are updated to be multiples of the block device's block_size, which may make them bigger than the original size used to allocate the static buffers. Detect when the size increases and set the corresponding buffer pointers to NULL in those cases. The littlefs module (in lfs_init) handles the buffer pointers being NULL by dynamically allocating appropriately sized buffers (based on the relevant updated size value).
Fixes: #77917