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

Conversation

pahindman
Copy link
Contributor

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

@@ -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;

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)

Copy link

Hello @pahindman, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@pahindman pahindman force-pushed the fix-issue-77917 branch 2 times, most recently from e3eb00c to 4fd8d97 Compare October 17, 2024 17:28
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]>
@de-nordic de-nordic added the bug The issue is a bug, or the PR is fixing a bug label Oct 18, 2024
@de-nordic de-nordic added this to the v4.0.0 milestone Oct 18, 2024
@dleach02 dleach02 merged commit 09574e6 into zephyrproject-rtos:main Oct 25, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: File System bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhance LittleFS lookahead buffer sizing for improved memory safety and block device compatibility
5 participants