Skip to content

Conversation

@zhaoxingyu12
Copy link
Contributor

@zhaoxingyu12 zhaoxingyu12 commented Jan 12, 2026

Note: Please adhere to Contributing Guidelines.

Summary

make some optimizations to the NVS module, including specifying macro names and fixing bugs.

  1. make the name of the macro clearer:
    a. Add NVS_ prefix to ADDR_XXX
    b. rename MTD_BLOCKSIZE_MULTIPLE to MTD_CONFIG_BLOCKSIZE_MULTIPLE
  2. dynamically obtain the page size of the storage device instead of needing to configure macros yourself
  3. Adaptation requires devices that do not support byte write, and flash operations need to be performed through bread/bwrite
  4. By obtaining the erase value to set the nvs special ID instead of using a fixed value
  5. fix a bug: reading expired content need in expired range

Impact

Scenarios for storing key value using nvs

Testing

This has been tested and passed in qemu, using NVS for KV read and write
testcases: apps/testing/mtd_config_fs/mtd_config_fs_test_main.c

test_nvs_mount: test begin
test_nvs_mount: success
test_nvs_write: test begin
test_nvs_write: success
test_nvs_corrupt_expire: test begin
test_nvs_corrupt_expire: success
test_nvs_corrupted_write: test begin
test_nvs_corrupted_write: success
test_nvs_gc: test begin
test_nvs_gc: success
test_nvs_gc_3sectors: test begin
test_nvs_gc_3sectors: success
test_nvs_corrupted_sector_close: test begin
test_nvs_corrupted_sector_close: success
test_nvs_full_sector: test begin
test_nvs_full_sector: success
test_nvs_gc_corrupt_close_ate: test begin
test_nvs_gc_corrupt_close_ate: success
test_nvs_gc_corrupt_ate: test begin
test_nvs_gc_corrupt_ate: success
test_nvs_gc_touched_deleted_ate: test begin
test_nvs_gc_touched_deleted_ate: success
test_nvs_gc_touched_expired_ate: test begin
test_nvs_gc_touched_expired_ate: success
test_nvs_gc_not_touched_expired_ate: test begin
test_nvs_gc_not_touched_expired_ate: success

Final memory usage:
VARIABLE BEFORE AFTER DELTA
arena 4294 4294 0
ordblks 1 1 0
mxordblk eb0 eb0 0
uordblks 33e4 33e4 0
fordblks eb0 eb0 0
nsh>

@github-actions github-actions bot added Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Jan 12, 2026
@xiaoxiang781216
Copy link
Contributor

fix check error:

../nuttx/tools/checkpatch.sh -c -u -m -g a22e26924d63cbbfc1ea4faa96b04cac030ed928..HEAD
❌ Missing git commit message
❌ Missing git commit message
❌ Missing git commit message
Used config files:
    1: .codespellrc
Some checks failed. For contributing guidelines, see:
  https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md

@zhaoxingyu12
Copy link
Contributor Author

fix check error:

../nuttx/tools/checkpatch.sh -c -u -m -g a22e26924d63cbbfc1ea4faa96b04cac030ed928..HEAD
❌ Missing git commit message
❌ Missing git commit message
❌ Missing git commit message
Used config files:
    1: .codespellrc
Some checks failed. For contributing guidelines, see:
  https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md

done

@jerpelea jerpelea changed the title optimize nvs module mtd/nvs: optimize nvs module Jan 12, 2026
jerpelea
jerpelea previously approved these changes Jan 12, 2026
@simbit18
Copy link
Contributor

Hi @zhaoxingyu12, please note this error.

====================================================================================
Configuration/Tool: esp32c3-legacy-devkit/nvcfgdata
2026-01-12 03:35:35
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Building NuttX...
mtd/mtd_config_fs.c: In function 'mtdconfig_ioctl':
Error: mtd/mtd_config_fs.c:2203:9: error: 'ret' undeclared (first use in this function)
 2203 |         ret = nvs_write(fs, pdata);
      |         ^~~
mtd/mtd_config_fs.c:2203:9: note: each undeclared identifier is reported only once for each function it appears in
make[1]: *** [Makefile:109: mtd_config_fs.o] Error 1
make[1]: Target 'libdrivers.a' not remade because of errors.
make: *** [tools/LibTargets.mk:107: drivers/libdrivers.a] Error 2
Error: mtd_config_fs_test_main.c:75:5: error: "CONFIG_MTD_WRITE_ALIGN_SIZE" is not defined, evaluates to 0 [-Werror=undef]
   75 | #if CONFIG_MTD_WRITE_ALIGN_SIZE <= 4
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Error: mtd_config_fs_test_main.c:53:41: error: 'CONFIG_MTD_WRITE_ALIGN_SIZE' undeclared here (not in a function)
   53 | #define NVS_ALIGN_SIZE                  CONFIG_MTD_WRITE_ALIGN_SIZE
      |                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
mtd_config_fs_test_main.c:78:20: note: in expansion of macro 'NVS_ALIGN_SIZE'
   78 |   uint8_t  expired[NVS_ALIGN_SIZE];
      |                    ^~~~~~~~~~~~~~
