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

fs: littlefs: Avoid using static buffers that are too small #79377

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions subsys/fs/littlefs_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,14 +836,30 @@ static int littlefs_init_cfg(struct fs_littlefs *fs, int flags)
lcp->context = fs->backend;
/* Set the validated/defaulted values. */
if (littlefs_on_blkdev(flags)) {
lfs_size_t new_cache_size = block_size;
lfs_size_t new_lookahead_size = block_size * 4;

lcp->read = lfs_api_read_blk;
lcp->prog = lfs_api_prog_blk;
lcp->erase = lfs_api_erase_blk;

lcp->read_size = block_size;
lcp->prog_size = block_size;
lcp->cache_size = block_size;
lcp->lookahead_size = block_size * 4;

Copy link
Collaborator

@de-nordic de-nordic Oct 10, 2024

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)

if (lcp->cache_size < new_cache_size) {
LOG_ERR("Configured cache size is too small: %d < %d", lcp->cache_size,
new_cache_size);
return -ENOMEM;
}
lcp->cache_size = new_cache_size;

if (lcp->lookahead_size < new_lookahead_size) {
LOG_ERR("Configured lookahead size is too small: %d < %d",
lcp->lookahead_size, new_lookahead_size);
return -ENOMEM;
}
lcp->lookahead_size = new_lookahead_size;

lcp->sync = lfs_api_sync_blk;

LOG_INF("sizes: rd %u ; pr %u ; ca %u ; la %u",
Expand Down
Loading