mtd_config_fs_test_main.c: In function 'test_nvs_corrupted_sector_close':
Error: mtd_config_fs_test_main.c:1342:11: error: unused variable 'wr_buf' [-Werror=unused-variable]
 1342 |   uint8_t wr_buf[NVS_ALIGN_UP(512) - sizeof(key1)];
      |           ^~~~~~
Error: mtd_config_fs_test_main.c:1341:11: error: unused variable 'rd_buf' [-Werror=unused-variable]
 1341 |   uint8_t rd_buf[NVS_ALIGN_UP(512) - sizeof(key1)];
      |           ^~~~~~
cc1: all warnings being treated as errors
make[2]: *** [/github/workspace/sources/apps/Application.mk:330: mtd_config_fs_test_main.c.github.workspace.sources.apps.testing.fs.mtd_config_fs.o] Error 1
make[2]: Target 'all' not remade because of errors.
make[1]: *** [Makefile:54: /github/workspace/sources/apps/testing/fs/mtd_config_fs_all] Error 2
make[1]: Target 'all' not remade because of errors.
make: *** [tools/LibTargets.mk:248: /github/workspace/sources/apps/libapps.a] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 385: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize esp32c3-legacy-devkit/nvcfgdata
====================================================================================

@xiaoxiang781216
Copy link
Contributor

@zhaoxingyu12 please fix:

../nuttx/tools/checkpatch.sh -c -u -m -g 38378149edcc7b767fe4fce4087801288d52271b..HEAD
❌ Remove Gerrit Change-ID's before submitting upstream
❌ Missing git commit message
Used config files:
    1: .codespellrc

@zhaoxingyu12
Copy link
Contributor Author

Error: mtd_config_fs_test_main.c:75:5: error: "CONFIG_MTD_WRITE_ALIGN_SIZE" is not defined, evaluates to 0 [-Werror=undef]

  1. the error in mtd/mtd_config_fs.c has fixed
  2. the errors in mtd_config_fs_test_main.c, fixed in apps/testing: update nvs testcases for struct nvs_ate changed nuttx-apps#3310

jerpelea
jerpelea previously approved these changes Jan 14, 2026
@zhaoxingyu12
Copy link
Contributor Author

Error: mtd_config_fs_test_main.c:75:5: error: "CONFIG_MTD_WRITE_ALIGN_SIZE" is not defined, evaluates to 0 [-Werror=undef]
75 | #if CONFIG_MTD_WRITE_ALIGN_SIZE <= 4
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Error: mtd_config_fs_test_main.c:53:41: error: 'CONFIG_MTD_WRITE_ALIGN_SIZE' undeclared here (not in a function)
53 | #define NVS_ALIGN_SIZE CONFIG_MTD_WRITE_ALIGN_SIZE
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
mtd_config_fs_test_main.c:78:20: note: in expansion of macro 'NVS_ALIGN_SIZE'
78 | uint8_t expired[NVS_ALIGN_SIZE];
| ^~~~~~~~~~~~~~
mtd_config_fs_test_main.c: In function 'test_nvs_corrupted_sector_close':
Error: mtd_config_fs_test_main.c:1342:11: error: unused variable 'wr_buf' [-Werror=unused-variable]
1342 | uint8_t wr_buf[NVS_ALIGN_UP(512) - sizeof(key1)];
| ^~~~~~
Error: mtd_config_fs_test_main.c:1341:11: error: unused variable 'rd_buf' [-Werror=unused-variable]
1341 | uint8_t rd_buf[NVS_ALIGN_UP(512) - sizeof(key1)];
| ^~~~~~
cc1: all warnings being treated as errors

pls help to retrigger CICT, these errors have already fixed in mtd_config_fs_test_main.c, thanks @xiaoxiang781216

@xiaoxiang781216
Copy link
Contributor

@zhaoxingyu12 please rebase and push your change.

remove CONFIG_MTD_WRITE_ALIGN_SIZE kconfig instead of
using geo.blocksize directly

Signed-off-by: zhaoxingyu1 <[email protected]>
add MTD_BREAD/MTD_BWRITE in nvs when
CONFIG_MTD_BYTE_WRITE is n

Signed-off-by: zhaoxingyu1 <[email protected]>
By obtaining the erase value to set the nvs
special ID instead of using a fixed value

Signed-off-by: zhaoxingyu1 <[email protected]>
xiaoxiang781216 and others added 3 commits January 16, 2026 16:04
and rename ret to rc for keeping the consistency with other macros

Signed-off-by: Xiang Xiao <[email protected]>
rename MTD_BLOCKSIZE_MULTIPLE to
CONFIG_MTD_CONFIG_BLOCKSIZE_MULTIPLE

Signed-off-by: zhaoxingyu1 <[email protected]>
By obtaining the erase value to set the nvs special ID
instead of using a fixed value

Signed-off-by: zhaoxingyu1 <[email protected]>
@zhaoxingyu12
Copy link
Contributor Author

@zhaoxingyu12 please rebase and push your change.

got it, thanks

@GUIDINGLI GUIDINGLI merged commit c4cf7ea into apache:master Jan 16, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants